From 78c29360bfbd9b68bce0b61b3771944755efdf0b Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sun, 22 Jan 2017 13:44:14 +0100 Subject: [PATCH] [Download] Safer moddownload --- src/Components/Modules/Download.cpp | 57 +++++++++++++++++++++++------ src/Components/Modules/Renderer.cpp | 2 +- src/Utils/Utils.hpp | 29 ++++++++++++++- 3 files changed, 74 insertions(+), 14 deletions(-) diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index 085b2e67..de6b5777 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -11,9 +11,12 @@ namespace Components { if (Download::CLDownload.running) return; - Localization::SetTemp("MPUI_EST_TIME_LEFT", Utils::String::FormatTimeSpan(0)); - Localization::SetTemp("MPUI_PROGRESS_DL", "(0/0) %"); - Localization::SetTemp("MPUI_TRANS_RATE", "0.0 MB/s"); + QuickPatch::Once([]() + { + Dvar::Var("ui_dl_timeLeft").set(Utils::String::FormatTimeSpan(0)); + Dvar::Var("ui_dl_progress").set("(0/0) %"); + Dvar::Var("ui_dl_transRate").set("0.0 MB/s"); + }); Command::Execute("openmenu mod_download_popmenu", false); @@ -93,7 +96,22 @@ namespace Components progress = (100.0 / fDownload->download->totalBytes) * fDownload->download->downBytes; } - Localization::SetTemp("MPUI_PROGRESS_DL", Utils::String::VA("(%d/%d) %d%%", fDownload->index + 1, fDownload->download->files.size(), static_cast(progress))); + static unsigned int dlIndex, dlSize, dlProgress; + dlIndex = fDownload->index + 1; + dlSize = fDownload->download->files.size(); + dlProgress = static_cast(progress); + + static bool framePushed = false; + + if (!framePushed) + { + framePushed = true; + QuickPatch::Once([]() + { + framePushed = false; + Dvar::Var("ui_dl_progress").set(Utils::String::VA("(%d/%d) %d%%", dlIndex, dlSize, dlProgress)); + }); + } int delta = Game::Sys_Milliseconds() - fDownload->download->lastTimeStamp; if (delta > 300) @@ -112,8 +130,17 @@ namespace Components if (doFormat) { - Localization::SetTemp("MPUI_EST_TIME_LEFT", Utils::String::FormatTimeSpan(timeLeft)); - Localization::SetTemp("MPUI_TRANS_RATE", Utils::String::FormatBandwidth(fDownload->download->timeStampBytes, delta)); + static size_t dlTsBytes; + static int dlDelta, dlTimeLeft; + dlTimeLeft = timeLeft; + dlDelta = delta; + dlTsBytes = fDownload->download->timeStampBytes; + + QuickPatch::Once([]() + { + Dvar::Var("ui_dl_timeLeft").set(Utils::String::FormatTimeSpan(dlTimeLeft)); + Dvar::Var("ui_dl_transRate").set(Utils::String::FormatBandwidth(dlTsBytes, dlDelta)); + }); } fDownload->download->timeStampBytes = 0; @@ -222,7 +249,8 @@ namespace Components if (download->terminateThread) return; - std::string mod = download->mod; + static std::string mod; + mod = download->mod; for (unsigned int i = 0; i < download->files.size(); ++i) { @@ -236,11 +264,11 @@ namespace Components download->thread.detach(); download->clear(); - QuickPatch::Once([mod] () + QuickPatch::Once([] () { Dvar::Var("partyend_reason").set(mod); + mod.clear(); - Localization::ClearTemp(); Command::Execute("closemenu mod_download_popmenu"); Command::Execute("openmenu menu_xboxlive_partyended"); }); @@ -255,13 +283,13 @@ namespace Components download->clear(); // Run this on the main thread - QuickPatch::Once([mod] () + QuickPatch::Once([] () { auto fsGame = Dvar::Var("fs_game"); fsGame.set(mod); fsGame.get()->modified = true; + mod.clear(); - Localization::ClearTemp(); Command::Execute("closemenu mod_download_popmenu", false); if (Dvar::Var("cl_modVidRestart").get()) @@ -585,6 +613,13 @@ namespace Components } else { + Dvar::OnInit([]() + { + Dvar::Register("ui_dl_timeLeft", "", Game::dvar_flag::DVAR_FLAG_NONE, ""); + Dvar::Register("ui_dl_progress", "", Game::dvar_flag::DVAR_FLAG_NONE, ""); + Dvar::Register("ui_dl_transRate", "", Game::dvar_flag::DVAR_FLAG_NONE, ""); + }); + UIScript::Add("mod_download_cancel", [] (UIScript::Token) { Download::CLDownload.clear(); diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index f5e771d6..3a77a30e 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -26,7 +26,7 @@ namespace Components { Renderer::FrameSignal(); - auto copy = Renderer::FrameOnceSignal; + Utils::Signal copy(Renderer::FrameOnceSignal); Renderer::FrameOnceSignal.clear(); copy(); } diff --git a/src/Utils/Utils.hpp b/src/Utils/Utils.hpp index 14233450..84b621fb 100644 --- a/src/Utils/Utils.hpp +++ b/src/Utils/Utils.hpp @@ -32,9 +32,26 @@ namespace Utils class Signal { public: + Signal() + { + this->slots.clear(); + } + + Signal(Signal& obj) : Signal() + { + Utils::Merge(&this->slots, obj.getSlots()); + } + void connect(Slot slot) { - this->slots.push_back(slot); + if (slot) + { + this->slots.push_back(slot); + } + else + { + __debugbreak(); + } } void clear() @@ -42,6 +59,11 @@ namespace Utils this->slots.clear(); } + std::vector>& getSlots() + { + return this->slots; + } + template void operator()(Args&&... args) const { @@ -50,7 +72,10 @@ namespace Utils for (auto slot : copiedSlots) { - slot(std::forward(args)...); + if (slot) + { + slot(std::forward(args)...); + } } }