From 95717573f58d3db2c64fad178dd4942f1915044f Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Mon, 10 Jan 2022 12:38:26 +0100 Subject: [PATCH 01/13] Upgrade platform_get_locale_measurement_format() --- src/openrct2/config/Config.cpp | 2 +- src/openrct2/platform/Android.cpp | 5 ----- src/openrct2/platform/Linux.cpp | 22 ---------------------- src/openrct2/platform/Platform.Android.cpp | 5 +++++ src/openrct2/platform/Platform.Linux.cpp | 21 +++++++++++++++++++++ src/openrct2/platform/Platform.Win32.cpp | 21 +++++++++++++++++++++ src/openrct2/platform/Platform.macOS.mm | 15 +++++++++++++++ src/openrct2/platform/Platform2.h | 1 + src/openrct2/platform/Windows.cpp | 21 --------------------- src/openrct2/platform/macos.mm | 15 --------------- src/openrct2/platform/platform.h | 1 - 11 files changed, 64 insertions(+), 65 deletions(-) diff --git a/src/openrct2/config/Config.cpp b/src/openrct2/config/Config.cpp index d8a4dfd097..2c478d476a 100644 --- a/src/openrct2/config/Config.cpp +++ b/src/openrct2/config/Config.cpp @@ -166,7 +166,7 @@ namespace Config model->landscape_smoothing = reader->GetBoolean("landscape_smoothing", true); model->language = reader->GetEnum("language", Platform::GetLocaleLanguage(), Enum_LanguageEnum); model->measurement_format = reader->GetEnum( - "measurement_format", platform_get_locale_measurement_format(), Enum_MeasurementFormat); + "measurement_format", Platform::GetLocaleMeasurementFormat(), Enum_MeasurementFormat); model->play_intro = reader->GetBoolean("play_intro", false); model->save_plugin_data = reader->GetBoolean("save_plugin_data", true); model->debugging_tools = reader->GetBoolean("debugging_tools", false); diff --git a/src/openrct2/platform/Android.cpp b/src/openrct2/platform/Android.cpp index a3e93557d5..7c56f22f8e 100644 --- a/src/openrct2/platform/Android.cpp +++ b/src/openrct2/platform/Android.cpp @@ -26,11 +26,6 @@ bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size) } # endif -MeasurementFormat platform_get_locale_measurement_format() -{ - return MeasurementFormat::Metric; -} - float platform_get_default_scale() { JNIEnv* env = static_cast(SDL_AndroidGetJNIEnv()); diff --git a/src/openrct2/platform/Linux.cpp b/src/openrct2/platform/Linux.cpp index 12aa1a54a3..4bd1f0edff 100644 --- a/src/openrct2/platform/Linux.cpp +++ b/src/openrct2/platform/Linux.cpp @@ -32,30 +32,8 @@ # include "../util/Util.h" # include "platform.h" -# include -# include # include -MeasurementFormat platform_get_locale_measurement_format() -{ -// LC_MEASUREMENT is GNU specific. -# ifdef LC_MEASUREMENT - const char* langstring = setlocale(LC_MEASUREMENT, ""); -# else - const char* langstring = setlocale(LC_ALL, ""); -# endif - - if (langstring != nullptr) - { - // using https://en.wikipedia.org/wiki/Metrication#Chronology_and_status_of_conversion_by_country as reference - if (!fnmatch("*_US*", langstring, 0) || !fnmatch("*_MM*", langstring, 0) || !fnmatch("*_LR*", langstring, 0)) - { - return MeasurementFormat::Imperial; - } - } - return MeasurementFormat::Metric; -} - bool platform_get_steam_path(utf8* outPath, size_t outSize) { const char* steamRoot = getenv("STEAMROOT"); diff --git a/src/openrct2/platform/Platform.Android.cpp b/src/openrct2/platform/Platform.Android.cpp index e626828354..736c7bc2db 100644 --- a/src/openrct2/platform/Platform.Android.cpp +++ b/src/openrct2/platform/Platform.Android.cpp @@ -65,6 +65,11 @@ namespace Platform { return Platform::GetCurrencyValue(NULL); } + + MeasurementFormat GetLocaleMeasurementFormat() + { + return MeasurementFormat::Metric; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Linux.cpp b/src/openrct2/platform/Platform.Linux.cpp index ebdb622e31..e90b8c6e47 100644 --- a/src/openrct2/platform/Platform.Linux.cpp +++ b/src/openrct2/platform/Platform.Linux.cpp @@ -12,6 +12,7 @@ # include # include # include +# include # include # include # if defined(__FreeBSD__) || defined(__NetBSD__) @@ -250,6 +251,26 @@ namespace Platform return Platform::GetCurrencyValue(lc->int_curr_symbol); } + + MeasurementFormat GetLocaleMeasurementFormat() + { +// LC_MEASUREMENT is GNU specific. +# ifdef LC_MEASUREMENT + const char* langstring = setlocale(LC_MEASUREMENT, ""); +# else + const char* langstring = setlocale(LC_ALL, ""); +# endif + + if (langstring != nullptr) + { + // using https://en.wikipedia.org/wiki/Metrication#Chronology_and_status_of_conversion_by_country as reference + if (!fnmatch("*_US*", langstring, 0) || !fnmatch("*_MM*", langstring, 0) || !fnmatch("*_LR*", langstring, 0)) + { + return MeasurementFormat::Imperial; + } + } + return MeasurementFormat::Metric; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index 229273eedc..8180107354 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -707,6 +707,27 @@ namespace Platform return Platform::GetCurrencyValue(currCode); } + + MeasurementFormat GetLocaleMeasurementFormat() + { + UINT measurement_system; + if (GetLocaleInfo( + LOCALE_USER_DEFAULT, LOCALE_IMEASURE | LOCALE_RETURN_NUMBER, reinterpret_cast(&measurement_system), + sizeof(measurement_system)) + == 0) + { + return MeasurementFormat::Metric; + } + + switch (measurement_system) + { + case 1: + return MeasurementFormat::Imperial; + case 0: + default: + return MeasurementFormat::Metric; + } + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.macOS.mm b/src/openrct2/platform/Platform.macOS.mm index dbd8060208..5ff2962b33 100644 --- a/src/openrct2/platform/Platform.macOS.mm +++ b/src/openrct2/platform/Platform.macOS.mm @@ -216,6 +216,21 @@ namespace Platform return Platform::GetCurrencyValue(currencyCode.UTF8String); } } + + MeasurementFormat GetLocaleMeasurementFormat() + { + @autoreleasepool + { + NSNumber* metricSystem = [[NSLocale currentLocale] objectForKey:NSLocaleUsesMetricSystem]; + + if (metricSystem.boolValue) + { + return MeasurementFormat::Metric; + } + + return MeasurementFormat::Imperial; + } + } } #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index 989ad727b4..f19428c5c9 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -45,6 +45,7 @@ namespace Platform uint16_t GetLocaleLanguage(); CurrencyType GetLocaleCurrency(); CurrencyType GetCurrencyValue(const char* currCode); + MeasurementFormat GetLocaleMeasurementFormat(); rct2_time GetTimeLocal(); rct2_date GetDateLocal(); diff --git a/src/openrct2/platform/Windows.cpp b/src/openrct2/platform/Windows.cpp index 1aad3ef08b..ba622ffa27 100644 --- a/src/openrct2/platform/Windows.cpp +++ b/src/openrct2/platform/Windows.cpp @@ -176,27 +176,6 @@ time_t platform_file_get_modified_time(const utf8* path) return 0; } -MeasurementFormat platform_get_locale_measurement_format() -{ - UINT measurement_system; - if (GetLocaleInfo( - LOCALE_USER_DEFAULT, LOCALE_IMEASURE | LOCALE_RETURN_NUMBER, reinterpret_cast(&measurement_system), - sizeof(measurement_system)) - == 0) - { - return MeasurementFormat::Metric; - } - - switch (measurement_system) - { - case 1: - return MeasurementFormat::Imperial; - case 0: - default: - return MeasurementFormat::Metric; - } -} - TemperatureUnit platform_get_locale_temperature_format() { UINT fahrenheit; diff --git a/src/openrct2/platform/macos.mm b/src/openrct2/platform/macos.mm index 09918ab389..2ea7864562 100644 --- a/src/openrct2/platform/macos.mm +++ b/src/openrct2/platform/macos.mm @@ -46,21 +46,6 @@ bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size) } # endif // NO_TTF -MeasurementFormat platform_get_locale_measurement_format() -{ - @autoreleasepool - { - NSNumber* metricSystem = [[NSLocale currentLocale] objectForKey:NSLocaleUsesMetricSystem]; - - if (metricSystem.boolValue) - { - return MeasurementFormat::Metric; - } - - return MeasurementFormat::Imperial; - } -} - bool platform_get_steam_path(utf8* outPath, size_t outSize) { char steamPath[1024] = { 0 }; diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index d5f5ca2686..e5bcaf2b59 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -100,7 +100,6 @@ void platform_sleep(uint32_t ms); void platform_get_user_directory(utf8* outPath, const utf8* subDirectory, size_t outSize); bool platform_open_common_file_dialog(utf8* outFilename, file_dialog_desc* desc, size_t outSize); utf8* platform_open_directory_browser(const utf8* title); -MeasurementFormat platform_get_locale_measurement_format(); TemperatureUnit platform_get_locale_temperature_format(); uint8_t platform_get_locale_date_format(); bool platform_process_is_elevated(); From 90a74df3bcadf9cb1c6b68853eaaa38b25091c1f Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Mon, 10 Jan 2022 12:45:36 +0100 Subject: [PATCH 02/13] Upgrade platform_get_locale_date_format() --- src/openrct2/config/Config.cpp | 2 +- src/openrct2/platform/Platform.Posix.cpp | 25 ++++++++++- src/openrct2/platform/Platform.Win32.cpp | 53 ++++++++++++++++++++++++ src/openrct2/platform/Platform2.h | 1 + src/openrct2/platform/Posix.cpp | 20 --------- src/openrct2/platform/Windows.cpp | 50 ---------------------- src/openrct2/platform/platform.h | 1 - 7 files changed, 78 insertions(+), 74 deletions(-) diff --git a/src/openrct2/config/Config.cpp b/src/openrct2/config/Config.cpp index 2c478d476a..369bd3bdbf 100644 --- a/src/openrct2/config/Config.cpp +++ b/src/openrct2/config/Config.cpp @@ -183,7 +183,7 @@ namespace Config model->use_vsync = reader->GetBoolean("use_vsync", true); model->virtual_floor_style = reader->GetEnum( "virtual_floor_style", VirtualFloorStyles::Glassy, Enum_VirtualFloorStyle); - model->date_format = reader->GetEnum("date_format", platform_get_locale_date_format(), Enum_DateFormat); + model->date_format = reader->GetEnum("date_format", Platform::GetLocaleDateFormat(), Enum_DateFormat); model->auto_staff_placement = reader->GetBoolean("auto_staff", true); model->handymen_mow_default = reader->GetBoolean("handymen_mow_default", false); model->default_inspection_interval = reader->GetInt32("default_inspection_interval", 2); diff --git a/src/openrct2/platform/Platform.Posix.cpp b/src/openrct2/platform/Platform.Posix.cpp index 7bb2bd09cf..4918a697e1 100644 --- a/src/openrct2/platform/Platform.Posix.cpp +++ b/src/openrct2/platform/Platform.Posix.cpp @@ -9,12 +9,12 @@ #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__) -# include "platform.h" - # include "../core/Memory.hpp" # include "../core/Path.hpp" # include "../core/String.hpp" +# include "../localisation/Date.h" # include "Platform2.h" +# include "platform.h" # include # include @@ -22,6 +22,7 @@ # include # include # include +# include # include # include @@ -267,6 +268,26 @@ namespace Platform } return result; } + + uint8_t GetLocaleDateFormat() + { + const std::time_base::dateorder dateorder = std::use_facet>(std::locale()).date_order(); + + switch (dateorder) + { + case std::time_base::mdy: + return DATE_FORMAT_MONTH_DAY_YEAR; + + case std::time_base::ymd: + return DATE_FORMAT_YEAR_MONTH_DAY; + + case std::time_base::ydm: + return DATE_FORMAT_YEAR_DAY_MONTH; + + default: + return DATE_FORMAT_DAY_MONTH_YEAR; + } + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index 8180107354..2ce55ff20c 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -34,11 +34,13 @@ # include "../common.h" # include "../core/Path.hpp" # include "../core/String.hpp" +# include "../localisation/Date.h" # include "../localisation/Language.h" # include "Platform2.h" # include "platform.h" # include +# include # if _WIN32_WINNT < 0x600 # define swprintf_s(a, b, c, d, ...) swprintf(a, b, c, ##__VA_ARGS__) @@ -728,6 +730,57 @@ namespace Platform return MeasurementFormat::Metric; } } + + uint8_t GetLocaleDateFormat() + { +# if _WIN32_WINNT >= 0x0600 + // Retrieve short date format, eg "MM/dd/yyyy" + wchar_t dateFormat[20]; + if (GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SSHORTDATE, dateFormat, static_cast(std::size(dateFormat))) + == 0) + { + return DATE_FORMAT_DAY_MONTH_YEAR; + } + + // The only valid characters for format types are: dgyM + // We try to find 3 strings of format types, ignore any characters in between. + // We also ignore 'g', as it represents 'era' and we don't have that concept + // in our date formats. + // https://msdn.microsoft.com/en-us/library/windows/desktop/dd317787(v=vs.85).aspx + // + wchar_t first[sizeof(dateFormat)]; + wchar_t second[sizeof(dateFormat)]; + if (swscanf_s( + dateFormat, L"%l[dyM]%*l[^dyM]%l[dyM]%*l[^dyM]%*l[dyM]", first, static_cast(std::size(first)), second, + static_cast(std::size(second))) + != 2) + { + return DATE_FORMAT_DAY_MONTH_YEAR; + } + + if (wcsncmp(L"d", first, 1) == 0) + { + return DATE_FORMAT_DAY_MONTH_YEAR; + } + if (wcsncmp(L"M", first, 1) == 0) + { + return DATE_FORMAT_MONTH_DAY_YEAR; + } + if (wcsncmp(L"y", first, 1) == 0) + { + if (wcsncmp(L"d", second, 1) == 0) + { + return DATE_FORMAT_YEAR_DAY_MONTH; + } + + // Closest possible option + return DATE_FORMAT_YEAR_MONTH_DAY; + } +# endif + + // Default fallback + return DATE_FORMAT_DAY_MONTH_YEAR; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index f19428c5c9..0ee285af04 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -46,6 +46,7 @@ namespace Platform CurrencyType GetLocaleCurrency(); CurrencyType GetCurrencyValue(const char* currCode); MeasurementFormat GetLocaleMeasurementFormat(); + uint8_t GetLocaleDateFormat(); rct2_time GetTimeLocal(); rct2_date GetDateLocal(); diff --git a/src/openrct2/platform/Posix.cpp b/src/openrct2/platform/Posix.cpp index c5e5c6207a..e778a60693 100644 --- a/src/openrct2/platform/Posix.cpp +++ b/src/openrct2/platform/Posix.cpp @@ -194,26 +194,6 @@ TemperatureUnit platform_get_locale_temperature_format() return TemperatureUnit::Celsius; } -uint8_t platform_get_locale_date_format() -{ - const std::time_base::dateorder dateorder = std::use_facet>(std::locale()).date_order(); - - switch (dateorder) - { - case std::time_base::mdy: - return DATE_FORMAT_MONTH_DAY_YEAR; - - case std::time_base::ymd: - return DATE_FORMAT_YEAR_MONTH_DAY; - - case std::time_base::ydm: - return DATE_FORMAT_YEAR_DAY_MONTH; - - default: - return DATE_FORMAT_DAY_MONTH_YEAR; - } -} - datetime64 platform_get_datetime_now_utc() { const datetime64 epochAsTicks = 621355968000000000; diff --git a/src/openrct2/platform/Windows.cpp b/src/openrct2/platform/Windows.cpp index ba622ffa27..fc41d51756 100644 --- a/src/openrct2/platform/Windows.cpp +++ b/src/openrct2/platform/Windows.cpp @@ -197,56 +197,6 @@ TemperatureUnit platform_get_locale_temperature_format() return TemperatureUnit::Celsius; } -uint8_t platform_get_locale_date_format() -{ -# if _WIN32_WINNT >= 0x0600 - // Retrieve short date format, eg "MM/dd/yyyy" - wchar_t dateFormat[20]; - if (GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SSHORTDATE, dateFormat, static_cast(std::size(dateFormat))) == 0) - { - return DATE_FORMAT_DAY_MONTH_YEAR; - } - - // The only valid characters for format types are: dgyM - // We try to find 3 strings of format types, ignore any characters in between. - // We also ignore 'g', as it represents 'era' and we don't have that concept - // in our date formats. - // https://msdn.microsoft.com/en-us/library/windows/desktop/dd317787(v=vs.85).aspx - // - wchar_t first[sizeof(dateFormat)]; - wchar_t second[sizeof(dateFormat)]; - if (swscanf_s( - dateFormat, L"%l[dyM]%*l[^dyM]%l[dyM]%*l[^dyM]%*l[dyM]", first, static_cast(std::size(first)), second, - static_cast(std::size(second))) - != 2) - { - return DATE_FORMAT_DAY_MONTH_YEAR; - } - - if (wcsncmp(L"d", first, 1) == 0) - { - return DATE_FORMAT_DAY_MONTH_YEAR; - } - if (wcsncmp(L"M", first, 1) == 0) - { - return DATE_FORMAT_MONTH_DAY_YEAR; - } - if (wcsncmp(L"y", first, 1) == 0) - { - if (wcsncmp(L"d", second, 1) == 0) - { - return DATE_FORMAT_YEAR_DAY_MONTH; - } - - // Closest possible option - return DATE_FORMAT_YEAR_MONTH_DAY; - } -# endif - - // Default fallback - return DATE_FORMAT_DAY_MONTH_YEAR; -} - # ifndef NO_TTF bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size) { diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index e5bcaf2b59..10d8157ee6 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -101,7 +101,6 @@ void platform_get_user_directory(utf8* outPath, const utf8* subDirectory, size_t bool platform_open_common_file_dialog(utf8* outFilename, file_dialog_desc* desc, size_t outSize); utf8* platform_open_directory_browser(const utf8* title); TemperatureUnit platform_get_locale_temperature_format(); -uint8_t platform_get_locale_date_format(); bool platform_process_is_elevated(); bool platform_get_steam_path(utf8* outPath, size_t outSize); std::string platform_get_rct1_steam_dir(); From 973c5999b0f979112ae62112f888de76d9e0da61 Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Mon, 10 Jan 2022 12:49:53 +0100 Subject: [PATCH 03/13] Upgrade platform_get_locale_temperature_format() --- src/openrct2/config/Config.cpp | 2 +- src/openrct2/platform/Platform.Posix.cpp | 21 +++++++++++++++++++++ src/openrct2/platform/Platform.Win32.cpp | 21 +++++++++++++++++++++ src/openrct2/platform/Platform2.h | 1 + src/openrct2/platform/Posix.cpp | 18 ------------------ src/openrct2/platform/Windows.cpp | 21 --------------------- src/openrct2/platform/platform.h | 1 - 7 files changed, 44 insertions(+), 41 deletions(-) diff --git a/src/openrct2/config/Config.cpp b/src/openrct2/config/Config.cpp index 369bd3bdbf..ee9e1a7071 100644 --- a/src/openrct2/config/Config.cpp +++ b/src/openrct2/config/Config.cpp @@ -172,7 +172,7 @@ namespace Config model->debugging_tools = reader->GetBoolean("debugging_tools", false); model->show_height_as_units = reader->GetBoolean("show_height_as_units", false); model->temperature_format = reader->GetEnum( - "temperature_format", platform_get_locale_temperature_format(), Enum_Temperature); + "temperature_format", Platform::GetLocaleTemperatureFormat(), Enum_Temperature); model->window_height = reader->GetInt32("window_height", -1); model->window_snap_proximity = reader->GetInt32("window_snap_proximity", 5); model->window_width = reader->GetInt32("window_width", -1); diff --git a/src/openrct2/platform/Platform.Posix.cpp b/src/openrct2/platform/Platform.Posix.cpp index 4918a697e1..f17338a690 100644 --- a/src/openrct2/platform/Platform.Posix.cpp +++ b/src/openrct2/platform/Platform.Posix.cpp @@ -22,6 +22,7 @@ # include # include # include +# include # include # include # include @@ -288,6 +289,26 @@ namespace Platform return DATE_FORMAT_DAY_MONTH_YEAR; } } + + TemperatureUnit GetLocaleTemperatureFormat() + { +// LC_MEASUREMENT is GNU specific. +# ifdef LC_MEASUREMENT + const char* langstring = setlocale(LC_MEASUREMENT, ""); +# else + const char* langstring = setlocale(LC_ALL, ""); +# endif + + if (langstring != nullptr) + { + if (!fnmatch("*_US*", langstring, 0) || !fnmatch("*_BS*", langstring, 0) || !fnmatch("*_BZ*", langstring, 0) + || !fnmatch("*_PW*", langstring, 0)) + { + return TemperatureUnit::Fahrenheit; + } + } + return TemperatureUnit::Celsius; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index 2ce55ff20c..18846b8aa8 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -781,6 +781,27 @@ namespace Platform // Default fallback return DATE_FORMAT_DAY_MONTH_YEAR; } + + TemperatureUnit GetLocaleTemperatureFormat() + { + UINT fahrenheit; + + // GetLocaleInfo will set fahrenheit to 1 if the locale on this computer + // uses the United States measurement system or 0 otherwise. + if (GetLocaleInfo( + LOCALE_USER_DEFAULT, LOCALE_IMEASURE | LOCALE_RETURN_NUMBER, reinterpret_cast(&fahrenheit), + sizeof(fahrenheit)) + == 0) + { + // Assume celsius by default if function call fails + return TemperatureUnit::Celsius; + } + + if (fahrenheit) + return TemperatureUnit::Fahrenheit; + + return TemperatureUnit::Celsius; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index 0ee285af04..b799d80fd7 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -47,6 +47,7 @@ namespace Platform CurrencyType GetCurrencyValue(const char* currCode); MeasurementFormat GetLocaleMeasurementFormat(); uint8_t GetLocaleDateFormat(); + TemperatureUnit GetLocaleTemperatureFormat(); rct2_time GetTimeLocal(); rct2_date GetDateLocal(); diff --git a/src/openrct2/platform/Posix.cpp b/src/openrct2/platform/Posix.cpp index e778a60693..0c31ce1aac 100644 --- a/src/openrct2/platform/Posix.cpp +++ b/src/openrct2/platform/Posix.cpp @@ -174,25 +174,7 @@ time_t platform_file_get_modified_time(const utf8* path) return 100; } -TemperatureUnit platform_get_locale_temperature_format() -{ -// LC_MEASUREMENT is GNU specific. -# ifdef LC_MEASUREMENT - const char* langstring = setlocale(LC_MEASUREMENT, ""); -# else - const char* langstring = setlocale(LC_ALL, ""); -# endif - if (langstring != nullptr) - { - if (!fnmatch("*_US*", langstring, 0) || !fnmatch("*_BS*", langstring, 0) || !fnmatch("*_BZ*", langstring, 0) - || !fnmatch("*_PW*", langstring, 0)) - { - return TemperatureUnit::Fahrenheit; - } - } - return TemperatureUnit::Celsius; -} datetime64 platform_get_datetime_now_utc() { diff --git a/src/openrct2/platform/Windows.cpp b/src/openrct2/platform/Windows.cpp index fc41d51756..ecc291cb16 100644 --- a/src/openrct2/platform/Windows.cpp +++ b/src/openrct2/platform/Windows.cpp @@ -176,27 +176,6 @@ time_t platform_file_get_modified_time(const utf8* path) return 0; } -TemperatureUnit platform_get_locale_temperature_format() -{ - UINT fahrenheit; - - // GetLocaleInfo will set fahrenheit to 1 if the locale on this computer - // uses the United States measurement system or 0 otherwise. - if (GetLocaleInfo( - LOCALE_USER_DEFAULT, LOCALE_IMEASURE | LOCALE_RETURN_NUMBER, reinterpret_cast(&fahrenheit), - sizeof(fahrenheit)) - == 0) - { - // Assume celsius by default if function call fails - return TemperatureUnit::Celsius; - } - - if (fahrenheit) - return TemperatureUnit::Fahrenheit; - - return TemperatureUnit::Celsius; -} - # ifndef NO_TTF bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size) { diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index 10d8157ee6..49a422b092 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -100,7 +100,6 @@ void platform_sleep(uint32_t ms); void platform_get_user_directory(utf8* outPath, const utf8* subDirectory, size_t outSize); bool platform_open_common_file_dialog(utf8* outFilename, file_dialog_desc* desc, size_t outSize); utf8* platform_open_directory_browser(const utf8* title); -TemperatureUnit platform_get_locale_temperature_format(); bool platform_process_is_elevated(); bool platform_get_steam_path(utf8* outPath, size_t outSize); std::string platform_get_rct1_steam_dir(); From 8d6cb974bfe0111c0896a46006f819dec0da077e Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Mon, 10 Jan 2022 12:52:33 +0100 Subject: [PATCH 04/13] Move platform_directory_delete() to Path --- src/openrct2/core/Path.cpp | 5 +++++ src/openrct2/core/Path.hpp | 1 + src/openrct2/platform/Posix.cpp | 5 ----- src/openrct2/platform/Windows.cpp | 20 -------------------- src/openrct2/platform/platform.h | 1 - src/openrct2/title/TitleSequenceManager.cpp | 2 +- 6 files changed, 7 insertions(+), 27 deletions(-) diff --git a/src/openrct2/core/Path.cpp b/src/openrct2/core/Path.cpp index 97e293ff1c..0a71bf9b05 100644 --- a/src/openrct2/core/Path.cpp +++ b/src/openrct2/core/Path.cpp @@ -104,4 +104,9 @@ namespace Path { return Platform::ResolveCasing(path, File::Exists(path)); } + + bool DeleteDirectory(std::string_view path) + { + return fs::remove_all(u8path(path)) > 0; + } } // namespace Path diff --git a/src/openrct2/core/Path.hpp b/src/openrct2/core/Path.hpp index b3de9dd13e..5c3be0715e 100644 --- a/src/openrct2/core/Path.hpp +++ b/src/openrct2/core/Path.hpp @@ -26,6 +26,7 @@ namespace Path std::string GetDirectory(std::string_view path); void CreateDirectory(std::string_view path); bool DirectoryExists(std::string_view path); + bool DeleteDirectory(std::string_view path); std::string GetFileName(std::string_view origPath); std::string GetFileNameWithoutExtension(std::string_view path); std::string GetExtension(std::string_view path); diff --git a/src/openrct2/platform/Posix.cpp b/src/openrct2/platform/Posix.cpp index 0c31ce1aac..66e5d18703 100644 --- a/src/openrct2/platform/Posix.cpp +++ b/src/openrct2/platform/Posix.cpp @@ -89,11 +89,6 @@ bool platform_ensure_directory_exists(const utf8* path) return true; } -bool platform_directory_delete(const utf8* path) -{ - return fs::remove_all(u8path(path)) > 0; -} - std::string platform_get_absolute_path(const utf8* relative_path, const utf8* base_path) { std::string result; diff --git a/src/openrct2/platform/Windows.cpp b/src/openrct2/platform/Windows.cpp index ecc291cb16..150139d1b9 100644 --- a/src/openrct2/platform/Windows.cpp +++ b/src/openrct2/platform/Windows.cpp @@ -70,26 +70,6 @@ bool platform_ensure_directory_exists(const utf8* path) return success != FALSE; } -bool platform_directory_delete(const utf8* path) -{ - // Needs to be double-null terminated as pFrom is a null terminated array of strings - auto wPath = String::ToWideChar(path) + L"\0"; - - SHFILEOPSTRUCTW fileop; - fileop.hwnd = nullptr; // no status display - fileop.wFunc = FO_DELETE; // delete operation - fileop.pFrom = wPath.c_str(); // source file name as double null terminated string - fileop.pTo = nullptr; // no destination needed - fileop.fFlags = FOF_NOCONFIRMATION | FOF_SILENT; // do not prompt the user - - fileop.fAnyOperationsAborted = FALSE; - fileop.lpszProgressTitle = nullptr; - fileop.hNameMappings = nullptr; - - int32_t ret = SHFileOperationW(&fileop); - return (ret == 0); -} - bool platform_lock_single_instance() { HANDLE mutex, status; diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index 49a422b092..bdf34c1a32 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -89,7 +89,6 @@ void platform_refresh_video(bool recreate_window); // Platform specific definitions 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); std::string platform_get_absolute_path(const utf8* relative_path, const utf8* base_path); bool platform_lock_single_instance(); diff --git a/src/openrct2/title/TitleSequenceManager.cpp b/src/openrct2/title/TitleSequenceManager.cpp index 62f863c62f..982f3f5608 100644 --- a/src/openrct2/title/TitleSequenceManager.cpp +++ b/src/openrct2/title/TitleSequenceManager.cpp @@ -90,7 +90,7 @@ namespace TitleSequenceManager } else { - platform_directory_delete(path); + Path::DeleteDirectory(path); } _items.erase(_items.begin() + i); } From feeadb3c988cf446707edff842319b52d606fa36 Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Mon, 10 Jan 2022 13:02:11 +0100 Subject: [PATCH 05/13] Upgrade platform_process_is_elevated() --- src/openrct2/Context.cpp | 2 +- src/openrct2/platform/Platform.Posix.cpp | 9 +++++++++ src/openrct2/platform/Platform.Win32.cpp | 20 ++++++++++++++++++++ src/openrct2/platform/Platform2.h | 1 + src/openrct2/platform/Posix.cpp | 9 --------- src/openrct2/platform/Windows.cpp | 20 -------------------- src/openrct2/platform/platform.h | 1 - 7 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index 4939bf1081..3c8822a3bd 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -430,7 +430,7 @@ namespace OpenRCT2 } #endif - if (platform_process_is_elevated()) + if (Platform::ProcessIsElevated()) { std::string elevationWarning = _localisationService->GetString(STR_ADMIN_NOT_RECOMMENDED); if (gOpenRCT2Headless) diff --git a/src/openrct2/platform/Platform.Posix.cpp b/src/openrct2/platform/Platform.Posix.cpp index f17338a690..7133daf511 100644 --- a/src/openrct2/platform/Platform.Posix.cpp +++ b/src/openrct2/platform/Platform.Posix.cpp @@ -309,6 +309,15 @@ namespace Platform } return TemperatureUnit::Celsius; } + + bool ProcessIsElevated() + { +# ifndef __EMSCRIPTEN__ + return (geteuid() == 0); +# else + return false; +# endif // __EMSCRIPTEN__ + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index 18846b8aa8..d7c04f04b8 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -802,6 +802,26 @@ namespace Platform return TemperatureUnit::Celsius; } + + bool ProcessIsElevated() + { + BOOL isElevated = FALSE; + HANDLE hToken = nullptr; + if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) + { + TOKEN_ELEVATION Elevation; + DWORD tokenSize = sizeof(TOKEN_ELEVATION); + if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &tokenSize)) + { + isElevated = Elevation.TokenIsElevated; + } + } + if (hToken) + { + CloseHandle(hToken); + } + return isElevated; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index b799d80fd7..a78fba368d 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -53,6 +53,7 @@ namespace Platform bool FindApp(std::string_view app, std::string* output); int32_t Execute(std::string_view command, std::string* output = nullptr); + bool ProcessIsElevated(); bool OriginalGameDataExists(std::string_view path); diff --git a/src/openrct2/platform/Posix.cpp b/src/openrct2/platform/Posix.cpp index 66e5d18703..0bfa9195c0 100644 --- a/src/openrct2/platform/Posix.cpp +++ b/src/openrct2/platform/Posix.cpp @@ -185,15 +185,6 @@ datetime64 platform_get_datetime_now_utc() return utcNow; } -bool platform_process_is_elevated() -{ -# ifndef __EMSCRIPTEN__ - return (geteuid() == 0); -# else - return false; -# endif // __EMSCRIPTEN__ -} - std::string platform_get_rct1_steam_dir() { return "app_285310" PATH_SEPARATOR "depot_285311"; diff --git a/src/openrct2/platform/Windows.cpp b/src/openrct2/platform/Windows.cpp index 150139d1b9..7fdbf8a754 100644 --- a/src/openrct2/platform/Windows.cpp +++ b/src/openrct2/platform/Windows.cpp @@ -226,26 +226,6 @@ datetime64 platform_get_datetime_now_utc() return utcNow; } -bool platform_process_is_elevated() -{ - BOOL isElevated = FALSE; - HANDLE hToken = nullptr; - if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) - { - TOKEN_ELEVATION Elevation; - DWORD tokenSize = sizeof(TOKEN_ELEVATION); - if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &tokenSize)) - { - isElevated = Elevation.TokenIsElevated; - } - } - if (hToken) - { - CloseHandle(hToken); - } - return isElevated; -} - /////////////////////////////////////////////////////////////////////////////// // URI protocol association setup /////////////////////////////////////////////////////////////////////////////// diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index bdf34c1a32..b711360e88 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -99,7 +99,6 @@ void platform_sleep(uint32_t ms); void platform_get_user_directory(utf8* outPath, const utf8* subDirectory, size_t outSize); bool platform_open_common_file_dialog(utf8* outFilename, file_dialog_desc* desc, size_t outSize); utf8* platform_open_directory_browser(const utf8* title); -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(); From eb2b0c153707889d5481de63d26924a0df5b7c97 Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Mon, 10 Jan 2022 13:10:56 +0100 Subject: [PATCH 06/13] Move platform_update_palette() to Drawing.cpp --- src/openrct2/Game.cpp | 6 ++-- src/openrct2/drawing/Drawing.cpp | 53 ++++++++++++++++++++++++++++-- src/openrct2/drawing/Drawing.h | 1 + src/openrct2/platform/Shared.cpp | 46 -------------------------- src/openrct2/platform/platform.h | 1 - src/openrct2/scenario/Scenario.cpp | 2 +- 6 files changed, 56 insertions(+), 53 deletions(-) diff --git a/src/openrct2/Game.cpp b/src/openrct2/Game.cpp index c2fdf8c540..caa7e6954c 100644 --- a/src/openrct2/Game.cpp +++ b/src/openrct2/Game.cpp @@ -167,7 +167,7 @@ void update_palette_effects() paletteOffset[(i * 4) + 1] = -((0xFF - g1->offset[(i * 3) + 1]) / 2) - 1; paletteOffset[(i * 4) + 2] = -((0xFF - g1->offset[(i * 3) + 2]) / 2) - 1; } - platform_update_palette(gGamePalette, PALETTE_OFFSET_DYNAMIC, PALETTE_LENGTH_DYNAMIC); + UpdatePalette(gGamePalette, PALETTE_OFFSET_DYNAMIC, PALETTE_LENGTH_DYNAMIC); } gClimateLightningFlash++; } @@ -286,10 +286,10 @@ void update_palette_effects() } } - platform_update_palette(gGamePalette, PALETTE_OFFSET_ANIMATED, PALETTE_LENGTH_ANIMATED); + UpdatePalette(gGamePalette, PALETTE_OFFSET_ANIMATED, PALETTE_LENGTH_ANIMATED); if (gClimateLightningFlash == 2) { - platform_update_palette(gGamePalette, PALETTE_OFFSET_DYNAMIC, PALETTE_LENGTH_DYNAMIC); + UpdatePalette(gGamePalette, PALETTE_OFFSET_DYNAMIC, PALETTE_LENGTH_DYNAMIC); gClimateLightningFlash = 0; } } diff --git a/src/openrct2/drawing/Drawing.cpp b/src/openrct2/drawing/Drawing.cpp index 93757015e1..5d5cf48c28 100644 --- a/src/openrct2/drawing/Drawing.cpp +++ b/src/openrct2/drawing/Drawing.cpp @@ -10,6 +10,7 @@ #include "Drawing.h" #include "../Context.h" +#include "../Game.h" #include "../OpenRCT2.h" #include "../common.h" #include "../core/Guard.hpp" @@ -17,8 +18,10 @@ #include "../platform/platform.h" #include "../sprites.h" #include "../util/Util.h" +#include "../world/Climate.h" #include "../world/Location.hpp" #include "../world/Water.h" +#include "LightFX.h" #include @@ -600,7 +603,7 @@ void gfx_transpose_palette(int32_t pal, uint8_t product) source_pointer += 3; dest_pointer += 4; } - platform_update_palette(gGamePalette, 10, 236); + UpdatePalette(gGamePalette, 10, 236); } } @@ -641,7 +644,7 @@ void load_palette() dst += 4; } } - platform_update_palette(gGamePalette, 10, 236); + UpdatePalette(gGamePalette, 10, 236); gfx_invalidate_screen(); } @@ -787,3 +790,49 @@ FilterPaletteID GetGlassPaletteId(colour_t c) { return GlassPaletteIds[c]; } + +void UpdatePalette(const uint8_t* colours, int32_t start_index, int32_t num_colours) +{ + colours += start_index * 4; + + for (int32_t i = start_index; i < num_colours + start_index; i++) + { + uint8_t r = colours[2]; + uint8_t g = colours[1]; + uint8_t b = colours[0]; + +#ifdef __ENABLE_LIGHTFX__ + if (lightfx_is_available()) + { + lightfx_apply_palette_filter(i, &r, &g, &b); + } + else +#endif + { + float night = gDayNightCycle; + if (night >= 0 && gClimateLightningFlash == 0) + { + r = lerp(r, soft_light(r, 8), night); + g = lerp(g, soft_light(g, 8), night); + b = lerp(b, soft_light(b, 128), night); + } + } + + gPalette[i].Red = r; + gPalette[i].Green = g; + gPalette[i].Blue = b; + gPalette[i].Alpha = 0; + colours += 4; + } + + // Fix #1749 and #6535: rainbow path, donut shop and pause button contain black spots that should be white. + gPalette[255].Alpha = 0; + gPalette[255].Red = 255; + gPalette[255].Green = 255; + gPalette[255].Blue = 255; + + if (!gOpenRCT2Headless) + { + drawing_engine_set_palette(gPalette); + } +} diff --git a/src/openrct2/drawing/Drawing.h b/src/openrct2/drawing/Drawing.h index eea7daa0c3..8a81b79a4d 100644 --- a/src/openrct2/drawing/Drawing.h +++ b/src/openrct2/drawing/Drawing.h @@ -599,5 +599,6 @@ extern void (*mask_fn)( std::optional GetPaletteG1Index(colour_t paletteId); std::optional GetPaletteMapForColour(colour_t paletteId); +void UpdatePalette(const uint8_t* colours, int32_t start_index, int32_t num_colours); #include "NewDrawing.h" diff --git a/src/openrct2/platform/Shared.cpp b/src/openrct2/platform/Shared.cpp index 513a965e91..dbe4637abd 100644 --- a/src/openrct2/platform/Shared.cpp +++ b/src/openrct2/platform/Shared.cpp @@ -141,52 +141,6 @@ namespace Platform GamePalette gPalette; -void platform_update_palette(const uint8_t* colours, int32_t start_index, int32_t num_colours) -{ - colours += start_index * 4; - - for (int32_t i = start_index; i < num_colours + start_index; i++) - { - uint8_t r = colours[2]; - uint8_t g = colours[1]; - uint8_t b = colours[0]; - -#ifdef __ENABLE_LIGHTFX__ - if (lightfx_is_available()) - { - lightfx_apply_palette_filter(i, &r, &g, &b); - } - else -#endif - { - float night = gDayNightCycle; - if (night >= 0 && gClimateLightningFlash == 0) - { - r = lerp(r, soft_light(r, 8), night); - g = lerp(g, soft_light(g, 8), night); - b = lerp(b, soft_light(b, 128), night); - } - } - - gPalette[i].Red = r; - gPalette[i].Green = g; - gPalette[i].Blue = b; - gPalette[i].Alpha = 0; - colours += 4; - } - - // Fix #1749 and #6535: rainbow path, donut shop and pause button contain black spots that should be white. - gPalette[255].Alpha = 0; - gPalette[255].Red = 255; - gPalette[255].Green = 255; - gPalette[255].Blue = 255; - - if (!gOpenRCT2Headless) - { - drawing_engine_set_palette(gPalette); - } -} - void platform_toggle_windowed_mode() { int32_t targetMode = gConfigGeneral.fullscreen_mode == 0 ? 2 : 0; diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index b711360e88..d8692737a6 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -82,7 +82,6 @@ struct file_dialog_desc }; // Platform shared definitions -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); diff --git a/src/openrct2/scenario/Scenario.cpp b/src/openrct2/scenario/Scenario.cpp index 50963f6af8..eb54d46dfb 100644 --- a/src/openrct2/scenario/Scenario.cpp +++ b/src/openrct2/scenario/Scenario.cpp @@ -389,7 +389,7 @@ static void scenario_update_daynight_cycle() // Only update palette if day / night cycle has changed if (gDayNightCycle != currentDayNightCycle) { - platform_update_palette(gGamePalette, 10, 236); + UpdatePalette(gGamePalette, 10, 236); } } From 310ad1e4000b2699a269bebe57284589ff41c79a Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Mon, 10 Jan 2022 13:28:23 +0100 Subject: [PATCH 07/13] Upgrade platform_get_steam_dir() --- src/openrct2/config/Config.cpp | 8 ++-- src/openrct2/platform/Android.cpp | 5 --- src/openrct2/platform/Linux.cpp | 46 ---------------------- src/openrct2/platform/Platform.Android.cpp | 5 +++ src/openrct2/platform/Platform.Linux.cpp | 39 ++++++++++++++++++ src/openrct2/platform/Platform.Win32.cpp | 30 ++++++++++++++ src/openrct2/platform/Platform.macOS.mm | 19 +++++++++ src/openrct2/platform/Platform2.h | 1 + src/openrct2/platform/Windows.cpp | 28 ------------- src/openrct2/platform/macos.mm | 18 --------- src/openrct2/platform/platform.h | 1 - 11 files changed, 98 insertions(+), 102 deletions(-) diff --git a/src/openrct2/config/Config.cpp b/src/openrct2/config/Config.cpp index ee9e1a7071..3c79ab6eef 100644 --- a/src/openrct2/config/Config.cpp +++ b/src/openrct2/config/Config.cpp @@ -652,8 +652,8 @@ namespace Config } } - utf8 steamPath[2048] = { 0 }; - if (platform_get_steam_path(steamPath, sizeof(steamPath))) + auto steamPath = Platform::GetSteamPath(); + if (!steamPath.empty()) { std::string location = Path::Combine(steamPath, platform_get_rct1_steam_dir()); if (RCT1DataPresentAtLocation(location.c_str())) @@ -701,8 +701,8 @@ namespace Config } } - utf8 steamPath[2048] = { 0 }; - if (platform_get_steam_path(steamPath, sizeof(steamPath))) + auto steamPath = Platform::GetSteamPath(); + if (!steamPath.empty()) { std::string location = Path::Combine(steamPath, platform_get_rct2_steam_dir()); if (Platform::OriginalGameDataExists(location)) diff --git a/src/openrct2/platform/Android.cpp b/src/openrct2/platform/Android.cpp index 7c56f22f8e..9380edd844 100644 --- a/src/openrct2/platform/Android.cpp +++ b/src/openrct2/platform/Android.cpp @@ -42,11 +42,6 @@ float platform_get_default_scale() return displayScale; } -bool platform_get_steam_path(utf8* outPath, size_t outSize) -{ - return false; -} - AndroidClassLoader::AndroidClassLoader() { log_info("Obtaining JNI class loader"); diff --git a/src/openrct2/platform/Linux.cpp b/src/openrct2/platform/Linux.cpp index 4bd1f0edff..3720d779cb 100644 --- a/src/openrct2/platform/Linux.cpp +++ b/src/openrct2/platform/Linux.cpp @@ -34,52 +34,6 @@ # include -bool platform_get_steam_path(utf8* outPath, size_t outSize) -{ - const char* steamRoot = getenv("STEAMROOT"); - if (steamRoot != nullptr) - { - safe_strcpy(outPath, steamRoot, outSize); - safe_strcat_path(outPath, "ubuntu12_32/steamapps/content", outSize); - return true; - } - - char steamPath[1024] = { 0 }; - const char* localSharePath = getenv("XDG_DATA_HOME"); - if (localSharePath != nullptr) - { - safe_strcpy(steamPath, localSharePath, sizeof(steamPath)); - safe_strcat_path(steamPath, "Steam/ubuntu12_32/steamapps/content", sizeof(steamPath)); - if (Path::DirectoryExists(steamPath)) - { - safe_strcpy(outPath, steamPath, outSize); - return true; - } - } - - const char* homeDir = getpwuid(getuid())->pw_dir; - if (homeDir != nullptr) - { - safe_strcpy(steamPath, homeDir, sizeof(steamPath)); - safe_strcat_path(steamPath, ".local/share/Steam/ubuntu12_32/steamapps/content", sizeof(steamPath)); - if (Path::DirectoryExists(steamPath)) - { - safe_strcpy(outPath, steamPath, outSize); - return true; - } - - std::fill_n(steamPath, sizeof(steamPath), 0x00); - safe_strcpy(steamPath, homeDir, sizeof(steamPath)); - safe_strcat_path(steamPath, ".steam/steam/ubuntu12_32/steamapps/content", sizeof(steamPath)); - if (Path::DirectoryExists(steamPath)) - { - safe_strcpy(outPath, steamPath, outSize); - return true; - } - } - return false; -} - # ifndef NO_TTF bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size) { diff --git a/src/openrct2/platform/Platform.Android.cpp b/src/openrct2/platform/Platform.Android.cpp index 736c7bc2db..8fa0a80f26 100644 --- a/src/openrct2/platform/Platform.Android.cpp +++ b/src/openrct2/platform/Platform.Android.cpp @@ -70,6 +70,11 @@ namespace Platform { return MeasurementFormat::Metric; } + + std::string GetSteamPath() + { + return ""; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Linux.cpp b/src/openrct2/platform/Platform.Linux.cpp index e90b8c6e47..d4ae5b7daa 100644 --- a/src/openrct2/platform/Platform.Linux.cpp +++ b/src/openrct2/platform/Platform.Linux.cpp @@ -271,6 +271,45 @@ namespace Platform } return MeasurementFormat::Metric; } + + std::string GetSteamPath() + { + const char* steamRoot = getenv("STEAMROOT"); + if (steamRoot != nullptr) + { + return Path::Combine(steamRoot, "ubuntu12_32/steamapps/content"); + } + + const char* localSharePath = getenv("XDG_DATA_HOME"); + if (localSharePath != nullptr) + { + auto steamPath = Path::Combine(localSharePath, "Steam/ubuntu12_32/steamapps/content"); + if (Path::DirectoryExists(steamPath)) + { + return steamPath; + } + } + + const char* homeDir = getpwuid(getuid())->pw_dir; + if (homeDir == nullptr) + { + return ""; + } + + auto steamPath = Path::Combine(homeDir, ".local/share/Steam/ubuntu12_32/steamapps/content"); + if (Path::DirectoryExists(steamPath)) + { + return steamPath; + } + + steamPath = Path::Combine(homeDir, ".steam/steam/ubuntu12_32/steamapps/content"); + if (Path::DirectoryExists(steamPath)) + { + return steamPath; + } + + return ""; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index d7c04f04b8..5959c71afd 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -822,6 +822,36 @@ namespace Platform } return isElevated; } + + std::string GetSteamPath() + { + wchar_t* wSteamPath; + HKEY hKey; + DWORD type, size; + LRESULT result; + + if (RegOpenKeyW(HKEY_CURRENT_USER, L"Software\\Valve\\Steam", &hKey) != ERROR_SUCCESS) + return ""; + + // Get the size of the path first + if (RegQueryValueExW(hKey, L"SteamPath", nullptr, &type, nullptr, &size) != ERROR_SUCCESS) + { + RegCloseKey(hKey); + return ""; + } + + std::string outPath = ""; + wSteamPath = reinterpret_cast(malloc(size)); + result = RegQueryValueExW(hKey, L"SteamPath", nullptr, &type, reinterpret_cast(wSteamPath), &size); + if (result == ERROR_SUCCESS) + { + auto utf8SteamPath = String::ToUtf8(wSteamPath); + outPath = Path::Combine(utf8SteamPath, "steamapps", "common"); + } + free(wSteamPath); + RegCloseKey(hKey); + return outPath; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.macOS.mm b/src/openrct2/platform/Platform.macOS.mm index 5ff2962b33..2b23da6135 100644 --- a/src/openrct2/platform/Platform.macOS.mm +++ b/src/openrct2/platform/Platform.macOS.mm @@ -21,6 +21,7 @@ # include # include +# include namespace Platform { @@ -231,6 +232,24 @@ namespace Platform return MeasurementFormat::Imperial; } } + + std::string GetSteamPath() + { + const char* homeDir = getpwuid(getuid())->pw_dir; + if (homeDir == nullptr) + { + return ""; + } + + auto steamPath = Path::Combine( + homeDir, "Library/Application Support/Steam/Steam.AppBundle/Steam/Contents/MacOS/steamapps"); + if (Path::DirectoryExists(steamPath)) + { + return steamPath; + } + + return ""; + } } #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index a78fba368d..b7d2c6e2e5 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -59,6 +59,7 @@ namespace Platform std::string GetUsername(); + std::string GetSteamPath(); #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__) std::string GetEnvironmentPath(const char* name); std::string GetHomePath(); diff --git a/src/openrct2/platform/Windows.cpp b/src/openrct2/platform/Windows.cpp index 7fdbf8a754..779952c5bf 100644 --- a/src/openrct2/platform/Windows.cpp +++ b/src/openrct2/platform/Windows.cpp @@ -96,35 +96,7 @@ int32_t platform_get_drives() return GetLogicalDrives(); } -bool platform_get_steam_path(utf8* outPath, size_t outSize) -{ - wchar_t* wSteamPath; - HKEY hKey; - DWORD type, size; - LRESULT result; - if (RegOpenKeyW(HKEY_CURRENT_USER, L"Software\\Valve\\Steam", &hKey) != ERROR_SUCCESS) - return false; - - // Get the size of the path first - if (RegQueryValueExW(hKey, L"SteamPath", nullptr, &type, nullptr, &size) != ERROR_SUCCESS) - { - RegCloseKey(hKey); - return false; - } - - wSteamPath = reinterpret_cast(malloc(size)); - result = RegQueryValueExW(hKey, L"SteamPath", nullptr, &type, reinterpret_cast(wSteamPath), &size); - if (result == ERROR_SUCCESS) - { - auto utf8SteamPath = String::ToUtf8(wSteamPath); - safe_strcpy(outPath, utf8SteamPath.c_str(), outSize); - safe_strcat_path(outPath, "steamapps\\common", outSize); - } - free(wSteamPath); - RegCloseKey(hKey); - return result == ERROR_SUCCESS; -} std::string platform_get_rct1_steam_dir() { diff --git a/src/openrct2/platform/macos.mm b/src/openrct2/platform/macos.mm index 2ea7864562..7a3e49e995 100644 --- a/src/openrct2/platform/macos.mm +++ b/src/openrct2/platform/macos.mm @@ -46,22 +46,4 @@ bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size) } # endif // NO_TTF -bool platform_get_steam_path(utf8* outPath, size_t outSize) -{ - char steamPath[1024] = { 0 }; - const char* homeDir = getpwuid(getuid())->pw_dir; - if (homeDir != NULL) - { - safe_strcpy(steamPath, homeDir, sizeof(steamPath)); - safe_strcat_path( - steamPath, "Library/Application Support/Steam/Steam.AppBundle/Steam/Contents/MacOS/steamapps", sizeof(steamPath)); - if (Path::DirectoryExists(steamPath)) - { - safe_strcpy(outPath, steamPath, outSize); - return true; - } - } - return false; -} - #endif diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index d8692737a6..1b0358e1c7 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -98,7 +98,6 @@ void platform_sleep(uint32_t ms); void platform_get_user_directory(utf8* outPath, const utf8* subDirectory, size_t outSize); bool platform_open_common_file_dialog(utf8* outFilename, file_dialog_desc* desc, size_t outSize); utf8* platform_open_directory_browser(const utf8* title); -bool platform_get_steam_path(utf8* outPath, size_t outSize); std::string platform_get_rct1_steam_dir(); std::string platform_get_rct2_steam_dir(); From f917fffe346560b6604c012b2e4d43c188ad8b97 Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Mon, 10 Jan 2022 13:41:12 +0100 Subject: [PATCH 08/13] Upgrade platform_get_font_path() --- src/openrct2/drawing/TTF.cpp | 9 ++-- src/openrct2/platform/Android.cpp | 8 --- src/openrct2/platform/Linux.cpp | 63 ---------------------- src/openrct2/platform/Platform.Android.cpp | 8 +++ src/openrct2/platform/Platform.Linux.cpp | 61 +++++++++++++++++++++ src/openrct2/platform/Platform.Win32.cpp | 20 +++++++ src/openrct2/platform/Platform.macOS.mm | 20 +++++++ src/openrct2/platform/Platform2.h | 3 ++ src/openrct2/platform/Windows.cpp | 31 ----------- src/openrct2/platform/macos.mm | 22 -------- src/openrct2/platform/platform.h | 4 -- 11 files changed, 117 insertions(+), 132 deletions(-) diff --git a/src/openrct2/drawing/TTF.cpp b/src/openrct2/drawing/TTF.cpp index 2c430f3672..005cb0746c 100644 --- a/src/openrct2/drawing/TTF.cpp +++ b/src/openrct2/drawing/TTF.cpp @@ -23,6 +23,7 @@ # include "../core/String.hpp" # include "../localisation/Localisation.h" # include "../localisation/LocalisationService.h" +# include "../platform/Platform2.h" # include "../platform/platform.h" # include "TTF.h" @@ -125,17 +126,17 @@ bool ttf_initialise() { TTFFontDescriptor* fontDesc = &(gCurrentTTFFontSet->size[i]); - utf8 fontPath[MAX_PATH]; - if (!platform_get_font_path(fontDesc, fontPath, sizeof(fontPath))) + auto fontPath = Platform::GetFontPath(*fontDesc); + if (fontPath.empty()) { log_verbose("Unable to load font '%s'", fontDesc->font_name); return false; } - fontDesc->font = ttf_open_font(fontPath, fontDesc->ptSize); + fontDesc->font = ttf_open_font(fontPath.c_str(), fontDesc->ptSize); if (fontDesc->font == nullptr) { - log_verbose("Unable to load '%s'", fontPath); + log_verbose("Unable to load '%s'", fontPath.c_str()); return false; } } diff --git a/src/openrct2/platform/Android.cpp b/src/openrct2/platform/Android.cpp index 9380edd844..979ab3dc81 100644 --- a/src/openrct2/platform/Android.cpp +++ b/src/openrct2/platform/Android.cpp @@ -18,14 +18,6 @@ # include # include -# ifndef NO_TTF -bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size) -{ - STUB(); - return false; -} -# endif - float platform_get_default_scale() { JNIEnv* env = static_cast(SDL_AndroidGetJNIEnv()); diff --git a/src/openrct2/platform/Linux.cpp b/src/openrct2/platform/Linux.cpp index 3720d779cb..001a14e325 100644 --- a/src/openrct2/platform/Linux.cpp +++ b/src/openrct2/platform/Linux.cpp @@ -34,67 +34,4 @@ # include -# ifndef NO_TTF -bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size) -{ - assert(buffer != nullptr); - assert(font != nullptr); - - log_verbose("Looking for font %s with FontConfig.", font->font_name); - FcConfig* config = FcInitLoadConfigAndFonts(); - if (!config) - { - log_error("Failed to initialize FontConfig library"); - FcFini(); - return false; - } - - FcPattern* pat = FcNameParse(reinterpret_cast(font->font_name)); - - FcConfigSubstitute(config, pat, FcMatchPattern); - FcDefaultSubstitute(pat); - - bool found = false; - FcResult result = FcResultNoMatch; - FcPattern* match = FcFontMatch(config, pat, &result); - - if (match) - { - bool is_substitute = false; - - // FontConfig implicitly falls back to any default font it is configured to handle. - // In our implementation, this cannot account for supported character sets, leading - // to unrendered characters (tofu) when trying to render e.g. CJK characters using a - // Western (sans-)serif font. We therefore ignore substitutions FontConfig provides, - // and instead rely on exact matches on the fonts predefined for each font family. - FcChar8* matched_font_face = nullptr; - if (FcPatternGetString(match, FC_FULLNAME, 0, &matched_font_face) == FcResultMatch - && strcmp(font->font_name, reinterpret_cast(matched_font_face)) != 0) - { - log_verbose("FontConfig provided substitute font %s -- disregarding.", matched_font_face); - is_substitute = true; - } - - FcChar8* filename = nullptr; - if (!is_substitute && FcPatternGetString(match, FC_FILE, 0, &filename) == FcResultMatch) - { - found = true; - safe_strcpy(buffer, reinterpret_cast(filename), size); - log_verbose("FontConfig provided font %s", filename); - } - - FcPatternDestroy(match); - } - else - { - log_warning("Failed to find required font."); - } - - FcPatternDestroy(pat); - FcConfigDestroy(config); - FcFini(); - return found; -} -# endif // NO_TTF - #endif diff --git a/src/openrct2/platform/Platform.Android.cpp b/src/openrct2/platform/Platform.Android.cpp index 8fa0a80f26..0062d89a29 100644 --- a/src/openrct2/platform/Platform.Android.cpp +++ b/src/openrct2/platform/Platform.Android.cpp @@ -75,6 +75,14 @@ namespace Platform { return ""; } + +# ifndef NO_TTF + std::string GetFontPath(const TTFFontDescriptor& font) + { + STUB(); + return ""; + } +# endif } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Linux.cpp b/src/openrct2/platform/Platform.Linux.cpp index d4ae5b7daa..4f69f37462 100644 --- a/src/openrct2/platform/Platform.Linux.cpp +++ b/src/openrct2/platform/Platform.Linux.cpp @@ -24,6 +24,10 @@ // for PATH_MAX # include # endif // __linux__ +# ifndef NO_TTF +# include +# endif // NO_TTF + # include "../OpenRCT2.h" # include "../core/Path.hpp" # include "../localisation/Language.h" @@ -310,6 +314,63 @@ namespace Platform return ""; } + + std::string GetFontPath(const TTFFontDescriptor& font) + { + log_verbose("Looking for font %s with FontConfig.", font.font_name); + FcConfig* config = FcInitLoadConfigAndFonts(); + if (!config) + { + log_error("Failed to initialize FontConfig library"); + FcFini(); + return ""; + } + + FcPattern* pat = FcNameParse(reinterpret_cast(font.font_name)); + + FcConfigSubstitute(config, pat, FcMatchPattern); + FcDefaultSubstitute(pat); + + std::string path = ""; + FcResult result = FcResultNoMatch; + FcPattern* match = FcFontMatch(config, pat, &result); + + if (match) + { + bool is_substitute = false; + + // FontConfig implicitly falls back to any default font it is configured to handle. + // In our implementation, this cannot account for supported character sets, leading + // to unrendered characters (tofu) when trying to render e.g. CJK characters using a + // Western (sans-)serif font. We therefore ignore substitutions FontConfig provides, + // and instead rely on exact matches on the fonts predefined for each font family. + FcChar8* matched_font_face = nullptr; + if (FcPatternGetString(match, FC_FULLNAME, 0, &matched_font_face) == FcResultMatch + && strcmp(font.font_name, reinterpret_cast(matched_font_face)) != 0) + { + log_verbose("FontConfig provided substitute font %s -- disregarding.", matched_font_face); + is_substitute = true; + } + + FcChar8* filename = nullptr; + if (!is_substitute && FcPatternGetString(match, FC_FILE, 0, &filename) == FcResultMatch) + { + path = reinterpret_cast(filename); + log_verbose("FontConfig provided font %s", filename); + } + + FcPatternDestroy(match); + } + else + { + log_warning("Failed to find required font."); + } + + FcPatternDestroy(pat); + FcConfigDestroy(config); + FcFini(); + return path; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index 5959c71afd..4cae076866 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -852,6 +852,26 @@ namespace Platform RegCloseKey(hKey); return outPath; } + + std::string GetFontPath(const TTFFontDescriptor& font) + { +# if !defined(__MINGW32__) && ((NTDDI_VERSION >= NTDDI_VISTA) && !defined(_USING_V110_SDK71_) && !defined(_ATL_XP_TARGETING)) + wchar_t* fontFolder; + if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Fonts, 0, nullptr, &fontFolder))) + { + // Convert wchar to utf8, then copy the font folder path to the buffer. + auto outPathTemp = String::ToUtf8(fontFolder); + CoTaskMemFree(fontFolder); + + return Path::Combine(outPathTemp, font.filename); + } + + return ""; +# else + log_warning("Compatibility hack: falling back to C:\\Windows\\Fonts"); + return Path::Combine("C:\\Windows\\Fonts\\", font.filename); +# endif + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.macOS.mm b/src/openrct2/platform/Platform.macOS.mm index 2b23da6135..ec31c5a99c 100644 --- a/src/openrct2/platform/Platform.macOS.mm +++ b/src/openrct2/platform/Platform.macOS.mm @@ -19,6 +19,7 @@ # undef interface # undef abstract +# include # include # include # include @@ -250,6 +251,25 @@ namespace Platform return ""; } + + std::string GetFontPath(const TTFFontDescriptor& font) + { + @autoreleasepool + { + CTFontDescriptorRef fontRef = CTFontDescriptorCreateWithNameAndSize( + static_cast([NSString stringWithUTF8String:font.font_name]), 0.0); + CFURLRef url = static_cast(CTFontDescriptorCopyAttribute(fontRef, kCTFontURLAttribute)); + if (url) + { + NSString* fontPath = [NSString stringWithString:[static_cast(CFBridgingRelease(url)) path]]; + return fontPath.UTF8String; + } + else + { + return ""; + } + } + } } #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index b7d2c6e2e5..f324da3af4 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -64,6 +64,9 @@ namespace Platform std::string GetEnvironmentPath(const char* name); std::string GetHomePath(); #endif +#ifndef NO_TTF + std::string GetFontPath(const TTFFontDescriptor& font); +#endif // NO_TTF std::string FormatShortDate(std::time_t timestamp); std::string FormatTime(std::time_t timestamp); diff --git a/src/openrct2/platform/Windows.cpp b/src/openrct2/platform/Windows.cpp index 779952c5bf..cd68f1312a 100644 --- a/src/openrct2/platform/Windows.cpp +++ b/src/openrct2/platform/Windows.cpp @@ -96,8 +96,6 @@ int32_t platform_get_drives() return GetLogicalDrives(); } - - std::string platform_get_rct1_steam_dir() { return "Rollercoaster Tycoon Deluxe"; @@ -128,35 +126,6 @@ time_t platform_file_get_modified_time(const utf8* path) return 0; } -# ifndef NO_TTF -bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size) -{ -# if !defined(__MINGW32__) \ - && ((NTDDI_VERSION >= NTDDI_VISTA) && !defined(_USING_V110_SDK71_) && !defined(_ATL_XP_TARGETING)) - wchar_t* fontFolder; - if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Fonts, 0, nullptr, &fontFolder))) - { - // Convert wchar to utf8, then copy the font folder path to the buffer. - auto outPathTemp = String::ToUtf8(fontFolder); - safe_strcpy(buffer, outPathTemp.c_str(), size); - - CoTaskMemFree(fontFolder); - - // Append the requested font's file name. - safe_strcat_path(buffer, font->filename, size); - return true; - } - - return false; -# else - log_warning("Compatibility hack: falling back to C:\\Windows\\Fonts"); - safe_strcpy(buffer, "C:\\Windows\\Fonts\\", size); - safe_strcat_path(buffer, font->filename, size); - return true; -# endif -} -# endif // NO_TTF - std::string platform_get_absolute_path(const utf8* relativePath, const utf8* basePath) { std::string result; diff --git a/src/openrct2/platform/macos.mm b/src/openrct2/platform/macos.mm index 7a3e49e995..fca8723d52 100644 --- a/src/openrct2/platform/macos.mm +++ b/src/openrct2/platform/macos.mm @@ -24,26 +24,4 @@ # include # include -# ifndef NO_TTF -bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size) -{ - @autoreleasepool - { - CTFontDescriptorRef fontRef = CTFontDescriptorCreateWithNameAndSize( - static_cast([NSString stringWithUTF8String:font->font_name]), 0.0); - CFURLRef url = static_cast(CTFontDescriptorCopyAttribute(fontRef, kCTFontURLAttribute)); - if (url) - { - NSString* fontPath = [NSString stringWithString:[static_cast(CFBridgingRelease(url)) path]]; - safe_strcpy(buffer, fontPath.UTF8String, size); - return true; - } - else - { - return false; - } - } -} -# endif // NO_TTF - #endif diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index 1b0358e1c7..e20392b576 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -101,10 +101,6 @@ utf8* platform_open_directory_browser(const utf8* title); std::string platform_get_rct1_steam_dir(); std::string platform_get_rct2_steam_dir(); -#ifndef NO_TTF -bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size); -#endif // NO_TTF - datetime64 platform_get_datetime_now_utc(); float platform_get_default_scale(); From 131022c37319be4b0862d1891c5745f082a072d9 Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Mon, 10 Jan 2022 13:44:22 +0100 Subject: [PATCH 09/13] Remove now-empty platform files --- src/openrct2/libopenrct2.vcxproj | 1 - src/openrct2/platform/Linux.cpp | 37 -------------------------------- src/openrct2/platform/macos.mm | 27 ----------------------- 3 files changed, 65 deletions(-) delete mode 100644 src/openrct2/platform/Linux.cpp delete mode 100644 src/openrct2/platform/macos.mm diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index cf21190006..349015db7c 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -785,7 +785,6 @@ - diff --git a/src/openrct2/platform/Linux.cpp b/src/openrct2/platform/Linux.cpp deleted file mode 100644 index 001a14e325..0000000000 --- a/src/openrct2/platform/Linux.cpp +++ /dev/null @@ -1,37 +0,0 @@ -/***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers - * - * For a complete list of all authors, please refer to contributors.md - * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 - * - * OpenRCT2 is licensed under the GNU General Public License version 3. - *****************************************************************************/ - -#include "../common.h" - -// Despite the name, this file contains support for more OSs besides Linux, provided the necessary ifdefs remain small. -// Otherwise, they should be spun off into their own files. -#if defined(__unix__) && !defined(__ANDROID__) && !defined(__APPLE__) - -# ifdef __FreeBSD__ -# include -# endif - -# include -# include -# include -# include -# ifndef NO_TTF -# include -# endif // NO_TTF -# include "../config/Config.h" -# include "../core/File.h" -# include "../core/Path.hpp" -# include "../localisation/Language.h" -# include "../localisation/StringIds.h" -# include "../util/Util.h" -# include "platform.h" - -# include - -#endif diff --git a/src/openrct2/platform/macos.mm b/src/openrct2/platform/macos.mm deleted file mode 100644 index fca8723d52..0000000000 --- a/src/openrct2/platform/macos.mm +++ /dev/null @@ -1,27 +0,0 @@ -/***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers - * - * For a complete list of all authors, please refer to contributors.md - * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 - * - * OpenRCT2 is licensed under the GNU General Public License version 3. - *****************************************************************************/ - -#if defined(__APPLE__) && defined(__MACH__) - -# include "../config/Config.h" -# include "../core/Path.hpp" -# include "../localisation/Language.h" -# include "../util/Util.h" -# include "platform.h" - -// undefine `interface` and `abstract`, because it's causing conflicts with Objective-C's keywords -# undef interface -# undef abstract - -# import -# import -# include -# include - -#endif From f5532687281f5627006c22f54a6c907754bf4ceb Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Mon, 10 Jan 2022 13:49:44 +0100 Subject: [PATCH 10/13] Fix formatting --- src/openrct2/platform/Platform.Posix.cpp | 3 ++- src/openrct2/platform/Posix.cpp | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/openrct2/platform/Platform.Posix.cpp b/src/openrct2/platform/Platform.Posix.cpp index 7133daf511..0fc76a2ab6 100644 --- a/src/openrct2/platform/Platform.Posix.cpp +++ b/src/openrct2/platform/Platform.Posix.cpp @@ -9,12 +9,13 @@ #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__) +# include "platform.h" + # include "../core/Memory.hpp" # include "../core/Path.hpp" # include "../core/String.hpp" # include "../localisation/Date.h" # include "Platform2.h" -# include "platform.h" # include # include diff --git a/src/openrct2/platform/Posix.cpp b/src/openrct2/platform/Posix.cpp index 0bfa9195c0..59af5224c2 100644 --- a/src/openrct2/platform/Posix.cpp +++ b/src/openrct2/platform/Posix.cpp @@ -169,8 +169,6 @@ time_t platform_file_get_modified_time(const utf8* path) return 100; } - - datetime64 platform_get_datetime_now_utc() { const datetime64 epochAsTicks = 621355968000000000; From b826c0706a70b0a65b88c5600a5c7bfa479c35db Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Tue, 11 Jan 2022 11:48:07 +0100 Subject: [PATCH 11/13] Upgrade platform_get_default_scale() --- src/openrct2/config/Config.cpp | 2 +- src/openrct2/platform/Android.cpp | 16 ---------------- src/openrct2/platform/Platform.Android.cpp | 16 ++++++++++++++++ src/openrct2/platform/Platform2.h | 1 + src/openrct2/platform/Shared.cpp | 14 +++++++------- src/openrct2/platform/platform.h | 2 -- 6 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/openrct2/config/Config.cpp b/src/openrct2/config/Config.cpp index 3c79ab6eef..6bd5c27888 100644 --- a/src/openrct2/config/Config.cpp +++ b/src/openrct2/config/Config.cpp @@ -202,7 +202,7 @@ namespace Config model->disable_lightning_effect = reader->GetBoolean("disable_lightning_effect", false); model->allow_loading_with_incorrect_checksum = reader->GetBoolean("allow_loading_with_incorrect_checksum", true); model->steam_overlay_pause = reader->GetBoolean("steam_overlay_pause", true); - model->window_scale = reader->GetFloat("window_scale", platform_get_default_scale()); + model->window_scale = reader->GetFloat("window_scale", Platform::GetDefaultScale()); model->scale_quality = reader->GetEnum( "scale_quality", ScaleQuality::SmoothNearestNeighbour, Enum_ScaleQuality); model->show_fps = reader->GetBoolean("show_fps", false); diff --git a/src/openrct2/platform/Android.cpp b/src/openrct2/platform/Android.cpp index 979ab3dc81..8f537c7852 100644 --- a/src/openrct2/platform/Android.cpp +++ b/src/openrct2/platform/Android.cpp @@ -18,22 +18,6 @@ # include # include -float platform_get_default_scale() -{ - JNIEnv* env = static_cast(SDL_AndroidGetJNIEnv()); - - jobject activity = static_cast(SDL_AndroidGetActivity()); - jclass activityClass = env->GetObjectClass(activity); - jmethodID getDefaultScale = env->GetMethodID(activityClass, "getDefaultScale", "()F"); - - jfloat displayScale = env->CallFloatMethod(activity, getDefaultScale); - - env->DeleteLocalRef(activity); - env->DeleteLocalRef(activityClass); - - return displayScale; -} - AndroidClassLoader::AndroidClassLoader() { log_info("Obtaining JNI class loader"); diff --git a/src/openrct2/platform/Platform.Android.cpp b/src/openrct2/platform/Platform.Android.cpp index 0062d89a29..1ac745f6b6 100644 --- a/src/openrct2/platform/Platform.Android.cpp +++ b/src/openrct2/platform/Platform.Android.cpp @@ -83,6 +83,22 @@ namespace Platform return ""; } # endif + + float GetDefaultScale() + { + JNIEnv* env = static_cast(SDL_AndroidGetJNIEnv()); + + jobject activity = static_cast(SDL_AndroidGetActivity()); + jclass activityClass = env->GetObjectClass(activity); + jmethodID getDefaultScale = env->GetMethodID(activityClass, "getDefaultScale", "()F"); + + jfloat displayScale = env->CallFloatMethod(activity, getDefaultScale); + + env->DeleteLocalRef(activity); + env->DeleteLocalRef(activityClass); + + return displayScale; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index f324da3af4..6689c5cb48 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -54,6 +54,7 @@ namespace Platform bool FindApp(std::string_view app, std::string* output); int32_t Execute(std::string_view command, std::string* output = nullptr); bool ProcessIsElevated(); + float GetDefaultScale(); bool OriginalGameDataExists(std::string_view path); diff --git a/src/openrct2/platform/Shared.cpp b/src/openrct2/platform/Shared.cpp index dbe4637abd..54c6258484 100644 --- a/src/openrct2/platform/Shared.cpp +++ b/src/openrct2/platform/Shared.cpp @@ -137,6 +137,13 @@ namespace Platform sanitised = String::Trim(sanitised); return sanitised; } + +#ifndef __ANDROID__ + float GetDefaultScale() + { + return 1; + } +#endif } // namespace Platform GamePalette gPalette; @@ -208,13 +215,6 @@ void platform_sleep(uint32_t ms) #endif } -#ifndef __ANDROID__ -float platform_get_default_scale() -{ - return 1; -} -#endif - void core_init() { static bool initialised = false; diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index e20392b576..dcacbc052d 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -103,8 +103,6 @@ std::string platform_get_rct2_steam_dir(); datetime64 platform_get_datetime_now_utc(); -float platform_get_default_scale(); - // Called very early in the program before parsing commandline arguments. void core_init(); From 0aa256a26a072b7506be73b8f7cbe39fb4e154d5 Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Tue, 11 Jan 2022 11:57:54 +0100 Subject: [PATCH 12/13] Move remaining stuff in Android.cpp --- src/openrct2/core/ZipAndroid.cpp | 3 +- src/openrct2/libopenrct2.vcxproj | 1 - src/openrct2/platform/Android.cpp | 77 ---------------------- src/openrct2/platform/Platform.Android.cpp | 61 +++++++++++++++++ src/openrct2/platform/Platform2.h | 16 +++++ src/openrct2/platform/Shared.cpp | 2 +- src/openrct2/platform/platform.h | 14 ---- 7 files changed, 80 insertions(+), 94 deletions(-) delete mode 100644 src/openrct2/platform/Android.cpp diff --git a/src/openrct2/core/ZipAndroid.cpp b/src/openrct2/core/ZipAndroid.cpp index c891b7023b..ae58016dad 100644 --- a/src/openrct2/core/ZipAndroid.cpp +++ b/src/openrct2/core/ZipAndroid.cpp @@ -9,6 +9,7 @@ #ifdef __ANDROID__ +# include "../platform/Platform2.h" # include "../platform/platform.h" # include "IStream.hpp" # include "MemoryStream.h" @@ -30,7 +31,7 @@ public: // retrieve the JNI environment. JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); - jclass jniClass = platform_android_find_class(env, "io/openrct2/ZipArchive"); + jclass jniClass = Platform::AndroidFindClass(env, "io/openrct2/ZipArchive"); jmethodID constructor = env->GetMethodID(jniClass, "", "(Ljava/lang/String;)V"); jstring jniPath = env->NewStringUTF(path.data()); diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 349015db7c..9888a01518 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -783,7 +783,6 @@ - diff --git a/src/openrct2/platform/Android.cpp b/src/openrct2/platform/Android.cpp deleted file mode 100644 index 8f537c7852..0000000000 --- a/src/openrct2/platform/Android.cpp +++ /dev/null @@ -1,77 +0,0 @@ -/***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers - * - * For a complete list of all authors, please refer to contributors.md - * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 - * - * OpenRCT2 is licensed under the GNU General Public License version 3. - *****************************************************************************/ - -#ifdef __ANDROID__ - -# include "../config/Config.h" -# include "../localisation/Language.h" -# include "../util/Util.h" -# include "platform.h" - -# include -# include -# include - -AndroidClassLoader::AndroidClassLoader() -{ - log_info("Obtaining JNI class loader"); - - // This is a workaround to be able to call JNI's ClassLoader from non-main - // thread, based on https://stackoverflow.com/a/16302771 - - // Apparently it's OK to use it from across different thread, but JNI - // only looks for ClassLoader in the _current_ thread and fails to find - // it when searched for from a native library's non-main thread. - - // The solution below works by obtaining a ClassLoader reference in main - // thread and caching it for future use from any thread, instead of using - // it via env->FindClass(). ClassLoader itself is abstract, so we cannot - // create it directly; instead we take an arbitrary class and call - // getClassLoader() on it to create a reference that way. - - // If we're here, SDL's JNI_OnLoad has already been called and set env - JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); - - // Take an arbitrary class. While the class does not really matter, it - // makes sense to use one that's most likely already loaded and is unlikely - // to be removed from code. - auto randomClass = env->FindClass("io/openrct2/MainActivity"); - jclass classClass = env->GetObjectClass(randomClass); - - // Get its class loader - auto classLoaderClass = env->FindClass("java/lang/ClassLoader"); - auto getClassLoaderMethod = env->GetMethodID(classClass, "getClassLoader", "()Ljava/lang/ClassLoader;"); - - // Store the class loader and its findClass method for future use - _classLoader = env->NewGlobalRef(env->CallObjectMethod(randomClass, getClassLoaderMethod)); - _findClassMethod = env->GetMethodID(classLoaderClass, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;"); -} - -AndroidClassLoader::~AndroidClassLoader() -{ - JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); - env->DeleteGlobalRef(_classLoader); -} - -jobject AndroidClassLoader::_classLoader; -jmethodID AndroidClassLoader::_findClassMethod; - -static std::shared_ptr acl; - -void platform_android_init_class_loader() -{ - acl = std::make_shared(); -} - -jclass platform_android_find_class(JNIEnv* env, const char* name) -{ - return static_cast( - env->CallObjectMethod(AndroidClassLoader::_classLoader, AndroidClassLoader::_findClassMethod, env->NewStringUTF(name))); -} -#endif diff --git a/src/openrct2/platform/Platform.Android.cpp b/src/openrct2/platform/Platform.Android.cpp index 1ac745f6b6..9927056dcb 100644 --- a/src/openrct2/platform/Platform.Android.cpp +++ b/src/openrct2/platform/Platform.Android.cpp @@ -13,6 +13,20 @@ # include "../localisation/Language.h" # include "Platform2.h" +# include +# include + +AndroidClassLoader::~AndroidClassLoader() +{ + JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); + env->DeleteGlobalRef(_classLoader); +} + +jobject AndroidClassLoader::_classLoader; +jmethodID AndroidClassLoader::_findClassMethod; + +static std::shared_ptr acl; + namespace Platform { std::string GetFolderPath(SPECIAL_FOLDER folder) @@ -99,6 +113,53 @@ namespace Platform return displayScale; } + + void AndroidInitClassLoader() + { + acl = std::make_shared(); + } + + jclass AndroidFindClass(JNIEnv* env, std::string_view name) + { + return static_cast(env->CallObjectMethod( + AndroidClassLoader::_classLoader, AndroidClassLoader::_findClassMethod, + env->NewStringUTF(std::string(name).c_str()))); + } } // namespace Platform +AndroidClassLoader::AndroidClassLoader() +{ + log_info("Obtaining JNI class loader"); + + // This is a workaround to be able to call JNI's ClassLoader from non-main + // thread, based on https://stackoverflow.com/a/16302771 + + // Apparently it's OK to use it from across different thread, but JNI + // only looks for ClassLoader in the _current_ thread and fails to find + // it when searched for from a native library's non-main thread. + + // The solution below works by obtaining a ClassLoader reference in main + // thread and caching it for future use from any thread, instead of using + // it via env->FindClass(). ClassLoader itself is abstract, so we cannot + // create it directly; instead we take an arbitrary class and call + // getClassLoader() on it to create a reference that way. + + // If we're here, SDL's JNI_OnLoad has already been called and set env + JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); + + // Take an arbitrary class. While the class does not really matter, it + // makes sense to use one that's most likely already loaded and is unlikely + // to be removed from code. + auto randomClass = env->FindClass("io/openrct2/MainActivity"); + jclass classClass = env->GetObjectClass(randomClass); + + // Get its class loader + auto classLoaderClass = env->FindClass("java/lang/ClassLoader"); + auto getClassLoaderMethod = env->GetMethodID(classClass, "getClassLoader", "()Ljava/lang/ClassLoader;"); + + // Store the class loader and its findClass method for future use + _classLoader = env->NewGlobalRef(env->CallObjectMethod(randomClass, getClassLoaderMethod)); + _findClassMethod = env->GetMethodID(classLoaderClass, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;"); +} + #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index 6689c5cb48..7225215d36 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -80,6 +80,10 @@ namespace Platform const uint32_t iconIndex); void RemoveFileAssociations(); #endif +#ifdef __ANDROID__ + void AndroidInitClassLoader(); + jclass AndroidFindClass(JNIEnv* env, std::string_view name); +#endif bool IsRunningInWine(); bool IsColourTerminalSupported(); @@ -87,3 +91,15 @@ namespace Platform utf8* StrDecompToPrecomp(utf8* input); bool RequireNewWindow(bool openGL); } // namespace Platform + +#ifdef __ANDROID__ +class AndroidClassLoader +{ +public: + AndroidClassLoader(); + ~AndroidClassLoader(); + static jobject _classLoader; + static jmethodID _findClassMethod; +}; + +#endif // __ANDROID__ diff --git a/src/openrct2/platform/Shared.cpp b/src/openrct2/platform/Shared.cpp index 54c6258484..02c75c6313 100644 --- a/src/openrct2/platform/Shared.cpp +++ b/src/openrct2/platform/Shared.cpp @@ -223,7 +223,7 @@ void core_init() initialised = true; #ifdef __ANDROID__ - platform_android_init_class_loader(); + Platform::AndroidInitClassLoader(); #endif // __ANDROID__ platform_ticks_init(); diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index dcacbc052d..ace4b5e73e 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -124,17 +124,3 @@ bool platform_setup_uri_protocol(); // as it requires external linkage, which 'static' prevents __declspec(dllexport) int32_t StartOpenRCT2(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int32_t nCmdShow); #endif // _WIN32 - -#ifdef __ANDROID__ -class AndroidClassLoader -{ -public: - AndroidClassLoader(); - ~AndroidClassLoader(); - static jobject _classLoader; - static jmethodID _findClassMethod; -}; - -void platform_android_init_class_loader(); -jclass platform_android_find_class(JNIEnv* env, const char* name); -#endif // __ANDROID__ From c69ef062ab6733bdb9810d0559aad6b8fc77cc3d Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Sun, 16 Jan 2022 13:18:41 +0100 Subject: [PATCH 13/13] Use return {} instead of return "" --- src/openrct2/platform/Platform.Android.cpp | 4 ++-- src/openrct2/platform/Platform.Linux.cpp | 6 +++--- src/openrct2/platform/Platform.Win32.cpp | 6 +++--- src/openrct2/platform/Platform.macOS.mm | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/openrct2/platform/Platform.Android.cpp b/src/openrct2/platform/Platform.Android.cpp index 9927056dcb..e6bc6e444f 100644 --- a/src/openrct2/platform/Platform.Android.cpp +++ b/src/openrct2/platform/Platform.Android.cpp @@ -87,14 +87,14 @@ namespace Platform std::string GetSteamPath() { - return ""; + return {}; } # ifndef NO_TTF std::string GetFontPath(const TTFFontDescriptor& font) { STUB(); - return ""; + return {}; } # endif diff --git a/src/openrct2/platform/Platform.Linux.cpp b/src/openrct2/platform/Platform.Linux.cpp index 4f69f37462..f70db58095 100644 --- a/src/openrct2/platform/Platform.Linux.cpp +++ b/src/openrct2/platform/Platform.Linux.cpp @@ -297,7 +297,7 @@ namespace Platform const char* homeDir = getpwuid(getuid())->pw_dir; if (homeDir == nullptr) { - return ""; + return {}; } auto steamPath = Path::Combine(homeDir, ".local/share/Steam/ubuntu12_32/steamapps/content"); @@ -312,7 +312,7 @@ namespace Platform return steamPath; } - return ""; + return {}; } std::string GetFontPath(const TTFFontDescriptor& font) @@ -323,7 +323,7 @@ namespace Platform { log_error("Failed to initialize FontConfig library"); FcFini(); - return ""; + return {}; } FcPattern* pat = FcNameParse(reinterpret_cast(font.font_name)); diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index 4cae076866..02a39ca464 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -831,13 +831,13 @@ namespace Platform LRESULT result; if (RegOpenKeyW(HKEY_CURRENT_USER, L"Software\\Valve\\Steam", &hKey) != ERROR_SUCCESS) - return ""; + return {}; // Get the size of the path first if (RegQueryValueExW(hKey, L"SteamPath", nullptr, &type, nullptr, &size) != ERROR_SUCCESS) { RegCloseKey(hKey); - return ""; + return {}; } std::string outPath = ""; @@ -866,7 +866,7 @@ namespace Platform return Path::Combine(outPathTemp, font.filename); } - return ""; + return {}; # else log_warning("Compatibility hack: falling back to C:\\Windows\\Fonts"); return Path::Combine("C:\\Windows\\Fonts\\", font.filename); diff --git a/src/openrct2/platform/Platform.macOS.mm b/src/openrct2/platform/Platform.macOS.mm index ec31c5a99c..cb419662eb 100644 --- a/src/openrct2/platform/Platform.macOS.mm +++ b/src/openrct2/platform/Platform.macOS.mm @@ -239,7 +239,7 @@ namespace Platform const char* homeDir = getpwuid(getuid())->pw_dir; if (homeDir == nullptr) { - return ""; + return {}; } auto steamPath = Path::Combine( @@ -249,7 +249,7 @@ namespace Platform return steamPath; } - return ""; + return {}; } std::string GetFontPath(const TTFFontDescriptor& font) @@ -266,7 +266,7 @@ namespace Platform } else { - return ""; + return {}; } } }