Index: ps/trunk/source/gui/scripting/ScriptFunctions.cpp =================================================================== --- ps/trunk/source/gui/scripting/ScriptFunctions.cpp +++ ps/trunk/source/gui/scripting/ScriptFunctions.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 Wildfire Games. +/* Copyright (C) 2018 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -32,6 +32,7 @@ #include "ps/scripting/JSInterface_Game.h" #include "ps/scripting/JSInterface_Main.h" #include "ps/scripting/JSInterface_Mod.h" +#include "ps/scripting/JSInterface_ModIo.h" #include "ps/scripting/JSInterface_SavedGame.h" #include "ps/scripting/JSInterface_VFS.h" #include "ps/scripting/JSInterface_VisualReplay.h" @@ -60,6 +61,7 @@ JSI_Lobby::RegisterScriptFunctions(scriptInterface); JSI_Main::RegisterScriptFunctions(scriptInterface); JSI_Mod::RegisterScriptFunctions(scriptInterface); + JSI_ModIo::RegisterScriptFunctions(scriptInterface); JSI_Network::RegisterScriptFunctions(scriptInterface); JSI_Renderer::RegisterScriptFunctions(scriptInterface); JSI_SavedGame::RegisterScriptFunctions(scriptInterface); Index: ps/trunk/source/ps/scripting/JSInterface_Mod.h =================================================================== --- ps/trunk/source/ps/scripting/JSInterface_Mod.h +++ ps/trunk/source/ps/scripting/JSInterface_Mod.h @@ -26,18 +26,11 @@ namespace JSI_Mod { void RegisterScriptFunctions(const ScriptInterface& scriptInterface); + JS::Value GetEngineInfo(ScriptInterface::CxPrivate* pCxPrivate); JS::Value GetAvailableMods(ScriptInterface::CxPrivate* pCxPrivate); void RestartEngine(ScriptInterface::CxPrivate* pCxPrivate); void SetMods(ScriptInterface::CxPrivate* pCxPrivate, const std::vector& mods); - - void ModIoStartGetGameId(ScriptInterface::CxPrivate* pCxPrivate); - void ModIoStartListMods(ScriptInterface::CxPrivate* pCxPrivate); - void ModIoStartDownloadMod(ScriptInterface::CxPrivate* pCxPrivate, uint32_t idx); - bool ModIoAdvanceRequest(ScriptInterface::CxPrivate* pCxPrivate); - void ModIoCancelRequest(ScriptInterface::CxPrivate* pCxPrivate); - JS::Value ModIoGetMods(ScriptInterface::CxPrivate* pCxPrivate); - JS::Value ModIoGetDownloadProgress(ScriptInterface::CxPrivate* pCxPrivate); } -#endif +#endif // INCLUDED_JSI_MOD Index: ps/trunk/source/ps/scripting/JSInterface_Mod.cpp =================================================================== --- ps/trunk/source/ps/scripting/JSInterface_Mod.cpp +++ ps/trunk/source/ps/scripting/JSInterface_Mod.cpp @@ -19,9 +19,7 @@ #include "JSInterface_Mod.h" -#include "ps/CLogger.h" #include "ps/Mod.h" -#include "ps/ModIo.h" extern void restart_engine(); @@ -55,142 +53,10 @@ g_modsLoaded = mods; } -void JSI_Mod::ModIoStartGetGameId(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) -{ - if (!g_ModIo) - g_ModIo = new ModIo(); - - ENSURE(g_ModIo); - - g_ModIo->StartGetGameId(); -} - -void JSI_Mod::ModIoStartListMods(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) -{ - if (!g_ModIo) - { - LOGERROR("ModIoStartListMods called before ModIoStartGetGameId"); - return; - } - - g_ModIo->StartListMods(); -} - -void JSI_Mod::ModIoStartDownloadMod(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), uint32_t idx) -{ - if (!g_ModIo) - { - LOGERROR("ModIoStartDownloadMod called before ModIoStartGetGameId"); - return; - } - - g_ModIo->StartDownloadMod(idx); -} - -bool JSI_Mod::ModIoAdvanceRequest(ScriptInterface::CxPrivate* pCxPrivate) -{ - if (!g_ModIo) - { - LOGERROR("ModIoAdvanceRequest called before ModIoGetMods"); - return false; - } - - ScriptInterface* scriptInterface = pCxPrivate->pScriptInterface; - return g_ModIo->AdvanceRequest(*scriptInterface); -} - -void JSI_Mod::ModIoCancelRequest(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) -{ - if (!g_ModIo) - { - LOGERROR("ModIoCancelRequest called before ModIoGetMods"); - return; - } - - g_ModIo->CancelRequest(); -} - -JS::Value JSI_Mod::ModIoGetMods(ScriptInterface::CxPrivate* pCxPrivate) -{ - if (!g_ModIo) - { - LOGERROR("ModIoGetMods called before ModIoStartGetGameId"); - return JS::NullValue(); - } - - ScriptInterface* scriptInterface = pCxPrivate->pScriptInterface; - JSContext* cx = scriptInterface->GetContext(); - JSAutoRequest rq(cx); - - const std::vector& availableMods = g_ModIo->GetMods(); - - JS::RootedObject mods(cx, JS_NewArrayObject(cx, availableMods.size())); - if (!mods) - return JS::NullValue(); - - u32 i = 0; - for (const ModIoModData& mod : availableMods) - { - JS::RootedValue m(cx, JS::ObjectValue(*JS_NewPlainObject(cx))); - for (const std::pair& prop : mod.properties) - scriptInterface->SetProperty(m, prop.first.c_str(), prop.second, true); - - scriptInterface->SetProperty(m, "dependencies", mod.dependencies, true); - - JS_SetElement(cx, mods, i++, m); - } - - return JS::ObjectValue(*mods); -} - -const std::map 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" } -}; - -JS::Value JSI_Mod::ModIoGetDownloadProgress(ScriptInterface::CxPrivate* pCxPrivate) -{ - if (!g_ModIo) - { - LOGERROR("ModIoGetDownloadProgress called before ModIoGetMods"); - return JS::NullValue(); - } - - ScriptInterface* scriptInterface = pCxPrivate->pScriptInterface; - JSContext* cx = scriptInterface->GetContext(); - JSAutoRequest rq(cx); - - JS::RootedValue progressData(cx, JS::ObjectValue(*JS_NewPlainObject(cx))); - const DownloadProgressData& progress = g_ModIo->GetDownloadProgress(); - - scriptInterface->SetProperty(progressData, "status", statusStrings.at(progress.status), true); - scriptInterface->SetProperty(progressData, "progress", progress.progress, true); - scriptInterface->SetProperty(progressData, "error", progress.error, true); - - return progressData; -} - void JSI_Mod::RegisterScriptFunctions(const ScriptInterface& scriptInterface) { scriptInterface.RegisterFunction("GetEngineInfo"); scriptInterface.RegisterFunction("GetAvailableMods"); scriptInterface.RegisterFunction("RestartEngine"); scriptInterface.RegisterFunction, &JSI_Mod::SetMods>("SetMods"); - - scriptInterface.RegisterFunction("ModIoStartGetGameId"); - scriptInterface.RegisterFunction("ModIoStartListMods"); - scriptInterface.RegisterFunction("ModIoStartDownloadMod"); - scriptInterface.RegisterFunction("ModIoAdvanceRequest"); - scriptInterface.RegisterFunction("ModIoCancelRequest"); - scriptInterface.RegisterFunction("ModIoGetMods"); - scriptInterface.RegisterFunction("ModIoGetDownloadProgress"); } Index: ps/trunk/source/ps/scripting/JSInterface_ModIo.h =================================================================== --- ps/trunk/source/ps/scripting/JSInterface_ModIo.h +++ ps/trunk/source/ps/scripting/JSInterface_ModIo.h @@ -0,0 +1,39 @@ +/* Copyright (C) 2018 Wildfire Games. + * 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 . + */ + +#ifndef INCLUDED_JSI_MODIO +#define INCLUDED_JSI_MODIO + +#include "ps/CStr.h" +#include "scriptinterface/ScriptInterface.h" + +class ScriptInterface; + +namespace JSI_ModIo +{ + void RegisterScriptFunctions(const ScriptInterface& scriptInterface); + + void StartGetGameId(ScriptInterface::CxPrivate* pCxPrivate); + void StartListMods(ScriptInterface::CxPrivate* pCxPrivate); + void StartDownloadMod(ScriptInterface::CxPrivate* pCxPrivate, uint32_t idx); + bool AdvanceRequest(ScriptInterface::CxPrivate* pCxPrivate); + void CancelRequest(ScriptInterface::CxPrivate* pCxPrivate); + JS::Value GetMods(ScriptInterface::CxPrivate* pCxPrivate); + JS::Value GetDownloadProgress(ScriptInterface::CxPrivate* pCxPrivate); +} + +#endif // INCLUDED_JSI_MODIO Index: ps/trunk/source/ps/scripting/JSInterface_ModIo.cpp =================================================================== --- ps/trunk/source/ps/scripting/JSInterface_ModIo.cpp +++ ps/trunk/source/ps/scripting/JSInterface_ModIo.cpp @@ -0,0 +1,159 @@ +/* Copyright (C) 2018 Wildfire Games. + * 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 . + */ + +#include "precompiled.h" + +#include "JSInterface_ModIo.h" + +#include "ps/CLogger.h" +#include "ps/Mod.h" +#include "ps/ModIo.h" + +void JSI_ModIo::StartGetGameId(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) +{ + if (!g_ModIo) + g_ModIo = new ModIo(); + + ENSURE(g_ModIo); + + g_ModIo->StartGetGameId(); +} + +void JSI_ModIo::StartListMods(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) +{ + if (!g_ModIo) + { + LOGERROR("ModIoStartListMods called before ModIoStartGetGameId"); + return; + } + + g_ModIo->StartListMods(); +} + +void JSI_ModIo::StartDownloadMod(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), uint32_t idx) +{ + if (!g_ModIo) + { + LOGERROR("ModIoStartDownloadMod called before ModIoStartGetGameId"); + return; + } + + g_ModIo->StartDownloadMod(idx); +} + +bool JSI_ModIo::AdvanceRequest(ScriptInterface::CxPrivate* pCxPrivate) +{ + if (!g_ModIo) + { + LOGERROR("ModIoAdvanceRequest called before ModIoGetMods"); + return false; + } + + ScriptInterface* scriptInterface = pCxPrivate->pScriptInterface; + return g_ModIo->AdvanceRequest(*scriptInterface); +} + +void JSI_ModIo::CancelRequest(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) +{ + if (!g_ModIo) + { + LOGERROR("ModIoCancelRequest called before ModIoGetMods"); + return; + } + + g_ModIo->CancelRequest(); +} + +JS::Value JSI_ModIo::GetMods(ScriptInterface::CxPrivate* pCxPrivate) +{ + if (!g_ModIo) + { + LOGERROR("ModIoGetMods called before ModIoStartGetGameId"); + return JS::NullValue(); + } + + ScriptInterface* scriptInterface = pCxPrivate->pScriptInterface; + JSContext* cx = scriptInterface->GetContext(); + JSAutoRequest rq(cx); + + const std::vector& availableMods = g_ModIo->GetMods(); + + JS::RootedObject mods(cx, JS_NewArrayObject(cx, availableMods.size())); + if (!mods) + return JS::NullValue(); + + u32 i = 0; + for (const ModIoModData& mod : availableMods) + { + JS::RootedValue m(cx, JS::ObjectValue(*JS_NewPlainObject(cx))); + for (const std::pair& prop : mod.properties) + scriptInterface->SetProperty(m, prop.first.c_str(), prop.second, true); + + scriptInterface->SetProperty(m, "dependencies", mod.dependencies, true); + + JS_SetElement(cx, mods, i++, m); + } + + return JS::ObjectValue(*mods); +} + +const std::map 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" } +}; + +JS::Value JSI_ModIo::GetDownloadProgress(ScriptInterface::CxPrivate* pCxPrivate) +{ + if (!g_ModIo) + { + LOGERROR("ModIoGetDownloadProgress called before ModIoGetMods"); + return JS::NullValue(); + } + + ScriptInterface* scriptInterface = pCxPrivate->pScriptInterface; + JSContext* cx = scriptInterface->GetContext(); + JSAutoRequest rq(cx); + + JS::RootedValue progressData(cx, JS::ObjectValue(*JS_NewPlainObject(cx))); + const DownloadProgressData& progress = g_ModIo->GetDownloadProgress(); + + scriptInterface->SetProperty(progressData, "status", statusStrings.at(progress.status), true); + scriptInterface->SetProperty(progressData, "progress", progress.progress, true); + scriptInterface->SetProperty(progressData, "error", progress.error, true); + + return progressData; +} + +void JSI_ModIo::RegisterScriptFunctions(const ScriptInterface& scriptInterface) +{ + scriptInterface.RegisterFunction("ModIoStartGetGameId"); + scriptInterface.RegisterFunction("ModIoStartListMods"); + scriptInterface.RegisterFunction("ModIoStartDownloadMod"); + scriptInterface.RegisterFunction("ModIoAdvanceRequest"); + scriptInterface.RegisterFunction("ModIoCancelRequest"); + scriptInterface.RegisterFunction("ModIoGetMods"); + scriptInterface.RegisterFunction("ModIoGetDownloadProgress"); +}