Index: ps/trunk/source/lib/path.h =================================================================== --- ps/trunk/source/lib/path.h (revision 27030) +++ ps/trunk/source/lib/path.h (revision 27031) @@ -1,323 +1,340 @@ /* Copyright (C) 2022 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* * Path string class, similar to boost::filesystem::basic_path. */ // notes: // - this module is independent of lib/file so that it can be used from // other code without pulling in the entire file manager. // - there is no restriction on buffer lengths except the underlying OS. // input buffers must not exceed PATH_MAX chars, while outputs // must hold at least that much. // - unless otherwise mentioned, all functions are intended to work with // native and VFS paths. // when reading, both '/' and SYS_DIR_SEP are accepted; '/' is written. #ifndef INCLUDED_PATH #define INCLUDED_PATH +#include "lib/sysdep/os.h" #include "lib/utf8.h" #include #include +#if OS_WIN +#include +#endif #include namespace ERR { const Status PATH_CHARACTER_ILLEGAL = -100300; const Status PATH_CHARACTER_UNSAFE = -100301; const Status PATH_NOT_FOUND = -100302; const Status PATH_MIXED_SEPARATORS = -100303; } /** * is s2 a subpath of s1, or vice versa? (equal counts as subpath) * * @param s1, s2 comparand strings * @return bool **/ bool path_is_subpath(const wchar_t* s1, const wchar_t* s2); /** * Get the path component of a path. * Skips over all characters up to the last dir separator, if any. * * @param path Input path. * @return pointer to path component within \. **/ const wchar_t* path_name_only(const wchar_t* path); // NB: there is a need for 'generic' paths (e.g. for Trace entry / archive pathnames). // converting between specialized variants via c_str would be inefficient, and the // Os/VfsPath types are hopefully sufficient to avoid errors. class Path { public: using String = std::wstring; Path() { DetectSeparator(); } Path(const Path& p) : path(p.path) { DetectSeparator(); } Path(const char* p) : path((const unsigned char*)p, (const unsigned char*)p+strlen(p)) // interpret bytes as unsigned; makes no difference for ASCII, // and ensures OsPath on Unix will only contain values 0 <= c < 0x100 { DetectSeparator(); } Path(const wchar_t* p) : path(p, p+wcslen(p)) { DetectSeparator(); } Path(const std::string& s) : path((const unsigned char*)s.c_str(), (const unsigned char*)s.c_str()+s.length()) { DetectSeparator(); } Path(const std::wstring& s) : path(s) { DetectSeparator(); } Path& operator=(const Path& rhs) { path = rhs.path; DetectSeparator(); // (warns if separators differ) return *this; } bool empty() const { return path.empty(); } + // TODO: This macro should be removed later when macOS supports std::filesystem. + // Currently it does in more recent SDKs, but it also causes a slowdown on + // OpenGL. See #6193. +#if OS_WIN + /** + * @returns a STL version of the path. + */ + std::filesystem::path fileSystemPath() const + { + return std::filesystem::path(path); + } +#endif + const String& string() const { return path; } /** * Return a UTF-8 version of the path, in a human-readable but potentially * lossy form. It is *not* safe to take this string and construct a new * Path object from it (it may fail for some non-ASCII paths) - it should * only be used for displaying paths to users. */ std::string string8() const { Status err; #if !OS_WIN // On Unix, assume paths consisting of 8-bit charactes saved in this wide string. std::string spath(path.begin(), path.end()); // Return it if it's valid UTF-8 wstring_from_utf8(spath, &err); if(err == INFO::OK) return spath; // Otherwise assume ISO-8859-1 and let utf8_from_wstring treat each character as a Unicode code point. #endif // On Windows, paths are UTF-16 strings. We don't support non-BMP characters so we can assume it's simply a wstring. return utf8_from_wstring(path, &err); } bool operator<(const Path& rhs) const { return path < rhs.path; } bool operator==(const Path& rhs) const { return path == rhs.path; } bool operator!=(const Path& rhs) const { return !operator==(rhs); } bool IsDirectory() const { if(empty()) // (ensure length()-1 is safe) return true; // (the VFS root directory is represented as an empty string) return path[path.length()-1] == separator; } Path Parent() const { const size_t idxSlash = path.find_last_of(separator); if(idxSlash == String::npos) return L""; return path.substr(0, idxSlash); } Path Filename() const { const size_t idxSlash = path.find_last_of(separator); if(idxSlash == String::npos) return path; return path.substr(idxSlash+1); } Path Basename() const { const Path filename = Filename(); const size_t idxDot = filename.string().find_last_of('.'); if(idxDot == String::npos) return filename; return filename.string().substr(0, idxDot); } // (Path return type allows callers to use our operator==) Path Extension() const { const Path filename = Filename(); const size_t idxDot = filename.string().find_last_of('.'); if(idxDot == String::npos) return Path(); return filename.string().substr(idxDot); } Path ChangeExtension(Path extension) const { return Parent() / Path(Basename().string() + extension.string()); } Path operator/(Path rhs) const { Path ret = *this; if(ret.path.empty()) // (empty paths assume '/') ret.separator = rhs.separator; if(!ret.IsDirectory()) ret.path += ret.separator; if(rhs.path.find((ret.separator == '/')? '\\' : '/') != String::npos) { PrintToDebugOutput(); rhs.PrintToDebugOutput(); DEBUG_WARN_ERR(ERR::PATH_MIXED_SEPARATORS); } ret.path += rhs.path; return ret; } /** * Return the path before the common part of both paths * @param other Indicates the start of the path which should be removed * @note other should be a VfsPath, while this should be an OsPath */ Path BeforeCommon(Path other) const { Path ret = *this; if(ret.empty() || other.empty()) return L""; // Convert the separator to allow for string comparison if(other.separator != ret.separator) std::replace(other.path.begin(), other.path.end(), other.separator, ret.separator); const size_t idx = ret.path.rfind(other.path); if(idx == String::npos) return L""; return path.substr(0, idx); } static Status Validate(String::value_type c); private: void PrintToDebugOutput() const { debug_printf("Path %s, separator %c\n", string8().c_str(), (char)separator); } void DetectSeparator() { const size_t idxBackslash = path.find('\\'); if(path.find('/') != String::npos && idxBackslash != String::npos) { PrintToDebugOutput(); DEBUG_WARN_ERR(ERR::PATH_MIXED_SEPARATORS); } // (default to '/' for empty strings) separator = (idxBackslash == String::npos)? '/' : '\\'; } String path; // note: ideally, path strings would only contain '/' or even SYS_DIR_SEP. // however, Windows-specific code (e.g. the sound driver detection) // uses these routines with '\\' strings. the boost::filesystem approach of // converting them all to '/' and then back via external_file_string is // annoying and inefficient. we allow either type of separators, // appending whichever was first encountered. when modifying the path, // we ensure the same separator is used. wchar_t separator = L'/'; }; static inline std::wostream& operator<<(std::wostream& s, const Path& path) { s << path.string(); return s; } static inline std::wistream& operator>>(std::wistream& s, Path& path) { Path::String string; s >> string; path = Path(string); return s; } namespace std { template<> struct hash { std::size_t operator()(const Path& path) const { return m_StringHash(path.string()); } private: std::hash m_StringHash; }; } #endif // #ifndef INCLUDED_PATH Index: ps/trunk/source/ps/Mod.cpp =================================================================== --- ps/trunk/source/ps/Mod.cpp (revision 27030) +++ ps/trunk/source/ps/Mod.cpp (revision 27031) @@ -1,370 +1,377 @@ -/* Copyright (C) 2021 Wildfire Games. +/* 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 . */ #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" #include #include #include +#if OS_WIN +#include +#endif #include #include #include 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 = (modsPath / mod / L"mod.json").string8(); +#endif // Attempt to open mod.json first. - std::ifstream modjson; - modjson.open((modsPath / mod / L"mod.json").string8()); - - if (!modjson.is_open()) + 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((modsPath / mod / L"mod.json").string8()); - if (out_mod_json.good()) + 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& Mod::GetEnabledMods() const { return m_EnabledMods; } const std::vector& Mod::GetIncompatibleMods() const { return m_IncompatibleMods; } const std::vector& Mod::GetAvailableMods() const { return m_AvailableMods; } bool Mod::EnableMods(const std::vector& mods, const bool addPublic) { m_IncompatibleMods.clear(); m_EnabledMods.clear(); std::unordered_map 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::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 Mod::GetEnabledModsData() const { std::vector 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& modsA, const std::vector& modsB) { // Mods must be loaded in the same order. std::vector::const_iterator a = modsA.begin(); std::vector::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 Mod::CheckForIncompatibleMods(const std::vector& mods) const { std::vector incompatibleMods; std::unordered_map> modDependencies; std::unordered_map modNameVersions; for (const CStr& mod : mods) { if (mod == "mod" || mod == "user") continue; std::vector::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 toCheck = { "<=", ">=", "=", "<", ">" }; for (const CStr& mod : mods) { if (mod == "mod" || mod == "user") continue; const std::unordered_map>::iterator res = modDependencies.find(mod); if (res == modDependencies.end()) continue; const std::vector 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::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 versionSplit; std::vector 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; }