1 /* Copyright (C) 2022 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 if (L10n::IsInitialised())
93 LOGWARNING(g_L10n
.Translate("Could not write external mod.json for zipped mod '%s'. The mod should be reinstalled."), mod
.string8());
95 LOGWARNING("Could not write external mod.json for zipped mod '%s'. The mod should be reinstalled.", mod
.string8());
101 std::stringstream buffer
;
102 buffer
<< modjson
.rdbuf();
108 bool ParseModJSON(const ScriptRequest
& rq
, const PIVFS
& vfs
, OsPath modsPath
, OsPath mod
, Mod::ModData
& data
)
111 if (!LoadModJSON(vfs
, modsPath
, mod
, text
))
114 JS::RootedValue
json(rq
.cx
);
115 if (!Script::ParseJSON(rq
, text
, &json
))
118 Script::FromJSVal(rq
, json
, data
);
120 // Complete - FromJSVal won't convert everything.
121 data
.m_Pathname
= utf8_from_wstring(mod
.string());
123 if (!Script::GetProperty(rq
, json
, "dependencies", data
.m_Dependencies
))
128 } // anonymous namespace
132 return g_ModInstance
;
135 const std::vector
<CStr
>& Mod::GetEnabledMods() const
137 return m_EnabledMods
;
140 const std::vector
<CStr
>& Mod::GetIncompatibleMods() const
142 return m_IncompatibleMods
;
145 const std::vector
<Mod::ModData
>& Mod::GetAvailableMods() const
147 return m_AvailableMods
;
150 bool Mod::EnableMods(const std::vector
<CStr
>& mods
, const bool addPublic
)
152 m_IncompatibleMods
.clear();
153 m_EnabledMods
.clear();
155 std::unordered_map
<CStr
, int> counts
;
156 for (const CStr
& mod
: mods
)
158 // Ignore duplicates.
159 if (counts
.try_emplace(mod
, 0).first
->second
++ > 0)
161 m_EnabledMods
.emplace_back(mod
);
164 if (addPublic
&& counts
["public"] == 0)
165 m_EnabledMods
.insert(m_EnabledMods
.begin(), "public");
167 if (counts
["mod"] == 0)
168 m_EnabledMods
.insert(m_EnabledMods
.begin(), "mod");
170 m_IncompatibleMods
= CheckForIncompatibleMods(m_EnabledMods
);
172 for (const CStr
& mod
: m_IncompatibleMods
)
173 m_EnabledMods
.erase(std::find(m_EnabledMods
.begin(), m_EnabledMods
.end(), mod
));
175 return m_IncompatibleMods
.empty();
178 const Mod::ModData
* Mod::GetModData(const CStr
& mod
) const
180 std::vector
<ModData
>::const_iterator it
= std::find_if(m_AvailableMods
.begin(), m_AvailableMods
.end(),
181 [&mod
](const ModData
& modData
) { return modData
.m_Pathname
== mod
; });
182 if (it
== m_AvailableMods
.end())
184 return std::addressof(*it
);
187 const std::vector
<const Mod::ModData
*> Mod::GetEnabledModsData() const
189 std::vector
<const ModData
*> loadedMods
;
190 for (const CStr
& mod
: m_EnabledMods
)
192 if (mod
== "mod" || mod
== "user")
195 const ModData
* data
= GetModData(mod
);
197 // This ought be impossible, but let's handle it anyways since it's not a reason to crash.
200 LOGERROR("Unavailable mod '%s' was enabled.", mod
);
204 loadedMods
.emplace_back(data
);
209 bool Mod::AreModsPlayCompatible(const std::vector
<const Mod::ModData
*>& modsA
, const std::vector
<const Mod::ModData
*>& modsB
)
211 // Mods must be loaded in the same order.
212 std::vector
<const Mod::ModData
*>::const_iterator a
= modsA
.begin();
213 std::vector
<const Mod::ModData
*>::const_iterator b
= modsB
.begin();
215 while (a
!= modsA
.end() || b
!= modsB
.end())
217 if (a
!= modsA
.end() && (*a
)->m_IgnoreInCompatibilityChecks
)
222 if (b
!= modsB
.end() && (*b
)->m_IgnoreInCompatibilityChecks
)
227 // If at this point one of the two lists still contains items, the sizes are different -> fail.
228 if (a
== modsA
.end() || b
== modsB
.end())
231 if ((*a
)->m_Pathname
!= (*b
)->m_Pathname
)
233 if ((*a
)->m_Version
!= (*b
)->m_Version
)
241 void Mod::UpdateAvailableMods(const ScriptInterface
& scriptInterface
)
243 PROFILE2("UpdateAvailableMods");
245 m_AvailableMods
.clear();
246 const Paths
paths(g_CmdLineArgs
);
248 // loop over all possible paths
249 OsPath modPath
= paths
.RData()/"mods";
250 OsPath modUserPath
= paths
.UserData()/"mods";
252 DirectoryNames modDirs
;
253 DirectoryNames modDirsUser
;
255 GetDirectoryEntries(modPath
, NULL
, &modDirs
);
256 // Sort modDirs so that we can do a fast lookup below
257 std::sort(modDirs
.begin(), modDirs
.end());
259 PIVFS vfs
= CreateVfs();
261 ScriptRequest
rq(scriptInterface
);
262 for (DirectoryNames::iterator iter
= modDirs
.begin(); iter
!= modDirs
.end(); ++iter
)
265 if (!ParseModJSON(rq
, vfs
, modPath
, *iter
, data
))
267 // Valid mod data, add it to our structure
268 m_AvailableMods
.emplace_back(std::move(data
));
271 GetDirectoryEntries(modUserPath
, NULL
, &modDirsUser
);
273 for (DirectoryNames::iterator iter
= modDirsUser
.begin(); iter
!= modDirsUser
.end(); ++iter
)
275 // Ignore mods in the user folder if we have already found them in modDirs.
276 if (std::binary_search(modDirs
.begin(), modDirs
.end(), *iter
))
280 if (!ParseModJSON(rq
, vfs
, modUserPath
, *iter
, data
))
282 // Valid mod data, add it to our structure
283 m_AvailableMods
.emplace_back(std::move(data
));
287 std::vector
<CStr
> Mod::CheckForIncompatibleMods(const std::vector
<CStr
>& mods
) const
289 std::vector
<CStr
> incompatibleMods
;
290 std::unordered_map
<CStr
, std::vector
<CStr
>> modDependencies
;
291 std::unordered_map
<CStr
, CStr
> modNameVersions
;
292 for (const CStr
& mod
: mods
)
294 if (mod
== "mod" || mod
== "user")
297 std::vector
<ModData
>::const_iterator it
= std::find_if(m_AvailableMods
.begin(), m_AvailableMods
.end(),
298 [&mod
](const ModData
& modData
) { return modData
.m_Pathname
== mod
; });
300 if (it
== m_AvailableMods
.end())
302 incompatibleMods
.push_back(mod
);
306 modNameVersions
.emplace(it
->m_Name
, it
->m_Version
);
307 modDependencies
.emplace(it
->m_Pathname
, it
->m_Dependencies
);
310 static const std::vector
<CStr
> toCheck
= { "<=", ">=", "=", "<", ">" };
311 for (const CStr
& mod
: mods
)
313 if (mod
== "mod" || mod
== "user")
316 const std::unordered_map
<CStr
, std::vector
<CStr
>>::iterator res
= modDependencies
.find(mod
);
317 if (res
== modDependencies
.end())
319 const std::vector
<CStr
> deps
= res
->second
;
323 for (const CStr
& dep
: deps
)
328 for (const CStr
& op
: toCheck
)
330 const int pos
= dep
.Find(op
.c_str());
334 const CStr modToCheck
= dep
.substr(0, pos
);
336 const CStr versionToCheck
= dep
.substr(pos
+ op
.size());
337 const std::unordered_map
<CStr
, CStr
>::iterator it
= modNameVersions
.find(modToCheck
);
338 // Could not find the mod, or 0.0.25(0ad) , <=, 0.0.24(required version)
339 if (it
== modNameVersions
.end() || !CompareVersionStrings(it
->second
, op
, versionToCheck
))
340 incompatibleMods
.push_back(mod
);
347 return incompatibleMods
;
350 bool Mod::CompareVersionStrings(const CStr
& version
, const CStr
& op
, const CStr
& required
) const
352 std::vector
<CStr
> versionSplit
;
353 std::vector
<CStr
> requiredSplit
;
354 static const std::string toIgnore
= "-,_";
355 boost::split(versionSplit
, version
, boost::is_any_of(toIgnore
), boost::token_compress_on
);
356 boost::split(requiredSplit
, required
, boost::is_any_of(toIgnore
), boost::token_compress_on
);
357 boost::split(versionSplit
, versionSplit
[0], boost::is_any_of("."), boost::token_compress_on
);
358 boost::split(requiredSplit
, requiredSplit
[0], boost::is_any_of("."), boost::token_compress_on
);
360 const bool eq
= op
.Find("=") != -1;
361 const bool lt
= op
.Find("<") != -1;
362 const bool gt
= op
.Find(">") != -1;
364 const size_t min
= std::min(versionSplit
.size(), requiredSplit
.size());
366 for (size_t i
= 0; i
< min
; ++i
)
368 const int diff
= versionSplit
[i
].ToInt() - requiredSplit
[i
].ToInt();
369 if ((gt
&& diff
> 0) || (lt
&& diff
< 0))
372 if ((gt
&& diff
< 0) || (lt
&& diff
> 0) || (eq
&& diff
))
376 const size_t versionSize
= versionSplit
.size();
377 const size_t requiredSize
= requiredSplit
.size();
378 if (versionSize
== requiredSize
)
380 return versionSize
< requiredSize
? lt
: gt
;