From af03c72f761d8a5c07ca1960e1635f10c9f9851a Mon Sep 17 00:00:00 2001 From: Imarok Date: Tue, 5 Dec 2017 17:17:21 +0000 Subject: [PATCH] Move `GetAvailableMods` Reviewed by: elexis Differential Revision: https://code.wildfiregames.com/D1106 This was SVN commit r20591. --- source/ps/Mod.cpp | 79 ++++++++++++++++++++++++- source/ps/Mod.h | 7 ++- source/ps/scripting/JSInterface_Mod.cpp | 79 +------------------------ source/ps/scripting/JSInterface_Mod.h | 2 + 4 files changed, 88 insertions(+), 79 deletions(-) diff --git a/source/ps/Mod.cpp b/source/ps/Mod.cpp index 2404e6cd0b..2d017b5a56 100644 --- a/source/ps/Mod.cpp +++ b/source/ps/Mod.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2014 Wildfire Games. +/* Copyright (C) 2017 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -19,6 +19,83 @@ #include "ps/Mod.h" +#include + +#include "lib/file/file_system.h" +#include "lib/file/vfs/vfs.h" +#include "lib/utf8.h" +#include "ps/Filesystem.h" +#include "ps/GameSetup/GameSetup.h" +#include "ps/GameSetup/Paths.h" + std::vector g_modsLoaded; CmdLineArgs g_args; + +JS::Value Mod::GetAvailableMods(const ScriptInterface& scriptInterface) +{ + JSContext* cx = scriptInterface.GetContext(); + JSAutoRequest rq(cx); + JS::RootedObject obj(cx, JS_NewPlainObject(cx)); + + const Paths paths(g_args); + + // loop over all possible paths + OsPath modPath = paths.RData()/"mods"; + OsPath modUserPath = paths.UserData()/"mods"; + + DirectoryNames modDirs; + DirectoryNames modDirsUser; + + GetDirectoryEntries(modPath, NULL, &modDirs); + // Sort modDirs so that we can do a fast lookup below + std::sort(modDirs.begin(), modDirs.end()); + + PIVFS vfs = CreateVfs(1); // No cache needed; TODO but 0 crashes + + for (DirectoryNames::iterator iter = modDirs.begin(); iter != modDirs.end(); ++iter) + { + vfs->Clear(); + if (vfs->Mount(L"", modPath / *iter, VFS_MOUNT_MUST_EXIST) < 0) + continue; + + CVFSFile modinfo; + if (modinfo.Load(vfs, L"mod.json", false) != PSRETURN_OK) + continue; + + JS::RootedValue json(cx); + if (!scriptInterface.ParseJSON(modinfo.GetAsString(), &json)) + continue; + + // Valid mod, add it to our structure + JS_SetProperty(cx, obj, utf8_from_wstring(iter->string()).c_str(), json); + } + + GetDirectoryEntries(modUserPath, NULL, &modDirsUser); + bool dev = InDevelopmentCopy(); + + for (DirectoryNames::iterator iter = modDirsUser.begin(); iter != modDirsUser.end(); ++iter) + { + // If we are in a dev copy we do not mount mods in the user mod folder that + // are already present in the mod folder, thus we skip those here. + if (dev && std::binary_search(modDirs.begin(), modDirs.end(), *iter)) + continue; + + vfs->Clear(); + if (vfs->Mount(L"", modUserPath / *iter, VFS_MOUNT_MUST_EXIST) < 0) + continue; + + CVFSFile modinfo; + if (modinfo.Load(vfs, L"mod.json", false) != PSRETURN_OK) + continue; + + JS::RootedValue json(cx); + if (!scriptInterface.ParseJSON(modinfo.GetAsString(), &json)) + continue; + + // Valid mod, add it to our structure + JS_SetProperty(cx, obj, utf8_from_wstring(iter->string()).c_str(), json); + } + + return JS::ObjectValue(*obj); +} diff --git a/source/ps/Mod.h b/source/ps/Mod.h index 46a473fd03..b1848522a4 100644 --- a/source/ps/Mod.h +++ b/source/ps/Mod.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2014 Wildfire Games. +/* Copyright (C) 2017 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -20,8 +20,13 @@ #include "ps/CStr.h" #include "ps/GameSetup/CmdLineArgs.h" +#include "scriptinterface/ScriptInterface.h" extern std::vector g_modsLoaded; extern CmdLineArgs g_args; +namespace Mod +{ + JS::Value GetAvailableMods(const ScriptInterface& scriptInterface); +} #endif // INCLUDED_MOD diff --git a/source/ps/scripting/JSInterface_Mod.cpp b/source/ps/scripting/JSInterface_Mod.cpp index 31d28e054e..21e4ecab0e 100644 --- a/source/ps/scripting/JSInterface_Mod.cpp +++ b/source/ps/scripting/JSInterface_Mod.cpp @@ -17,20 +17,9 @@ #include "precompiled.h" -#include "scriptinterface/ScriptInterface.h" -#include "scriptinterface/ScriptVal.h" - -#include "lib/file/file_system.h" -#include "lib/file/vfs/vfs.h" -#include "lib/utf8.h" -#include "ps/Filesystem.h" -#include "ps/GameSetup/GameSetup.h" -#include "ps/GameSetup/Paths.h" -#include "ps/Mod.h" -#include "ps/Profile.h" #include "ps/scripting/JSInterface_Mod.h" -#include +#include "ps/Mod.h" extern void restart_engine(); @@ -46,71 +35,7 @@ extern void restart_engine(); */ JS::Value JSI_Mod::GetAvailableMods(ScriptInterface::CxPrivate* pCxPrivate) { - ScriptInterface* scriptInterface = pCxPrivate->pScriptInterface; - JSContext* cx = scriptInterface->GetContext(); - JSAutoRequest rq(cx); - JS::RootedObject obj(cx, JS_NewPlainObject(cx)); - - const Paths paths(g_args); - - // loop over all possible paths - OsPath modPath = paths.RData()/"mods"; - OsPath modUserPath = paths.UserData()/"mods"; - - DirectoryNames modDirs; - DirectoryNames modDirsUser; - - GetDirectoryEntries(modPath, NULL, &modDirs); - // Sort modDirs so that we can do a fast lookup below - std::sort(modDirs.begin(), modDirs.end()); - - PIVFS vfs = CreateVfs(1); // No cache needed; TODO but 0 crashes - - for (DirectoryNames::iterator iter = modDirs.begin(); iter != modDirs.end(); ++iter) - { - vfs->Clear(); - if (vfs->Mount(L"", modPath / *iter, VFS_MOUNT_MUST_EXIST) < 0) - continue; - - CVFSFile modinfo; - if (modinfo.Load(vfs, L"mod.json", false) != PSRETURN_OK) - continue; - - JS::RootedValue json(cx); - if (!scriptInterface->ParseJSON(modinfo.GetAsString(), &json)) - continue; - - // Valid mod, add it to our structure - JS_SetProperty(cx, obj, utf8_from_wstring(iter->string()).c_str(), json); - } - - GetDirectoryEntries(modUserPath, NULL, &modDirsUser); - bool dev = InDevelopmentCopy(); - - for (DirectoryNames::iterator iter = modDirsUser.begin(); iter != modDirsUser.end(); ++iter) - { - // If we are in a dev copy we do not mount mods in the user mod folder that - // are already present in the mod folder, thus we skip those here. - if (dev && std::binary_search(modDirs.begin(), modDirs.end(), *iter)) - continue; - - vfs->Clear(); - if (vfs->Mount(L"", modUserPath / *iter, VFS_MOUNT_MUST_EXIST) < 0) - continue; - - CVFSFile modinfo; - if (modinfo.Load(vfs, L"mod.json", false) != PSRETURN_OK) - continue; - - JS::RootedValue json(cx); - if (!scriptInterface->ParseJSON(modinfo.GetAsString(), &json)) - continue; - - // Valid mod, add it to our structure - JS_SetProperty(cx, obj, utf8_from_wstring(iter->string()).c_str(), json); - } - - return JS::ObjectValue(*obj); + return Mod::GetAvailableMods(*(pCxPrivate->pScriptInterface)); } void JSI_Mod::RestartEngine(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) diff --git a/source/ps/scripting/JSInterface_Mod.h b/source/ps/scripting/JSInterface_Mod.h index 0930070565..f364bb66dc 100644 --- a/source/ps/scripting/JSInterface_Mod.h +++ b/source/ps/scripting/JSInterface_Mod.h @@ -18,6 +18,8 @@ #ifndef INCLUDED_JSI_MOD #define INCLUDED_JSI_MOD +#include "scriptinterface/ScriptInterface.h" + class ScriptInterface; namespace JSI_Mod