From fb5c47ee11a34ef39e1076d2729e4776094cdad0 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Wed, 31 Aug 2016 00:23:17 +0200 Subject: [PATCH] Implement updating routine --- deps/protobuf | 2 +- src/Components/Modules/Console.cpp | 9 +++ src/Components/Modules/Console.hpp | 8 ++- src/Components/Modules/News.cpp | 104 +++++++++++++++++++++++++++++ src/Components/Modules/News.hpp | 4 ++ 5 files changed, 123 insertions(+), 4 deletions(-) diff --git a/deps/protobuf b/deps/protobuf index 8c936063..c0a6a6b4 160000 --- a/deps/protobuf +++ b/deps/protobuf @@ -1 +1 @@ -Subproject commit 8c936063570e5ede2ac41cf49aefe1075f1c7251 +Subproject commit c0a6a6b4628a634f6a0529c9f7e9e1e0fe66d4d6 diff --git a/src/Components/Modules/Console.cpp b/src/Components/Modules/Console.cpp index dc3f19d6..6d4ae10a 100644 --- a/src/Components/Modules/Console.cpp +++ b/src/Components/Modules/Console.cpp @@ -18,6 +18,7 @@ namespace Components int Console::LineBufferIndex = 0; bool Console::HasConsole = false; + bool Console::SkipShutdown = false; std::thread Console::ConsoleThread; @@ -383,6 +384,7 @@ namespace Components void Console::ConsoleRunner() { + Console::SkipShutdown = false; Game::Sys_ShowConsole(); MSG message; @@ -391,6 +393,8 @@ namespace Components TranslateMessage(&message); DispatchMessageA(&message); } + + if (Console::SkipShutdown) return; if (Game::Sys_Milliseconds() - Console::LastRefresh > 100 && MessageBoxA(0, "The application is not responding anymore, do you want to force its termination?", "Application is not responding", MB_ICONEXCLAMATION | MB_YESNO) == IDYES) @@ -473,6 +477,11 @@ namespace Components // Restore the initial safe area *Game::safeArea = Console::OriginalSafeArea; } + + void Console::SetSkipShutdown() + { + Console::SkipShutdown = true; + } Console::Console() { diff --git a/src/Components/Modules/Console.hpp b/src/Components/Modules/Console.hpp index 73a9e0cf..fc38497e 100644 --- a/src/Components/Modules/Console.hpp +++ b/src/Components/Modules/Console.hpp @@ -13,9 +13,7 @@ namespace Components const char* GetName() { return "Console"; }; #endif - private: - static void ToggleConsole(); - static char** GetAutoCompleteFileList(const char *path, const char *extension, Game::FsListBehavior_e behavior, int *numfiles, int allocTrackType); + static void SetSkipShutdown(); private: // Text-based console stuff @@ -35,6 +33,7 @@ namespace Components static int LineBufferIndex; static bool HasConsole; + static bool SkipShutdown; static std::thread ConsoleThread; @@ -59,5 +58,8 @@ namespace Components static void DrawSolidConsoleStub(); static void StoreSafeArea(); static void RestoreSafeArea(); + + static void ToggleConsole(); + static char** GetAutoCompleteFileList(const char *path, const char *extension, Game::FsListBehavior_e behavior, int *numfiles, int allocTrackType); }; } diff --git a/src/Components/Modules/News.cpp b/src/Components/Modules/News.cpp index ae929e55..e9e8515e 100644 --- a/src/Components/Modules/News.cpp +++ b/src/Components/Modules/News.cpp @@ -4,6 +4,7 @@ namespace Components { + bool News::Terminate; std::thread News::Thread; bool News::UnitTest() @@ -39,11 +40,99 @@ namespace Components return result; } + void News::ExitProcessStub(unsigned int exitCode) + { + std::this_thread::sleep_for(10ms); + + STARTUPINFOA sInfo; + PROCESS_INFORMATION pInfo; + + ZeroMemory(&sInfo, sizeof(sInfo)); + ZeroMemory(&pInfo, sizeof(pInfo)); + sInfo.cb = sizeof(sInfo); + + CreateProcessA("updater.dat", NULL, NULL, NULL, false, CREATE_NO_WINDOW, NULL, NULL, &sInfo, &pInfo); + + Console::SetSkipShutdown(); + TerminateProcess(GetCurrentProcess(), exitCode); + } + + void News::CheckForUpdate() + { + std::string caches = Utils::WebIO("IW4x", "https://iw4xcachep26muba.onion.to/iw4/caches.xml").SetTimeout(5000)->Get(); + + if (!caches.empty()) + { + std::string str = "()->current.integer = version; + Dvar::Var("cl_updateavailable").Get()->current.boolean = (version > /*REVISION*/1); + } + } + } + } + News::News() { + Dvar::Register("cl_updateoldversion", REVISION, REVISION, REVISION, Game::DVAR_FLAG_WRITEPROTECTED, "Current version number."); + Dvar::Register("cl_updateversion", 0, 0, -1, Game::DVAR_FLAG_WRITEPROTECTED, "New version number."); + Dvar::Register("cl_updateavailable", 0, Game::DVAR_FLAG_WRITEPROTECTED, "New update is available."); + Localization::Set("MPUI_CHANGELOG_TEXT", "Loading..."); Localization::Set("MPUI_MOTD_TEXT", NEWS_MOTD_DEFUALT); + if (Utils::IO::FileExists("updater.dat")) + { + remove("updater.dat"); + } + + Command::Add("checkforupdate", [] (Command::Params) + { + News::CheckForUpdate(); + }); + + Command::Add("getautoupdate", [] (Command::Params) + { + if (!Dvar::Var("cl_updateavailable").Get()->current.boolean) return; + + Localization::SetTemp("MENU_RECONNECTING_TO_PARTY", "Downloading updater"); + Command::Execute("openmenu popup_reconnectingtoparty", true); + + // Run the updater on shutdown + Utils::Hook::Set(0x6D72A0, ExitProcessStub); + + std::async([] () + { + std::string data = Utils::WebIO("IW4x", "http://localhost/iw4/updater/updater.exe").SetTimeout(5000)->Get(); + + if (data.empty()) + { + Localization::ClearTemp(); + Command::Execute("closemenu popup_reconnectingtoparty", false); + Game::MessageBox("Failed to download the updater!", "Error"); + } + else + { + Utils::IO::WriteFile("updater.dat", data); + Command::Execute("wait 300; quit;", false); + } + }); + }); + + News::Terminate = false; News::Thread = std::thread([] () { Localization::Set("MPUI_CHANGELOG_TEXT", Utils::WebIO("IW4x", "https://iw4xcachep26muba.onion.to/iw4/changelog.txt").SetTimeout(5000)->Get()); @@ -56,11 +145,26 @@ namespace Components } // TODO: Implement update checks here! + if (!Loader::PerformingUnitTests()) + { + while (!News::Terminate) + { + News::CheckForUpdate(); + + // Sleep for 3 minutes + for (int i = 0; i < 180 && !News::Terminate; ++i) + { + std::this_thread::sleep_for(1s); + } + } + } }); } News::~News() { + News::Terminate = true; + if (News::Thread.joinable()) { News::Thread.join(); diff --git a/src/Components/Modules/News.hpp b/src/Components/Modules/News.hpp index 0299f16b..924e89c4 100644 --- a/src/Components/Modules/News.hpp +++ b/src/Components/Modules/News.hpp @@ -14,5 +14,9 @@ namespace Components private: static std::thread Thread; + static bool Terminate; + + static void CheckForUpdate(); + static void ExitProcessStub(unsigned int exitCode); }; }