Changeset View
Standalone View
source/ps/scripting/JSInterface_Mod.cpp
/* Copyright (C) 2018 Wildfire Games. | /* Copyright (C) 2018 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 "ps/scripting/JSInterface_Mod.h" | #include "JSInterface_Mod.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 "ps/CLogger.h" | |||||
#include "ps/Mod.h" | #include "ps/Mod.h" | ||||
#include "ps/ModIo.h" | |||||
extern void restart_engine(); | extern void restart_engine(); | ||||
JS::Value JSI_Mod::GetEngineInfo(ScriptInterface::CxPrivate* pCxPrivate) | JS::Value JSI_Mod::GetEngineInfo(ScriptInterface::CxPrivate* pCxPrivate) | ||||
{ | { | ||||
return Mod::GetEngineInfo(*(pCxPrivate->pScriptInterface)); | return Mod::GetEngineInfo(*(pCxPrivate->pScriptInterface)); | ||||
} | } | ||||
Show All 17 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; | ||||
} | } | ||||
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) | |||||
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. | |||||
{ | |||||
LOGERROR("ModIoStartDownloadMod called before ModIoStartGetGameId"); | |||||
return; | |||||
} | |||||
g_ModIo->StartDownloadMod(idx); | |||||
} | |||||
bool JSI_Mod::ModIoAdvanceRequest(ScriptInterface::CxPrivate* pCxPrivate) | |||||
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… | |||||
{ | |||||
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<ModIoModData>& 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<std::string, std::string>& 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<DownloadProgressStatus, std::string> statusStrings = { | |||||
leperUnsubmitted 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… | |||||
ItmsAuthorUnsubmitted Not Done Inline ActionsFixed with unordered_map locally. Itms: Fixed with `unordered_map` locally. | |||||
{ 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) | |||||
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… | |||||
{ | |||||
if (!g_ModIo) | |||||
{ | |||||
LOGERROR("ModIoGetDownloadProgress called before ModIoGetMods"); | |||||
return JS::NullValue(); | |||||
} | |||||
ScriptInterface* scriptInterface = pCxPrivate->pScriptInterface; | |||||
Done Inline ActionsWhy two? leper: Why two? | |||||
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) | void JSI_Mod::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
scriptInterface.RegisterFunction<JS::Value, &GetEngineInfo>("GetEngineInfo"); | scriptInterface.RegisterFunction<JS::Value, &GetEngineInfo>("GetEngineInfo"); | ||||
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<void, &JSI_Mod::ModIoStartGetGameId>("ModIoStartGetGameId"); | |||||
scriptInterface.RegisterFunction<void, &JSI_Mod::ModIoStartListMods>("ModIoStartListMods"); | |||||
scriptInterface.RegisterFunction<void, uint32_t, &JSI_Mod::ModIoStartDownloadMod>("ModIoStartDownloadMod"); | |||||
scriptInterface.RegisterFunction<bool, &JSI_Mod::ModIoAdvanceRequest>("ModIoAdvanceRequest"); | |||||
scriptInterface.RegisterFunction<void, &JSI_Mod::ModIoCancelRequest>("ModIoCancelRequest"); | |||||
scriptInterface.RegisterFunction<JS::Value, &JSI_Mod::ModIoGetMods>("ModIoGetMods"); | |||||
scriptInterface.RegisterFunction<JS::Value, &JSI_Mod::ModIoGetDownloadProgress>("ModIoGetDownloadProgress"); | |||||
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. | |||||
} | } |
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.