1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-28 01:04:50 +01:00

Merge pull request #16380 from Gymnasiast/refactor/platform-to-new

Convert a few platform functions to new framework
This commit is contained in:
Michael Steenbeek
2022-01-06 19:46:43 +01:00
committed by GitHub
13 changed files with 74 additions and 144 deletions

View File

@@ -772,7 +772,7 @@ namespace OpenRCT2
if (String::IsNullOrEmpty(gCustomRCT2DataPath))
{
// Check install directory
if (gConfigGeneral.rct2_path.empty() || !platform_original_game_data_exists(gConfigGeneral.rct2_path.c_str()))
if (gConfigGeneral.rct2_path.empty() || !Platform::OriginalGameDataExists(gConfigGeneral.rct2_path))
{
log_verbose(
"install directory does not exist or invalid directory selected, %s", gConfigGeneral.rct2_path.c_str());
@@ -1557,14 +1557,3 @@ void platform_get_user_directory(utf8* outPath, const utf8* subDirectory, size_t
}
String::Set(outPath, outSize, path.c_str());
}
/**
* This function is deprecated.
* Use IPlatformEnvironment instead.
*/
void platform_get_openrct2_data_path(utf8* outPath, size_t outSize)
{
auto env = GetContext()->GetPlatformEnvironment();
auto path = env->GetDirectoryPath(DIRBASE::OPENRCT2);
String::Set(outPath, outSize, path.c_str());
}

View File

@@ -388,7 +388,7 @@ namespace Config
auto playerName = reader->GetString("player_name", "");
if (playerName.empty())
{
playerName = platform_get_username();
playerName = Platform::GetUsername();
if (playerName.empty())
{
playerName = "Player";
@@ -695,7 +695,7 @@ namespace Config
for (const utf8* location : searchLocations)
{
if (platform_original_game_data_exists(location))
if (Platform::OriginalGameDataExists(location))
{
return location;
}
@@ -705,20 +705,20 @@ namespace Config
if (platform_get_steam_path(steamPath, sizeof(steamPath)))
{
std::string location = Path::Combine(steamPath, platform_get_rct2_steam_dir());
if (platform_original_game_data_exists(location.c_str()))
if (Platform::OriginalGameDataExists(location))
{
return location;
}
}
auto discordPath = Platform::GetFolderPath(SPECIAL_FOLDER::RCT2_DISCORD);
if (!discordPath.empty() && platform_original_game_data_exists(discordPath.c_str()))
if (!discordPath.empty() && Platform::OriginalGameDataExists(discordPath))
{
return discordPath;
}
auto exePath = Path::GetDirectory(Platform::GetCurrentExecutablePath());
if (platform_original_game_data_exists(exePath.c_str()))
if (Platform::OriginalGameDataExists(exePath))
{
return exePath;
}
@@ -922,7 +922,7 @@ bool config_find_or_browse_install_directory()
}
gConfigGeneral.rct2_path = installPath;
if (platform_original_game_data_exists(installPath.c_str()))
if (Platform::OriginalGameDataExists(installPath))
{
return true;
}

View File

@@ -265,10 +265,10 @@ bool gfx_load_g2()
{
log_verbose("gfx_load_g2()");
char path[MAX_PATH];
auto env = GetContext()->GetPlatformEnvironment();
std::string path = Path::Combine(env->GetDirectoryPath(DIRBASE::OPENRCT2), "g2.dat");
platform_get_openrct2_data_path(path, sizeof(path));
safe_strcat_path(path, "g2.dat", MAX_PATH);
try
{
auto fs = FileStream(path, FILE_MODE_OPEN);

View File

@@ -139,7 +139,7 @@ static std::optional<std::string> screenshot_get_next_path()
// Generate a path with a `tries` number
auto pathComposer = [&screenshotDirectory, &name](int tries) {
auto composedFilename = platform_sanitise_filename(
auto composedFilename = Platform::SanitiseFilename(
name + ((tries > 0) ? " ("s + std::to_string(tries) + ")" : ""s) + ".png");
return screenshotDirectory + PATH_SEPARATOR + composedFilename;
};

View File

@@ -253,6 +253,17 @@ namespace Platform
{
return true;
}
std::string GetUsername()
{
std::string result;
auto pw = getpwuid(getuid());
if (pw != nullptr)
{
result = std::string(pw->pw_name);
}
return result;
}
} // namespace Platform
#endif

View File

@@ -16,6 +16,7 @@
# include "../Version.h"
# include <datetimeapi.h>
# include <lmcons.h>
# include <memory>
# include <shlobj.h>
# undef GetEnvironmentVariable
@@ -613,6 +614,18 @@ namespace Platform
// using the same window in an unaccelerated and accelerated context is unsupported by SDL2
return openGL;
}
std::string GetUsername()
{
std::string result;
wchar_t usernameW[UNLEN + 1]{};
DWORD usernameLength = UNLEN + 1;
if (GetUserNameW(usernameW, &usernameLength))
{
result = String::ToUtf8(usernameW);
}
return result;
}
} // namespace Platform
#endif

View File

@@ -46,6 +46,12 @@ namespace Platform
bool FindApp(const std::string& app, std::string* output);
int32_t Execute(const std::string& command, std::string* output = nullptr);
bool OriginalGameDataExists(std::string_view path);
std::string GetUsername();
std::string SanitiseFilename(std::string_view originalName);
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__)
std::string GetEnvironmentPath(const char* name);
std::string GetHomePath();

View File

@@ -42,33 +42,6 @@
static utf8 _userDataDirectoryPath[MAX_PATH] = { 0 };
void platform_get_date_utc(rct2_date* out_date)
{
assert(out_date != nullptr);
time_t rawtime;
struct tm* timeinfo;
struct tm buf;
time(&rawtime);
timeinfo = gmtime_r(&rawtime, &buf);
out_date->day = timeinfo->tm_mday;
out_date->month = timeinfo->tm_mon + 1;
out_date->year = timeinfo->tm_year + 1900;
out_date->day_of_week = timeinfo->tm_wday;
}
void platform_get_time_utc(rct2_time* out_time)
{
assert(out_time != nullptr);
time_t rawtime;
struct tm* timeinfo;
struct tm buf;
time(&rawtime);
timeinfo = gmtime_r(&rawtime, &buf);
out_time->second = timeinfo->tm_sec;
out_time->minute = timeinfo->tm_min;
out_time->hour = timeinfo->tm_hour;
}
bool platform_directory_exists(const utf8* path)
{
struct stat dirinfo;
@@ -77,15 +50,6 @@ bool platform_directory_exists(const utf8* path)
return result == 0 && S_ISDIR(dirinfo.st_mode);
}
bool platform_original_game_data_exists(const utf8* path)
{
char checkPath[MAX_PATH];
safe_strcpy(checkPath, path, MAX_PATH);
safe_strcat_path(checkPath, "Data", MAX_PATH);
safe_strcat_path(checkPath, "g1.dat", MAX_PATH);
return Platform::FileExists(checkPath);
}
// Implement our own version of getumask(), as it is documented being
// "a vaporware GNU extension".
static mode_t openrct2_getumask()
@@ -346,17 +310,6 @@ datetime64 platform_get_datetime_now_utc()
return utcNow;
}
std::string platform_get_username()
{
std::string result;
auto pw = getpwuid(getuid());
if (pw != nullptr)
{
result = std::string(pw->pw_name);
}
return result;
}
bool platform_process_is_elevated()
{
# ifndef __EMSCRIPTEN__

View File

@@ -20,6 +20,8 @@
#include "../OpenRCT2.h"
#include "../config/Config.h"
#include "../core/FileSystem.hpp"
#include "../core/Path.hpp"
#include "../core/String.hpp"
#include "../drawing/Drawing.h"
#include "../drawing/LightFX.h"
#include "../localisation/Currency.h"
@@ -99,9 +101,31 @@ namespace Platform
log_verbose("Checking if file exists: %s", path.c_str());
return fs::exists(file);
}
} // namespace Platform
using update_palette_func = void (*)(const uint8_t*, int32_t, int32_t);
bool OriginalGameDataExists(std::string_view path)
{
std::string combinedPath = Path::ResolveCasing(Path::Combine(path, "Data", "g1.dat"));
return Platform::FileExists(combinedPath);
}
std::string SanitiseFilename(std::string_view originalName)
{
#ifdef _WIN32
static constexpr std::array prohibited = { '<', '>', '*', '\\', ':', '|', '?', '"', '/' };
#else
static constexpr std::array prohibited = { '/' };
#endif
auto sanitised = std::string(originalName);
std::replace_if(
sanitised.begin(), sanitised.end(),
[](const std::string::value_type& ch) -> bool {
return std::find(prohibited.begin(), prohibited.end(), ch) != prohibited.end();
},
'_');
sanitised = String::Trim(sanitised);
return sanitised;
}
} // namespace Platform
GamePalette gPalette;
@@ -236,21 +260,6 @@ CurrencyType platform_get_currency_value(const char* currCode)
return CurrencyType::Pounds;
}
#ifndef _WIN32
std::string platform_sanitise_filename(const std::string& path)
{
static constexpr std::array prohibited = { '/' };
auto sanitised = path;
std::replace_if(
sanitised.begin(), sanitised.end(),
[](const std::string::value_type& ch) -> bool {
return std::find(prohibited.begin(), prohibited.end(), ch) != prohibited.end();
},
'_');
return sanitised;
}
#endif
#ifndef __ANDROID__
float platform_get_default_scale()
{

View File

@@ -66,15 +66,6 @@ bool platform_directory_exists(const utf8* path)
return dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
}
bool platform_original_game_data_exists(const utf8* path)
{
utf8 checkPath[MAX_PATH];
safe_strcpy(checkPath, path, MAX_PATH);
safe_strcat_path(checkPath, "Data", MAX_PATH);
safe_strcat_path(checkPath, "g1.dat", MAX_PATH);
return Platform::FileExists(checkPath);
}
bool platform_ensure_directory_exists(const utf8* path)
{
if (platform_directory_exists(path))
@@ -194,20 +185,6 @@ std::string platform_get_rct2_steam_dir()
return "Rollercoaster Tycoon 2";
}
std::string platform_sanitise_filename(const std::string& path)
{
static constexpr std::array prohibited = { '<', '>', '*', '\\', ':', '|', '?', '"', '/' };
auto sanitised = path;
std::replace_if(
sanitised.begin(), sanitised.end(),
[](const std::string::value_type& ch) -> bool {
return std::find(prohibited.begin(), prohibited.end(), ch) != prohibited.end();
},
'_');
sanitised = String::Trim(sanitised);
return sanitised;
}
uint16_t platform_get_locale_language()
{
CHAR langCode[4];
@@ -469,18 +446,6 @@ datetime64 platform_get_datetime_now_utc()
return utcNow;
}
std::string platform_get_username()
{
std::string result;
wchar_t usernameW[UNLEN + 1]{};
DWORD usernameLength = UNLEN + 1;
if (GetUserNameW(usernameW, &usernameLength))
{
result = String::ToUtf8(usernameW);
}
return result;
}
bool platform_process_is_elevated()
{
BOOL isElevated = FALSE;

View File

@@ -137,12 +137,6 @@ MeasurementFormat platform_get_locale_measurement_format()
}
}
void platform_get_changelog_path(utf8* outPath, size_t outSize)
{
platform_get_openrct2_data_path(outPath, outSize);
safe_strcat_path(outPath, "changelog.txt", outSize);
}
bool platform_get_steam_path(utf8* outPath, size_t outSize)
{
char steamPath[1024] = { 0 };

View File

@@ -85,12 +85,9 @@ struct file_dialog_desc
void platform_update_palette(const uint8_t* colours, int32_t start_index, int32_t num_colours);
void platform_toggle_windowed_mode();
void platform_refresh_video(bool recreate_window);
void platform_get_date_utc(rct2_date* out_date);
void platform_get_time_utc(rct2_time* out_time);
// Platform specific definitions
bool platform_directory_exists(const utf8* path);
bool platform_original_game_data_exists(const utf8* path);
time_t platform_file_get_modified_time(const utf8* path);
bool platform_ensure_directory_exists(const utf8* path);
bool platform_directory_delete(const utf8* path);
@@ -105,9 +102,7 @@ bool platform_file_move(const utf8* srcPath, const utf8* dstPath);
bool platform_file_delete(const utf8* path);
uint32_t platform_get_ticks();
void platform_sleep(uint32_t ms);
void platform_get_openrct2_data_path(utf8* outPath, size_t outSize);
void platform_get_user_directory(utf8* outPath, const utf8* subDirectory, size_t outSize);
std::string platform_get_username();
bool platform_open_common_file_dialog(utf8* outFilename, file_dialog_desc* desc, size_t outSize);
utf8* platform_open_directory_browser(const utf8* title);
CurrencyType platform_get_locale_currency();
@@ -120,7 +115,6 @@ bool platform_process_is_elevated();
bool platform_get_steam_path(utf8* outPath, size_t outSize);
std::string platform_get_rct1_steam_dir();
std::string platform_get_rct2_steam_dir();
std::string platform_sanitise_filename(const std::string&);
#ifndef NO_TTF
bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size);

