From 28653a0e8dfe0a172712e57f6b5d6dbde081c5d7 Mon Sep 17 00:00:00 2001 From: Edo Date: Mon, 22 Aug 2022 18:17:42 +0200 Subject: [PATCH] [Download] Nullsub some quirky gsc functions (#449) * [Download] Nullsub some quirky gsc functions * [Script] Show deprecation warning * [Script] Expand error message --- src/Components/Modules/Download.cpp | 76 +--------------- src/Components/Modules/Download.hpp | 124 -------------------------- src/Components/Modules/GSC/Script.cpp | 8 ++ src/Components/Modules/GSC/Script.hpp | 3 +- 4 files changed, 12 insertions(+), 199 deletions(-) diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index 7561cfe8..cea04936 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -5,7 +5,6 @@ namespace Components { mg_mgr Download::Mgr; Download::ClientDownload Download::CLDownload; - std::vector> Download::ScriptDownloads; std::thread Download::ServerThread; bool Download::Terminate; @@ -927,77 +926,8 @@ namespace Components Dvar::Register("mod_force_download_server", false, Game::DVAR_ARCHIVE, "Set to true to force the client to run the download server for mods (for mods in private matches)."); }, Scheduler::Pipeline::MAIN); - Scheduler::Loop([] - { - int workingCount = 0; - - for (auto i = Download::ScriptDownloads.begin(); i != Download::ScriptDownloads.end();) - { - auto download = *i; - - if (download->isDone()) - { - download->notifyDone(); - i = Download::ScriptDownloads.erase(i); - continue; - } - - if (download->isWorking()) - { - download->notifyProgress(); - ++workingCount; - } - - ++i; - } - - for (auto& download : Download::ScriptDownloads) - { - if (workingCount > 5) break; - if (!download->isWorking()) - { - download->startWorking(); - ++workingCount; - } - } - - }, Scheduler::Pipeline::MAIN); - - Events::OnVMShutdown([] - { - Download::ScriptDownloads.clear(); - }); - - Script::AddFunction("HttpGet", [] - { - const auto* url = Game::Scr_GetString(0); - - if (url == nullptr) - { - Game::Scr_ParamError(0, "^1HttpGet: Illegal parameter!\n"); - return; - } - - auto object = Game::AllocObject(); - - Game::Scr_AddObject(object); - - Download::ScriptDownloads.push_back(std::make_shared(url, object)); - Game::RemoveRefToObject(object); - }); - - Script::AddFunction("HttpCancel", [] - { - const auto object = Game::Scr_GetObject(0); - for (const auto& download : Download::ScriptDownloads) - { - if (object == download->getObject()) - { - download->cancel(); - break; - } - } - }); + Script::AddFunction("HttpGet", Script::ShowDeprecationWarning); + Script::AddFunction("HttpCancel", Script::ShowDeprecationWarning); } Download::~Download() @@ -1020,7 +950,5 @@ namespace Components { Download::CLDownload.clear(); } - - Download::ScriptDownloads.clear(); } } diff --git a/src/Components/Modules/Download.hpp b/src/Components/Modules/Download.hpp index 59e8b155..67bfb9b9 100644 --- a/src/Components/Modules/Download.hpp +++ b/src/Components/Modules/Download.hpp @@ -82,132 +82,8 @@ namespace Components size_t receivedBytes; }; - class ScriptDownload - { - public: - ScriptDownload(const std::string& _url, unsigned int _object) : url(_url), object(_object), webIO(nullptr), done(false), notifyRequired(false), totalSize(0), currentSize(0) - { - Game::AddRefToObject(this->getObject()); - } - - ScriptDownload(ScriptDownload&& other) noexcept = delete; - ScriptDownload& operator=(ScriptDownload&& other) noexcept = delete; - - ~ScriptDownload() - { - if (this->getObject()) - { - Game::RemoveRefToObject(this->getObject()); - this->object = 0; - } - - if (this->workerThread.joinable()) - { - this->workerThread.join(); - } - - this->destroyWebIO(); - } - - void startWorking() - { - if (!this->isWorking()) - { - this->workerThread = std::thread(std::bind(&ScriptDownload::handler, this)); - } - } - - bool isWorking() - { - return this->workerThread.joinable(); - } - - void notifyProgress() - { - if (this->notifyRequired) - { - this->notifyRequired = false; - - if (Game::Scr_IsSystemActive()) - { - Game::Scr_AddInt(static_cast(this->totalSize)); - Game::Scr_AddInt(static_cast(this->currentSize)); - Game::Scr_NotifyId(this->getObject(), Game::SL_GetString("progress", 0), 2); - } - } - } - - void updateProgress(size_t _currentSize, size_t _toalSize) - { - this->currentSize = _currentSize; - this->totalSize = _toalSize; - this->notifyRequired = true; - } - - void notifyDone() - { - if (!this->isDone()) return; - - if (Game::Scr_IsSystemActive()) - { - Game::Scr_AddString(this->result.data()); // No binary data supported yet - Game::Scr_AddInt(this->success); - Game::Scr_NotifyId(this->getObject(), Game::SL_GetString("done", 0), 2); - } - } - - bool isDone() { return this->done; }; - - std::string getUrl() { return this->url; } - unsigned int getObject() { return this->object; } - - void cancel() - { - if (this->webIO) - { - this->webIO->cancelDownload(); - } - } - - private: - std::string url; - std::string result; - unsigned int object; - std::thread workerThread; - Utils::WebIO* webIO; - - bool done; - bool success; - bool notifyRequired; - size_t totalSize; - size_t currentSize; - - void handler() - { - this->destroyWebIO(); - - this->webIO = new Utils::WebIO("IW4x"); - this->webIO->setProgressCallback(std::bind(&ScriptDownload::updateProgress, this, std::placeholders::_1, std::placeholders::_2)); - - this->result = this->webIO->get(this->url, &this->success); - - this->destroyWebIO(); - this->done = true; - } - - void destroyWebIO() - { - if (this->webIO) - { - delete this->webIO; - this->webIO = nullptr; - } - } - }; - static mg_mgr Mgr; static ClientDownload CLDownload; - static std::vector> ScriptDownloads; static std::thread ServerThread; static bool Terminate; static bool ServerRunning; diff --git a/src/Components/Modules/GSC/Script.cpp b/src/Components/Modules/GSC/Script.cpp index 8cd33cad..ee3a1595 100644 --- a/src/Components/Modules/GSC/Script.cpp +++ b/src/Components/Modules/GSC/Script.cpp @@ -531,6 +531,14 @@ namespace Components return &Game::svs_clients[ent->s.number]; } + void Script::ShowDeprecationWarning() + { + Toast::Show("cardicon_gumby", "WARNING!", "You are using deprecated HttpGet/HttpCancel GSC function.", 2048); + Logger::Print(Game::CON_CHANNEL_SCRIPT, "*** DEPRECATION WARNING ***\n"); + Logger::PrintError(Game::CON_CHANNEL_ERROR, "Attempted to execute deprecated built-in HttpGet/HttpCancel! These functions have been deemed unsafe and are scheduled for removal. Please update your mod!\n"); + Logger::Print(Game::CON_CHANNEL_SCRIPT, "***************************\n"); + } + void Script::AddFunctions() { Script::AddFunction("ReplaceFunc", [] // gsc: ReplaceFunc(, ) diff --git a/src/Components/Modules/GSC/Script.hpp b/src/Components/Modules/GSC/Script.hpp index 0b585d94..d80f952e 100644 --- a/src/Components/Modules/GSC/Script.hpp +++ b/src/Components/Modules/GSC/Script.hpp @@ -14,6 +14,8 @@ namespace Components static const char* GetCodePosForParam(int index); + static void ShowDeprecationWarning(); + private: struct ScriptFunction { @@ -59,7 +61,6 @@ namespace Components static void Scr_StartupGameType_Stub(); static void GScr_LoadGameTypeScript_Stub(); - static bool IsDeprecated(const std::string& name); static Game::BuiltinFunction BuiltIn_GetFunctionStub(const char** pName, int* type); static Game::BuiltinMethod BuiltIn_GetMethodStub(const char** pName, int* type);