This repository has been archived by the owner on Jan 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.lua
194 lines (158 loc) · 3.17 KB
/
launcher.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
local VERSION_DIR = "https://raw.githubusercontent.com/MaximPixel/CCLauncher/master/version.txt"
local LAUNCHER_DIR = "https://raw.githubusercontent.com/MaximPixel/CCLauncher/master/launcher.lua"
local INSTALLER_DIR = "https://raw.githubusercontent.com/MaximPixel/CCLauncher/master/installer.lua"
local CURRENT_VERSION = 13
tArgs = { ... }
mode = 0
function downloadInstaller()
data = http.get(INSTALLER_DIR)
if data then
text = data.readAll()
data.close()
if text then
file = fs.open("installer.lua", "w")
file.write(text)
file.close()
return true
end
end
return false
end
if tArgs[1] then
if tArgs[1] == "gui" then
mode = 1
elseif tArgs[1] == "installer" then
mode = 2
end
end
if mode == 2 then
print("Install installer? [y/n]")
answer = read()
if answer == "y" or answer == "Y" then
downloadInstaller()
print("Installed!");
end
end
if mode ~= 1 then
return
end
Button = {}
function Button:new(x, y, text)
local obj = {}
obj.x = x
obj.y = y
obj.w = #text
obj.h = 1
obj.text = text
function obj:draw()
term.setCursorPos(self.x, self.y)
if self.text then
term.write(self.text)
else
--TODO
end
end
function obj:isinside(x, y)
return x >= self.x and y >= self.y and x < self.x + self.w and y < self.y + self.h
end
setmetatable(obj, self)
self.__index = self;
return obj
end
function getLatestVersion()
data = http.get(VERSION_DIR)
if data then
text = data.readAll()
data.close()
if text then
version = tonumber(text)
if version then
return version
else
return -1
end
else
return -1
end
end
return -1
end
function downloadLatest()
data = http.get(LAUNCHER_DIR)
if data then
text = data.readAll()
data.close()
if text then
file = fs.open("launcher.lua", "w")
file.write(text)
file.close()
print("Updated!")
return true
end
end
return false
end
function cc(c1, c2)
term.setTextColor(c1)
term.setBackgroundColor(c2)
end
function clearAll()
cc(colors.white, colors.black)
term.clear()
term.setCursorPos(1, 1)
end
newVersionExist = false
latestVersion = CURRENT_VERSION
function updateLatestVersion()
latestVersion = getLatestVersion()
end
function gui()
updateLatestVersion()
clearAll()
but = Button:new(2, 2, "Update")
cc(colors.black, colors.white)
but:draw()
cc(colors.white, colors.black)
term.setCursorPos(2, 4)
term.write("Or press E to update")
term.setCursorPos(2, 5)
term.write("Press Q to exit")
term.setCursorPos(1, 6)
term.write("Latest version: " .. latestVersion)
term.setCursorPos(1, 7)
term.write("Current version: " .. CURRENT_VERSION)
if CURRENT_VERSION < latestVersion then
term.setCursorPos(1, 1)
term.write("New version available!")
end
end
run = true
dirty = true
relauch = false
while run do
if dirty then
gui()
dirty = false
end
local e, k = os.pullEvent()
if e == "key" then
if k == keys.q then
run = false
end
if k == keys.e then
run = false
latestVersion = getLatestVersion()
if latestVersion >= 0 then
if latestVersion > CURRENT_VERSION then
downloadLatest()
end
end
relauch = true
end
end
end
clearAll()
os.sleep(0)
if relauch then
shell.run("launcher.lua")
end