From 82f37d006c85f6443adf1a3241feb03ec91b331c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Sun, 22 Mar 2026 17:44:26 +0100 Subject: [PATCH] Prepare Platform, File, FileStream for handling embedded assets --- src/openrct2/PlatformEnvironment.cpp | 11 +++++ src/openrct2/core/File.cpp | 32 ++++++++------ src/openrct2/core/FileStream.cpp | 52 ++++++++++++++++++++++- src/openrct2/core/FileStream.h | 1 + src/openrct2/core/Path.cpp | 12 ++++++ src/openrct2/platform/Platform.Common.cpp | 43 +++++++++++++++++++ src/openrct2/platform/Platform.h | 23 ++++++++++ 7 files changed, 160 insertions(+), 14 deletions(-) diff --git a/src/openrct2/PlatformEnvironment.cpp b/src/openrct2/PlatformEnvironment.cpp index c7e8aef9dc..89d4243f6e 100644 --- a/src/openrct2/PlatformEnvironment.cpp +++ b/src/openrct2/PlatformEnvironment.cpp @@ -145,6 +145,17 @@ public: auto dirbase = GetDefaultBaseDirectory(pathid); auto basePath = GetDirectoryPath(dirbase); auto fileName = kFileNames[EnumValue(pathid)]; + + auto assetPath = Platform::GetAssetPath(); + if (!assetPath.empty()) + { + auto combinedAssetPath = Path::Combine(assetPath, basePath, fileName); + if (File::Exists(combinedAssetPath)) + { + return combinedAssetPath; + } + } + return Path::Combine(basePath, fileName); } diff --git a/src/openrct2/core/File.cpp b/src/openrct2/core/File.cpp index fd1756bb15..1b752fa2e8 100644 --- a/src/openrct2/core/File.cpp +++ b/src/openrct2/core/File.cpp @@ -28,8 +28,20 @@ namespace OpenRCT2::File { bool Exists(u8string_view path) { - fs::path file = fs::u8path(path); LOG_VERBOSE("Checking if file exists: %s", u8string(path).c_str()); + auto assetCheckResult = Platform::CheckAssetExists(path); + switch (assetCheckResult) + { + case Platform::AssetCheckResult::Found: + return true; + case Platform::AssetCheckResult::NotFound: + return false; + case Platform::AssetCheckResult::NotApplicable: + default: + break; + } + + fs::path file = fs::u8path(path); std::error_code ec; const auto result = fs::exists(file, ec); return result && ec.value() == 0; @@ -64,14 +76,9 @@ namespace OpenRCT2::File std::vector ReadAllBytes(u8string_view path) { - std::ifstream fs(fs::u8path(u8string(path)), std::ios::in | std::ios::binary); - if (!fs.is_open()) - { - throw IOException("Unable to open " + u8string(path)); - } - + FileStream fstream(path, FileMode::open); std::vector result; - auto fsize = Platform::GetFileSize(path); + auto fsize = fstream.GetLength(); if (fsize > SIZE_MAX) { u8string message = String::stdFormat( @@ -80,9 +87,8 @@ namespace OpenRCT2::File } else { - result.resize(fsize); - fs.read(reinterpret_cast(result.data()), result.size()); - fs.exceptions(fs.failbit); + result.resize(static_cast(fsize)); + fstream.Read(result.data(), result.size()); } return result; } @@ -127,8 +133,8 @@ namespace OpenRCT2::File void WriteAllBytes(u8string_view path, const void* buffer, size_t length) { - auto fs = FileStream(path, FileMode::write); - fs.Write(buffer, length); + auto fstream = FileStream(path, FileMode::write); + fstream.Write(buffer, length); } uint64_t GetLastModified(u8string_view path) diff --git a/src/openrct2/core/FileStream.cpp b/src/openrct2/core/FileStream.cpp index 301ef639fa..d3abb0e07a 100644 --- a/src/openrct2/core/FileStream.cpp +++ b/src/openrct2/core/FileStream.cpp @@ -9,6 +9,7 @@ #include "FileStream.h" +#include "../platform/Platform.h" #include "Path.hpp" #include "String.hpp" @@ -44,6 +45,20 @@ namespace OpenRCT2 FileStream::FileStream(const utf8* path, FileMode fileMode) { + if (fileMode == FileMode::open) + { + auto assetOpen = Platform::OpenAssetFile(path); + if (assetOpen.result == Platform::AssetCheckResult::Found) + { + _asset = assetOpen.handle; + _fileSize = assetOpen.size; + _canRead = true; + _canWrite = false; + _ownsFilePtr = true; + return; + } + } + const char* mode; switch (fileMode) { @@ -117,7 +132,17 @@ namespace OpenRCT2 _disposed = true; if (_ownsFilePtr) { - fclose(_file); + if (_asset != nullptr) + { + Platform::CloseAssetFile(_asset); + } + else + { + if (_file != nullptr) + { + fclose(_file); + } + } } } } @@ -139,6 +164,10 @@ namespace OpenRCT2 uint64_t FileStream::GetPosition() const { + if (_asset != nullptr) + { + return Platform::GetAssetPosition(_asset); + } return ftello(_file); } @@ -149,6 +178,11 @@ namespace OpenRCT2 void FileStream::Seek(int64_t offset, int32_t origin) { + if (_asset != nullptr) + { + Platform::SeekAsset(_asset, offset, origin); + return; + } switch (origin) { case STREAM_SEEK_BEGIN: @@ -165,6 +199,14 @@ namespace OpenRCT2 void FileStream::Read(void* buffer, uint64_t length) { + if (_asset != nullptr) + { + if (Platform::ReadAsset(_asset, buffer, length) == length) + { + return; + } + throw IOException("Attempted to read past end of file."); + } if (fread(buffer, 1, static_cast(length), _file) == length) { return; @@ -174,6 +216,10 @@ namespace OpenRCT2 void FileStream::Write(const void* buffer, uint64_t length) { + if (_canWrite == false) + { + throw IOException("Cannot write to a read-only stream."); + } if (length == 0) { return; @@ -191,6 +237,10 @@ namespace OpenRCT2 uint64_t FileStream::TryRead(void* buffer, uint64_t length) { + if (_asset != nullptr) + { + return Platform::TryReadAsset(_asset, buffer, length); + } size_t readBytes = fread(buffer, 1, static_cast(length), _file); return readBytes; } diff --git a/src/openrct2/core/FileStream.h b/src/openrct2/core/FileStream.h index dc22add600..6c805b5ad4 100644 --- a/src/openrct2/core/FileStream.h +++ b/src/openrct2/core/FileStream.h @@ -28,6 +28,7 @@ namespace OpenRCT2 { private: FILE* _file = nullptr; + void* _asset = nullptr; bool _ownsFilePtr = false; bool _canRead = false; bool _canWrite = false; diff --git a/src/openrct2/core/Path.cpp b/src/openrct2/core/Path.cpp index 5a9f414b1a..e8ed912f7c 100644 --- a/src/openrct2/core/Path.cpp +++ b/src/openrct2/core/Path.cpp @@ -60,6 +60,18 @@ namespace OpenRCT2::Path bool DirectoryExists(u8string_view path) { + auto assetCheckResult = Platform::CheckAssetDirectoryExists(path); + switch (assetCheckResult) + { + case Platform::AssetCheckResult::Found: + return true; + case Platform::AssetCheckResult::NotFound: + return false; + case Platform::AssetCheckResult::NotApplicable: + default: + break; + } + std::error_code ec; const auto result = fs::is_directory(fs::u8path(path), ec); return result && ec.value() == 0; diff --git a/src/openrct2/platform/Platform.Common.cpp b/src/openrct2/platform/Platform.Common.cpp index 65c88ce133..9952194caf 100644 --- a/src/openrct2/platform/Platform.Common.cpp +++ b/src/openrct2/platform/Platform.Common.cpp @@ -91,6 +91,49 @@ namespace OpenRCT2::Platform return outTime; } + AssetCheckResult CheckAssetDirectoryExists([[maybe_unused]] u8string_view path) + { + return AssetCheckResult::NotApplicable; + } + + AssetCheckResult CheckAssetExists([[maybe_unused]] u8string_view path) + { + return AssetCheckResult::NotApplicable; + } + + AssetFileOpenResult OpenAssetFile([[maybe_unused]] u8string_view path) + { + return AssetFileOpenResult{ AssetCheckResult::NotApplicable, nullptr, 0 }; + } + + void CloseAssetFile([[maybe_unused]] void* handle) + { + } + + uint64_t GetAssetPosition([[maybe_unused]] void* handle) + { + return 0; + } + + void SeekAsset([[maybe_unused]] void* handle, [[maybe_unused]] int64_t offset, [[maybe_unused]] int32_t origin) + { + } + + uint64_t ReadAsset([[maybe_unused]] void* handle, [[maybe_unused]] void* buffer, [[maybe_unused]] uint64_t length) + { + return 0; + } + + uint64_t TryReadAsset([[maybe_unused]] void* handle, [[maybe_unused]] void* buffer, [[maybe_unused]] uint64_t length) + { + return 0; + } + + u8string GetAssetPath() + { + return {}; + } + std::optional classifyGamePath(std::string_view path) { auto combinedPath = Path::ResolveCasing(Path::Combine(path, u8"Data", u8"g1.dat")); diff --git a/src/openrct2/platform/Platform.h b/src/openrct2/platform/Platform.h index 978662b905..f88f7121df 100644 --- a/src/openrct2/platform/Platform.h +++ b/src/openrct2/platform/Platform.h @@ -61,6 +61,13 @@ struct TTFFontDescriptor; namespace OpenRCT2::Platform { + enum class AssetCheckResult + { + NotApplicable, + Found, + NotFound, + }; + struct SteamGameData { u8string nativeFolder; @@ -112,6 +119,13 @@ namespace OpenRCT2::Platform u8"RCT Classic+.app" PATH_SEPARATOR "Contents" PATH_SEPARATOR "Resources"; // clang-format on + struct AssetFileOpenResult + { + AssetCheckResult result; + void* handle; + uint64_t size; + }; + std::string GetEnvironmentVariable(std::string_view name); std::string GetFolderPath(SpecialFolder folder); std::string GetInstallPath(); @@ -125,6 +139,15 @@ namespace OpenRCT2::Platform std::string ResolveCasing(std::string_view path, bool fileExists); std::string SanitiseFilename(std::string_view originalName); bool IsFilenameValid(u8string_view fileName); + AssetCheckResult CheckAssetDirectoryExists(u8string_view path); + AssetCheckResult CheckAssetExists(u8string_view path); + AssetFileOpenResult OpenAssetFile(u8string_view path); + void CloseAssetFile(void* handle); + uint64_t GetAssetPosition(void* handle); + void SeekAsset(void* handle, int64_t offset, int32_t origin); + uint64_t ReadAsset(void* handle, void* buffer, uint64_t length); + uint64_t TryReadAsset(void* handle, void* buffer, uint64_t length); + u8string GetAssetPath(); uint16_t GetLocaleLanguage(); CurrencyType GetLocaleCurrency();