forked from rosebudai/OpenRCT2-Web
Prepare Platform, File, FileStream for handling embedded assets
This commit is contained in:
7 files changed
+160
-14
No files matched your search
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
+19
-13
@@ -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<uint8_t> 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<uint8_t> 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<char*>(result.data()), result.size());
|
||||
fs.exceptions(fs.failbit);
|
||||
result.resize(static_cast<size_t>(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)
|
||||
|
||||
@@ -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<size_t>(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<size_t>(length), _file);
|
||||
return readBytes;
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ namespace OpenRCT2
|
||||
{
|
||||
private:
|
||||
FILE* _file = nullptr;
|
||||
void* _asset = nullptr;
|
||||
bool _ownsFilePtr = false;
|
||||
bool _canRead = false;
|
||||
bool _canWrite = false;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<RCT2Variant> classifyGamePath(std::string_view path)
|
||||
{
|
||||
auto combinedPath = Path::ResolveCasing(Path::Combine(path, u8"Data", u8"g1.dat"));
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user