2023-07-07 13:12:16 -07:00
|
|
|
/* Copyright (C) 2023 Wildfire Games.
|
2018-04-24 06:12:08 -07:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "JSInterface_ModIo.h"
|
|
|
|
|
|
|
|
|
|
#include "ps/CLogger.h"
|
|
|
|
|
#include "ps/ModIo.h"
|
2021-03-02 12:01:14 -08:00
|
|
|
#include "scriptinterface/FunctionWrapper.h"
|
2023-07-07 13:12:16 -07:00
|
|
|
#include "scriptinterface/Object.h"
|
2018-04-24 06:12:08 -07:00
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
namespace JSI_ModIo
|
2018-04-24 06:12:08 -07:00
|
|
|
{
|
2021-03-02 12:01:14 -08:00
|
|
|
ModIo* ModIoGetter(const ScriptRequest&, JS::CallArgs&)
|
2018-04-24 06:12:08 -07:00
|
|
|
{
|
|
|
|
|
if (!g_ModIo)
|
|
|
|
|
{
|
2021-03-02 12:01:14 -08:00
|
|
|
LOGERROR("Trying to access ModIO when it's not initialized!");
|
|
|
|
|
return nullptr;
|
2018-04-24 06:12:08 -07:00
|
|
|
}
|
2021-03-02 12:01:14 -08:00
|
|
|
return g_ModIo;
|
2018-04-24 06:12:08 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
void StartGetGameId()
|
2018-04-24 06:12:08 -07:00
|
|
|
{
|
|
|
|
|
if (!g_ModIo)
|
2021-03-02 12:01:14 -08:00
|
|
|
g_ModIo = new ModIo();
|
2018-04-24 06:12:08 -07:00
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
ENSURE(g_ModIo);
|
2018-04-24 06:12:08 -07:00
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
g_ModIo->StartGetGameId();
|
2018-04-24 06:12:08 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
// TODO: could provide a FromJSVal for ModIoModData
|
2021-05-13 10:23:52 -07:00
|
|
|
JS::Value GetMods(const ScriptRequest& rq)
|
2018-04-24 06:12:08 -07:00
|
|
|
{
|
|
|
|
|
if (!g_ModIo)
|
|
|
|
|
{
|
|
|
|
|
LOGERROR("ModIoGetMods called before ModIoStartGetGameId");
|
|
|
|
|
return JS::NullValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::vector<ModIoModData>& availableMods = g_ModIo->GetMods();
|
|
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue mods(rq.cx);
|
2021-05-13 10:23:52 -07:00
|
|
|
Script::CreateArray(rq, &mods, availableMods.size());
|
2018-04-24 06:12:08 -07:00
|
|
|
|
|
|
|
|
u32 i = 0;
|
|
|
|
|
for (const ModIoModData& mod : availableMods)
|
|
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue m(rq.cx);
|
2021-05-13 10:23:52 -07:00
|
|
|
Script::CreateObject(rq, &m);
|
2019-07-22 12:35:14 -07:00
|
|
|
|
2020-12-31 06:27:02 -08:00
|
|
|
for (const std::pair<const std::string, std::string>& prop : mod.properties)
|
2021-05-13 10:23:52 -07:00
|
|
|
Script::SetProperty(rq, m, prop.first.c_str(), prop.second, true);
|
2018-04-24 06:12:08 -07:00
|
|
|
|
2021-05-13 10:23:52 -07:00
|
|
|
Script::SetProperty(rq, m, "dependencies", mod.dependencies, true);
|
|
|
|
|
Script::SetPropertyInt(rq, mods, i++, m);
|
2018-04-24 06:12:08 -07:00
|
|
|
}
|
|
|
|
|
|
2019-07-22 12:35:14 -07:00
|
|
|
return mods;
|
2018-04-24 06:12:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::map<DownloadProgressStatus, std::string> statusStrings = {
|
|
|
|
|
{ DownloadProgressStatus::NONE, "none" },
|
|
|
|
|
{ DownloadProgressStatus::GAMEID, "gameid" },
|
|
|
|
|
{ DownloadProgressStatus::READY, "ready" },
|
|
|
|
|
{ DownloadProgressStatus::LISTING, "listing" },
|
|
|
|
|
{ DownloadProgressStatus::LISTED, "listed" },
|
|
|
|
|
{ DownloadProgressStatus::DOWNLOADING, "downloading" },
|
|
|
|
|
{ DownloadProgressStatus::SUCCESS, "success" },
|
|
|
|
|
{ DownloadProgressStatus::FAILED_GAMEID, "failed_gameid" },
|
|
|
|
|
{ DownloadProgressStatus::FAILED_LISTING, "failed_listing" },
|
|
|
|
|
{ DownloadProgressStatus::FAILED_DOWNLOADING, "failed_downloading" },
|
|
|
|
|
{ DownloadProgressStatus::FAILED_FILECHECK, "failed_filecheck" }
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
// TODO: could provide a FromJSVal for DownloadProgressData
|
2021-05-13 10:23:52 -07:00
|
|
|
JS::Value GetDownloadProgress(const ScriptRequest& rq)
|
2018-04-24 06:12:08 -07:00
|
|
|
{
|
|
|
|
|
if (!g_ModIo)
|
|
|
|
|
{
|
|
|
|
|
LOGERROR("ModIoGetDownloadProgress called before ModIoGetMods");
|
|
|
|
|
return JS::NullValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const DownloadProgressData& progress = g_ModIo->GetDownloadProgress();
|
|
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue progressData(rq.cx);
|
2021-05-13 10:23:52 -07:00
|
|
|
Script::CreateObject(rq, &progressData);
|
|
|
|
|
Script::SetProperty(rq, progressData, "status", statusStrings.at(progress.status), true);
|
|
|
|
|
Script::SetProperty(rq, progressData, "progress", progress.progress, true);
|
|
|
|
|
Script::SetProperty(rq, progressData, "error", progress.error, true);
|
2018-04-24 06:12:08 -07:00
|
|
|
|
|
|
|
|
return progressData;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
void RegisterScriptFunctions(const ScriptRequest& rq)
|
2018-04-24 06:12:08 -07:00
|
|
|
{
|
2021-03-02 12:01:14 -08:00
|
|
|
ScriptFunction::Register<&StartGetGameId>(rq, "ModIoStartGetGameId");
|
|
|
|
|
ScriptFunction::Register<&ModIo::StartListMods, &ModIoGetter>(rq, "ModIoStartListMods");
|
|
|
|
|
ScriptFunction::Register<&ModIo::StartDownloadMod, &ModIoGetter>(rq, "ModIoStartDownloadMod");
|
|
|
|
|
ScriptFunction::Register<&ModIo::AdvanceRequest, &ModIoGetter>(rq, "ModIoAdvanceRequest");
|
|
|
|
|
ScriptFunction::Register<&ModIo::CancelRequest, &ModIoGetter>(rq, "ModIoCancelRequest");
|
|
|
|
|
ScriptFunction::Register<&GetMods>(rq, "ModIoGetMods");
|
|
|
|
|
ScriptFunction::Register<&GetDownloadProgress>(rq, "ModIoGetDownloadProgress");
|
|
|
|
|
}
|
2018-04-24 06:12:08 -07:00
|
|
|
}
|