RMG-Core: use noexcept versions of std::filesystem functions

This commit is contained in:
Rosalie Wanders
2025-02-24 21:32:51 +01:00
parent 9ef92c3578
commit ea9313aa1b
5 changed files with 36 additions and 67 deletions
+5 -10
View File
@@ -436,6 +436,7 @@ bool CoreUnzip(std::filesystem::path file, std::filesystem::path path)
std::string error;
std::filesystem::path targetPath;
std::filesystem::path fileNamePath;
std::error_code errorCode;
unzFile zipFile;
unz_global_info zipInfo;
@@ -491,20 +492,14 @@ bool CoreUnzip(std::filesystem::path file, std::filesystem::path path)
if (fileNamePath.has_filename() &&
fileNamePath.has_parent_path())
{ // create parent directories
try
{
if (!std::filesystem::is_directory(targetPath.parent_path()) &&
!std::filesystem::create_directories(targetPath.parent_path()))
{
throw std::exception();
}
}
catch (...)
if (!std::filesystem::is_directory(targetPath.parent_path(), errorCode) &&
!std::filesystem::create_directories(targetPath.parent_path(), errorCode))
{
unzClose(zipFile);
error = "CoreUnzip: std::filesystem::create_directory(";
error += targetPath.string();
error += ") Failed!";
error += ") Failed: ";
error += errorCode.message();
CoreSetError(error);
return false;
}
+3 -9
View File
@@ -131,6 +131,7 @@ static std::filesystem::path get_user_cheat_file_path(CoreRomHeader romHeader, C
{
std::filesystem::path oldCheatFilePath;
std::filesystem::path cheatFilePath;
std::error_code errorCode;
oldCheatFilePath = CoreGetUserDataDirectory();
oldCheatFilePath += CORE_DIR_SEPERATOR_STR;
@@ -146,16 +147,9 @@ static std::filesystem::path get_user_cheat_file_path(CoreRomHeader romHeader, C
// try to make the user cheats directory
// if it doesn't exist yet
try
if (!std::filesystem::is_directory(cheatFilePath.parent_path(), errorCode))
{
if (!std::filesystem::is_directory(cheatFilePath.parent_path()))
{
std::filesystem::create_directory(cheatFilePath.parent_path());
}
}
catch (...)
{
// we'll fail later...
std::filesystem::create_directory(cheatFilePath.parent_path(), errorCode);
}
// keep compatability with <v0.4.1
+10 -12
View File
@@ -42,6 +42,8 @@ static std::filesystem::path get_exe_directory(void)
static std::filesystem::path directory;
#ifdef _WIN32
wchar_t buffer[MAX_PATH] = {0};
#else
std::error_code errorCode;
#endif // _WIN32
if (!directory.empty())
@@ -57,13 +59,10 @@ static std::filesystem::path get_exe_directory(void)
}
directory = std::filesystem::path(buffer).parent_path();
#else // _WIN32
try
directory = std::filesystem::canonical("/proc/self/exe", errorCode).parent_path();
if (errorCode)
{
directory = std::filesystem::canonical("/proc/self/exe").parent_path();
}
catch (...)
{ // fail silently and fallback to current path
std::cerr << "get_exe_directory: std::filesystem::canonical(\"/proc/self/exe\") threw an exception!" << std::endl;
std::cerr << "get_exe_directory: std::filesystem::canonical(\"/proc/self/exe\") failed: " << errorCode.message() << std::endl;
std::terminate();
}
#endif // _WIN32
@@ -133,6 +132,7 @@ static std::filesystem::path get_var_directory(std::string var, std::string appe
bool CoreCreateDirectories(void)
{
std::string error;
std::error_code errorCode;
std::filesystem::path directories[] =
{
@@ -151,15 +151,13 @@ bool CoreCreateDirectories(void)
for (const std::filesystem::path& directory : directories)
{
try
{
std::filesystem::create_directories(directory);
}
catch (...)
if (!std::filesystem::is_directory(directory) &&
!std::filesystem::create_directories(directory, errorCode))
{
error = "CoreCreateDirectories Failed: cannot create the '";
error += directory.string();
error += "' directory!";
error += "' directory: ";
error += errorCode.message();
CoreSetError(error);
return false;
}
+9 -17
View File
@@ -146,16 +146,12 @@ bool CoreSetupMediaLoader(void)
void CoreResetMediaLoader(void)
{
std::error_code errorCode;
// attempt to remove extracted disk file
if (l_HasExtractedDisk && !l_DdRomFile.empty())
{
try
{
std::filesystem::remove(l_DdDiskFile);
}
catch (...)
{
}
std::filesystem::remove(l_DdDiskFile, errorCode);
}
l_HasExtractedDisk = false;
@@ -165,6 +161,7 @@ void CoreResetMediaLoader(void)
void CoreMediaLoaderSetDiskFile(std::filesystem::path disk)
{
std::error_code error_code;
std::vector<char> buf;
std::string file_extension;
@@ -190,19 +187,14 @@ void CoreMediaLoaderSetDiskFile(std::filesystem::path disk)
}
disk = CoreGetUserCacheDirectory();
disk += "/extracted_disks/";
disk += CORE_DIR_SEPERATOR_STR;
disk += "extracted_disks";
disk += CORE_DIR_SEPERATOR_STR;
disk += extracted_file.filename();
// attempt to create extraction directory
try
{
if (!std::filesystem::exists(disk.parent_path()) &&
!std::filesystem::create_directory(disk.parent_path()))
{
throw std::exception();
}
}
catch (...)
if (!std::filesystem::is_directory(disk.parent_path(), error_code) &&
!std::filesystem::create_directory(disk.parent_path(), error_code))
{
return;
}
+9 -19
View File
@@ -41,6 +41,7 @@ static std::filesystem::path l_RomPath;
bool CoreOpenRom(std::filesystem::path file)
{
std::string error;
std::error_code error_code;
m64p_error ret;
std::vector<char> buf;
std::string file_extension;
@@ -80,20 +81,14 @@ bool CoreOpenRom(std::filesystem::path file)
disk_file += extracted_file.filename();
// attempt to create extraction directory
try
{
if (!std::filesystem::exists(disk_file.parent_path()) &&
!std::filesystem::create_directory(disk_file.parent_path()))
{
throw std::exception();
}
}
catch (...)
if (!std::filesystem::exists(disk_file.parent_path(), error_code) &&
!std::filesystem::create_directory(disk_file.parent_path(), error_code))
{
error = "CoreOpenRom Failed: ";
error += "Failed to create \"";
error += disk_file.parent_path().string();
error += "\"!";
error += "\": ";
error += error_code.message();
CoreSetError(error);
return false;
}
@@ -204,6 +199,7 @@ bool CoreGetRomPath(std::filesystem::path& path)
bool CoreCloseRom(void)
{
std::string error;
std::error_code errorCode;
m64p_error ret;
if (!m64p::Core.IsHooked())
@@ -255,18 +251,12 @@ bool CoreCloseRom(void)
// attempt to clean temporary extracted disk file
if (l_HasExtractedDisk)
{
try
{
if (!std::filesystem::remove(l_ExtractedDiskPath))
{
throw std::exception();
}
}
catch (...)
if (!std::filesystem::remove(l_ExtractedDiskPath, errorCode))
{
error = "CoreCloseRom: Failed to remove \"";
error += l_ExtractedDiskPath.string();
error += "\"!";
error += "\": ";
error += errorCode.message();
CoreSetError(error);
return false;
}