-
Notifications
You must be signed in to change notification settings - Fork 10
/
autorun.lua
1472 lines (1420 loc) · 45.9 KB
/
autorun.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--Cracker64's Autorun Script Manager
--The autorun to end all autoruns
--Version 3.15
--TODO:
--manual file addition (that can be anywhere and any extension)
--Moving window (because why not)
--some more API functions
--prettier, organize code
--CHANGES:
--Version 3.15: Bracket keys now scroll 5x faster, fix bracket scrolling being impossible on some keyboard layouts. Add ability to scroll by clicking and dragging, or with up/down arrow keys
--Version 3.14: Fix extra newlines being inserted into scripts on Windows
--Version 3.13: Better support for upcoming versions of TPT, all script downloads now async, settings now stored separately per scripts directory, fix another rare failure on startup
--Version 3.12: Use https for all requests, online view loads async, add FILTER button to online, fix rare failure on startup if downloaded scripts list is corrupted
--Version 3.11: Fix icons in 94.0, fix "view script in browser"
--Version 3.10: Fix HTTP requests, without this update the online section may break
--Version 3.9: Minor icon fix for latest version of jacob1's mod
--Version 3.8: Fix being unable to download scripts with / in the name, make sure tooltips don't go offscreen
--Version 3.7: Account for extra menu in TPT 91.4
--Version 3.6: Fix bug where it might delete your scripts after updating on windows
--Version 3.5: Lua5.2 support, TPT 91.0 platform API support, [] can be used to scroll, misc fixes
--Version 3.4: some new buttons, better tooltips, fix 'Change dir' button, fix broken buttons on OS X
--Version 3.3: fix apostophes in filenames, allow authors to rename their scripts on the server
--Version 3.2: put MANAGER stuff in table, fix displaying changelogs
--Version 3.1: Organize scripts less randomly, fix scripts being run twice, fix other bugs
--central script / update server at starcatcher.us / delete local scripts / lots of other things by jacob1 v3.0
--Scan all subdirectories in scripts folder! v2.25
--Remove step hooks, v87 fixes them
--Socket is now default in v87+ , horray, everyone can now use update features without extra downloads.
--Handles up to 50 extra step functions, up from the default 5 (not including the manager's step) v2.1
--Other various nice API functions
--Scripts can store/retrieve settings through the manager, see comments below v2.0
--Small fillrect change for v85, boxes can have backgrounds v1.92
--Support spaces in exe names v1.91
--Auto-update for OTHER scripts now works, is a bit messy, will fix later, but nothing should change for users to use this
-- Place a line '--VER num UPDATE link' in one of the first four lines of the file, see my above example
-- The link at top downloads a file that contains ONLY version,full link,and prints the rest(changelog). See my link for example
local jacobsmod = tpt.version.jacob1s_mod
local icons = {
["delete1"] = "\xEE\x80\x85",
["delete2"] = "\xEE\x80\x86",
["folder"] = "\xEE\x80\x93"
}
if not evt then
icons = {
["delete1"] = "\133",
["delete2"] = "\134",
["folder"] = "\147"
}
end
if not socket then error("TPT version not supported") end
if MANAGER then error("manager is already running") end
local scriptversion = 17
MANAGER = {["version"] = "3.15", ["scriptversion"] = scriptversion, ["hidden"] = true}
local type = type -- people like to overwrite this function with a global a lot
local TPT_LUA_PATH = 'scripts'
local PATH_SEP = '\\'
local OS = "WIN32"
local CHECKUPDATE = false
local EXE_NAME
if platform then
OS = platform.platform()
if OS ~= "WIN32" and OS ~= "WIN64" then
PATH_SEP = '/'
end
EXE_NAME = platform.exeName()
local temp = EXE_NAME:reverse():find(PATH_SEP)
EXE_NAME = EXE_NAME:sub(#EXE_NAME-temp+2)
else
if os.getenv('HOME') then
PATH_SEP = '/'
if fs.exists("/Applications") then
OS = "MACOSX"
else
OS = "LIN64"
end
end
if OS == "WIN32" or OS == "WIN64" then
EXE_NAME = jacobsmod and "Jacob1\'s Mod.exe" or "Powder.exe"
elseif OS == "MACOSX" then
EXE_NAME = "powder-x" --can't restart on OS X (if using < 91.0)
else
EXE_NAME = jacobsmod and "Jacob1\'s Mod" or "powder"
end
end
local beginInput, beginConfirm = ui.beginInput, ui.beginConfirm
if not beginInput then
beginInput = function(...)
local args = {...}
local cb = table.remove(args)
local input = tpt.input(unpack(args))
cb(input)
end
end
if not beginConfirm then
beginConfirm = function(...)
local args = {...}
local cb = table.remove(args)
local confirmed = tpt.confirm(unpack(args))
cb(confirmed)
end
end
local filenames = {}
local num_files = 0 --downloaded scripts aren't stored in filenames
local localscripts = {}
local onlinescripts = {}
local running = {}
local requiresrestart=false
local online = false
local first_online = true
local online_req = nil
local script_manager_update_req = nil
local updatetable --temporarily holds info on script manager updates
local gen_buttons
local count_local_scripts
local check_req_status
local sidebutton
local download_file
local settings = {}
local search_terms = {}
math.randomseed(os.time()) math.random() math.random() math.random() --some filler randoms
--get line that can be saved into scriptinfo file
local function scriptInfoString(info)
--Write table into data format
if type(info)~="table" then return end
local t = {}
for k,v in pairs(info) do
table.insert(t,k..":\""..v.."\"")
end
local rstr = table.concat(t,","):gsub("\r",""):gsub("\n","\\n")
return rstr
end
--read a scriptinfo line
local function readScriptInfo(list)
if not list then return {} end
local scriptlist = {}
for i in list:gmatch("[^\n]+") do
local t = {}
local ID = 0
for k,v in i:gmatch("(%w+):\"([^\"]*)\"") do
t[k]= tonumber(v) or v:gsub("\r",""):gsub("\\n","\n")
end
if not t.ID then
print("Skipping invalid script in script list")
else
scriptlist[t.ID] = t
end
end
return scriptlist
end
--save settings
local function save_dir()
-- Older versions of script manager stored settings here when TPT_LUA_PATH was changed away from scripts/
-- But now, only the "DIR" argument is kept here
fs.removeFile("autorunsettings.txt")
if TPT_LUA_PATH ~= "scripts" then
f = io.open("autorunsettings.txt", "wb")
if f then
f:write("DIR "..TPT_LUA_PATH)
f:close()
end
end
end
local function save_last()
local savestring=""
for script,v in pairs(running) do
savestring = savestring.." \""..script.."\""
end
savestring = "SAV "..savestring.."\nDIR "..TPT_LUA_PATH
for k,t in pairs(settings) do
for n,v in pairs(t) do
savestring = savestring.."\nSET "..k.." "..n..":\""..v.."\""
end
end
local f = io.open(TPT_LUA_PATH..PATH_SEP.."autorunsettings.txt", "wb")
if f then
f:write(savestring)
f:close()
else
MANAGER.print("Couldn't save autorunsettings.txt")
end
save_dir()
f = io.open(TPT_LUA_PATH..PATH_SEP.."downloaded"..PATH_SEP.."scriptinfo", "wb")
if f then
for k,v in pairs(localscripts) do
f:write(scriptInfoString(v).."\n")
end
f:close()
end
end
local function load_downloaded()
localscripts = {}
local f = io.open(TPT_LUA_PATH..PATH_SEP.."downloaded"..PATH_SEP.."scriptinfo","r")
if f then
local lines = f:read("*a")
f:close()
localscripts = readScriptInfo(lines)
for k,v in pairs(localscripts) do
if k ~= 1 then
if not v["ID"] or not v["name"] or not v["description"] or not v["path"] or not v["version"] then
localscripts[k] = nil
elseif not fs.exists(TPT_LUA_PATH.."/"..v["path"]:gsub("\\","/")) then
localscripts[k] = nil
end
end
end
end
end
--load settings before anything else
local function load_settings(settings_file)
local f = io.open(settings_file, "r")
if f then
local lines = {}
local line = f:read("*l")
while line do
table.insert(lines,(line:gsub("\r","")))
line = f:read("*l")
end
f:close()
for i=1, #lines do
local tok=lines[i]:sub(1,3)
local str=lines[i]:sub(5)
if tok=="SAV" then
for word in string.gmatch(str, "\"(.-)\"") do running[word] = true end
elseif tok=="EXE" then
EXE_NAME=str
elseif tok=="DIR" then
TPT_LUA_PATH=str
elseif tok=="SET" then
local ident,name,val = string.match(str,"(.-) (.-):\"(.-)\"")
if ident and name then
if settings[ident] then settings[ident][name]=val
else settings[ident]={[name]=val} end
end
end
end
end
end
local function load_last()
-- Load settings from both places.
-- Older versions of script manager may keep all settings in base-dir autorunsettings.txt
-- Modern versions only keep the DIR arg there, and put everything else into the scripts subfolder
load_settings("autorunsettings.txt")
load_settings(TPT_LUA_PATH .. PATH_SEP .. "autorunsettings.txt")
load_downloaded()
end
load_last()
--get list of files in scripts folder
local function load_filenames()
filenames = {}
local function searchRecursive(directory)
local dirlist = fs.list(directory)
if not dirlist then return end
for i,v in ipairs(dirlist) do
local file = directory.."/"..v
if fs.isDirectory(file) and v ~= "downloaded" then
searchRecursive(file)
elseif fs.isFile(file) then
if file:find("%.lua$") then
local toinsert = file:sub(#TPT_LUA_PATH+2)
if OS == "WIN32" or OS == "WIN64" then
toinsert = toinsert:gsub("/", "\\") --not actually required
end
table.insert(filenames, toinsert)
end
end
end
end
searchRecursive(TPT_LUA_PATH)
table.sort(filenames, function(first,second) return first:lower() < second:lower() end)
end
--ui object stuff
local ui_base local ui_box local ui_line local ui_text local ui_button local ui_scrollbar local ui_tooltip local ui_checkbox local ui_console local ui_window
local tooltip
ui_base = {
new = function()
local b={}
b.drawlist = {}
function b:drawadd(f)
table.insert(self.drawlist,f)
end
function b:draw(...)
for _,f in ipairs(self.drawlist) do
if type(f)=="function" then
f(self,...)
end
end
end
b.movelist = {}
function b:moveadd(f)
table.insert(self.movelist,f)
end
function b:onmove(x,y)
for _,f in ipairs(self.movelist) do
if type(f)=="function" then
f(self,x,y)
end
end
end
return b
end
}
ui_box = {
new = function(x,y,w,h,r,g,b)
local box=ui_base.new()
box.x=x box.y=y box.w=w box.h=h box.x2=x+w box.y2=y+h
box.r=r or 255 box.g=g or 255 box.b=b or 255
function box:setcolor(r,g,b) self.r=r self.g=g self.b=b end
function box:setbackground(r,g,b,a) self.br=r self.bg=g self.bb=b self.ba=a end
box.drawbox=true
box.drawbackground=false
box:drawadd(function(self) if self.drawbackground then tpt.fillrect(self.x,self.y,self.w+1,self.h+1,self.br,self.bg,self.bb,self.ba) end
if self.drawbox then tpt.drawrect(self.x,self.y,self.w,self.h,self.r,self.g,self.b) end end)
box:moveadd(function(self,x,y)
if x then self.x=self.x+x self.x2=self.x2+x end
if y then self.y=self.y+y self.y2=self.y2+y end
end)
return box
end
}
ui_line = {
new=function(x,y,x2,y2,r,g,b)
local line=ui_box.new(x,y,x2-x,y2-y,r,g,b)
--Line is essentially a box, but with a different draw
line.drawlist={}
line:drawadd(function(self) tpt.drawline(self.x,self.y,self.x2,self.y2,self.r,self.g,self.b) end)
return line
end
}
ui_text = {
new = function(text,x,y,r,g,b)
local txt = ui_base.new()
txt.text = text
txt.x=x or 0 txt.y=y or 0 txt.r=r or 255 txt.g=g or 255 txt.b=b or 255
function txt:setcolor(r,g,b) self.r=r self.g=g self.b=b end
txt:drawadd(function(self,x,y) tpt.drawtext(x or self.x,y or self.y,self.text,self.r,self.g,self.b) end)
txt:moveadd(function(self,x,y)
if x then self.x=self.x+x end
if y then self.y=self.y+y end
end)
function txt:process() return false end
return txt
end,
--Scrolls while holding mouse over
newscroll = function(text,x,y,vis,r,g,b)
local txt = ui_text.new(text,x,y,r,g,b)
if tpt.textwidth(text)<vis then return txt end
txt.visible=vis
txt.length=string.len(text)
txt.start=1
txt.drawlist={} --reset draw
txt.timer=socket.gettime()+3
function txt:cuttext(self)
local last = self.start+1
while tpt.textwidth(self.text:sub(self.start,last))<txt.visible and last<=self.length do
last = last+1
end
self.last=last-1
end
txt:cuttext(txt)
txt.minlast=txt.last-1
txt.ppl=((txt.visible-6)/(txt.length-txt.minlast+1))
txt:drawadd(function(self,x,y)
if socket.gettime() > self.timer then
if self.last >= self.length then
self.start = 1
self:cuttext(self)
self.timer = socket.gettime()+3
else
self.start = self.start + 1
self:cuttext(self)
if self.last >= self.length then
self.timer = socket.gettime()+3
else
self.timer = socket.gettime()+.15
end
end
end
tpt.drawtext(x or self.x,y or self.y, self.text:sub(self.start,self.last) ,self.r,self.g,self.b)
end)
function txt:process(mx,my,button,event,wheel)
if event==3 then
local newlast = math.floor((mx-self.x)/self.ppl)+self.minlast
if newlast<self.minlast then newlast=self.minlast end
if newlast>0 and newlast~=self.last then
local newstart=1
while tpt.textwidth(self.text:sub(newstart,newlast))>= self.visible do
newstart=newstart+1
end
self.start=newstart self.last=newlast
self.timer = socket.gettime()+3
end
end
end
return txt
end
}
ui_scrollbar = {
new = function(x,y,h,t,m)
local bar = ui_base.new() --use line object as base?
bar.x=x bar.y=y bar.h=h
bar.total=t
bar.numshown=m
bar.pos=0
bar.length=math.floor((1/math.ceil(bar.total-bar.numshown+1))*bar.h)
bar.soffset=math.floor(bar.pos*((bar.h-bar.length)/(bar.total-bar.numshown)))
bar.isClicked = false
bar.lastY = 0
function bar:update(total,shown,pos)
self.pos=pos or 0
if self.pos<0 then self.pos=0 end
self.total=total
self.numshown=shown
self.length= math.floor((1/math.ceil(self.total-self.numshown+1))*self.h)
self.soffset= math.floor(self.pos*((self.h-self.length)/(self.total-self.numshown)))
end
function bar:move(wheel)
self.pos = self.pos-wheel
if self.pos < 0 then self.pos=0 end
if self.pos > (self.total-self.numshown) then self.pos=(self.total-self.numshown) end
self.soffset= math.floor(self.pos*((self.h-self.length)/(self.total-self.numshown)))
end
bar:drawadd(function(self)
if self.total > self.numshown then
tpt.drawline(self.x,self.y+self.soffset,self.x,self.y+self.soffset+self.length)
end
end)
bar:moveadd(function(self,x,y)
if x then self.x=self.x+x end
if y then self.y=self.y+y end
end)
function bar:process(mx,my,button,event,wheel)
if self.total <= self.numshown then
return false
end
-- mousedown
if event == 1 then
if button == 1 then
self.isClicked = true
self.lastY = my
end
-- mouseup
elseif event == 2 then
self.isClicked = false
-- mousemove (scroll items if we're dragging)
elseif event == 3 then
if self.isClicked then
local diff = my - self.lastY
-- 8 is hardcoded height of each item so it works ...
if math.abs(diff) > 8 then
local previous = self.pos
if diff > 0 then
self:move(1)
else
self:move(-1)
end
self.lastY = my
return previous - self.pos
end
end
end
-- mousewheel (scroll items)
if wheel~=0 and not MANAGER.hidden then
local previous = self.pos
self:move(wheel)
if self.pos~=previous then
return previous-self.pos
end
end
return false
end
return bar
end
}
ui_button = {
new = function(x,y,w,h,f,text)
local b = ui_box.new(x,y,w,h)
b.f=f
b.t=ui_text.new(text,x+2,y+2)
b.drawbox=false
b.clicked=false
b.almostselected=false
b.invert=true
b:setbackground(127,127,127,125)
b:drawadd(function(self)
if self.invert and self.almostselected then
self.almostselected=false
tpt.fillrect(self.x,self.y,self.w,self.h)
local tr=self.t.r local tg=self.t.g local tb=self.t.b
b.t:setcolor(0,0,0)
b.t:draw()
b.t:setcolor(tr,tg,tb)
else
if tpt.mousex>=self.x and tpt.mousex<=self.x2 and tpt.mousey>=self.y and tpt.mousey<=self.y2 then
self.drawbackground=true
else
self.drawbackground=false
end
b.t:draw()
end
end)
b:moveadd(function(self,x,y)
self.t:onmove(x,y)
end)
function b:process(mx,my,button,event,wheel)
local clicked = self.clicked
if event==2 then self.clicked = false end
if mx<self.x or mx>self.x2 or my<self.y or my>self.y2 then self.clicked = false return false end
if event==1 then
self.clicked=true
elseif clicked then
if event==3 then self.almostselected=true end
if event==2 then self:f() end
return true
end
end
return b
end
}
ui_tooltip = {
new = function(x,y,w,text)
local b = ui_box.new(x,y-1,w,0)
function b:updatetooltip(tooltip)
self.tooltip = tooltip
self.length = #tooltip
self.lines = 1
local linebreak,lastspace = 0,nil
for i=0,#self.tooltip do
local width = tpt.textwidth(tooltip:sub(linebreak,i+1))
if width > self.w/2 and tooltip:sub(i,i):match("[%s,_%.%-?!]") then
lastspace = i
end
local isnewline = (self.tooltip:sub(i,i) == '\n')
if width > self.w or isnewline then
local pos = (i==#tooltip or not lastspace) and i or lastspace
self.lines = self.lines + 1
if self.tooltip:sub(pos,pos) == ' ' then
self.tooltip = self.tooltip:sub(1,pos-1).."\n"..self.tooltip:sub(pos+1)
elseif not isnewline then
self.length = self.length + 1
self.tooltip = self.tooltip:sub(1,pos-1).."\n"..self.tooltip:sub(pos)
i = i + 1
pos = pos + 1
end
linebreak = pos+1
lastspace = nil
end
end
self.h = self.lines*12+2
if self.y + self.h > gfx.HEIGHT then
local movement = (gfx.HEIGHT-self.h-1)-self.y
if self.y+movement < 0 then
movement = -self.y
end
self:onmove(0, movement)
end
--self.w = tpt.textwidth(self.tooltip)+3
self.drawbox = tooltip ~= ""
self.drawbackground = tooltip ~= ""
end
function b:settooltip(tooltip_)
tooltip:onmove(tpt.mousex+5-tooltip.x, tpt.mousey+5-tooltip.y)
tooltip:updatetooltip(tooltip_)
end
b:updatetooltip(text)
b:setbackground(0,0,0,255)
b.drawbackground = true
b:drawadd(function(self)
if self.tooltip ~= "" then
tpt.drawtext(self.x+1,self.y+2,self.tooltip)
end
self:updatetooltip("")
end)
function b:process(mx,my,button,event,wheel) end
return b
end
}
ui_checkbox = {
up_button = function(x,y,w,h,f,text)
local b=ui_button.new(x,y,w,h,f,text)
b.canupdate=false
return b
end,
new_button = function(x,y,w,h,splitx,f,f2,text,localscript)
local b = ui_box.new(x,y,splitx,h)
b.f=f b.f2=f2
b.localscript=localscript
b.splitx = splitx
b.t=ui_text.newscroll(text,x+24,y+2,splitx-24)
b.clicked=false
b.selected=false
b.checkbut=ui_checkbox.up_button(x+splitx+9,y,33,9,ui_button.scriptcheck,"Update")
b.drawbox=false
b:setbackground(127,127,127,100)
b:drawadd(function(self)
if self.t.text == "" then return end
self.drawbackground = false
if tpt.mousey >= self.y and tpt.mousey < self.y2 then
if tpt.mousex >= self.x and tpt.mousex < self.x+8 then
if self.localscript then
tooltip:settooltip("delete this script")
else
tooltip:settooltip("view script in browser")
end
elseif tpt.mousex>=self.x and tpt.mousex<self.x2 then
local script
if online and onlinescripts[self.ID]["description"] then
script = onlinescripts[self.ID]
elseif not online and localscripts[self.ID] then
script = localscripts[self.ID]
end
if script then
tooltip:settooltip(script["name"].." by "..script["author"].."\n\n"..script["description"])
end
self.drawbackground = true
elseif tpt.mousex >= self.x2 then
if tpt.mousex < self.x2+9 and self.running then
tooltip:settooltip(online and "downloaded" or "running")
elseif tpt.mousex >= self.x2+9 and tpt.mousex < self.x2+43 and self.checkbut.canupdate and onlinescripts[self.ID] and onlinescripts[self.ID]["changelog"] then
tooltip:settooltip(onlinescripts[self.ID]["changelog"])
end
end
end
self.t:draw()
if self.localscript then
local swapicon = tpt.version.jacob1s_mod_build and tpt.version.jacob1s_mod_build > 76
local offsetX = swapicon and 1 or 0
local offsetY = swapicon and 2 or 0
local innericon = swapicon and icons["delete1"] or icons["delete2"]
local outericon = swapicon and icons["delete2"] or icons["delete1"]
if self.deletealmostselected then
self.deletealmostselected = false
tpt.drawtext(self.x+1+offsetX, self.y+1+offsetY, innericon, 255, 48, 32, 255)
else
tpt.drawtext(self.x+1+offsetX, self.y+1+offsetY, innericon, 160, 48, 32, 255)
end
tpt.drawtext(self.x+1+offsetX, self.y+1+offsetY, outericon, 255, 255, 255, 255)
else
tpt.drawtext(self.x+1, self.y+1, icons["folder"], 255, 200, 80, 255)
end
tpt.drawrect(self.x+12,self.y+1,8,8)
if self.almostselected then self.almostselected=false tpt.fillrect(self.x+12,self.y+1,8,8,150,150,150)
elseif self.selected then tpt.fillrect(self.x+12,self.y+1,8,8) end
local filepath = self.ID and localscripts[self.ID] and localscripts[self.ID]["path"] or self.t.text
if self.running then tpt.drawtext(self.x+self.splitx+2,self.y+2,online and "D" or "R") end
if self.checkbut.canupdate then self.checkbut:draw() end
end)
b:moveadd(function(self,x,y)
self.t:onmove(x,y)
self.checkbut:onmove(x,y)
end)
function b:process(mx,my,button,event,wheel)
if self.f2 and mx <= self.x+8 then
if event==1 then
self.clicked = 1
elseif self.clicked == 1 then
if event==3 then self.deletealmostselected = true end
if event==2 then self:f2() end
end
elseif self.f and mx<=self.x+self.splitx then
if event==1 then
self.clicked = 2
elseif self.clicked == 2 then
if event==3 then self.almostselected=true end
if event==2 then self:f() end
self.t:process(mx,my,button,event,wheel)
end
else
if self.checkbut.canupdate then self.checkbut:process(mx,my,button,event,wheel) end
self.clicked = 0
end
return true
end
return b
end,
new = function(x,y,w,h)
local box = ui_box.new(x,y,w,h)
box.list={}
box.numlist = 0
box.max_lines = math.floor(box.h/10)-1
box.max_text_width = math.floor(box.w*0.8)
box.splitx=x+box.max_text_width
box.scrollbar = ui_scrollbar.new(box.x2-2,box.y+11,box.h-12,0,box.max_lines)
box.lines={
ui_line.new(box.x+1,box.y+10,box.x2-1,box.y+10,170,170,170),
ui_line.new(box.x+22,box.y+10,box.x+22,box.y2-1,170,170,170),
ui_line.new(box.splitx,box.y+10,box.splitx,box.y2-1,170,170,170),
ui_line.new(box.splitx+9,box.y+10,box.splitx+9,box.y2-1,170,170,170),
}
function box:updatescroll()
self.scrollbar:update(self.numlist,self.max_lines)
end
function box:clear()
self.list={}
self.numlist=0
end
function box:add(f,f2,text,localscript)
local but = ui_checkbox.new_button(self.x,self.y+1+((self.numlist+1)*10),tpt.textwidth(text)+4,10,self.max_text_width,f,f2,text,localscript)
table.insert(self.list,but)
self.numlist = #self.list
return but
end
box:drawadd(function (self)
tpt.drawtext(self.x+24,self.y+2,"Files in "..TPT_LUA_PATH.." folder")
tpt.drawtext(self.splitx+11,self.y+2,"Update")
for i,line in ipairs(self.lines) do
line:draw()
end
self.scrollbar:draw()
local restart = false
for i,check in ipairs(self.list) do
local filepath = check.ID and localscripts[check.ID] and localscripts[check.ID]["path"] or check.t.text
if not check.selected and running[filepath] then
restart = true
end
if i>self.scrollbar.pos and i<=self.scrollbar.pos+self.max_lines then
check:draw()
end
end
requiresrestart = restart and not online
end)
box:moveadd(function(self,x,y)
for i,line in ipairs(self.lines) do
line:onmove(x,y)
end
for i,check in ipairs(self.list) do
check:onmove(x,y)
end
end)
function box:scroll(amount)
local move = amount*10
if move==0 then return end
for i,check in ipairs(self.list) do
check:onmove(0,move)
check.clicked = 0
end
end
function box:process(mx,my,button,event,wheel)
if mx<self.x or mx>self.x2 or my<self.y or my>self.y2-7 then return false end
local scrolled = self.scrollbar:process(mx,my,button,event,wheel)
if scrolled then self:scroll(scrolled) end
local which = math.floor((my-self.y-11)/10)+1
if which>0 and which<=self.numlist then self.list[which+self.scrollbar.pos]:process(mx,my,button,event,wheel) end
if event == 2 then
for i,v in ipairs(self.list) do v.clicked = false end
end
return true
end
return box
end
}
ui_console = {
new = function(x,y,w,h)
local con = ui_box.new(x,y,w,h)
con.shown_lines = math.floor(con.h/10)
con.max_lines = 300
con.max_width = con.w-4
con.lines = {}
con.scrollbar = ui_scrollbar.new(con.x2-2,con.y+1,con.h-2,0,con.shown_lines)
con:drawadd(function(self)
self.scrollbar:draw()
local count=0
for i,line in ipairs(self.lines) do
if i>self.scrollbar.pos and i<= self.scrollbar.pos+self.shown_lines then
line:draw(self.x+3,self.y+3+(count*10))
count = count+1
end
end
end)
con:moveadd(function(self,x,y)
self.scrollbar:onmove(x,y)
end)
function con:clear()
self.lines = {}
self.scrollbar:update(0,con.shown_lines)
end
function con:addstr(str,r,g,b)
str = tostring(str)
local nextl = str:find('\n')
while nextl do
local line = str:sub(1,nextl-1)
self:addline(line,r,g,b)
str = str:sub(nextl+1)
nextl = str:find('\n')
end
self:addline(str,r,g,b) --anything leftover
end
function con:addline(line,r,g,b)
if not line or line=="" then return end --No blank lines
table.insert(self.lines,ui_text.newscroll(line,self.x,0,self.max_width,r,g,b))
if #self.lines>self.max_lines then table.remove(self.lines,1) end
self.scrollbar:update(#self.lines,self.shown_lines,#self.lines-self.shown_lines)
end
function con:process(mx,my,button,event,wheel)
if mx<self.x or mx>self.x2 or my<self.y or my>self.y2 then return false end
self.scrollbar:process(mx,my,button,event,wheel)
local which = math.floor((my-self.y-1)/10)+1
if which>0 and which<=self.shown_lines and self.lines[which+self.scrollbar.pos] then self.lines[which+self.scrollbar.pos]:process(mx,my,button,event,wheel) end
return true
end
return con
end
}
ui_window = {
new = function(x,y,w,h)
local w=ui_box.new(x,y,w,h)
w.sub={}
function w:add(m,name)
if name then w[name]=m end
table.insert(self.sub,m)
return m
end
w:drawadd(function(self)
for i,sub in ipairs(self.sub) do
sub:draw()
end
end)
w:moveadd(function(self,x,y)
for i,sub in ipairs(self.sub) do
sub:onmove(x,y)
end
end)
function w:process(mx,my,button,event,wheel)
if (mx<self.x or mx>self.x2 or my<self.y or my>self.y2) and event == 1 then ui_button.sidepressed() return true end
local ret
for i,sub in ipairs(self.sub) do
if sub:process(mx,my,button,event,wheel) then ret = true end
end
return ret
end
return w
end
}
--Main window with everything!
local mainwindow = ui_window.new(50,50,525,300)
mainwindow:setbackground(10,10,10,235) mainwindow.drawbackground=true
mainwindow:add(ui_console.new(275,148,300,189),"menuconsole")
mainwindow:add(ui_checkbox.new(50,80,225,257),"checkbox")
tooltip = ui_tooltip.new(0,1,250,"")
--Some API functions you can call from other scripts
--put 'using_manager=MANAGER ~= nil' or similar in your scripts, using_manager will be true if the manager is active
--Print a message to the manager console, can be colored
function MANAGER.print(msg,...)
mainwindow.menuconsole:addstr(msg,...)
end
-- Gets script info table for a script, or all scripts if nil is used as id. Data is fetched from the server.
-- Returns table as argument to callback function once info download finishes, or nil and http status code if download / parsing failed
function MANAGER.scriptinfo(id, callback)
if not callback then error("Callback function argument is required") end
local url = "https://starcatcher.us/scripts/main.lua"
if id then
url = url.."?info="..id
end
download_file(url, function(info, status_code)
if status_code == 200 then
local infotable = readScriptInfo(info)
callback(id and infotable[id] or infotable)
else
callback(nil, status_code)
end
end)
end
--Get various info about the system (operating system, script directory, path seperator, if socket is loaded)
function MANAGER.sysinfo()
return {["OS"]=OS, ["scriptDir"]=TPT_LUA_PATH, ["pathSep"]=PATH_SEP, ["exeName"] = EXE_NAME}
end
--Save a setting in the autorun settings file, ident should be your script name no one else would use.
--Name is variable name, val is the value which will be saved/returned as a string
function MANAGER.savesetting(ident,name,val)
ident = tostring(ident)
name = tostring(name)
val = tostring(val)
if settings[ident] then settings[ident][name]=val
else settings[ident]={[name]=val} end
save_last()
end
--Get a previously saved value, if it has one
function MANAGER.getsetting(ident,name)
if settings[ident] then return settings[ident][name] end
return nil
end
--delete a setting, leave name nil to delete all of ident
function MANAGER.delsetting(ident,name)
if settings[ident] then
if name then settings[ident][name]=nil
else settings[ident]=nil end
save_last()
end
end
local active_downloads = {}
function download_file(url, cb)
if not http then
MANAGER.print("TPT 95.0 or greater required to use http api", 255, 0, 0)
return false
end
if not cb then
MANAGER.print("Callback function required for async download", 255, 0, 0)
return false
end
local req = http.get(url)
local timeout_after = socket.gettime() + 3
table.insert(active_downloads, {req=req, timeout_after=timeout_after, cb=cb})
end
local function process_downloads()
for k,v in pairs(active_downloads) do
local req = v["req"]
local cb = v["cb"]
local timeout_after = v["timeout_after"]
local status = req:status()
if status ~= "running" then
active_downloads[k] = nil
local body, status_code = req:finish()
if status_code and status_code ~= 200 then
MANAGER.print("http download failed with status code " .. status_code, 255, 0, 0)
end
cb(body, status_code)
end
if socket.gettime() > timeout_after then
active_downloads[k] = nil
MANAGER.print("http download timed out ", 255, 0, 0)
req:cancel()
cb(nil, 408)
end
end
end
--Downloads script to a location, runs callback function with either true or false argument indicading success
local function download_script(ID, location, cb)
download_file("https://starcatcher.us/scripts/main.lua?get=" .. ID, function(file, status_code)
if file and status_code == 200 then
f = io.open(location, "wb")
f:write(file)
f:close()
cb(true, status_code)
else
MANAGER.print("Got http status " .. status_code .. " while downloading script", 255, 0, 0)
cb(false, status_code)
end
end)
end
--Restart exe (if named correctly)
local function do_restart(skip_save)
if not skip_save then
save_last()
end
if platform then
platform.restart()
end
if OS == "WIN32" or OS == "WIN64" then
os.execute("TASKKILL /IM \""..EXE_NAME.."\" /F &&START .\\\""..EXE_NAME.."\"")
elseif OS == "OSX" then
MANAGER.print("Can't restart on OS X when using game versions less than 91.0, please manually close and reopen The Powder Toy")
return
else
os.execute("killall -s KILL \""..EXE_NAME.."\" && ./\""..EXE_NAME.."\"")
end
MANAGER.print("Restart failed, do you have the exe name right?",255,0,0)
end
local function open_link(url)
if platform then
platform.openLink(url)
else
local command = (OS == "WIN32" or OS == "WIN64") and "start" or (OS == "MACOSX" and "open" or "xdg-open")
os.execute(command.." "..url)
end
end
--TPT interface
local function step()
if jacobsmod then
tpt.fillrect(0,0,gfx.WIDTH,gfx.HEIGHT,0,0,0,150)
else
tpt.fillrect(-1,-1,gfx.WIDTH,gfx.HEIGHT,0,0,0,150)
end
mainwindow:draw()
tpt.drawtext(280,140,"Console Output:")
if requiresrestart then