Files
OpenRCT2-Web/src/openrct2/PlatformEnvironment.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

454 lines
16 KiB
C++
Raw Normal View History

2016-12-07 15:49:29 +00:00
/*****************************************************************************
2026-01-05 14:42:59 +01:00
* Copyright (c) 2014-2026 OpenRCT2 developers
2016-12-07 15:49:29 +00:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2016-12-07 15:49:29 +00:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2016-12-07 15:49:29 +00:00
*****************************************************************************/
2018-06-22 23:25:16 +02:00
#include "PlatformEnvironment.h"
2024-07-09 16:23:31 +02:00
#include "Diagnostic.h"
2018-06-22 23:25:16 +02:00
#include "OpenRCT2.h"
2017-06-11 12:53:37 +01:00
#include "config/Config.h"
2024-12-15 17:39:52 +01:00
#include "core/EnumUtils.hpp"
#include "core/File.h"
#include "core/FileSystem.hpp"
2016-12-07 16:28:07 +00:00
#include "core/Path.hpp"
#include "core/String.hpp"
2022-02-18 21:57:00 +01:00
#include "platform/Platform.h"
2016-12-07 15:49:29 +00:00
#include <map>
#include <mutex>
#include <optional>
#include <unordered_map>
2017-06-11 12:53:37 +01:00
using namespace OpenRCT2;
2025-01-19 16:59:45 +01:00
static constexpr const char* kDirectoryNamesRCT2[] = {
"Data", // DATA
"Landscapes", // LANDSCAPE
nullptr, // LANGUAGE
nullptr, // LOG_CHAT
nullptr, // LOG_SERVER
nullptr, // NETWORK_KEY
"ObjData", // OBJECT
nullptr, // PLUGIN
"Saved Games", // SAVE
"Scenarios", // SCENARIO
nullptr, // SCREENSHOT
nullptr, // SEQUENCE
nullptr, // SHADER
nullptr, // THEME
"Tracks", // TRACK
};
2025-01-19 16:59:45 +01:00
static constexpr u8string_view kDirectoryNamesOpenRCT2[] = {
u8"data", // DATA
u8"landscape", // LANDSCAPE
u8"language", // LANGUAGE
u8"chatlogs", // LOG_CHAT
u8"serverlogs", // LOG_SERVER
u8"keys", // NETWORK_KEY
u8"object", // OBJECT
u8"plugin", // PLUGIN
u8"save", // SAVE
u8"scenario", // SCENARIO
u8"screenshot", // SCREENSHOT
u8"sequence", // SEQUENCE
u8"shaders", // SHADER
u8"themes", // THEME
u8"track", // TRACK
u8"heightmap", // HEIGHTMAP
u8"replay", // REPLAY
u8"desyncs", // DESYNCS
u8"crash", // CRASH
u8"assetpack", // ASSET_PACK
u8"scenario_patches", // SCENARIO_PATCHES
};
2025-01-19 16:59:45 +01:00
static constexpr u8string_view kFileNames[] = {
u8"config.ini", // CONFIG
u8"hotkeys.dat", // CONFIG_SHORTCUTS_LEGACY
u8"shortcuts.json", // CONFIG_SHORTCUTS
u8"objects.idx", // CACHE_OBJECTS
u8"tracks.idx", // CACHE_TRACKS
u8"scenarios.idx", // CACHE_SCENARIOS
u8"groups.json", // NETWORK_GROUPS
u8"servers.cfg", // NETWORK_SERVERS
u8"users.json", // NETWORK_USERS
u8"highscores.dat", // SCORES
u8"scores.dat", // SCORES (LEGACY)
u8"Saved Games" PATH_SEPARATOR "scores.dat", // SCORES (RCT2)
u8"changelog.txt", // CHANGELOG
u8"plugin.store.json", // PLUGIN_STORE
u8"contributors.md", // CONTRIBUTORS
};
/**
* Lower cases ASCII characters only, which matches how String::iequals compares strings.
*/
2026-10-11 12:16:34 +02:00
static u8string toLowerAscii(const u8string_view str)
{
u8string result(str);
for (auto& c : result)
{
2026-10-11 12:10:02 +02:00
c = tolower(c);
}
return result;
}
/**
* Caches the file names of the original game data directories so that they can be looked up case
* insensitively without hitting the file system.
*
* The game refers to the original files by their MS-DOS era upper case names, while an actual
* installation may use any casing, so every look up has to consider all the names in a directory.
* Doing that per look up means listing a directory of a few thousand files hundreds of times while
* loading a park, which is ruinously slow on file systems with a high per-operation cost, such as
* the FUSE file system Flatpak's document portal provides. The original game data does not change
* while the game is running, so each directory only has to be listed once.
*/
class OriginalGameDataIndex
{
private:
struct Entry
{
u8string path;
int32_t depth{};
};
// Maps the lower cased name of a file to the file it was found as.
using Listing = std::unordered_map<u8string, Entry, String::Hash, std::equal_to<>>;
std::mutex _mutex;
std::map<u8string, std::optional<Listing>, std::less<>> _listings;
public:
/**
* Returns the real path of the file with the given name inside the given directory or one of its
* sub directories, matching the name case insensitively. Returns an empty string if there is no
* such file.
*/
2026-10-11 12:16:34 +02:00
u8string findFile(const u8string& directory, u8string_view fileName)
{
std::lock_guard guard(_mutex);
2026-10-11 12:16:34 +02:00
const auto& listing = getListing(directory);
if (!listing.has_value())
{
// The directory could not be listed, fall back to querying the file system.
return Path::ResolveCasing(Path::Combine(directory, fileName));
}
2026-10-11 12:16:34 +02:00
auto it = listing->find(toLowerAscii(fileName));
if (it == listing->end())
return {};
return it->second.path;
}
2026-10-11 12:16:34 +02:00
void clear()
{
std::lock_guard guard(_mutex);
_listings.clear();
}
private:
2026-10-11 12:16:34 +02:00
const std::optional<Listing>& getListing(const u8string& directory)
{
auto it = _listings.find(directory);
if (it != _listings.end())
return it->second;
2026-10-11 12:16:34 +02:00
return _listings.emplace(directory, readListing(directory)).first->second;
}
2026-10-11 12:16:34 +02:00
static std::optional<Listing> readListing(const u8string& directory)
{
std::error_code ec;
auto it = fs::recursive_directory_iterator(fs::u8path(directory), fs::directory_options::skip_permission_denied, ec);
if (ec)
return std::nullopt;
Listing listing;
const auto end = fs::recursive_directory_iterator();
for (; it != end; it.increment(ec))
{
if (ec)
break;
// Asks the directory entry rather than the file system, as the entry already knows the
// type of the file on the platforms that report it while listing a directory.
std::error_code typeEc;
if (it->is_directory(typeEc) || typeEc)
continue;
const auto depth = it.depth();
auto entry = Entry{ it->path().u8string(), depth };
2026-10-11 12:16:34 +02:00
auto [entryIt, inserted] = listing.try_emplace(toLowerAscii(it->path().filename().u8string()), entry);
// Prefer the files closest to the root of the directory, files in sub directories are
// only meant to be used when there is none of that name next to the other game files.
if (!inserted && depth < entryIt->second.depth)
entryIt->second = std::move(entry);
}
return listing;
}
};
2017-06-06 17:02:45 +01:00
class PlatformEnvironment final : public IPlatformEnvironment
2016-12-07 15:49:29 +00:00
{
2016-12-07 16:28:07 +00:00
private:
2025-01-19 16:59:45 +01:00
u8string _basePath[kDirBaseCount];
RCT2Variant _rct2Variant = RCT2Variant::rct2Original;
mutable OriginalGameDataIndex _originalGameDataIndex;
2016-12-07 16:28:07 +00:00
2016-12-07 15:49:29 +00:00
public:
2025-03-25 22:27:57 +01:00
explicit PlatformEnvironment(DirBaseValues basePaths)
2016-12-07 15:49:29 +00:00
{
2025-01-19 16:59:45 +01:00
for (size_t i = 0; i < kDirBaseCount; i++)
2016-12-07 16:28:07 +00:00
{
_basePath[i] = basePaths[i];
}
2016-12-07 15:49:29 +00:00
}
2025-03-25 22:27:57 +01:00
u8string GetDirectoryPath(DirBase base) const override
2017-06-06 17:02:45 +01:00
{
2024-03-04 19:21:12 +00:00
return _basePath[EnumValue(base)];
2017-06-06 17:02:45 +01:00
}
2025-03-25 23:39:27 +01:00
u8string GetDirectoryPath(DirBase base, DirId did) const override
2016-12-07 15:49:29 +00:00
{
2017-06-06 17:02:45 +01:00
auto basePath = GetDirectoryPath(base);
2022-02-27 22:23:35 +01:00
u8string_view directoryName;
2018-06-22 23:25:16 +02:00
switch (base)
{
default:
2025-03-25 22:27:57 +01:00
case DirBase::rct1:
if (basePath.empty())
return {};
2025-01-19 16:59:45 +01:00
directoryName = kDirectoryNamesRCT2[EnumValue(did)];
2018-06-22 23:25:16 +02:00
break;
2025-03-25 22:27:57 +01:00
case DirBase::rct2:
switch (_rct2Variant)
{
case RCT2Variant::rct2Original:
directoryName = kDirectoryNamesRCT2[EnumValue(did)];
break;
case RCT2Variant::rctClassicWindows:
directoryName = Platform::kRCTClassicWindowsDataFolder;
break;
case RCT2Variant::rctClassicMac:
directoryName = Platform::kRCTClassicMacOSDataFolder;
break;
2025-04-17 21:20:59 +02:00
case RCT2Variant::rctClassicPlusMac:
directoryName = Platform::kRCTClassicPlusMacOSDataFolder;
2025-04-17 21:20:59 +02:00
break;
}
2022-08-16 19:38:00 +01:00
break;
2025-03-25 22:27:57 +01:00
case DirBase::openrct2:
case DirBase::user:
case DirBase::config:
2025-01-19 16:59:45 +01:00
directoryName = kDirectoryNamesOpenRCT2[EnumValue(did)];
2018-06-22 23:25:16 +02:00
break;
2016-12-07 16:28:07 +00:00
}
2016-12-07 15:49:29 +00:00
2017-06-06 17:02:45 +01:00
return Path::Combine(basePath, directoryName);
2016-12-07 15:49:29 +00:00
}
2016-12-07 16:28:07 +00:00
2025-03-25 23:57:36 +01:00
u8string GetFilePath(PathId pathid) const override
2016-12-07 16:28:07 +00:00
{
2017-11-28 20:57:34 +00:00
auto dirbase = GetDefaultBaseDirectory(pathid);
auto basePath = GetDirectoryPath(dirbase);
2025-01-19 16:59:45 +01:00
auto fileName = kFileNames[EnumValue(pathid)];
auto assetPath = Platform::GetAssetPath();
if (!assetPath.empty())
{
auto combinedAssetPath = Path::Combine(assetPath, basePath, fileName);
if (File::Exists(combinedAssetPath))
{
return combinedAssetPath;
}
}
2017-11-28 20:57:34 +00:00
return Path::Combine(basePath, fileName);
2016-12-07 16:28:07 +00:00
}
2025-03-25 23:39:27 +01:00
u8string FindFile(DirBase base, DirId did, u8string_view fileName) const override
{
auto dataPath = GetDirectoryPath(base, did);
2022-08-16 20:01:40 +01:00
std::string alternativeFilename;
if (_rct2Variant != RCT2Variant::rct2Original && base == DirBase::rct2 && did == DirId::data)
2022-08-16 20:01:40 +01:00
{
// Special case, handle RCT Classic css ogg files
2024-12-11 22:28:31 +01:00
if (String::startsWith(fileName, "css", true) && String::endsWith(fileName, ".dat", true))
2022-08-16 20:01:40 +01:00
{
alternativeFilename = fileName.substr(0, fileName.size() - 3);
alternativeFilename.append("ogg");
fileName = alternativeFilename;
}
}
auto path = ResolveOriginalGameDataPath(base, dataPath, fileName);
2025-03-25 23:39:27 +01:00
if (base == DirBase::rct1 && did == DirId::data && !File::Exists(path))
{
// Special case, handle RCT1 steam layout where some data files are under a CD root
auto basePath = GetDirectoryPath(base);
auto alternativePath = Path::ResolveCasing(Path::Combine(basePath, "RCTdeluxe_install", "Data", fileName));
if (File::Exists(alternativePath))
{
path = alternativePath;
}
}
2022-08-16 20:01:40 +01:00
return path;
}
2025-03-25 22:27:57 +01:00
void SetBasePath(DirBase base, u8string_view path) override
2017-06-13 00:38:56 +01:00
{
2024-03-04 19:21:12 +00:00
_basePath[EnumValue(base)] = path;
2026-10-11 12:16:34 +02:00
_originalGameDataIndex.clear();
2022-08-16 19:38:00 +01:00
2025-03-25 22:27:57 +01:00
if (base == DirBase::rct2)
2022-08-16 19:38:00 +01:00
{
// This value being empty can be valid on headless.
auto variant = Platform::classifyGamePath(path);
if (variant.has_value())
_rct2Variant = variant.value();
2022-08-16 19:38:00 +01:00
}
2017-06-13 00:38:56 +01:00
}
2022-08-29 10:50:42 +01:00
bool IsUsingClassic() const override
{
return _rct2Variant != RCT2Variant::rct2Original;
2022-08-29 10:50:42 +01:00
}
2016-12-07 16:28:07 +00:00
private:
/**
* Resolves the casing of a file inside one of the original game data directories, using the
* cached directory listings where possible.
*/
u8string ResolveOriginalGameDataPath(DirBase base, const u8string& dataPath, u8string_view fileName) const
{
const auto isOriginalGameData = base == DirBase::rct1 || base == DirBase::rct2;
const auto isPlainFileName = fileName.find_first_of("/\\") == u8string_view::npos;
if (!isOriginalGameData || !isPlainFileName || dataPath.empty())
{
return Path::ResolveCasing(Path::Combine(dataPath, fileName));
}
2026-10-11 12:16:34 +02:00
return _originalGameDataIndex.findFile(dataPath, fileName);
}
2025-03-25 23:57:36 +01:00
static DirBase GetDefaultBaseDirectory(PathId pathid)
2017-11-28 20:57:34 +00:00
{
switch (pathid)
{
2025-03-25 23:57:36 +01:00
case PathId::config:
case PathId::configShortcutsLegacy:
case PathId::configShortcuts:
2025-03-25 22:27:57 +01:00
return DirBase::config;
2025-03-25 23:57:36 +01:00
case PathId::cacheObjects:
case PathId::cacheTracks:
case PathId::cacheScenarios:
2025-03-25 22:27:57 +01:00
return DirBase::cache;
2025-03-25 23:57:36 +01:00
case PathId::scoresRCT2:
2025-03-25 22:27:57 +01:00
return DirBase::rct2;
2025-03-25 23:57:36 +01:00
case PathId::changelog:
case PathId::contributors:
2025-03-25 22:27:57 +01:00
return DirBase::documentation;
2025-03-25 23:57:36 +01:00
case PathId::networkGroups:
case PathId::networkServers:
case PathId::networkUsers:
case PathId::scores:
case PathId::scoresLegacy:
2018-06-22 23:25:16 +02:00
default:
2025-03-25 22:27:57 +01:00
return DirBase::user;
2017-11-28 20:57:34 +00:00
}
}
2016-12-07 16:28:07 +00:00
};
2025-03-25 22:27:57 +01:00
std::unique_ptr<IPlatformEnvironment> OpenRCT2::CreatePlatformEnvironment(DirBaseValues basePaths)
2016-12-07 16:28:07 +00:00
{
2018-04-20 19:58:59 +01:00
return std::make_unique<PlatformEnvironment>(basePaths);
2016-12-07 16:28:07 +00:00
}
2022-02-27 22:23:35 +01:00
static u8string GetOpenRCT2DirectoryName()
{
#if defined(__ANDROID__)
2022-02-27 22:23:35 +01:00
return u8"openrct2-user";
#else
2022-02-27 22:23:35 +01:00
return u8"OpenRCT2";
#endif
}
2018-04-20 19:58:59 +01:00
std::unique_ptr<IPlatformEnvironment> OpenRCT2::CreatePlatformEnvironment()
2017-06-11 12:53:37 +01:00
{
auto subDirectory = GetOpenRCT2DirectoryName();
2017-06-11 12:53:37 +01:00
// Set default paths
2025-01-19 16:59:45 +01:00
std::string basePaths[kDirBaseCount];
2025-03-25 22:27:57 +01:00
basePaths[EnumValue(DirBase::openrct2)] = Platform::GetInstallPath();
2025-03-26 12:06:31 +01:00
basePaths[EnumValue(DirBase::user)] = Path::Combine(Platform::GetFolderPath(SpecialFolder::userData), subDirectory);
basePaths[EnumValue(DirBase::config)] = Path::Combine(Platform::GetFolderPath(SpecialFolder::userConfig), subDirectory);
basePaths[EnumValue(DirBase::cache)] = Path::Combine(Platform::GetFolderPath(SpecialFolder::userCache), subDirectory);
2025-03-25 22:27:57 +01:00
basePaths[EnumValue(DirBase::documentation)] = Platform::GetDocsPath();
2017-11-28 20:57:34 +00:00
// Override paths that have been specified via the command line
2022-01-27 14:21:46 +01:00
if (!gCustomRCT1DataPath.empty())
{
2025-03-25 22:27:57 +01:00
basePaths[EnumValue(DirBase::rct1)] = gCustomRCT1DataPath;
}
2022-01-27 14:21:46 +01:00
if (!gCustomRCT2DataPath.empty())
2017-11-28 20:57:34 +00:00
{
2025-03-25 22:27:57 +01:00
basePaths[EnumValue(DirBase::rct2)] = gCustomRCT2DataPath;
2017-11-28 20:57:34 +00:00
}
2022-01-27 14:21:46 +01:00
if (!gCustomOpenRCT2DataPath.empty())
2017-11-28 20:57:34 +00:00
{
2025-03-25 22:27:57 +01:00
basePaths[EnumValue(DirBase::openrct2)] = gCustomOpenRCT2DataPath;
2017-11-28 20:57:34 +00:00
}
2022-01-27 14:21:46 +01:00
if (!gCustomUserDataPath.empty())
2017-11-28 20:57:34 +00:00
{
2025-03-25 22:27:57 +01:00
basePaths[EnumValue(DirBase::user)] = gCustomUserDataPath;
basePaths[EnumValue(DirBase::config)] = gCustomUserDataPath;
basePaths[EnumValue(DirBase::cache)] = gCustomUserDataPath;
2017-11-28 20:57:34 +00:00
}
2017-06-11 12:53:37 +01:00
2025-03-25 22:27:57 +01:00
if (basePaths[EnumValue(DirBase::documentation)].empty())
2018-01-21 02:13:32 +00:00
{
2025-03-25 22:27:57 +01:00
basePaths[EnumValue(DirBase::documentation)] = basePaths[EnumValue(DirBase::openrct2)];
2018-01-21 02:13:32 +00:00
}
auto env = CreatePlatformEnvironment(basePaths);
// Now load the config so we can get the RCT1 and RCT2 paths
2025-03-25 23:57:36 +01:00
auto configPath = env->GetFilePath(PathId::config);
2024-05-10 12:10:21 +02:00
Config::SetDefaults();
if (!Config::OpenFromPath(configPath))
{
2024-05-10 12:10:21 +02:00
Config::SaveToPath(configPath);
}
2022-01-27 14:21:46 +01:00
if (gCustomRCT1DataPath.empty())
{
2025-10-07 20:14:37 +02:00
env->SetBasePath(DirBase::rct1, Config::Get().general.rct1Path);
}
2022-01-27 14:21:46 +01:00
if (gCustomRCT2DataPath.empty())
{
2025-10-07 20:14:37 +02:00
env->SetBasePath(DirBase::rct2, Config::Get().general.rct2Path);
}
2017-06-11 12:53:37 +01:00
// Log base paths
2025-03-25 22:27:57 +01:00
LOG_VERBOSE("DirBase::rct1 : %s", env->GetDirectoryPath(DirBase::rct1).c_str());
LOG_VERBOSE("DirBase::rct2 : %s", env->GetDirectoryPath(DirBase::rct2).c_str());
LOG_VERBOSE("DirBase::openrct2: %s", env->GetDirectoryPath(DirBase::openrct2).c_str());
LOG_VERBOSE("DirBase::user : %s", env->GetDirectoryPath(DirBase::user).c_str());
LOG_VERBOSE("DirBase::config : %s", env->GetDirectoryPath(DirBase::config).c_str());
LOG_VERBOSE("DirBase::cache : %s", env->GetDirectoryPath(DirBase::cache).c_str());
2017-06-11 12:53:37 +01:00
return env;
}