Changeset View
Standalone View
source/ps/scripting/JSInterface_Mod.cpp
/* Copyright (C) 2017 Wildfire Games. | /* Copyright (C) 2017 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 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 | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
* GNU General Public License for more details. | * GNU General Public License for more details. | ||||
* | * | ||||
* You should have received a copy of the GNU General Public License | * 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/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
leper: I might have mentioned it elsewhere, but given a few "contributions" to this thing since I last… | |||||
Not Done Inline ActionsThat shouldn't be a problem, but you are talking about the two ModIo source files, in order to allow others to use our mod.io API code, right? Itms: That shouldn't be a problem, but you are talking about the two `ModIo` source files, in order… | |||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "scriptinterface/ScriptVal.h" | #include "scriptinterface/ScriptVal.h" | ||||
Done Inline ActionsWhy? leper: Why? | |||||
Not Done Inline ActionsI thought you were following the convention from your comments above. This is it. Itms: I thought you were following the convention from your comments above. This is it. | |||||
Not Done Inline ActionsI'm quite certain I didn't have a TODO comment here about messing up includes. leper: I'm quite certain I didn't have a TODO comment here about messing up includes. | |||||
Not Done Inline ActionsSee elsewhere, still why and to be a little less obscure, WTH? leper: See elsewhere, still why and to be a little less obscure, WTH? | |||||
Not Done Inline ActionsSource files and their corresponding header will always move together, no? It's just style here to my eyes. Itms: Source files and their corresponding header will always move together, no? It's just style here… | |||||
#include "lib/file/file_system.h" | #include "lib/file/file_system.h" | ||||
#include "lib/file/vfs/vfs.h" | #include "lib/file/vfs/vfs.h" | ||||
#include "lib/utf8.h" | #include "lib/utf8.h" | ||||
#include "ps/Filesystem.h" | #include "ps/Filesystem.h" | ||||
#include "ps/GameSetup/GameSetup.h" | #include "ps/GameSetup/GameSetup.h" | ||||
#include "ps/GameSetup/Paths.h" | #include "ps/GameSetup/Paths.h" | ||||
#include "ps/Mod.h" | #include "ps/Mod.h" | ||||
#include "ps/ModIo.h" | |||||
#include "ps/Profile.h" | #include "ps/Profile.h" | ||||
#include "ps/scripting/JSInterface_Mod.h" | #include "ps/scripting/JSInterface_Mod.h" | ||||
#include <algorithm> | #include <algorithm> | ||||
extern void restart_engine(); | extern void restart_engine(); | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | void JSI_Mod::RestartEngine(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | ||||
restart_engine(); | restart_engine(); | ||||
} | } | ||||
void JSI_Mod::SetMods(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::vector<CStr>& mods) | void JSI_Mod::SetMods(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::vector<CStr>& mods) | ||||
{ | { | ||||
g_modsLoaded = mods; | g_modsLoaded = mods; | ||||
} | } | ||||
JS::Value JSI_Mod::ModIoGetMods(ScriptInterface::CxPrivate* pCxPrivate) | |||||
{ | |||||
ScriptInterface* scriptInterface = pCxPrivate->pScriptInterface; | |||||
JSContext* cx = scriptInterface->GetContext(); | |||||
JSAutoRequest rq(cx); | |||||
if (!g_ModIo) | |||||
g_ModIo = new ModIo(); | |||||
JS::RootedValue mods(cx); | |||||
if (!g_ModIo) | |||||
return mods; // TODO: error/warning? | |||||
scriptInterface->Eval("([])", &mods); | |||||
for (const std::map<std::string, std::string>& mod : g_ModIo->GetMods(*scriptInterface)) | |||||
{ | |||||
JS::RootedValue m(cx); | |||||
scriptInterface->Eval("({})", &m); | |||||
for (const std::pair<std::string, std::string>& prop : mod) | |||||
scriptInterface->SetProperty(m, prop.first.c_str(), prop.second, true); | |||||
scriptInterface->CallFunctionVoid(mods, "push", m); | |||||
Not Done Inline ActionsSee VisualReplay::GetReplays for Imaroks example how to create an array in c++ without calling JS functions like this. elexis: See `VisualReplay::GetReplays` for Imaroks example how to create an array in c++ without… | |||||
Not Done Inline ActionsThe interface should be minimized. elexis: The interface should be minimized. | |||||
} | |||||
return mods; | |||||
} | |||||
void JSI_Mod::ModIoDownloadMod(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), uint32_t idx) | |||||
{ | |||||
if (!g_ModIo) | |||||
return; // TODO: Warn and tell users to fix their code? | |||||
Done Inline ActionsENSURE would be the common solution elexis: ENSURE would be the common solution | |||||
Not Done Inline ActionsBecause having a nice way for a user to make the game running in full screen stop responding because it's now waiting for input for the debugger prompt is great usability. leper: Because having a nice way for a user to make the game running in full screen stop responding… | |||||
g_ModIo->DownloadMod(idx); | |||||
} | |||||
void JSI_Mod::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | void JSI_Mod::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
scriptInterface.RegisterFunction<JS::Value, &JSI_Mod::GetAvailableMods>("GetAvailableMods"); | scriptInterface.RegisterFunction<JS::Value, &JSI_Mod::GetAvailableMods>("GetAvailableMods"); | ||||
scriptInterface.RegisterFunction<void, &JSI_Mod::RestartEngine>("RestartEngine"); | scriptInterface.RegisterFunction<void, &JSI_Mod::RestartEngine>("RestartEngine"); | ||||
scriptInterface.RegisterFunction<void, std::vector<CStr>, &JSI_Mod::SetMods>("SetMods"); | scriptInterface.RegisterFunction<void, std::vector<CStr>, &JSI_Mod::SetMods>("SetMods"); | ||||
scriptInterface.RegisterFunction<JS::Value, &JSI_Mod::ModIoGetMods>("ModIoGetMods"); | |||||
scriptInterface.RegisterFunction<void, uint32_t, &JSI_Mod::ModIoDownloadMod>("ModIoDownloadMod"); | |||||
} | } | ||||
Not Done Inline ActionsThis block seems ugly. leper: This block seems ugly. | |||||
Not Done Inline ActionsAnd given that there is a huge fancy struct I'm somewhat surprised that this code reads as if one would use a pair or similar. leper: And given that there is a huge fancy struct I'm somewhat surprised that this code reads as if… | |||||
Not Done Inline ActionsI'm sorry I really don't see it. Could you give an example? Itms: I'm sorry I really don't see it. Could you give an example? | |||||
Not Done Inline ActionsIf we have a public definiton of a type, why not also have a public conversion function for that type, instead of having one line for each of those in here? (hint, the block starts here.) leper: If we have a public definiton of a type, why not also have a public conversion function for… | |||||
Done Inline ActionsThose have already been declared. I'm not really sure how they even ended up here. leper: Those have already been declared. I'm not really sure how they even ended up here. | |||||
Not Done Inline ActionsHuh, didn't even think that would compile. Itms: Huh, didn't even think that would compile. | |||||
Done Inline ActionsWhy two? leper: Why two? | |||||
Done Inline ActionsWill probably be a nice candidate for constexpr at some point, similar to other const things in the ModIo implementation. Could also be const char *, but that shouldn't really make any noticable difference here. Or unordered_map, since nothing seems to require any order here. leper: Will probably be a nice candidate for `constexpr` at some point, similar to other const things… | |||||
Not Done Inline ActionsFixed with unordered_map locally. Itms: Fixed with `unordered_map` locally. |
I might have mentioned it elsewhere, but given a few "contributions" to this thing since I last touched it, I'd still like to see this under a license that prefers distribution.