Merge pull request #16415 from Gymnasiast/android-fs

Enable std::filesystem for Android
This commit is contained in:
Michael Steenbeek 2022-03-19 14:24:00 +01:00 committed by GitHub
commit c84da5512c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 27 additions and 33 deletions

View File

@ -194,7 +194,7 @@ void ShortcutManager::LoadUserBindings()
{ {
try try
{ {
auto path = u8path(_env->GetFilePath(PATHID::CONFIG_SHORTCUTS)); auto path = fs::u8path(_env->GetFilePath(PATHID::CONFIG_SHORTCUTS));
if (fs::exists(path)) if (fs::exists(path))
{ {
LoadUserBindings(path); LoadUserBindings(path);
@ -204,7 +204,7 @@ void ShortcutManager::LoadUserBindings()
try try
{ {
Console::WriteLine("Importing legacy shortcuts..."); Console::WriteLine("Importing legacy shortcuts...");
auto legacyPath = u8path(_env->GetFilePath(PATHID::CONFIG_SHORTCUTS_LEGACY)); auto legacyPath = fs::u8path(_env->GetFilePath(PATHID::CONFIG_SHORTCUTS_LEGACY));
if (fs::exists(legacyPath)) if (fs::exists(legacyPath))
{ {
LoadLegacyBindings(legacyPath); LoadLegacyBindings(legacyPath);
@ -315,7 +315,7 @@ void ShortcutManager::SaveUserBindings()
{ {
try try
{ {
auto path = u8path(_env->GetFilePath(PATHID::CONFIG_SHORTCUTS)); auto path = fs::u8path(_env->GetFilePath(PATHID::CONFIG_SHORTCUTS));
SaveUserBindings(path); SaveUserBindings(path);
} }
catch (const std::exception& e) catch (const std::exception& e)

View File

@ -66,7 +66,7 @@ public:
const std::string GetChangelogText() const std::string GetChangelogText()
{ {
auto path = GetChangelogPath(); auto path = GetChangelogPath();
auto fs = std::ifstream(u8path(path), std::ios::in); auto fs = std::ifstream(fs::u8path(path), std::ios::in);
if (!fs.is_open()) if (!fs.is_open())
{ {
throw std::runtime_error("Unable to open " + path); throw std::runtime_error("Unable to open " + path);

View File

@ -582,7 +582,7 @@ namespace OpenRCT2
{ {
if (String::Equals(Path::GetExtension(path), ".sea", true)) if (String::Equals(Path::GetExtension(path), ".sea", true))
{ {
auto data = DecryptSea(u8path(path)); auto data = DecryptSea(fs::u8path(path));
auto ms = MemoryStream(data.data(), data.size(), MEMORY_ACCESS::READ); auto ms = MemoryStream(data.data(), data.size(), MEMORY_ACCESS::READ);
if (!LoadParkFromStream(&ms, path, loadTitleScreenOnFail, asScenario)) if (!LoadParkFromStream(&ms, path, loadTitleScreenOnFail, asScenario))
{ {

View File

@ -25,7 +25,7 @@ namespace File
{ {
bool Exists(u8string_view path) bool Exists(u8string_view path)
{ {
fs::path file = u8path(path); fs::path file = fs::u8path(path);
log_verbose("Checking if file exists: %s", u8string(path).c_str()); log_verbose("Checking if file exists: %s", u8string(path).c_str());
std::error_code ec; std::error_code ec;
const auto result = fs::exists(file, ec); const auto result = fs::exists(file, ec);
@ -41,27 +41,27 @@ namespace File
} }
std::error_code ec; std::error_code ec;
const auto result = fs::copy_file(u8path(srcPath), u8path(dstPath), ec); const auto result = fs::copy_file(fs::u8path(srcPath), fs::u8path(dstPath), ec);
return result && ec.value() == 0; return result && ec.value() == 0;
} }
bool Delete(u8string_view path) bool Delete(u8string_view path)
{ {
std::error_code ec; std::error_code ec;
const auto result = fs::remove(u8path(path), ec); const auto result = fs::remove(fs::u8path(path), ec);
return result && ec.value() == 0; return result && ec.value() == 0;
} }
bool Move(u8string_view srcPath, u8string_view dstPath) bool Move(u8string_view srcPath, u8string_view dstPath)
{ {
std::error_code ec; std::error_code ec;
fs::rename(u8path(srcPath), u8path(dstPath), ec); fs::rename(fs::u8path(srcPath), fs::u8path(dstPath), ec);
return ec.value() == 0; return ec.value() == 0;
} }
std::vector<uint8_t> ReadAllBytes(u8string_view path) std::vector<uint8_t> ReadAllBytes(u8string_view path)
{ {
std::ifstream fs(u8path(u8string(path)), std::ios::in | std::ios::binary); std::ifstream fs(fs::u8path(u8string(path)), std::ios::in | std::ios::binary);
if (!fs.is_open()) if (!fs.is_open())
{ {
throw IOException("Unable to open " + u8string(path)); throw IOException("Unable to open " + u8string(path));

View File

@ -110,7 +110,7 @@ namespace OpenRCT2
_fileSize = _filelengthi64(_fileno(_file)); _fileSize = _filelengthi64(_fileno(_file));
#else #else
std::error_code ec; std::error_code ec;
_fileSize = fs::file_size(u8path(path), ec); _fileSize = fs::file_size(fs::u8path(path), ec);
#endif #endif
_ownsFilePtr = true; _ownsFilePtr = true;

View File

@ -20,7 +20,7 @@
#elif defined(__APPLE__) // XCode has the header, but reports error when included. #elif defined(__APPLE__) // XCode has the header, but reports error when included.
# define HAVE_STD_FILESYSTEM 0 # define HAVE_STD_FILESYSTEM 0
#elif defined(__ANDROID__) #elif defined(__ANDROID__)
# define HAVE_STD_FILESYSTEM 0 # define HAVE_STD_FILESYSTEM 1
#elif defined(__has_include) // For GCC/Clang check if the header exists. #elif defined(__has_include) // For GCC/Clang check if the header exists.
# if __has_include(<filesystem>) # if __has_include(<filesystem>)
# define HAVE_STD_FILESYSTEM 1 # define HAVE_STD_FILESYSTEM 1
@ -57,9 +57,3 @@ namespace fs = ghc::filesystem;
#endif #endif
#undef HAVE_STD_FILESYSTEM // Not needed any more, don't make it public. #undef HAVE_STD_FILESYSTEM // Not needed any more, don't make it public.
#ifdef __ANDROID__
# define u8path(path) fs::u8path(std::string(path))
#else
# define u8path(path) fs::u8path(path)
#endif

View File

@ -312,7 +312,7 @@ namespace Imaging
return ReadFromFile(path, GetImageFormatFromPath(path)); return ReadFromFile(path, GetImageFormatFromPath(path));
default: default:
{ {
std::ifstream fs(u8path(path), std::ios::binary); std::ifstream fs(fs::u8path(path), std::ios::binary);
return ReadFromStream(fs, format); return ReadFromStream(fs, format);
} }
} }
@ -333,7 +333,7 @@ namespace Imaging
break; break;
case IMAGE_FORMAT::PNG: case IMAGE_FORMAT::PNG:
{ {
std::ofstream fs(u8path(path), std::ios::binary); std::ofstream fs(fs::u8path(path), std::ios::binary);
WritePng(fs, image); WritePng(fs, image);
break; break;
} }

View File

@ -50,7 +50,7 @@ namespace Path
u8string GetDirectory(u8string_view path) u8string GetDirectory(u8string_view path)
{ {
return u8path(path).parent_path().u8string(); return fs::u8path(path).parent_path().u8string();
} }
void CreateDirectory(u8string_view path) void CreateDirectory(u8string_view path)
@ -61,34 +61,34 @@ namespace Path
bool DirectoryExists(u8string_view path) bool DirectoryExists(u8string_view path)
{ {
std::error_code ec; std::error_code ec;
const auto result = fs::is_directory(u8path(path), ec); const auto result = fs::is_directory(fs::u8path(path), ec);
return result && ec.value() == 0; return result && ec.value() == 0;
} }
u8string GetFileName(u8string_view path) u8string GetFileName(u8string_view path)
{ {
return u8path(path).filename().u8string(); return fs::u8path(path).filename().u8string();
} }
u8string GetFileNameWithoutExtension(u8string_view path) u8string GetFileNameWithoutExtension(u8string_view path)
{ {
return u8path(path).stem().u8string(); return fs::u8path(path).stem().u8string();
} }
u8string GetExtension(u8string_view path) u8string GetExtension(u8string_view path)
{ {
return u8path(path).extension().u8string(); return fs::u8path(path).extension().u8string();
} }
u8string WithExtension(u8string_view path, u8string_view newExtension) u8string WithExtension(u8string_view path, u8string_view newExtension)
{ {
return u8path(path).replace_extension(u8path(newExtension)).u8string(); return fs::u8path(path).replace_extension(fs::u8path(newExtension)).u8string();
} }
u8string GetAbsolute(u8string_view relative) u8string GetAbsolute(u8string_view relative)
{ {
std::error_code ec; std::error_code ec;
return fs::absolute(u8path(relative), ec).u8string(); return fs::absolute(fs::u8path(relative), ec).u8string();
} }
bool Equals(u8string_view a, u8string_view b) bool Equals(u8string_view a, u8string_view b)
@ -104,7 +104,7 @@ namespace Path
bool DeleteDirectory(u8string_view path) bool DeleteDirectory(u8string_view path)
{ {
std::error_code ec; std::error_code ec;
const auto result = fs::remove_all(u8path(path), ec); const auto result = fs::remove_all(fs::u8path(path), ec);
return (result > 0) && ec.value() == 0; return (result > 0) && ec.value() == 0;
} }
} // namespace Path } // namespace Path

View File

@ -773,7 +773,7 @@ static std::string ResolveFilenameForCapture(const fs::path& filename)
return *path; return *path;
} }
auto screenshotDirectory = u8path(screenshot_get_directory()); auto screenshotDirectory = fs::u8path(screenshot_get_directory());
auto screenshotPath = fs::absolute(screenshotDirectory / filename); auto screenshotPath = fs::absolute(screenshotDirectory / filename);
// Check the filename isn't attempting to leave the screenshot directory for security // Check the filename isn't attempting to leave the screenshot directory for security

View File

@ -1085,7 +1085,7 @@ void NetworkBase::BeginChatLog()
auto env = GetContext().GetPlatformEnvironment(); auto env = GetContext().GetPlatformEnvironment();
auto directory = env->GetDirectoryPath(DIRBASE::USER, DIRID::LOG_CHAT); auto directory = env->GetDirectoryPath(DIRBASE::USER, DIRID::LOG_CHAT);
_chatLogPath = BeginLog(directory, "", _chatLogFilenameFormat); _chatLogPath = BeginLog(directory, "", _chatLogFilenameFormat);
_chat_log_fs.open(u8path(_chatLogPath), std::ios::out | std::ios::app); _chat_log_fs.open(fs::u8path(_chatLogPath), std::ios::out | std::ios::app);
} }
void NetworkBase::AppendChatLog(std::string_view s) void NetworkBase::AppendChatLog(std::string_view s)
@ -1106,7 +1106,7 @@ void NetworkBase::BeginServerLog()
auto env = GetContext().GetPlatformEnvironment(); auto env = GetContext().GetPlatformEnvironment();
auto directory = env->GetDirectoryPath(DIRBASE::USER, DIRID::LOG_SERVER); auto directory = env->GetDirectoryPath(DIRBASE::USER, DIRID::LOG_SERVER);
_serverLogPath = BeginLog(directory, ServerName, _serverLogFilenameFormat); _serverLogPath = BeginLog(directory, ServerName, _serverLogFilenameFormat);
_server_log_fs.open(u8path(_serverLogPath), std::ios::out | std::ios::app | std::ios::binary); _server_log_fs.open(fs::u8path(_serverLogPath), std::ios::out | std::ios::app | std::ios::binary);
// Log server start event // Log server start event
utf8 logMessage[256]; utf8 logMessage[256];

View File

@ -176,7 +176,7 @@ private:
{ {
if (String::Equals(Path::GetExtension(path), ".sea", true)) if (String::Equals(Path::GetExtension(path), ".sea", true))
{ {
auto data = DecryptSea(u8path(path)); auto data = DecryptSea(fs::u8path(path));
auto ms = std::make_unique<MemoryStream>(); auto ms = std::make_unique<MemoryStream>();
// Need to copy the data into MemoryStream as the overload will borrow instead of copy. // Need to copy the data into MemoryStream as the overload will borrow instead of copy.
ms->Write(data.data(), data.size()); ms->Write(data.data(), data.size());

View File

@ -116,7 +116,7 @@ namespace OpenRCT2::Scripting
try try
{ {
CaptureOptions captureOptions; CaptureOptions captureOptions;
captureOptions.Filename = u8path(AsOrDefault(options["filename"], "")); captureOptions.Filename = fs::u8path(AsOrDefault(options["filename"], ""));
captureOptions.Rotation = options["rotation"].as_int() & 3; captureOptions.Rotation = options["rotation"].as_int() & 3;
captureOptions.Zoom = ZoomLevel(options["zoom"].as_int()); captureOptions.Zoom = ZoomLevel(options["zoom"].as_int());
captureOptions.Transparent = AsOrDefault(options["transparent"], false); captureOptions.Transparent = AsOrDefault(options["transparent"], false);