1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
/* Copyright (C) 2022 Wildfire Games.
* 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"
#include "i18n/L10n.h"
#include "lib/file/file_system.h"
#include "lib/file/vfs/vfs.h"
#include "lib/sysdep/os.h"
#include "lib/utf8.h"
#include "ps/Filesystem.h"
#include "ps/GameSetup/GameSetup.h"
#include "ps/GameSetup/Paths.h"
#include "ps/Profiler2.h"
#include "scriptinterface/JSON.h"
#include "scriptinterface/Object.h"
#include "scriptinterface/ScriptExceptions.h"
#include "scriptinterface/ScriptInterface.h"
#if !OS_WIN
#include "lib/os_path.h"
#endif
#include <algorithm>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#if OS_WIN
#include <filesystem>
#endif
#include <fstream>
#include <sstream>
#include <unordered_map>
namespace
{
/**
* Global instance of Mod, always exists.
*/
Mod g_ModInstance;
bool LoadModJSON(const PIVFS& vfs, OsPath modsPath, OsPath mod, std::string& text)
{
#if OS_WIN
const std::filesystem::path modJsonPath = (modsPath / mod / L"mod.json").fileSystemPath();
#else
const std::string modJsonPath = OsString(modsPath / mod / L"mod.json");
#endif
// Attempt to open mod.json first.
std::ifstream modjson(modJsonPath);
if (!modjson)
{
modjson.close();
// Fallback: open the archive and read mod.json there.
// This can take in the hundreds of milliseconds with large mods.
vfs->Clear();
if (vfs->Mount(L"", modsPath / mod / "", VFS_MOUNT_MUST_EXIST, VFS_MIN_PRIORITY) < 0)
return false;
CVFSFile modinfo;
if (modinfo.Load(vfs, L"mod.json", false) != PSRETURN_OK)
return false;
text = modinfo.GetAsString();
// Attempt to write the mod.json file so we'll take the fast path next time.
std::ofstream out_mod_json(modJsonPath);
if (out_mod_json)
{
out_mod_json << text;
out_mod_json.close();
}
else
{
// Print a warning - we'll keep trying, which could have adverse effects.
if (L10n::IsInitialised())
LOGWARNING(g_L10n.Translate("Could not write external mod.json for zipped mod '%s'. The mod should be reinstalled."), mod.string8());
else
LOGWARNING("Could not write external mod.json for zipped mod '%s'. The mod should be reinstalled.", mod.string8());
}
return true;
}
else
{
std::stringstream buffer;
buffer << modjson.rdbuf();
text = buffer.str();
return true;
}
}
bool ParseModJSON(const ScriptRequest& rq, const PIVFS& vfs, OsPath modsPath, OsPath mod, Mod::ModData& data)
{
std::string text;
if (!LoadModJSON(vfs, modsPath, mod, text))
return false;
JS::RootedValue json(rq.cx);
if (!Script::ParseJSON(rq, text, &json))
return false;
Script::FromJSVal(rq, json, data);
// Complete - FromJSVal won't convert everything.
data.m_Pathname = utf8_from_wstring(mod.string());
data.m_Text = text;
if (!Script::GetProperty(rq, json, "dependencies", data.m_Dependencies))
return false;
return true;
}
} // anonymous namespace
Mod& Mod::Instance()
{
return g_ModInstance;
}
const std::vector<CStr>& Mod::GetEnabledMods() const
{
return m_EnabledMods;
}
const std::vector<CStr>& Mod::GetIncompatibleMods() const
{
return m_IncompatibleMods;
}
const std::vector<Mod::ModData>& Mod::GetAvailableMods() const
{
return m_AvailableMods;
}
bool Mod::EnableMods(const std::vector<CStr>& mods, const bool addPublic)
{
m_IncompatibleMods.clear();
m_EnabledMods.clear();
std::unordered_map<CStr, int> counts;
for (const CStr& mod : mods)
{
// Ignore duplicates.
if (counts.try_emplace(mod, 0).first->second++ > 0)
continue;
m_EnabledMods.emplace_back(mod);
}
if (addPublic && counts["public"] == 0)
m_EnabledMods.insert(m_EnabledMods.begin(), "public");
if (counts["mod"] == 0)
m_EnabledMods.insert(m_EnabledMods.begin(), "mod");
m_IncompatibleMods = CheckForIncompatibleMods(m_EnabledMods);
for (const CStr& mod : m_IncompatibleMods)
m_EnabledMods.erase(std::find(m_EnabledMods.begin(), m_EnabledMods.end(), mod));
return m_IncompatibleMods.empty();
}
const Mod::ModData* Mod::GetModData(const CStr& mod) const
{
std::vector<ModData>::const_iterator it = std::find_if(m_AvailableMods.begin(), m_AvailableMods.end(),
[&mod](const ModData& modData) { return modData.m_Pathname == mod; });
if (it == m_AvailableMods.end())
return nullptr;
return std::addressof(*it);
}
const std::vector<const Mod::ModData*> Mod::GetEnabledModsData() const
{
std::vector<const ModData*> loadedMods;
for (const CStr& mod : m_EnabledMods)
{
if (mod == "mod" || mod == "user")
continue;
const ModData* data = GetModData(mod);
// This ought be impossible, but let's handle it anyways since it's not a reason to crash.
if (!data)
{
LOGERROR("Unavailable mod '%s' was enabled.", mod);
continue;
}
loadedMods.emplace_back(data);
}
return loadedMods;
}
bool Mod::AreModsPlayCompatible(const std::vector<const Mod::ModData*>& modsA, const std::vector<const Mod::ModData*>& modsB)
{
// Mods must be loaded in the same order.
std::vector<const Mod::ModData*>::const_iterator a = modsA.begin();
std::vector<const Mod::ModData*>::const_iterator b = modsB.begin();
while (a != modsA.end() || b != modsB.end())
{
if (a != modsA.end() && (*a)->m_IgnoreInCompatibilityChecks)
{
++a;
continue;
}
if (b != modsB.end() && (*b)->m_IgnoreInCompatibilityChecks)
{
++b;
continue;
}
// If at this point one of the two lists still contains items, the sizes are different -> fail.
if (a == modsA.end() || b == modsB.end())
return false;
if ((*a)->m_Pathname != (*b)->m_Pathname)
return false;
if ((*a)->m_Version != (*b)->m_Version)
return false;
++a;
++b;
}
return true;
}
void Mod::UpdateAvailableMods(const ScriptInterface& scriptInterface)
{
PROFILE2("UpdateAvailableMods");
m_AvailableMods.clear();
const Paths paths(g_CmdLineArgs);
// 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();
ScriptRequest rq(scriptInterface);
for (DirectoryNames::iterator iter = modDirs.begin(); iter != modDirs.end(); ++iter)
{
ModData data;
if (!ParseModJSON(rq, vfs, modPath, *iter, data))
continue;
// Valid mod data, add it to our structure
m_AvailableMods.emplace_back(std::move(data));
}
GetDirectoryEntries(modUserPath, NULL, &modDirsUser);
for (DirectoryNames::iterator iter = modDirsUser.begin(); iter != modDirsUser.end(); ++iter)
{
// Ignore mods in the user folder if we have already found them in modDirs.
if (std::binary_search(modDirs.begin(), modDirs.end(), *iter))
continue;
ModData data;
if (!ParseModJSON(rq, vfs, modUserPath, *iter, data))
continue;
// Valid mod data, add it to our structure
m_AvailableMods.emplace_back(std::move(data));
}
}
std::vector<CStr> Mod::CheckForIncompatibleMods(const std::vector<CStr>& mods) const
{
std::vector<CStr> incompatibleMods;
std::unordered_map<CStr, std::vector<CStr>> modDependencies;
std::unordered_map<CStr, CStr> modNameVersions;
for (const CStr& mod : mods)
{
if (mod == "mod" || mod == "user")
continue;
std::vector<ModData>::const_iterator it = std::find_if(m_AvailableMods.begin(), m_AvailableMods.end(),
[&mod](const ModData& modData) { return modData.m_Pathname == mod; });
if (it == m_AvailableMods.end())
{
incompatibleMods.push_back(mod);
continue;
}
modNameVersions.emplace(it->m_Name, it->m_Version);
modDependencies.emplace(it->m_Pathname, it->m_Dependencies);
}
static const std::vector<CStr> toCheck = { "<=", ">=", "=", "<", ">" };
for (const CStr& mod : mods)
{
if (mod == "mod" || mod == "user")
continue;
const std::unordered_map<CStr, std::vector<CStr>>::iterator res = modDependencies.find(mod);
if (res == modDependencies.end())
continue;
const std::vector<CStr> deps = res->second;
if (deps.empty())
continue;
for (const CStr& dep : deps)
{
if (dep.empty())
continue;
// 0ad<=0.0.24
for (const CStr& op : toCheck)
{
const int pos = dep.Find(op.c_str());
if (pos == -1)
continue;
//0ad
const CStr modToCheck = dep.substr(0, pos);
//0.0.24
const CStr versionToCheck = dep.substr(pos + op.size());
const std::unordered_map<CStr, CStr>::iterator it = modNameVersions.find(modToCheck);
// Could not find the mod, or 0.0.25(0ad) , <=, 0.0.24(required version)
if (it == modNameVersions.end() || !CompareVersionStrings(it->second, op, versionToCheck))
incompatibleMods.push_back(mod);
break;
}
}
}
return incompatibleMods;
}
bool Mod::CompareVersionStrings(const CStr& version, const CStr& op, const CStr& required) const
{
std::vector<CStr> versionSplit;
std::vector<CStr> requiredSplit;
static const std::string toIgnore = "-,_";
boost::split(versionSplit, version, boost::is_any_of(toIgnore), boost::token_compress_on);
boost::split(requiredSplit, required, boost::is_any_of(toIgnore), boost::token_compress_on);
boost::split(versionSplit, versionSplit[0], boost::is_any_of("."), boost::token_compress_on);
boost::split(requiredSplit, requiredSplit[0], boost::is_any_of("."), boost::token_compress_on);
const bool eq = op.Find("=") != -1;
const bool lt = op.Find("<") != -1;
const bool gt = op.Find(">") != -1;
const size_t min = std::min(versionSplit.size(), requiredSplit.size());
for (size_t i = 0; i < min; ++i)
{
const int diff = versionSplit[i].ToInt() - requiredSplit[i].ToInt();
if ((gt && diff > 0) || (lt && diff < 0))
return true;
if ((gt && diff < 0) || (lt && diff > 0) || (eq && diff))
return false;
}
const size_t versionSize = versionSplit.size();
const size_t requiredSize = requiredSplit.size();
if (versionSize == requiredSize)
return eq;
return versionSize < requiredSize ? lt : gt;
}
|