View File

@@ -8,21 +8,17 @@
*****************************************************************************/
#include <gtest/gtest.h>
#include <openrct2/platform/platform.h>
#include <openrct2/platform/Platform2.h>
TEST(platform, sanitise_filename)
{
ASSERT_EQ("normal-filename.png", Platform::SanitiseFilename("normal-filename.png"));
ASSERT_EQ("utf🎱", Platform::SanitiseFilename("utf🎱"));
ASSERT_EQ("forbidden_char", Platform::SanitiseFilename("forbidden/char"));
ASSERT_EQ("non trimmed", Platform::SanitiseFilename(" non trimmed "));
#ifndef _WIN32
ASSERT_EQ("normal-filename.png", platform_sanitise_filename("normal-filename.png"));
ASSERT_EQ("utf🎱", platform_sanitise_filename("utf🎱"));
ASSERT_EQ("forbidden_char", platform_sanitise_filename("forbidden/char"));
ASSERT_EQ("forbidden_\\:\"|?*chars", platform_sanitise_filename("forbidden/\\:\"|?*chars"));
ASSERT_EQ(" non trimmed ", platform_sanitise_filename(" non trimmed "));
ASSERT_EQ("forbidden_\\:\"|?*chars", Platform::SanitiseFilename("forbidden/\\:\"|?*chars"));
#else
ASSERT_EQ("normal-filename.png", platform_sanitise_filename("normal-filename.png"));
ASSERT_EQ("utf🎱", platform_sanitise_filename("utf🎱"));
ASSERT_EQ("forbidden_char", platform_sanitise_filename("forbidden/char"));
ASSERT_EQ("forbidden_______chars", platform_sanitise_filename("forbidden/\\:\"|?*chars"));
ASSERT_EQ("non trimmed", platform_sanitise_filename(" non trimmed "));
ASSERT_EQ("forbidden_______chars", Platform::SanitiseFilename("forbidden/\\:\"|?*chars"));
#endif
}