From 502b7ac3dd9e1080eb290757f4057bbd96a0b0d3 Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Mon, 30 May 2022 23:03:36 +0200 Subject: [PATCH] Updater --- data/ui_scripts/discord/__init__.lua | 14 +- src/client/component/ui_scripting.cpp | 26 +++ src/client/component/updater.cpp | 34 ++- src/client/resources/ui_scripts/common.lua | 6 +- src/client/resources/ui_scripts/updater.lua | 242 ++++++++++---------- 5 files changed, 170 insertions(+), 152 deletions(-) diff --git a/data/ui_scripts/discord/__init__.lua b/data/ui_scripts/discord/__init__.lua index cd2e70fc..bb6e5075 100644 --- a/data/ui_scripts/discord/__init__.lua +++ b/data/ui_scripts/discord/__init__.lua @@ -67,7 +67,7 @@ function addrequest(request) left = 1, bottom = -1, right = -1, - material = luiglobals.RegisterMaterial("white"), + material = RegisterMaterial("white"), color = { r = 0, b = 0, @@ -81,7 +81,7 @@ function addrequest(request) leftAnchor = true, rightAnchor = true, bottomAnchor = true, - material = luiglobals.RegisterMaterial("btn_focused_rect_innerglow"), + material = RegisterMaterial("btn_focused_rect_innerglow"), }) border:setup9SliceImage(10, 5, 0.25, 0.12) @@ -105,7 +105,7 @@ function addrequest(request) width = 32, height = 32, left = 1, - material = luiglobals.RegisterMaterial(avatarmaterial) + material = RegisterMaterial(avatarmaterial) }) local username = LUI.UIText.new({ @@ -113,7 +113,7 @@ function addrequest(request) topAnchor = true, height = 12, left = 32 + paddingvalue, - color = luiglobals.Colors.white, + color = Colors.white, alignment = LUI.Alignment.Left, rightAnchor = true, font = CoD.TextSettings.BodyFontBold.Font @@ -137,7 +137,7 @@ function addrequest(request) topAnchor = true, height = 18, width = 85, - material = luiglobals.RegisterMaterial("btn_focused_rect_innerglow"), + material = RegisterMaterial("btn_focused_rect_innerglow"), }) local center = LUI.UIText.new({ @@ -214,7 +214,7 @@ function addrequest(request) bottom = -3, left = 3, width = 200 - 6, - material = luiglobals.RegisterMaterial("white"), + material = RegisterMaterial("white"), height = 2, color = { r = 92 / 255, @@ -236,7 +236,7 @@ function addrequest(request) avatar:registerEventHandler("update", function() local avatarmaterial = discord.getavatarmaterial(request.userid) - avatar:setImage(luiglobals.RegisterMaterial(avatarmaterial)) + avatar:setImage(RegisterMaterial(avatarmaterial)) end) avatar:addElement(LUI.UITimer.new(100, "update")) diff --git a/src/client/component/ui_scripting.cpp b/src/client/component/ui_scripting.cpp index cbaa0f6a..8ee38cfa 100644 --- a/src/client/component/ui_scripting.cpp +++ b/src/client/component/ui_scripting.cpp @@ -16,6 +16,7 @@ #include "mods.hpp" #include "fastfiles.hpp" #include "scripting.hpp" +#include "updater.hpp" #include "game/ui_scripting/execution.hpp" #include "game/scripting/execution.hpp" @@ -40,6 +41,7 @@ namespace ui_scripting utils::hook::detour hks_load_hook; const auto lui_common = utils::nt::load_resource(LUI_COMMON); + const auto lui_updater = utils::nt::load_resource(LUI_UPDATER); const auto lua_json = utils::nt::load_resource(LUA_JSON); struct script @@ -331,6 +333,29 @@ namespace ui_scripting }, ::scheduler::pipeline::server); }; } + + auto updater_table = table(); + lua["updater"] = updater_table; + + updater_table["relaunch"] = updater::relaunch; + + updater_table["sethastriedupdate"] = updater::set_has_tried_update; + updater_table["gethastriedupdate"] = updater::get_has_tried_update; + updater_table["autoupdatesenabled"] = updater::auto_updates_enabled; + + updater_table["startupdatecheck"] = updater::start_update_check; + updater_table["isupdatecheckdone"] = updater::is_update_check_done; + updater_table["getupdatecheckstatus"] = updater::get_update_check_status; + updater_table["isupdateavailable"] = updater::is_update_available; + + updater_table["startupdatedownload"] = updater::start_update_download; + updater_table["isupdatedownloaddone"] = updater::is_update_download_done; + updater_table["getupdatedownloadstatus"] = updater::get_update_download_status; + updater_table["cancelupdate"] = updater::cancel_update; + updater_table["isrestartrequired"] = updater::is_restart_required; + + updater_table["getlasterror"] = updater::get_last_error; + updater_table["getcurrentfile"] = updater::get_current_file; } void start() @@ -347,6 +372,7 @@ namespace ui_scripting lua["luiglobals"] = lua; load_script("lui_common", lui_common); + load_script("lui_updater", lui_updater); load_script("lua_json", lua_json); for (const auto& path : filesystem::get_search_paths()) diff --git a/src/client/component/updater.cpp b/src/client/component/updater.cpp index 4403d821..44d2b611 100644 --- a/src/client/component/updater.cpp +++ b/src/client/component/updater.cpp @@ -4,6 +4,7 @@ #include "scheduler.hpp" #include "dvars.hpp" #include "updater.hpp" +#include "game/ui_scripting/execution.hpp" #include "version.h" @@ -63,14 +64,9 @@ namespace updater utils::concurrency::container update_data; - std::string get_branch() - { - return GIT_BRANCH; - } - std::string select(const std::string& main, const std::string& develop) { - if (get_branch() == "develop") + if (GIT_BRANCH == "develop"s) { return develop; } @@ -78,14 +74,12 @@ namespace updater return main; } - std::string get_data_path() + void notify(const std::string& name) { - if (get_branch() == "develop") + scheduler::once([=]() { - return DATA_PATH_DEV; - } - - return DATA_PATH; + ui_scripting::notify(name, {}); + }, scheduler::pipeline::lui); } void set_update_check_status(bool done, bool success, const std::string& error = {}) @@ -95,6 +89,8 @@ namespace updater data_.check.done = done; data_.check.success = success; data_.error = error; + + notify("update_check_done"); }); } @@ -105,6 +101,7 @@ namespace updater data_.download.done = done; data_.download.success = success; data_.error = error; + notify("update_done"); }); } @@ -126,11 +123,8 @@ namespace updater std::string load_binary_name() { - // utils::nt::library self; - // return self.get_name(); - // returns the game's name and not the client's - - return BINARY_NAME; + utils::nt::library self; + return self.get_name(); } std::string get_binary_name() @@ -217,7 +211,7 @@ namespace updater char current_dir[MAX_PATH]; GetCurrentDirectoryA(sizeof(current_dir), current_dir); - char buf[1024] = {0}; + char buf[1024] = {0}; const auto command_line = utils::string::va("%s %s", GetCommandLineA(), get_mode_flag().data()); strcpy_s(buf, 1024, command_line); @@ -392,6 +386,8 @@ namespace updater data_.check.success = true; data_.required_files = required_files; }); + + notify("update_check_done"); }, scheduler::pipeline::async); } @@ -468,4 +464,4 @@ namespace updater }; } -//REGISTER_COMPONENT(updater::component) +REGISTER_COMPONENT(updater::component) diff --git a/src/client/resources/ui_scripts/common.lua b/src/client/resources/ui_scripts/common.lua index a2579707..f106207c 100644 --- a/src/client/resources/ui_scripts/common.lua +++ b/src/client/resources/ui_scripts/common.lua @@ -38,7 +38,9 @@ LUI.MenuBuilder.m_types_build["generic_waiting_popup_"] = function (menu, event) end }) - popup.text = popup:getLastChild():getPreviousSibling():getPreviousSibling() + local listchildren = getchildren(popup:getChildById("LUIHorizontalList")) + local children = getchildren(listchildren[2]) + popup.text = children[2] stack = { ret = popup @@ -171,4 +173,4 @@ LUI.confirmationpopup = function(data) end LUI.FlowManager.RequestPopupMenu(nil, "generic_confirmation_popup_") return stack.ret -end \ No newline at end of file +end diff --git a/src/client/resources/ui_scripts/updater.lua b/src/client/resources/ui_scripts/updater.lua index 01d77360..90008700 100644 --- a/src/client/resources/ui_scripts/updater.lua +++ b/src/client/resources/ui_scripts/updater.lua @@ -1,164 +1,158 @@ -updatecancelled = false -taskinterval = 100 +if (not Engine.InFrontend()) then + return +end +updatecancelled = false updater.cancelupdate() function startupdatecheck(popup, autoclose) - updatecancelled = false + Engine.GetLuiRoot():registerEventHandler("update_check_done", function(element, event) + if (updatecancelled) then + return + end - local callback = function() - if (not updater.getupdatecheckstatus()) then - if (autoclose) then - LUI.FlowManager.RequestLeaveMenu(popup) - return - end + if (not updater.getupdatecheckstatus()) then + if (autoclose) then + LUI.FlowManager.RequestLeaveMenu(popup) + return + end - popup.text:setText("Error: " .. updater.getlasterror()) - return - end + popup.text:setText("Error: " .. updater.getlasterror()) + return + end - if (not updater.isupdateavailable()) then - if (autoclose) then - LUI.FlowManager.RequestLeaveMenu(popup) - return - end + if (not updater.isupdateavailable()) then + if (autoclose) then + LUI.FlowManager.RequestLeaveMenu(popup) + return + end - popup.text:setText("No updates available") - return - end + popup.text:setText("No updates available") + return + end - LUI.yesnopopup({ - title = "NOTICE", - text = "An update is available, proceed with installation?", - callback = function(result) - if (result) then - startupdatedownload(popup, autoclose) - else - LUI.FlowManager.RequestLeaveMenu(popup) - end - end - }) - end + LUI.yesnopopup({ + title = "NOTICE", + text = "An update is available, proceed with installation?", + callback = function(result) + if (result) then + startupdatedownload(popup, autoclose) + else + LUI.FlowManager.RequestLeaveMenu(popup) + end + end + }) + end) - updater.startupdatecheck() - createtask({ - done = updater.isupdatecheckdone, - cancelled = isupdatecancelled, - callback = callback, - interval = taskinterval - }) + updater.startupdatecheck() end function startupdatedownload(popup, autoclose) - updater.startupdatedownload() + local textupdate = nil + local previousfile = nil + local timer = LUI.UITimer.new(10, "update_file") - local textupdate = nil - local previousfile = nil - textupdate = game:oninterval(function() - local file = updater.getcurrentfile() - if (file == previousfile) then - return - end + popup:addElement(timer) + popup:registerEventHandler("update_file", function() + local file = updater.getcurrentfile() + if (file == previousfile) then + return + end - file = previousfile - popup.text:setText("Downloading file " .. updater.getcurrentfile() .. "...") - end, 10) + file = previousfile + popup.text:setText("Downloading file " .. updater.getcurrentfile() .. "...") + end) - local callback = function() - textupdate:clear() + Engine.GetLuiRoot():registerEventHandler("update_done", function(element, event) + timer:close() - if (not updater.getupdatedownloadstatus()) then - if (autoclose) then - LUI.FlowManager.RequestLeaveMenu(popup) - return - end + if (updatecancelled) then + return + end - popup.text:setText("Error: " .. updater.getlasterror()) - return - end + if (not updater.getupdatedownloadstatus()) then + if (autoclose) then + LUI.FlowManager.RequestLeaveMenu(popup) + return + end - popup.text:setText("Update successful") + popup.text:setText("Error: " .. updater.getlasterror()) + return + end - if (updater.isrestartrequired()) then - LUI.confirmationpopup({ - title = "RESTART REQUIRED", - text = "Update requires restart", - buttontext = "RESTART", - callback = function() - updater.relaunch() - end - }) - else - if (LUI.mp_menus) then - Engine.Exec("lui_restart; lui_open mp_main_menu") - else - Engine.Exec("lui_restart") - end - end + popup.text:setText("Update successful") - if (autoclose) then - LUI.FlowManager.RequestLeaveMenu(popup) - end - end + if (updater.isrestartrequired()) then + LUI.confirmationpopup({ + title = "RESTART REQUIRED", + text = "Update requires restart", + buttontext = "RESTART", + callback = function() + updater.relaunch() + end + }) + else + Engine.Exec("lui_restart") + end - createtask({ - done = updater.isupdatedownloaddone, - cancelled = isupdatecancelled, - callback = callback, - interval = taskinterval - }) + if (autoclose) then + LUI.FlowManager.RequestLeaveMenu(popup) + end + end) + + updater.startupdatedownload() end function updaterpopup(oncancel) - return LUI.openpopupmenu("generic_waiting_popup_", { - oncancel = oncancel, - withcancel = true, - text = "Checking for updates..." - }) + return LUI.openpopupmenu("generic_waiting_popup_", { + oncancel = oncancel, + withcancel = true, + text = "Checking for updates..." + }) end function createtask(data) - local interval = nil - interval = game:oninterval(function() - if (data.cancelled()) then - interval:clear() - return - end + local interval = nil + interval = game:oninterval(function() + if (data.cancelled()) then + interval:clear() + return + end - if (data.done()) then - interval:clear() - data.callback() - end - end, data.interval) - return interval -end - -function isupdatecancelled() - return updatecancelled + if (data.done()) then + interval:clear() + data.callback() + end + end, data.interval) + return interval end function tryupdate(autoclose) - updatecancelled = false - local popup = updaterpopup(function() - updater.cancelupdate() - updatecancelled = true - end) + updatecancelled = false + local popup = updaterpopup(function() + updater.cancelupdate() + updatecancelled = true + end) - startupdatecheck(popup, autoclose) + startupdatecheck(popup, autoclose) end function tryautoupdate() - if (not updater.autoupdatesenabled()) then - return - end + if (not updater.autoupdatesenabled()) then + return + end - if (not updater.gethastriedupdate()) then - game:ontimeout(function() - updater.sethastriedupdate(true) - tryupdate(true) - end, 100) - end + if (not updater.gethastriedupdate()) then + local timer = LUI.UITimer.new(100, "tryupdate") + Engine.GetLuiRoot():addElement(timer) + Engine.GetLuiRoot():registerEventHandler("tryupdate", function() + timer:close() + updater.sethastriedupdate(true) + tryupdate(true) + end) + end end +LUI.tryupdating = tryupdate +LUI.onmenuopen("main_lockout", tryautoupdate) LUI.onmenuopen("mp_main_menu", tryautoupdate) -LUI.onmenuopen("main_lockout", tryautoupdate) \ No newline at end of file