Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "JSInterface_Mod.h"
21 :
22 : #include "ps/CLogger.h"
23 : #include "ps/CStr.h"
24 : #include "ps/Mod.h"
25 : #include "ps/Pyrogenesis.h"
26 : #include "scriptinterface/FunctionWrapper.h"
27 : #include "scriptinterface/JSON.h"
28 : #include "scriptinterface/Object.h"
29 : #include "scriptinterface/Conversions.h"
30 : #include "scriptinterface/Request.h"
31 :
32 : #include <fmt/format.h>
33 : #include <js/Array.h>
34 : #include <js/RootingAPI.h>
35 : #include <js/TypeDecls.h>
36 : #include <js/Value.h>
37 : #include <stdexcept>
38 : #include <vector>
39 :
40 : namespace JS { class CallArgs; }
41 : namespace Script { class Interface; }
42 :
43 : extern void RestartEngine();
44 :
45 : // To avoid copying data needlessly in GetEngineInfo, implement a ToJSVal for pointer types.
46 : using ModDataCPtr = const Mod::ModData*;
47 :
48 : template<>
49 0 : void Script::ToJSVal(const Script::Request& rq, JS::MutableHandleValue ret, const ModDataCPtr& data)
50 : {
51 0 : ret.set(Script::CreateObject(rq));
52 0 : Script::SetProperty(rq, ret, "mod", data->m_Pathname);
53 0 : Script::SetProperty(rq, ret, "name", data->m_Name);
54 0 : Script::SetProperty(rq, ret, "version", data->m_Version);
55 0 : Script::SetProperty(rq, ret, "ignoreInCompatibilityChecks", data->m_IgnoreInCompatibilityChecks);
56 0 : }
57 :
58 : // Required by JSVAL_VECTOR, but can't be implemented.
59 : template<>
60 0 : bool Script::FromJSVal(const Script::Request &, const JS::HandleValue, ModDataCPtr&)
61 : {
62 0 : LOGERROR("Not implemented");
63 0 : return false;
64 : }
65 :
66 0 : JSVAL_VECTOR(const Mod::ModData*);
67 :
68 : // Implement FromJSVal as a non-pointer type.
69 : template<>
70 0 : void Script::ToJSVal(const Script::Request& rq, JS::MutableHandleValue ret, const Mod::ModData& data)
71 : {
72 0 : ret.set(Script::CreateObject(rq));
73 0 : Script::SetProperty(rq, ret, "mod", data.m_Pathname);
74 0 : Script::SetProperty(rq, ret, "name", data.m_Name);
75 0 : Script::SetProperty(rq, ret, "version", data.m_Version);
76 0 : Script::SetProperty(rq, ret, "ignoreInCompatibilityChecks", data.m_IgnoreInCompatibilityChecks);
77 0 : }
78 :
79 : template<>
80 0 : bool Script::FromJSVal(const Script::Request& rq, const JS::HandleValue val, Mod::ModData& data)
81 : {
82 : // To avoid errors & for convenience, some retro-compatibility when reading
83 : // TODO: remove this once we hit A26.
84 0 : JS::RootedObject obj(rq.cx, val.toObjectOrNull());
85 0 : bool isArray = false;
86 0 : if (JS::IsArray(rq.cx, obj, &isArray) && isArray)
87 : {
88 0 : if (!Script::GetPropertyInt(rq, val, 0, data.m_Pathname))
89 : return false;
90 0 : if (!Script::GetPropertyInt(rq, val, 1, data.m_Version))
91 : return false;
92 : // Set a sane default for this.
93 0 : data.m_Name = data.m_Pathname;
94 : return true;
95 : }
96 :
97 : // This property is not set in mod.json files, so don't fail if it's not there.
98 0 : if (Script::HasProperty(rq, val, "mod") && !Script::GetProperty(rq, val, "mod", data.m_Pathname))
99 : return false;
100 :
101 0 : if (!Script::GetProperty(rq, val, "version", data.m_Version))
102 : return false;
103 0 : if (!Script::GetProperty(rq, val, "name", data.m_Name))
104 : return false;
105 :
106 : // Optional - this makes the mod 'GUI-only'.
107 0 : if (Script::HasProperty(rq, val, "ignoreInCompatibilityChecks"))
108 : {
109 0 : if (!Script::GetProperty(rq, val, "ignoreInCompatibilityChecks", data.m_IgnoreInCompatibilityChecks))
110 : return false;
111 : }
112 : else
113 0 : data.m_IgnoreInCompatibilityChecks = false;
114 :
115 : return true;
116 0 : }
117 :
118 0 : JSVAL_VECTOR(Mod::ModData);
119 :
120 : namespace JSI_Mod
121 : {
122 0 : Mod* ModGetter(const Script::Request&, JS::CallArgs&)
123 : {
124 0 : return &g_Mods;
125 : }
126 :
127 0 : JS::Value GetEngineInfo(const Script::Interface& scriptInterface)
128 : {
129 0 : Script::Request rq(scriptInterface);
130 :
131 0 : JS::RootedValue mods(rq.cx);
132 0 : Script::ToJSVal(rq, &mods, g_Mods.GetEnabledModsData());
133 0 : JS::RootedValue metainfo(rq.cx);
134 :
135 0 : Script::CreateObject(
136 : rq,
137 : &metainfo,
138 : "engine_version", PS_VERSION,
139 : "engine_serialization_version", PS_SERIALIZATION_VERSION,
140 : "mods", mods);
141 :
142 0 : Script::DeepFreezeObject(rq, metainfo);
143 :
144 0 : return metainfo;
145 0 : }
146 :
147 0 : JS::Value GetAvailableMods(const Script::Request& rq)
148 : {
149 0 : JS::RootedValue ret(rq.cx, Script::CreateObject(rq));
150 0 : for (const Mod::ModData& data : g_Mods.GetAvailableMods())
151 : {
152 0 : JS::RootedValue json(rq.cx);
153 0 : if (!Script::ParseJSON(rq, data.m_Text, &json))
154 : {
155 0 : throw std::runtime_error{fmt::format("Error parsing mod.json of '{}'",
156 0 : data.m_Pathname.c_str())};
157 : }
158 0 : Script::SetProperty(rq, ret, data.m_Pathname.c_str(), json);
159 0 : }
160 0 : return ret.get();
161 0 : }
162 :
163 0 : bool AreModsPlayCompatible(const std::vector<Mod::ModData>& a, const std::vector<Mod::ModData>& b)
164 : {
165 0 : std::vector<const Mod::ModData*> modsA, modsB;
166 0 : modsA.reserve(a.size());
167 0 : for (const Mod::ModData& mod : a)
168 0 : modsA.push_back(&mod);
169 0 : modsB.reserve(b.size());
170 0 : for (const Mod::ModData& mod : b)
171 0 : modsB.push_back(&mod);
172 0 : return Mod::AreModsPlayCompatible(modsA, modsB);
173 0 : }
174 :
175 0 : bool SetModsAndRestartEngine(const std::vector<CStr>& mods)
176 : {
177 0 : if (!g_Mods.EnableMods(mods, false))
178 : return false;
179 :
180 0 : RestartEngine();
181 0 : return true;
182 : }
183 :
184 0 : bool HasIncompatibleMods()
185 : {
186 0 : return g_Mods.GetIncompatibleMods().size() > 0;
187 : }
188 :
189 26 : void RegisterScriptFunctions(const Script::Request& rq)
190 : {
191 26 : Script::Function::Register<GetEngineInfo>(rq, "GetEngineInfo");
192 26 : Script::Function::Register<GetAvailableMods>(rq, "GetAvailableMods");
193 26 : Script::Function::Register<&Mod::GetEnabledMods, ModGetter>(rq, "GetEnabledMods");
194 26 : Script::Function::Register<AreModsPlayCompatible>(rq, "AreModsPlayCompatible");
195 26 : Script::Function::Register<HasIncompatibleMods> (rq, "HasIncompatibleMods");
196 26 : Script::Function::Register<&Mod::GetIncompatibleMods, ModGetter>(rq, "GetIncompatibleMods");
197 26 : Script::Function::Register<&SetModsAndRestartEngine>(rq, "SetModsAndRestartEngine");
198 26 : }
199 : }
|