[Download] Nullsub some quirky gsc functions (#449)

* [Download] Nullsub some quirky gsc functions

* [Script] Show deprecation warning

* [Script] Expand error message
This commit is contained in:
Edo 2022-08-22 18:17:42 +02:00 committed by GitHub
parent 92ed9afae7
commit 28653a0e8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 199 deletions

View File

@ -5,7 +5,6 @@ namespace Components
{
mg_mgr Download::Mgr;
Download::ClientDownload Download::CLDownload;
std::vector<std::shared_ptr<Download::ScriptDownload>> Download::ScriptDownloads;
std::thread Download::ServerThread;
bool Download::Terminate;
@ -927,77 +926,8 @@ namespace Components
Dvar::Register<bool>("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<ScriptDownload>(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();
}
}

View File

@ -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<int>(this->totalSize));
Game::Scr_AddInt(static_cast<int>(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<std::shared_ptr<ScriptDownload>> ScriptDownloads;
static std::thread ServerThread;
static bool Terminate;
static bool ServerRunning;

View File

@ -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(<function>, <function>)

View File

@ -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);