57 Star 297 Fork 71

幸福的闪电 / PathOfBuildingCN17173

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Launch.lua 13.59 KB
一键复制 编辑 原始数据 按行查看 历史
mlzzen 提交于 2023-02-09 10:34 . feat: 项目调试说明以及报错解决
#@ SimpleGraphic
-- Path of Building
--
-- Module: Launch
-- Program entry point; loads and runs the Main module within a protected environment
--
APP_NAME = "Path of Building-CN"
SetWindowTitle(APP_NAME)
ConExecute("set vid_mode 8")
ConExecute("set vid_resizable 3")
launch = { }
SetMainObject(launch)
function launch:OnInit()
-- This is the path to emmy_core.dll. The ?.dll at the end is intentional.
-- package.cpath = package.cpath .. ";C:/Users/echo/.vscode/extensions/tangzx.emmylua-0.5.11/debugger/emmy/windows/x86/?.dll"
-- local dbg = require("emmy_core")
-- This port must match the Visual Studio Code configuration. Default is 9966.
-- dbg.tcpListen("localhost", 9966)
-- Uncomment the next line if you want Path of Building to block until the debugger is attached
-- dbg.waitIDE()
self.devMode = false
self.versionNumber = "?"
self.versionBranch = "?"
self.versionPlatform = "?"
self.lastUpdateCheck = GetTime()
self.subScripts = { }
local firstRunFile = io.open("first.run", "r")
if firstRunFile then
firstRunFile:close()
os.remove("first.run")
-- This is a fresh installation
-- Perform an immediate update to download the latest version
ConClear()
ConPrintf("Please wait while we complete installation...\n")
local updateMode, errMsg = LoadModule("UpdateCheck")
if not updateMode then
self.updateErrMsg = errMsg
elseif updateMode ~= "none" then
self:ApplyUpdate(updateMode)
return
end
end
local xml = require("xml")
local localManXML = xml.LoadXMLFile("manifest.xml") or 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
SetWindowTitle("Path of Building CN "..self.versionNumber)
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
RenderInit()
ConPrintf("Loading main script...")
local errMsg
errMsg, self.main = PLoadModule("Modules/Main")
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 and not firstRunFile 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
self.devModeAlt = self.devMode and IsKeyDown("ALT")
SetDrawLayer(1000)
SetViewport()
if self.promptMsg then
local r, g, b = unpack(self.promptCol)
self:DrawPopup(r, g, b, "^0%s", self.promptMsg)
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 == "F6" and self.devMode then
local before = collectgarbage("count")
collectgarbage("collect")
ConPrintf("%dkB => %dkB", before, collectgarbage("count"))
elseif key == "u" and IsKeyDown("CTRL") then
if not self.devMode then
-- self:CheckForUpdate()
end
elseif self.promptMsg then
self:RunPromptFunc(key)
else
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 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)
else
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 == "UpdateProgress" then
self.updateProgress = string.format(...)
end
if _G[func] then
return _G[func](...)
end
end
function launch:OnSubError(id, errMsg)
if self.subScripts[id].type == "UPDATE" then
self:ShowErrMsg("In update thread: %s", errMsg)
self.updateCheckRunning = false
elseif self.subScripts[id].type == "DOWNLOAD" then
local errMsg = PCall(self.subScripts[id].callback, nil, errMsg)
if errMsg then
self:ShowErrMsg("In download callback: %s", errMsg)
end
end
self.subScripts[id] = nil
end
function launch:OnSubFinished(id, ...)
if self.subScripts[id].type == "UPDATE" then
self.updateAvailable, self.updateErrMsg = ...
self.updateCheckRunning = false
if self.updateCheckBackground and self.updateAvailable == "none" then
self.updateAvailable = nil
end
elseif self.subScripts[id].type == "DOWNLOAD" then
local errMsg = PCall(self.subScripts[id].callback, ...)
if errMsg then
self:ShowErrMsg("In download callback: %s", errMsg)
end
elseif self.subScripts[id].type == "CUSTOM" then
if self.subScripts[id].callback then
local errMsg = PCall(self.subScripts[id].callback, ...)
if errMsg then
self:ShowErrMsg("In subscript callback: %s", errMsg)
end
end
end
self.subScripts[id] = nil
end
function launch:RegisterSubScript(id, callback)
if id then
self.subScripts[id] = {
type = "CUSTOM",
callback = callback,
}
end
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)
local script = [[
local url, cookies, proxyURL = ...
ConPrintf("Downloading page at: %s", url)
local curl = require("lcurl.safe")
local page = ""
local easy = curl.easy()
easy:setopt_url(url)
easy:setopt(curl.OPT_USERAGENT, "Path of Building/]]..self.versionNumber..[[")
easy:setopt(curl.OPT_ACCEPT_ENCODING, "")
-- 不校验 https证书
easy:setopt(curl.OPT_SSL_VERIFYPEER, false)
if cookies then
easy:setopt(curl.OPT_COOKIE, cookies)
end
if proxyURL then
easy:setopt(curl.OPT_PROXY, proxyURL)
end
easy:setopt_writefunction(function(data)
page = page..data
return true
end)
local _, error = easy:perform()
local code = easy:getinfo(curl.INFO_RESPONSE_CODE)
easy:close()
local errMsg
if error then
errMsg = error:msg()
elseif 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
]]
local id = LaunchSubScript(script, "", "ConPrintf", url, cookies, self.proxyURL)
if id then
self.subScripts[id] = {
type = "DOWNLOAD",
callback = callback
}
end
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 self.updateCheckRunning then
return
end
self.updateCheckBackground = inBackground
self.updateMsg = "Initialising..."
self.updateProgress = "Checking..."
self.lastUpdateCheck = GetTime()
local update = io.open("UpdateCheck.lua", "r")
local id = LaunchSubScript(update:read("*a"), "GetScriptPath,GetRuntimePath,GetWorkDir,MakeDir", "ConPrintf,UpdateProgress", self.proxyURL)
if id then
self.subScripts[id] = {
type = "UPDATE"
}
self.updateCheckRunning = true
end
update:close()
end
function launch:Curl(url)
local curl = require("lcurl.safe")
for i = 1, 5 do
if i > 1 then
ConPrintf("Retrying... (%d of 5)", i)
end
local text = ""
local easy = curl.easy()
easy:setopt_url(url)
easy:setopt_httpheader({ "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:33.0) Gecko/20100101 Firefox/33.0" })
easy:setopt(curl.OPT_ACCEPT_ENCODING, "")
easy:setopt_writefunction(function(data)
text = text..data
return true
end)
local _, error = easy:perform()
easy:close()
if not error then
return text
end
ConPrintf("Curl failed (%s)", error:msg())
end
end
function launch:ReadCurrentRepoUrl()
local xml = require("xml")
local localManXML = xml.LoadXMLFile("manifest.xml") or 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 == "Source" then
return node.attrib.url
end
end
end
end
return nil
end
function launch:UpdateRepoUrl()
local repoURL = launch:Curl("https://pastebin.com/raw/Ydg65xUi")
-- local oldRepoURL = launch:ReadCurrentRepoUrl()
-- local manifestFile = io.open("manifest.xml", "r")
-- local content = manifestFile:read("*a")
-- manifestFile:close()
-- local newContent, count = content:gsub(oldRepoURL, repoURL)
-- manifestFile = io.open("manifest.xml", "w+")
-- manifestFile:write(newContent)
-- manifestFile:close()
local xml = require("xml")
local localManXML = xml.LoadXMLFile("manifest.xml") or 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 == "Source" then
node.attrib.url = repoURL
end
end
end
xml.SaveXMLFile(localManXML[1], "manifest.xml")
end
return repoURL
end
function launch:ShowPrompt(r, g, b, str, func)
self.promptMsg = utf8.sub(str,0) --lucifer
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, "^1国服版:"..self.versionNumber.."\n\n错误信息:\n^0" .. string.format(fmt, ...) .. "\n\n按下 Enter/Esc 关闭本消息, 或 F5 重新加载POB.")
end
end
function launch:ShowErrMsg2(fmt)
if not self.promptMsg then
if fmt ~=nil then
self:ShowPrompt(1, 0, 0, "【开发信息】\n\n" ..fmt .. "\n\n回车或ESC关闭本提示, F5重启程序.")
else
self:ShowPrompt(1, 0, 0, "【开发信息】\n\nNIL\n\n回车或ESC关闭本提示, F5重启程序.")
end
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
function launch:ToStringEx(value)
if type(value)=='table' then
return launch:TableToStr(value)
elseif type(value)=='string' then
return "\'"..value.."\'"
else
return tostring(value)
end
end
function launch:TableToStr(t)
if t == nil then return "" end
local retstr= "{"
local i = 1
for key,value in pairs(t) do
local signal = ","
if i==1 then
signal = ""
end
if key == i then
retstr = retstr..signal..launch:ToStringEx(value).."\r\n"
else
if type(key)=='number' or type(key) == 'string' then
retstr = retstr..signal..'['..launch:ToStringEx(key).."]="..launch:ToStringEx(value).."\r\n"
else
if type(key)=='userdata' then
retstr = retstr..signal.."*s"..launch:TableToStr(getmetatable(key)).."*e".."="..launch:ToStringEx(value).."\r\n"
else
retstr = retstr..signal..key.."="..launch:ToStringEx(value).."\r\n"
end
end
end
i = i+1
end
retstr = retstr.."}"
return retstr
end
Lua
1
https://gitee.com/echo28/PathOfBuildingCN17173.git
git@gitee.com:echo28/PathOfBuildingCN17173.git
echo28
PathOfBuildingCN17173
PathOfBuildingCN17173
main

搜索帮助