Skip to content

Commit

Permalink
Merge pull request #16380 from Gymnasiast/refactor/platform-to-new
Browse files Browse the repository at this point in the history
Convert a few platform functions to new framework
  • Loading branch information
Gymnasiast committed Jan 6, 2022
2 parents f9ca2ba + e4496c4 commit 2e9e549
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 144 deletions.
13 changes: 1 addition & 12 deletions src/openrct2/Context.cpp
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
}
12 changes: 6 additions & 6 deletions src/openrct2/config/Config.cpp
Expand Up @@ -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";
Expand Down Expand Up @@ -695,7 +695,7 @@ namespace Config

for (const utf8* location : searchLocations)
{
if (platform_original_game_data_exists(location))
if (Platform::OriginalGameDataExists(location))
{
return location;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/openrct2/drawing/Drawing.Sprite.cpp
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/interface/Screenshot.cpp
Expand Up @@ -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;
};
Expand Down
11 changes: 11 additions & 0 deletions src/openrct2/platform/Platform.Posix.cpp
Expand Up @@ -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
13 changes: 13 additions & 0 deletions src/openrct2/platform/Platform.Win32.cpp
Expand Up @@ -16,6 +16,7 @@
# include "../Version.h"

# include <datetimeapi.h>
# include <lmcons.h>
# include <memory>
# include <shlobj.h>
# undef GetEnvironmentVariable
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions src/openrct2/platform/Platform2.h
Expand Up @@ -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();
Expand Down
47 changes: 0 additions & 47 deletions src/openrct2/platform/Posix.cpp
Expand Up @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -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__
Expand Down
43 changes: 26 additions & 17 deletions src/openrct2/platform/Shared.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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()
{
Expand Down
35 changes: 0 additions & 35 deletions src/openrct2/platform/Windows.cpp
Expand Up @@ -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))
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 0 additions & 6 deletions src/openrct2/platform/macos.mm
Expand Up @@ -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 };
Expand Down
6 changes: 0 additions & 6 deletions src/openrct2/platform/platform.h
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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);
Expand Down

0 comments on commit 2e9e549

Please sign in to comment.