forked from Openarl/PathOfBuilding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaunch.lua
329 lines (309 loc) · 9.2 KB
/
Launch.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
#@ SimpleGraphic
-- Path of Building
--
-- Module: Launch
-- Program entry point; loads and runs the Main module within a protected environment
--
SetWindowTitle("Path of Building")
ConExecute("set vid_mode 8")
ConExecute("set vid_resizable 3")
local launch = { }
SetMainObject(launch)
function launch:OnInit()
self.devMode = false
self.versionNumber = "?"
self.versionBranch = "?"
self.versionPlatform = "?"
self.lastUpdateCheck = GetTime()
ConPrintf("Loading main script...")
local changeLogFile = io.open("changelog.txt")
if changeLogFile then
changeLogFile:close()
else
-- Changelog isn't present, this must be a fresh installation
-- Perform an immediate update to download the latest version
ConClear()
ConPrintf("Please wait while we complete installation...\n")
local updateMode = LoadModule("UpdateCheck")
if not updateMode or updateMode == "none" then
Exit("Failed to install.")
else
self:ApplyUpdate(updateMode)
end
return
end
local xml = require("xml")
local localManXML = xml.LoadXMLFile("manifest.xml")
if localManXML and localManXML[1].elem == "PoBVersion" then
for _, node in ipairs(localManXML[1]) do
if type(node) == "table" then
if node.elem == "Version" then
self.versionNumber = node.attrib.number
self.versionBranch = node.attrib.branch
self.versionPlatform = node.attrib.platform
end
end
end
end
if localManXML and not self.versionBranch and not self.versionPlatform then
-- Looks like a remote manifest, so we're probably running from a repository
-- Enable dev mode to disable updates and set user path to be the script path
self.devMode = true
end
local errMsg
errMsg, self.main = PLoadModule("Modules/Main", self)
if errMsg then
self:ShowErrMsg("Error loading main script: %s", errMsg)
elseif not self.main then
self:ShowErrMsg("Error loading main script: no object returned")
elseif self.main.Init then
errMsg = PCall(self.main.Init, self.main)
if errMsg then
self:ShowErrMsg("In 'Init': %s", errMsg)
end
end
if not self.devMode then
-- Run a background update check if developer mode is off
self:CheckForUpdate(true)
end
end
function launch:CanExit()
if self.main and self.main.CanExit and not self.promptMsg then
local errMsg, ret = PCall(self.main.CanExit, self.main)
if errMsg then
self:ShowErrMsg("In 'CanExit': %s", errMsg)
return false
else
return ret
end
end
return true
end
function launch:OnExit()
if self.main and self.main.Shutdown then
PCall(self.main.Shutdown, self.main)
end
end
function launch:OnFrame()
if self.main then
if self.main.OnFrame then
local errMsg = PCall(self.main.OnFrame, self.main)
if errMsg then
self:ShowErrMsg("In 'OnFrame': %s", errMsg)
end
end
end
SetDrawLayer(1000)
SetViewport()
if self.promptMsg then
local r, g, b = unpack(self.promptCol)
self:DrawPopup(r, g, b, "^0%s", self.promptMsg)
elseif self.updateChecking then
self:DrawPopup(0, 0.5, 0, "^0%s", self.updateMsg)
end
if self.doRestart then
local screenW, screenH = GetScreenSize()
SetDrawColor(0, 0, 0, 0.75)
DrawImage(nil, 0, 0, screenW, screenH)
SetDrawColor(1, 1, 1)
DrawString(0, screenH/2, "CENTER", 24, "FIXED", self.doRestart)
Restart()
end
if not self.devMode and (GetTime() - self.lastUpdateCheck) > 1000*60*60*12 then
-- Do an update check every 12 hours if the user keeps the program open
self:CheckForUpdate(true)
end
end
function launch:OnKeyDown(key, doubleClick)
if key == "F5" and self.devMode then
self.doRestart = "Restarting..."
elseif key == "u" and IsKeyDown("CTRL") then
if not self.devMode then
self:CheckForUpdate()
end
elseif self.promptMsg then
self:RunPromptFunc(key)
elseif not self.updateChecking then
if self.main and self.main.OnKeyDown then
local errMsg = PCall(self.main.OnKeyDown, self.main, key, doubleClick)
if errMsg then
self:ShowErrMsg("In 'OnKeyDown': %s", errMsg)
end
end
end
end
function launch:OnKeyUp(key)
if not self.promptMsg and not self.updateChecking then
if self.main and self.main.OnKeyUp then
local errMsg = PCall(self.main.OnKeyUp, self.main, key)
if errMsg then
self:ShowErrMsg("In 'OnKeyUp': %s", errMsg)
end
end
end
end
function launch:OnChar(key)
if self.promptMsg then
self:RunPromptFunc(key)
elseif not self.updateChecking then
if self.main and self.main.OnChar then
local errMsg = PCall(self.main.OnChar, self.main, key)
if errMsg then
self:ShowErrMsg("In 'OnChar': %s", errMsg)
end
end
end
end
function launch:OnSubCall(func, ...)
if func == "ConPrintf" and self.subScriptType == "UPDATE" and self.updateChecking then
self.updateMsg = string.format(...)
elseif func == "UpdateProgress" then
self.updateProgress = string.format(...)
end
if _G[func] then
return _G[func](...)
end
end
function launch:OnSubError(errMsg)
if self.subScriptType == "UPDATE" then
self:ShowErrMsg("In update thread: %s", errMsg)
elseif self.subScriptType == "DOWNLOAD" then
local errMsg = PCall(self.downloadCallback, nil, errMsg)
if errMsg then
self:ShowErrMsg("In download callback: %s", errMsg)
end
self.downloadCallback = nil
end
self.subScriptType = nil
end
function launch:OnSubFinished(...)
if self.subScriptType == "UPDATE" then
local ret = (...)
self.updateAvailable = ret
if self.updateChecking then
if not ret then
self:ShowPrompt(1, 0, 0, self.updateMsg .. "\n\nPress Enter/Escape to dismiss")
elseif ret == "none" then
self:ShowPrompt(0, 0, 0, "No update available.", function(key) return true end)
else
self:ShowPrompt(0.2, 0.8, 0.2, "An update has been downloaded.\n\nClick 'Update Ready' at bottom left when you are ready to update.", function(key) return true end)
end
self.updateChecking = false
end
elseif self.subScriptType == "DOWNLOAD" then
local errMsg = PCall(self.downloadCallback, ...)
if errMsg then
self:ShowErrMsg("In download callback: %s", errMsg)
end
self.downloadCallback = nil
end
self.subScriptType = nil
end
function launch:DownloadPage(url, callback, cookies)
-- Download the given page in the background, and calls the provided callback function when done:
-- callback(pageText, errMsg)
if IsSubScriptRunning() then
callback(nil, "Already downloading a page")
return
end
self.downloadCallback = callback
self.subScriptType = "DOWNLOAD"
LaunchSubScript([[
local url, cookies = ...
ConPrintf("Downloading page at: %s", url)
local curl = require("lcurl")
local page = ""
local easy = curl.easy()
easy:setopt_url(url)
if cookies then
easy:setopt(curl.OPT_COOKIE, cookies)
end
easy:setopt_writefunction(function(data)
page = page..data
return true
end)
easy:perform()
local code = easy:getinfo(curl.INFO_RESPONSE_CODE)
easy:close()
local errMsg
if code ~= 200 then
errMsg = "Response code: "..code
elseif #page == 0 then
errMsg = "No data returned"
end
ConPrintf("Download complete. Status: %s", errMsg or "OK")
if errMsg then
return nil, errMsg
else
return page
end
]], "", "ConPrintf", url, cookies)
end
function launch:ApplyUpdate(mode)
if mode == "basic" then
-- Need to revert to the basic environment to fully apply the update
LoadModule("UpdateApply", "Update/opFile.txt")
SpawnProcess(GetRuntimePath()..'/Update', 'UpdateApply.lua Update/opFileRuntime.txt')
Exit()
elseif mode == "normal" then
-- Update can be applied while normal environment is running
LoadModule("UpdateApply", "Update/opFile.txt")
Restart()
self.doRestart = "Updating..."
end
end
function launch:CheckForUpdate(inBackground)
if not IsSubScriptRunning() then
self.updateChecking = not inBackground
self.updateMsg = "Initialising..."
self.updateProgress = "Checking..."
self.lastUpdateCheck = GetTime()
local update = io.open("UpdateCheck.lua", "r")
self.subScriptType = "UPDATE"
LaunchSubScript(update:read("*a"), "GetScriptPath,GetRuntimePath,GetWorkDir,MakeDir", "ConPrintf,UpdateProgress", "CHECK")
update:close()
end
end
function launch:ShowPrompt(r, g, b, str, func)
self.promptMsg = str
self.promptCol = {r, g, b}
self.promptFunc = func or function(key)
if key == "RETURN" or key == "ESCAPE" then
return true
elseif key == "F5" then
self.doRestart = "Restarting..."
return true
end
end
end
function launch:ShowErrMsg(fmt, ...)
if not self.promptMsg then
self:ShowPrompt(1, 0, 0, "^1Error:\n\n^0" .. string.format(fmt, ...) .. "\n\nPress Enter/Escape to Dismiss, or F5 to restart the application.")
end
end
function launch:RunPromptFunc(key)
local curMsg = self.promptMsg
local errMsg, ret = PCall(self.promptFunc, key)
if errMsg then
self:ShowErrMsg("In prompt func: %s", errMsg)
elseif ret and self.promptMsg == curMsg then
self.promptMsg = nil
end
end
function launch:DrawPopup(r, g, b, fmt, ...)
local screenW, screenH = GetScreenSize()
SetDrawColor(0, 0, 0, 0.5)
DrawImage(nil, 0, 0, screenW, screenH)
local txt = string.format(fmt, ...)
local w = DrawStringWidth(20, "VAR", txt) + 20
local h = (#txt:gsub("[^\n]","") + 2) * 20
local ox = (screenW - w) / 2
local oy = (screenH - h) / 2
SetDrawColor(1, 1, 1)
DrawImage(nil, ox, oy, w, h)
SetDrawColor(r, g, b)
DrawImage(nil, ox + 2, oy + 2, w - 4, h - 4)
SetDrawColor(1, 1, 1)
DrawImage(nil, ox + 4, oy + 4, w - 8, h - 8)
DrawString(0, oy + 10, "CENTER", 20, "VAR", txt)
end