1+ if not (lOS and LevelOS and lUtils ) then
2+ error (" This launcher is only available for LevelOS!" )
3+ end
4+
5+ -- Setup splashscreen window
6+ local nX ,nY = LevelOS .self .window .win .getPosition ()
7+ local nW ,nH = 32 ,12
8+ local nNativeW ,nNativeH = lOS .wAll .getSize ()
9+ LevelOS .setWin (math.floor (nNativeW / 2 - 16 ),math.floor (nNativeH / 2 - 6 ), nW ,nH , " borderless" )
10+
11+ -- Variables
12+ local ccstrs = require (" cc.strings" )
13+ local sPath = fs .getDir (shell .getRunningProgram ())
14+ local sGithub = {
15+ [" api" ]= " https://api.github.com/repos/1turtle/consult/releases/latest" ,
16+ [" latest" ]= " https://github.com/1Turtle/consult/releases/latest/download/cosu.lua"
17+ }
18+ local sVersion
19+
20+ --- Searches for the current version of consult in the header
21+ --- @param path string Path to consult
22+ --- @return string version Current version
23+ local function getVersion (path )
24+ local ver = " "
25+ local f = fs .open (path , ' r' )
26+ for i = 1 ,155 do
27+ local line = f .readLine ()
28+ if line :find (" sVersion" ) then
29+ ver = loadstring (line .. " return sVersion" )()
30+ break
31+ end
32+ end
33+ f .close ()
34+ return ver
35+ end
36+
37+ --- Renders a limg file (very basic)
38+ --- @param path any
39+ --- @return boolean
40+ local function image (path )
41+ local x ,y = term .getCursorPos ()
42+ local f = fs .open (path , ' r' )
43+ local img = textutils .unserialise (f .readAll ())
44+ f .close ()
45+ if type (img ) ~= " table" then return false end
46+
47+ -- Render bimg file
48+ for _ ,line in pairs (img [1 ]) do
49+ term .setCursorPos (x ,y )
50+ term .blit (line [1 ], line [2 ], line [3 ])
51+ y = y + 1
52+ end
53+
54+ return true
55+ end
56+
57+ --- Draws the UI once.
58+ --- @param sPath string Path to installation (where images are stored )
59+ local function UI (sPath )
60+ term .setBackgroundColor (colors .blue )
61+ term .setTextColor (colors .white )
62+ term .clear ()
63+
64+ -- Title
65+ term .setCursorPos (2 ,3 )
66+ if not image (fs .combine (sPath ," title.limg" )) then
67+ term .write (" CONSULT" )
68+ end
69+
70+ -- Logo
71+ paintutils .drawFilledBox (nW - 5 ,2 ,nW - 1 ,6 ,colors .white )
72+ term .setCursorPos (nW - 4 ,3 )
73+ image (fs .combine (sPath ," icon.limg" ))
74+
75+ -- Version
76+ sVersion = getVersion (fs .combine (sPath ," cosu.lua" ))
77+ if not sVersion then
78+ sVersion = " v0.0.0"
79+ end
80+ term .setCursorPos (nW -# sVersion ,nH )
81+ term .setBackgroundColor (colors .blue )
82+ term .write (sVersion )
83+ end
84+
85+ local function statusMsg (msg )
86+ msg = tostring (msg )
87+ term .setCursorPos (2 ,nH - 1 )
88+ term .setTextColor (colors .lightGray )
89+ term .clearLine ()
90+ term .write (msg )
91+ end
92+
93+ --- Checks for aupdate
94+ local function update ()
95+ local bSkip = false
96+ -- Update
97+ parallel .waitForAll (
98+ function ()
99+ statusMsg (" Check for Updates" )
100+ for i = 1 ,3 do
101+ if not bSkip then sleep (0.75 ) end
102+ term .write (' .' )
103+ end
104+ end ,
105+ function ()
106+ local newV
107+ if http then
108+ -- Check
109+ local gitAPI = http .get (sGithub .api )
110+ if gitAPI and gitAPI .getResponseCode ()== 200 then
111+ local tGitContent = textutils .unserialiseJSON (gitAPI .readAll ())
112+ if tGitContent .tag_name ~= sVersion then
113+ newV = tGitContent .tag_name
114+ end
115+ gitAPI .close ()
116+ else
117+ bSkip = true
118+ statusMsg (" Reached gitAPI limit; Skipping." )
119+ end
120+ -- Get
121+ if newV then
122+ bSkip = true
123+ sleep ()
124+ statusMsg (" Updating to: " .. newV )
125+ sleep (0.2 )
126+
127+ local gitAPI = http .get (sGithub .latest )
128+ if gitAPI and gitAPI .getResponseCode ()== 200 then
129+ local tGitContent = gitAPI .readAll ()
130+ local file = fs .open (fs .combine (sPath ," cosu.lua" ),' w' )
131+ file .flush ()
132+ file .write (tGitContent )
133+ file .close ()
134+ gitAPI .close ()
135+ statusMsg (" Done updating!" )
136+ term .setCursorPos (nW -# newV ,nH )
137+ term .setTextColor (colors .white )
138+ term .write (newV )
139+ sleep (0.75 )
140+ end
141+ end
142+ end
143+ skip = true
144+ end
145+ )
146+ end
147+
148+ local function errPop (msg )
149+ local tmp = ccstrs .wrap (msg , 51 - 7 )
150+ local msg = " "
151+ for i = 1 ,# tmp do
152+ msg = msg .. tostring (tmp [i ]).. ' \n '
153+ end
154+ lUtils .popup (" Fatal Error" , msg , 51 - 6 ,# tmp + 6 , {" Ok" }, true )
155+ end
156+
157+ ---- -----------------------------------------------
158+ UI (sPath )
159+
160+ -- Setup Args
161+ local tArgs = { ... }
162+ if # tArgs == 0 or tArgs [1 ] == " " then
163+ update ()
164+ end
165+
166+ -- Running program (and send error)
167+ LevelOS .setWin (math.floor (nNativeW / 2 - 25 ),math.floor (nNativeH / 2 - 9 ), 51 ,19 , " windowed" )
168+ local func ,msg = loadfile (fs .combine (sPath , " cosu.lua" ), " t" , _ENV )
169+ if not func then errPop (msg ) end
170+ local success ,unexpectedMsg = pcall (func , table.unpack (tArgs ))
171+ if not success then errPop (unexpectedMsg ) end
0 commit comments