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

287 lines
9.9 KiB
C++
Raw Normal View History

2016-12-07 15:49:29 +00:00
/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 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"
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
2017-06-11 12:53:37 +01:00
using namespace OpenRCT2;
static constexpr const char* DirectoryNamesRCT2[] = {
"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
};
static constexpr u8string_view DirectoryNamesOpenRCT2[] = {
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
};
static constexpr u8string_view FileNames[] = {
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
};
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:
2022-02-12 21:42:55 +01:00
u8string _basePath[DIRBASE_COUNT];
bool _usingRCTClassic{};
2016-12-07 16:28:07 +00:00
2016-12-07 15:49:29 +00:00
public:
2018-01-04 02:12:34 +01:00
explicit PlatformEnvironment(DIRBASE_VALUES basePaths)
2016-12-07 15:49:29 +00:00
{
for (size_t i = 0; i < DIRBASE_COUNT; i++)
2016-12-07 16:28:07 +00:00
{
_basePath[i] = basePaths[i];
}
2016-12-07 15:49:29 +00:00
}
2022-02-12 21:42:55 +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
}
2022-02-12 21:42:55 +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:
case DIRBASE::RCT1:
2024-03-04 19:21:12 +00:00
directoryName = DirectoryNamesRCT2[EnumValue(did)];
2018-06-22 23:25:16 +02:00
break;
2022-08-16 19:38:00 +01:00
case DIRBASE::RCT2:
2024-03-04 19:21:12 +00:00
directoryName = _usingRCTClassic ? "Assets" : DirectoryNamesRCT2[EnumValue(did)];
2022-08-16 19:38:00 +01:00
break;
2018-06-22 23:25:16 +02:00
case DIRBASE::OPENRCT2:
case DIRBASE::USER:
case DIRBASE::CONFIG:
2024-03-04 19:21:12 +00:00
directoryName = DirectoryNamesOpenRCT2[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
2022-02-12 21:42:55 +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);
2024-03-04 19:21:12 +00:00
auto fileName = FileNames[EnumValue(pathid)];
2017-11-28 20:57:34 +00:00
return Path::Combine(basePath, fileName);
2016-12-07 16:28:07 +00: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 (_usingRCTClassic && 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 = Path::ResolveCasing(Path::Combine(dataPath, fileName));
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;
}
2022-02-12 21:42:55 +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;
2022-08-16 19:38:00 +01:00
if (base == DIRBASE::RCT2)
{
_usingRCTClassic = Platform::IsRCTClassicPath(path);
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 _usingRCTClassic;
2022-08-29 10:50:42 +01:00
}
2016-12-07 16:28:07 +00:00
private:
2017-11-28 20:57:34 +00:00
static DIRBASE GetDefaultBaseDirectory(PATHID pathid)
{
switch (pathid)
{
2018-06-22 23:25:16 +02:00
case PATHID::CONFIG:
2021-01-12 22:34:30 +00:00
case PATHID::CONFIG_SHORTCUTS_LEGACY:
case PATHID::CONFIG_SHORTCUTS:
2018-06-22 23:25:16 +02:00
return DIRBASE::CONFIG;
case PATHID::CACHE_OBJECTS:
case PATHID::CACHE_TRACKS:
case PATHID::CACHE_SCENARIOS:
return DIRBASE::CACHE;
case PATHID::SCORES_RCT2:
return DIRBASE::RCT2;
case PATHID::CHANGELOG:
case PATHID::CONTRIBUTORS:
2018-06-22 23:25:16 +02:00
return DIRBASE::DOCUMENTATION;
case PATHID::NETWORK_GROUPS:
case PATHID::NETWORK_SERVERS:
case PATHID::NETWORK_USERS:
case PATHID::SCORES:
case PATHID::SCORES_LEGACY:
default:
return DIRBASE::USER;
2017-11-28 20:57:34 +00:00
}
}
2016-12-07 16:28:07 +00:00
};
2018-04-20 19:58:59 +01:00
std::unique_ptr<IPlatformEnvironment> OpenRCT2::CreatePlatformEnvironment(DIRBASE_VALUES 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
2017-11-28 20:57:34 +00:00
std::string basePaths[DIRBASE_COUNT];
2024-03-04 19:21:12 +00:00
basePaths[EnumValue(DIRBASE::OPENRCT2)] = Platform::GetInstallPath();
basePaths[EnumValue(DIRBASE::USER)] = Path::Combine(Platform::GetFolderPath(SPECIAL_FOLDER::USER_DATA), subDirectory);
basePaths[EnumValue(DIRBASE::CONFIG)] = Path::Combine(Platform::GetFolderPath(SPECIAL_FOLDER::USER_CONFIG), subDirectory);
basePaths[EnumValue(DIRBASE::CACHE)] = Path::Combine(Platform::GetFolderPath(SPECIAL_FOLDER::USER_CACHE), subDirectory);
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())
{
2024-03-04 19:21:12 +00: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
{
2024-03-04 19:21:12 +00: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
{
2024-03-04 19:21:12 +00: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
{
2024-03-04 19:21:12 +00: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
2024-03-04 19:21:12 +00:00
if (basePaths[EnumValue(DIRBASE::DOCUMENTATION)].empty())
2018-01-21 02:13:32 +00:00
{
2024-03-04 19:21:12 +00:00
basePaths[EnumValue(DIRBASE::DOCUMENTATION)] = basePaths[EnumValue(DIRBASE::OPENRCT2)];
2018-01-21 02:13:32 +00:00
}
auto env = OpenRCT2::CreatePlatformEnvironment(basePaths);
// Now load the config so we can get the RCT1 and RCT2 paths
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())
{
2024-05-10 12:10:21 +02:00
env->SetBasePath(DIRBASE::RCT1, Config::Get().general.RCT1Path);
}
2022-01-27 14:21:46 +01:00
if (gCustomRCT2DataPath.empty())
{
2024-05-10 12:10:21 +02:00
env->SetBasePath(DIRBASE::RCT2, Config::Get().general.RCT2Path);
}
2017-06-11 12:53:37 +01:00
// Log base paths
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;
}