From f372117939b6e29051670f28f1719003169cbdef Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Mon, 23 Nov 2020 00:23:07 -0300 Subject: [PATCH 1/6] Extract GetLastModified to Platform --- src/openrct2/core/File.cpp | 25 +----------------------- src/openrct2/platform/Platform.Posix.cpp | 14 +++++++++++++ src/openrct2/platform/Platform.Win32.cpp | 18 +++++++++++++++++ src/openrct2/platform/Platform2.h | 1 + 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/openrct2/core/File.cpp b/src/openrct2/core/File.cpp index af5753da13..df722e1196 100644 --- a/src/openrct2/core/File.cpp +++ b/src/openrct2/core/File.cpp @@ -121,30 +121,7 @@ namespace File uint64_t GetLastModified(const std::string& path) { - uint64_t lastModified = 0; -#ifdef _WIN32 - auto pathW = String::ToWideChar(path); - auto hFile = CreateFileW(pathW.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); - if (hFile != INVALID_HANDLE_VALUE) - { - FILETIME ftCreate, ftAccess, ftWrite; - if (GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite)) - { - lastModified = (static_cast(ftWrite.dwHighDateTime) << 32ULL) - | static_cast(ftWrite.dwLowDateTime); - } - CloseHandle(hFile); - } -#else - struct stat statInfo - { - }; - if (stat(path.c_str(), &statInfo) == 0) - { - lastModified = statInfo.st_mtime; - } -#endif - return lastModified; + return Platform::GetLastModified(path); } } // namespace File diff --git a/src/openrct2/platform/Platform.Posix.cpp b/src/openrct2/platform/Platform.Posix.cpp index aee87c5764..61e57976b5 100644 --- a/src/openrct2/platform/Platform.Posix.cpp +++ b/src/openrct2/platform/Platform.Posix.cpp @@ -18,6 +18,7 @@ # include # include # include +# include namespace Platform { @@ -159,6 +160,19 @@ namespace Platform return -1; # endif // __EMSCRIPTEN__ } + + uint64_t GetLastModified(const std::string& path) + { + uint64_t lastModified = 0; + struct stat statInfo + { + }; + if (stat(path.c_str(), &statInfo) == 0) + { + lastModified = statInfo.st_mtime; + } + return lastModified; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index 08e029887e..1ab0ca0764 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -535,6 +535,24 @@ namespace Platform log_warning("Execute() not implemented for Windows!"); return -1; } + + uint64_t GetLastModified(const std::string& path) + { + uint64_t lastModified = 0; + auto pathW = String::ToWideChar(path); + auto hFile = CreateFileW(pathW.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); + if (hFile != INVALID_HANDLE_VALUE) + { + FILETIME ftCreate, ftAccess, ftWrite; + if (GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite)) + { + lastModified = (static_cast(ftWrite.dwHighDateTime) << 32ULL) + | static_cast(ftWrite.dwLowDateTime); + } + CloseHandle(hFile); + } + return lastModified; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index 2c73971df0..c2b8216298 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -35,6 +35,7 @@ namespace Platform std::string GetCurrentExecutablePath(); std::string GetCurrentExecutableDirectory(); bool FileExists(const std::string path); + uint64_t GetLastModified(const std::string& path); rct2_time GetTimeLocal(); rct2_date GetDateLocal(); bool FindApp(const std::string& app, std::string* output); From f15bbd220afe80426c22292105bfe031a943ef17 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Thu, 25 Mar 2021 23:45:01 -0300 Subject: [PATCH 2/6] Extract IsPathSeparator to Platform --- src/openrct2/core/Path.cpp | 16 ++++------------ src/openrct2/platform/Platform.Posix.cpp | 5 +++++ src/openrct2/platform/Platform.Win32.cpp | 6 ++++++ src/openrct2/platform/Platform2.h | 1 + 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/openrct2/core/Path.cpp b/src/openrct2/core/Path.cpp index 8bb3269049..4f8f42fb76 100644 --- a/src/openrct2/core/Path.cpp +++ b/src/openrct2/core/Path.cpp @@ -13,6 +13,7 @@ #endif #include "../localisation/Language.h" +#include "../platform/Platform2.h" #include "../platform/platform.h" #include "../util/Util.h" #include "File.h" @@ -30,15 +31,6 @@ namespace Path return safe_strcat_path(buffer, src, bufferSize); } - static constexpr bool IsPathSeparator(char c) - { -#ifdef _WIN32 - if (c == '\\') - return true; -#endif - return c == '/'; - } - std::string Combine(std::string_view a, std::string_view b) { if (a.empty()) @@ -47,9 +39,9 @@ namespace Path return std::string(a); auto aEnd = a.back(); auto bBegin = b.front(); - if (IsPathSeparator(aEnd)) + if (Platform::IsPathSeparator(aEnd)) { - if (IsPathSeparator(bBegin)) + if (Platform::IsPathSeparator(bBegin)) { return std::string(a) + std::string(b.substr(1)); } @@ -60,7 +52,7 @@ namespace Path } else { - if (IsPathSeparator(bBegin)) + if (Platform::IsPathSeparator(bBegin)) { return std::string(a) + std::string(b); } diff --git a/src/openrct2/platform/Platform.Posix.cpp b/src/openrct2/platform/Platform.Posix.cpp index 61e57976b5..fbad60a74e 100644 --- a/src/openrct2/platform/Platform.Posix.cpp +++ b/src/openrct2/platform/Platform.Posix.cpp @@ -173,6 +173,11 @@ namespace Platform } return lastModified; } + + bool IsPathSeparator(char c) + { + return c == '/'; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index 1ab0ca0764..cb99189578 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -553,6 +553,12 @@ namespace Platform } return lastModified; } + + bool IsPathSeparator(char c) + { + return c == '\\' || c == '/'; + } + } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index c2b8216298..d09fade07a 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -35,6 +35,7 @@ namespace Platform std::string GetCurrentExecutablePath(); std::string GetCurrentExecutableDirectory(); bool FileExists(const std::string path); + bool IsPathSeparator(char c); uint64_t GetLastModified(const std::string& path); rct2_time GetTimeLocal(); rct2_date GetDateLocal(); From 9a071aef76e190eb5f89564eccc9dea5999a0b13 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Thu, 25 Mar 2021 23:51:33 -0300 Subject: [PATCH 3/6] Extract GetAbsolutePath to Platform --- src/openrct2/core/Path.cpp | 29 +----------------------- src/openrct2/platform/Platform.Posix.cpp | 16 +++++++++++++ src/openrct2/platform/Platform.Win32.cpp | 18 +++++++++++++++ src/openrct2/platform/Platform2.h | 1 + 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/openrct2/core/Path.cpp b/src/openrct2/core/Path.cpp index 4f8f42fb76..e45e1b2e1b 100644 --- a/src/openrct2/core/Path.cpp +++ b/src/openrct2/core/Path.cpp @@ -196,34 +196,7 @@ namespace Path utf8* GetAbsolute(utf8* buffer, size_t bufferSize, const utf8* relativePath) { -#ifdef _WIN32 - auto relativePathW = String::ToWideChar(relativePath); - wchar_t absolutePathW[MAX_PATH]; - DWORD length = GetFullPathNameW( - relativePathW.c_str(), static_cast(std::size(absolutePathW)), absolutePathW, nullptr); - if (length == 0) - { - return String::Set(buffer, bufferSize, relativePath); - } - else - { - auto absolutePath = String::ToUtf8(absolutePathW); - String::Set(buffer, bufferSize, absolutePath.c_str()); - return buffer; - } -#else - utf8* absolutePath = realpath(relativePath, nullptr); - if (absolutePath == nullptr) - { - return String::Set(buffer, bufferSize, relativePath); - } - else - { - String::Set(buffer, bufferSize, absolutePath); - Memory::Free(absolutePath); - return buffer; - } -#endif + return Platform::GetAbsolutePath(buffer, bufferSize, relativePath); } std::string GetAbsolute(const std::string& relative) diff --git a/src/openrct2/platform/Platform.Posix.cpp b/src/openrct2/platform/Platform.Posix.cpp index fbad60a74e..f1ae448934 100644 --- a/src/openrct2/platform/Platform.Posix.cpp +++ b/src/openrct2/platform/Platform.Posix.cpp @@ -9,6 +9,7 @@ #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__) +# include "../core/Memory.hpp" # include "../core/String.hpp" # include "Platform2.h" # include "platform.h" @@ -178,6 +179,21 @@ namespace Platform { return c == '/'; } + + utf8* GetAbsolutePath(utf8* buffer, size_t bufferSize, const utf8* relativePath) + { + utf8* absolutePath = realpath(relativePath, nullptr); + if (absolutePath == nullptr) + { + return String::Set(buffer, bufferSize, relativePath); + } + else + { + String::Set(buffer, bufferSize, absolutePath); + Memory::Free(absolutePath); + return buffer; + } + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index cb99189578..f898911f1b 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -559,6 +559,24 @@ namespace Platform return c == '\\' || c == '/'; } + utf8* GetAbsolutePath(utf8* buffer, size_t bufferSize, const utf8* relativePath) + { + auto relativePathW = String::ToWideChar(relativePath); + wchar_t absolutePathW[MAX_PATH]; + DWORD length = GetFullPathNameW( + relativePathW.c_str(), static_cast(std::size(absolutePathW)), absolutePathW, nullptr); + if (length == 0) + { + return String::Set(buffer, bufferSize, relativePath); + } + else + { + auto absolutePath = String::ToUtf8(absolutePathW); + String::Set(buffer, bufferSize, absolutePath.c_str()); + return buffer; + } + } + } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index d09fade07a..91c159fa05 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -36,6 +36,7 @@ namespace Platform std::string GetCurrentExecutableDirectory(); bool FileExists(const std::string path); bool IsPathSeparator(char c); + utf8* GetAbsolutePath(utf8* buffer, size_t bufferSize, const utf8* relativePath); uint64_t GetLastModified(const std::string& path); rct2_time GetTimeLocal(); rct2_date GetDateLocal(); From c7ae064a4cae1ed1b90c51b52f9de19b67afe718 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Thu, 25 Mar 2021 23:55:45 -0300 Subject: [PATCH 4/6] Extract ShouldIgnoreCase to Platform --- src/openrct2/core/Path.cpp | 6 +----- src/openrct2/platform/Platform.Posix.cpp | 5 +++++ src/openrct2/platform/Platform.Win32.cpp | 5 +++++ src/openrct2/platform/Platform2.h | 1 + 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/openrct2/core/Path.cpp b/src/openrct2/core/Path.cpp index e45e1b2e1b..a90be9b6ec 100644 --- a/src/openrct2/core/Path.cpp +++ b/src/openrct2/core/Path.cpp @@ -212,11 +212,7 @@ namespace Path bool Equals(const utf8* a, const utf8* b) { - bool ignoreCase = false; -#ifdef _WIN32 - ignoreCase = true; -#endif - return String::Equals(a, b, ignoreCase); + return String::Equals(a, b, Platform::ShouldIgnoreCase()); } std::string ResolveCasing(const std::string& path) diff --git a/src/openrct2/platform/Platform.Posix.cpp b/src/openrct2/platform/Platform.Posix.cpp index f1ae448934..52fd7149da 100644 --- a/src/openrct2/platform/Platform.Posix.cpp +++ b/src/openrct2/platform/Platform.Posix.cpp @@ -175,6 +175,11 @@ namespace Platform return lastModified; } + bool ShouldIgnoreCase() + { + return false; + } + bool IsPathSeparator(char c) { return c == '/'; diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index f898911f1b..a1fd9fc8e0 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -554,6 +554,11 @@ namespace Platform return lastModified; } + bool ShouldIgnoreCase() + { + return true; + } + bool IsPathSeparator(char c) { return c == '\\' || c == '/'; diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index 91c159fa05..79c0b3f77a 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -34,6 +34,7 @@ namespace Platform std::string GetDocsPath(); std::string GetCurrentExecutablePath(); std::string GetCurrentExecutableDirectory(); + bool ShouldIgnoreCase(); bool FileExists(const std::string path); bool IsPathSeparator(char c); utf8* GetAbsolutePath(utf8* buffer, size_t bufferSize, const utf8* relativePath); From 62fc4c803497be0dbf32ebfed2099cfbec896582 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Fri, 26 Mar 2021 00:07:53 -0300 Subject: [PATCH 5/6] Extract ResolveCasing to Platform --- src/openrct2/core/Path.cpp | 45 ++---------------------- src/openrct2/platform/Platform.Posix.cpp | 41 +++++++++++++++++++++ src/openrct2/platform/Platform.Win32.cpp | 11 ++++++ src/openrct2/platform/Platform2.h | 1 + 4 files changed, 56 insertions(+), 42 deletions(-) diff --git a/src/openrct2/core/Path.cpp b/src/openrct2/core/Path.cpp index a90be9b6ec..33a3667d28 100644 --- a/src/openrct2/core/Path.cpp +++ b/src/openrct2/core/Path.cpp @@ -7,10 +7,7 @@ * OpenRCT2 is licensed under the GNU General Public License version 3. *****************************************************************************/ -#include -#ifndef _WIN32 -# include -#endif +#include "Path.hpp" #include "../localisation/Language.h" #include "../platform/Platform2.h" @@ -19,9 +16,9 @@ #include "File.h" #include "FileSystem.hpp" #include "Memory.hpp" -#include "Path.hpp" #include "String.hpp" +#include #include namespace Path @@ -217,42 +214,6 @@ namespace Path std::string ResolveCasing(const std::string& path) { - std::string result; - if (File::Exists(path)) - { - // Windows is case insensitive so it will exist and that is all that matters - // for now. We can properly resolve the casing if we ever need to. - result = path; - } -#ifndef _WIN32 - else - { - std::string fileName = Path::GetFileName(path); - std::string directory = Path::GetDirectory(path); - - struct dirent** files; - auto count = scandir(directory.c_str(), &files, nullptr, alphasort); - if (count != -1) - { - // Find a file which matches by name (case insensitive) - for (int32_t i = 0; i < count; i++) - { - if (String::Equals(files[i]->d_name, fileName.c_str(), true)) - { - result = Path::Combine(directory, std::string(files[i]->d_name)); - break; - } - } - - // Free memory - for (int32_t i = 0; i < count; i++) - { - free(files[i]); - } - free(files); - } - } -#endif - return result; + return Platform::ResolveCasing(path, File::Exists(path)); } } // namespace Path diff --git a/src/openrct2/platform/Platform.Posix.cpp b/src/openrct2/platform/Platform.Posix.cpp index 52fd7149da..9948a8fa55 100644 --- a/src/openrct2/platform/Platform.Posix.cpp +++ b/src/openrct2/platform/Platform.Posix.cpp @@ -10,6 +10,7 @@ #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__) # include "../core/Memory.hpp" +# include "../core/Path.hpp" # include "../core/String.hpp" # include "Platform2.h" # include "platform.h" @@ -18,6 +19,7 @@ # include # include # include +# include # include # include @@ -199,6 +201,45 @@ namespace Platform return buffer; } } + + std::string ResolveCasing(const std::string& path, bool fileExists) + { + std::string result; + if (fileExists) + { + // Windows is case insensitive so it will exist and that is all that matters + // for now. We can properly resolve the casing if we ever need to. + result = path; + } + else + { + std::string fileName = Path::GetFileName(path); + std::string directory = Path::GetDirectory(path); + + struct dirent** files; + auto count = scandir(directory.c_str(), &files, nullptr, alphasort); + if (count != -1) + { + // Find a file which matches by name (case insensitive) + for (int32_t i = 0; i < count; i++) + { + if (String::Equals(files[i]->d_name, fileName.c_str(), true)) + { + result = Path::Combine(directory, std::string(files[i]->d_name)); + break; + } + } + + // Free memory + for (int32_t i = 0; i < count; i++) + { + free(files[i]); + } + free(files); + } + } + return result; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index a1fd9fc8e0..02f9be341d 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -582,6 +582,17 @@ namespace Platform } } + std::string ResolveCasing(const std::string& path, bool fileExists) + { + std::string result; + if (fileExists) + { + // Windows is case insensitive so it will exist and that is all that matters + // for now. We can properly resolve the casing if we ever need to. + result = path; + } + return result; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index 79c0b3f77a..9a2daaba83 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -39,6 +39,7 @@ namespace Platform bool IsPathSeparator(char c); utf8* GetAbsolutePath(utf8* buffer, size_t bufferSize, const utf8* relativePath); uint64_t GetLastModified(const std::string& path); + std::string ResolveCasing(const std::string& path, bool fileExists); rct2_time GetTimeLocal(); rct2_date GetDateLocal(); bool FindApp(const std::string& app, std::string* output); From e23eaa164b116b9710d995d37e852219defd0967 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Fri, 26 Mar 2021 00:24:55 -0300 Subject: [PATCH 6/6] Extract RequireNewWindow to Platform --- src/openrct2/drawing/NewDrawing.cpp | 13 +++---------- src/openrct2/platform/Platform.Posix.cpp | 5 +++++ src/openrct2/platform/Platform.Win32.cpp | 7 +++++++ src/openrct2/platform/Platform2.h | 1 + 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/openrct2/drawing/NewDrawing.cpp b/src/openrct2/drawing/NewDrawing.cpp index 7b1b3de45f..e29b5417c6 100644 --- a/src/openrct2/drawing/NewDrawing.cpp +++ b/src/openrct2/drawing/NewDrawing.cpp @@ -15,6 +15,7 @@ #include "../interface/Screenshot.h" #include "../localisation/StringIds.h" #include "../paint/Painter.h" +#include "../platform/Platform2.h" #include "../ui/UiContext.h" #include "../world/Location.hpp" #include "IDrawingContext.h" @@ -52,16 +53,8 @@ static IDrawingEngine* GetDrawingEngine() bool drawing_engine_requires_new_window(DrawingEngine srcEngine, DrawingEngine dstEngine) { -#ifdef _WIN32 - if (srcEngine != DrawingEngine::OpenGL && dstEngine != DrawingEngine::OpenGL) - { - // Windows is apparently able to switch to hardware rendering on the fly although - // using the same window in an unaccelerated and accelerated context is unsupported by SDL2 - return false; - } -#endif - - return true; + bool openGL = srcEngine == DrawingEngine::OpenGL || dstEngine == DrawingEngine::OpenGL; + return Platform::RequireNewWindow(openGL); } void drawing_engine_init() diff --git a/src/openrct2/platform/Platform.Posix.cpp b/src/openrct2/platform/Platform.Posix.cpp index 9948a8fa55..8889749720 100644 --- a/src/openrct2/platform/Platform.Posix.cpp +++ b/src/openrct2/platform/Platform.Posix.cpp @@ -240,6 +240,11 @@ namespace Platform } return result; } + + bool RequireNewWindow(bool openGL) + { + return true; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index 02f9be341d..d7d22fbbea 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -593,6 +593,13 @@ namespace Platform } return result; } + + bool RequireNewWindow(bool openGL) + { + // Windows is apparently able to switch to hardware rendering on the fly although + // using the same window in an unaccelerated and accelerated context is unsupported by SDL2 + return openGL; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index 9a2daaba83..6189f21b96 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -66,4 +66,5 @@ namespace Platform bool IsColourTerminalSupported(); bool HandleSpecialCommandLineArgument(const char* argument); utf8* StrDecompToPrecomp(utf8* input); + bool RequireNewWindow(bool openGL); } // namespace Platform