2020-11-13 14:18:22 +01:00
|
|
|
/* Copyright (C) 2020 Wildfire Games.
|
2014-08-25 18:02:40 +02: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 "ps/Mod.h"
|
|
|
|
|
2017-12-05 18:17:21 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#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"
|
2020-01-02 19:07:40 +01:00
|
|
|
#include "ps/Pyrogenesis.h"
|
2018-04-27 18:48:44 +02:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2017-12-05 18:17:21 +01:00
|
|
|
|
2014-08-25 18:02:40 +02:00
|
|
|
std::vector<CStr> g_modsLoaded;
|
|
|
|
|
2018-05-24 20:08:56 +02:00
|
|
|
std::vector<std::vector<CStr>> g_LoadedModVersions;
|
|
|
|
|
2014-08-25 18:02:40 +02:00
|
|
|
CmdLineArgs g_args;
|
2017-12-05 18:17:21 +01:00
|
|
|
|
|
|
|
JS::Value Mod::GetAvailableMods(const ScriptInterface& scriptInterface)
|
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptRequest rq(scriptInterface);
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedObject obj(rq.cx, JS_NewPlainObject(rq.cx));
|
2017-12-05 18:17:21 +01:00
|
|
|
|
|
|
|
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());
|
|
|
|
|
2017-12-10 18:33:03 +01:00
|
|
|
PIVFS vfs = CreateVfs();
|
2017-12-05 18:17:21 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue json(rq.cx);
|
2017-12-05 18:17:21 +01:00
|
|
|
if (!scriptInterface.ParseJSON(modinfo.GetAsString(), &json))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Valid mod, add it to our structure
|
2020-11-13 14:18:22 +01:00
|
|
|
JS_SetProperty(rq.cx, obj, utf8_from_wstring(iter->string()).c_str(), json);
|
2017-12-05 18:17:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue json(rq.cx);
|
2017-12-05 18:17:21 +01:00
|
|
|
if (!scriptInterface.ParseJSON(modinfo.GetAsString(), &json))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Valid mod, add it to our structure
|
2020-11-13 14:18:22 +01:00
|
|
|
JS_SetProperty(rq.cx, obj, utf8_from_wstring(iter->string()).c_str(), json);
|
2017-12-05 18:17:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return JS::ObjectValue(*obj);
|
|
|
|
}
|
2018-02-17 17:53:14 +01:00
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
void Mod::CacheEnabledModVersions(const shared_ptr<ScriptContext>& scriptContext)
|
2018-02-17 17:53:14 +01:00
|
|
|
{
|
2020-11-14 11:57:50 +01:00
|
|
|
ScriptInterface scriptInterface("Engine", "CacheEnabledModVersions", scriptContext);
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptRequest rq(scriptInterface);
|
2018-02-17 17:53:14 +01:00
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue availableMods(rq.cx, GetAvailableMods(scriptInterface));
|
2018-02-17 17:53:14 +01:00
|
|
|
|
2018-05-24 20:08:56 +02:00
|
|
|
g_LoadedModVersions.clear();
|
2018-02-17 17:53:14 +01:00
|
|
|
|
2018-05-24 20:08:56 +02:00
|
|
|
for (const CStr& mod : g_modsLoaded)
|
2018-02-17 17:53:14 +01:00
|
|
|
{
|
|
|
|
// Ignore user and mod mod as they are irrelevant for compatibility checks
|
2018-05-24 20:08:56 +02:00
|
|
|
if (mod == "mod" || mod == "user")
|
2018-02-17 17:53:14 +01:00
|
|
|
continue;
|
2018-05-24 20:08:56 +02:00
|
|
|
|
2018-02-17 17:53:14 +01:00
|
|
|
CStr version;
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue modData(rq.cx);
|
2018-05-24 20:08:56 +02:00
|
|
|
if (scriptInterface.GetProperty(availableMods, mod.c_str(), &modData))
|
2018-02-17 17:53:14 +01:00
|
|
|
scriptInterface.GetProperty(modData, "version", version);
|
2018-05-24 20:08:56 +02:00
|
|
|
|
|
|
|
g_LoadedModVersions.push_back({mod, version});
|
2018-02-17 17:53:14 +01:00
|
|
|
}
|
2018-05-24 20:08:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value Mod::GetLoadedModsWithVersions(const ScriptInterface& scriptInterface)
|
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptRequest rq(scriptInterface);
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue returnValue(rq.cx);
|
|
|
|
scriptInterface.ToJSVal(rq, &returnValue, g_LoadedModVersions);
|
2018-05-24 20:08:56 +02:00
|
|
|
return returnValue;
|
2018-02-17 17:53:14 +01:00
|
|
|
}
|
2018-02-17 18:36:43 +01:00
|
|
|
|
|
|
|
JS::Value Mod::GetEngineInfo(const ScriptInterface& scriptInterface)
|
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptRequest rq(scriptInterface);
|
2018-02-17 18:36:43 +01:00
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue mods(rq.cx, Mod::GetLoadedModsWithVersions(scriptInterface));
|
|
|
|
JS::RootedValue metainfo(rq.cx);
|
2019-07-22 21:35:14 +02:00
|
|
|
|
2019-09-13 02:56:51 +02:00
|
|
|
ScriptInterface::CreateObject(
|
2020-11-13 14:18:22 +01:00
|
|
|
rq,
|
2019-07-22 21:35:14 +02:00
|
|
|
&metainfo,
|
2019-09-13 02:56:51 +02:00
|
|
|
"engine_version", engine_version,
|
2019-07-22 21:35:14 +02:00
|
|
|
"mods", mods);
|
2018-02-17 18:36:43 +01:00
|
|
|
|
|
|
|
scriptInterface.FreezeObject(metainfo, true);
|
|
|
|
|
|
|
|
return metainfo;
|
|
|
|
}
|