1 /* Copyright (C) 2024 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #include "precompiled.h"
22 #include "i18n/L10n.h"
23 #include "lib/file/file_system.h"
24 #include "lib/file/vfs/vfs.h"
25 #include "lib/sysdep/os.h"
27 #include "ps/Filesystem.h"
28 #include "ps/GameSetup/GameSetup.h"
29 #include "ps/GameSetup/Paths.h"
30 #include "ps/Profiler2.h"
31 #include "scriptinterface/JSON.h"
32 #include "scriptinterface/Object.h"
33 #include "scriptinterface/ScriptExceptions.h"
34 #include "scriptinterface/ScriptInterface.h"
37 #include "lib/os_path.h"
41 #include <boost/algorithm/string/split.hpp>
42 #include <boost/algorithm/string/classification.hpp>
48 #include <unordered_map>
53 * Global instance of Mod, always exists.
57 bool LoadModJSON(const PIVFS
& vfs
, OsPath modsPath
, OsPath mod
, std::string
& text
)
60 const std::filesystem::path modJsonPath
= (modsPath
/ mod
/ L
"mod.json").fileSystemPath();
62 const std::string modJsonPath
= OsString(modsPath
/ mod
/ L
"mod.json");
64 // Attempt to open mod.json first.
65 std::ifstream
modjson(modJsonPath
);
70 // Fallback: open the archive and read mod.json there.
71 // This can take in the hundreds of milliseconds with large mods.
73 if (vfs
->Mount(L
"", modsPath
/ mod
/ "", VFS_MOUNT_MUST_EXIST
, VFS_MIN_PRIORITY
) < 0)
77 if (modinfo
.Load(vfs
, L
"mod.json", false) != PSRETURN_OK
)
80 text
= modinfo
.GetAsString();
82 // Attempt to write the mod.json file so we'll take the fast path next time.
83 std::ofstream
out_mod_json(modJsonPath
);
91 // Print a warning - we'll keep trying, which could have adverse effects.
92 LOGWARNING("Could not write external mod.json for zipped mod '%s'. The mod should be reinstalled.", mod
.string8());
98 std::stringstream buffer
;
99 buffer
<< modjson
.rdbuf();
105 bool ParseModJSON(const ScriptRequest
& rq
, const PIVFS
& vfs
, OsPath modsPath
, OsPath mod
, Mod::ModData
& data
)
108 if (!LoadModJSON(vfs
, modsPath
, mod
, text
))
111 JS::RootedValue
json(rq
.cx
);
112 if (!Script::ParseJSON(rq
, text
, &json
))
115 Script::FromJSVal(rq
, json
, data
);
117 // Complete - FromJSVal won't convert everything.
118 data
.m_Pathname
= utf8_from_wstring(mod
.string());
120 if (!Script::GetProperty(rq
, json
, "dependencies", data
.m_Dependencies
))
125 } // anonymous namespace
129 return g_ModInstance
;
132 const std::vector
<CStr
>& Mod::GetEnabledMods() const
134 return m_EnabledMods
;
137 const std::vector
<CStr
>& Mod::GetIncompatibleMods() const
139 return m_IncompatibleMods
;
142 const std::vector
<Mod::ModData
>& Mod::GetAvailableMods() const
144 return m_AvailableMods
;
147 bool Mod::EnableMods(const std::vector
<CStr
>& mods
, const bool addPublic
)
149 m_IncompatibleMods
.clear();
150 m_EnabledMods
.clear();
152 std::unordered_map
<CStr
, int> counts
;
153 for (const CStr
& mod
: mods
)
155 // Ignore duplicates.
156 if (counts
.try_emplace(mod
, 0).first
->second
++ > 0)
158 m_EnabledMods
.emplace_back(mod
);
161 if (addPublic
&& counts
["public"] == 0)
162 m_EnabledMods
.insert(m_EnabledMods
.begin(), "public");
164 if (counts
["mod"] == 0)
165 m_EnabledMods
.insert(m_EnabledMods
.begin(), "mod");
167 m_IncompatibleMods
= CheckForIncompatibleMods(m_EnabledMods
);
169 for (const CStr
& mod
: m_IncompatibleMods
)
170 m_EnabledMods
.erase(std::find(m_EnabledMods
.begin(), m_EnabledMods
.end(), mod
));
172 return m_IncompatibleMods
.empty();
175 const Mod::ModData
* Mod::GetModData(const CStr
& mod
) const
177 std::vector
<ModData
>::const_iterator it
= std::find_if(m_AvailableMods
.begin(), m_AvailableMods
.end(),
178 [&mod
](const ModData
& modData
) { return modData
.m_Pathname
== mod
; });
179 if (it
== m_AvailableMods
.end())
181 return std::addressof(*it
);
184 const std::vector
<const Mod::ModData
*> Mod::GetEnabledModsData() const
186 std::vector
<const ModData
*> loadedMods
;
187 for (const CStr
& mod
: m_EnabledMods
)
189 if (mod
== "mod" || mod
== "user")
192 const ModData
* data
= GetModData(mod
);
194 // This ought be impossible, but let's handle it anyways since it's not a reason to crash.
197 LOGERROR("Unavailable mod '%s' was enabled.", mod
);
201 loadedMods
.emplace_back(data
);
206 bool Mod::AreModsPlayCompatible(const std::vector
<const Mod::ModData
*>& modsA
, const std::vector
<const Mod::ModData
*>& modsB
)
208 // Mods must be loaded in the same order.
209 std::vector
<const Mod::ModData
*>::const_iterator a
= modsA
.begin();
210 std::vector
<const Mod::ModData
*>::const_iterator b
= modsB
.begin();
212 while (a
!= modsA
.end() || b
!= modsB
.end())
214 if (a
!= modsA
.end() && (*a
)->m_IgnoreInCompatibilityChecks
)
219 if (b
!= modsB
.end() && (*b
)->m_IgnoreInCompatibilityChecks
)
224 // If at this point one of the two lists still contains items, the sizes are different -> fail.
225 if (a
== modsA
.end() || b
== modsB
.end())
228 if ((*a
)->m_Pathname
!= (*b
)->m_Pathname
)
230 if ((*a
)->m_Version
!= (*b
)->m_Version
)
238 void Mod::UpdateAvailableMods(const ScriptInterface
& scriptInterface
)
240 PROFILE2("UpdateAvailableMods");
242 m_AvailableMods
.clear();
243 const Paths
paths(g_CmdLineArgs
);
245 // loop over all possible paths
246 OsPath modPath
= paths
.RData()/"mods";
247 OsPath modUserPath
= paths
.UserData()/"mods";
249 DirectoryNames modDirs
;
250 DirectoryNames modDirsUser
;
252 GetDirectoryEntries(modPath
, NULL
, &modDirs
);
253 // Sort modDirs so that we can do a fast lookup below
254 std::sort(modDirs
.begin(), modDirs
.end());
256 PIVFS vfs
= CreateVfs();
258 ScriptRequest
rq(scriptInterface
);
259 for (DirectoryNames::iterator iter
= modDirs
.begin(); iter
!= modDirs
.end(); ++iter
)
262 if (!ParseModJSON(rq
, vfs
, modPath
, *iter
, data
))
264 // Valid mod data, add it to our structure
265 m_AvailableMods
.emplace_back(std::move(data
));
268 GetDirectoryEntries(modUserPath
, NULL
, &modDirsUser
);
270 for (DirectoryNames::iterator iter
= modDirsUser
.begin(); iter
!= modDirsUser
.end(); ++iter
)
272 // Ignore mods in the user folder if we have already found them in modDirs.
273 if (std::binary_search(modDirs
.begin(), modDirs
.end(), *iter
))
277 if (!ParseModJSON(rq
, vfs
, modUserPath
, *iter
, data
))
279 // Valid mod data, add it to our structure
280 m_AvailableMods
.emplace_back(std::move(data
));
284 std::vector
<CStr
> Mod::CheckForIncompatibleMods(const std::vector
<CStr
>& mods
) const
286 std::vector
<CStr
> incompatibleMods
;
287 std::unordered_map
<CStr
, std::vector
<CStr
>> modDependencies
;
288 std::unordered_map
<CStr
, CStr
> modNameVersions
;
289 for (const CStr
& mod
: mods
)
291 if (mod
== "mod" || mod
== "user")
294 std::vector
<ModData
>::const_iterator it
= std::find_if(m_AvailableMods
.begin(), m_AvailableMods
.end(),
295 [&mod
](const ModData
& modData
) { return modData
.m_Pathname
== mod
; });
297 if (it
== m_AvailableMods
.end())
299 incompatibleMods
.push_back(mod
);
303 modNameVersions
.emplace(it
->m_Name
, it
->m_Version
);
304 modDependencies
.emplace(it
->m_Pathname
, it
->m_Dependencies
);
307 static const std::vector
<CStr
> toCheck
= { "<=", ">=", "=", "<", ">" };
308 for (const CStr
& mod
: mods
)
310 if (mod
== "mod" || mod
== "user")
313 const std::unordered_map
<CStr
, std::vector
<CStr
>>::iterator res
= modDependencies
.find(mod
);
314 if (res
== modDependencies
.end())
316 const std::vector
<CStr
> deps
= res
->second
;
320 for (const CStr
& dep
: deps
)
325 for (const CStr
& op
: toCheck
)
327 const int pos
= dep
.Find(op
.c_str());
331 const CStr modToCheck
= dep
.substr(0, pos
);
333 const CStr versionToCheck
= dep
.substr(pos
+ op
.size());
334 const std::unordered_map
<CStr
, CStr
>::iterator it
= modNameVersions
.find(modToCheck
);
335 // Could not find the mod, or 0.0.25(0ad) , <=, 0.0.24(required version)
336 if (it
== modNameVersions
.end() || !CompareVersionStrings(it
->second
, op
, versionToCheck
))
337 incompatibleMods
.push_back(mod
);
344 return incompatibleMods
;
347 bool Mod::CompareVersionStrings(const CStr
& version
, const CStr
& op
, const CStr
& required
) const
349 std::vector
<CStr
> versionSplit
;
350 std::vector
<CStr
> requiredSplit
;
351 static const std::string toIgnore
= "-,_";
352 boost::split(versionSplit
, version
, boost::is_any_of(toIgnore
), boost::token_compress_on
);
353 boost::split(requiredSplit
, required
, boost::is_any_of(toIgnore
), boost::token_compress_on
);
354 boost::split(versionSplit
, versionSplit
[0], boost::is_any_of("."), boost::token_compress_on
);
355 boost::split(requiredSplit
, requiredSplit
[0], boost::is_any_of("."), boost::token_compress_on
);
357 const bool eq
= op
.Find("=") != -1;
358 const bool lt
= op
.Find("<") != -1;
359 const bool gt
= op
.Find(">") != -1;
361 const size_t min
= std::min(versionSplit
.size(), requiredSplit
.size());
363 for (size_t i
= 0; i
< min
; ++i
)
365 const int diff
= versionSplit
[i
].ToInt() - requiredSplit
[i
].ToInt();
366 if ((gt
&& diff
> 0) || (lt
&& diff
< 0))
369 if ((gt
&& diff
< 0) || (lt
&& diff
> 0) || (eq
&& diff
))
373 const size_t versionSize
= versionSplit
.size();
374 const size_t requiredSize
= requiredSplit
.size();
375 if (versionSize
== requiredSize
)
377 return versionSize
< requiredSize
? lt
: gt
;