Enhance Error Handling with Try-Pattern Refactoring

This commit is contained in:
Marco Carvalho 2024-04-06 11:57:33 -03:00
commit 0b0f11a0e7
2 changed files with 45 additions and 29 deletions

View file

@ -104,20 +104,15 @@ namespace Ryujinx.HLE.FileSystem
foreach (StorageId storageId in Enum.GetValues<StorageId>()) foreach (StorageId storageId in Enum.GetValues<StorageId>())
{ {
string contentDirectory = null; if (!ContentPath.TryGetContentPath(storageId, out var contentPathString))
string contentPathString = null;
string registeredDirectory = null;
try
{
contentPathString = ContentPath.GetContentPath(storageId);
contentDirectory = ContentPath.GetRealPath(contentPathString);
registeredDirectory = Path.Combine(contentDirectory, "registered");
}
catch (NotSupportedException)
{ {
continue; continue;
} }
if (!ContentPath.TryGetRealPath(contentPathString, out var contentDirectory))
{
continue;
}
var registeredDirectory = Path.Combine(contentDirectory, "registered");
Directory.CreateDirectory(registeredDirectory); Directory.CreateDirectory(registeredDirectory);
@ -471,8 +466,8 @@ namespace Ryujinx.HLE.FileSystem
public void InstallFirmware(string firmwareSource) public void InstallFirmware(string firmwareSource)
{ {
string contentPathString = ContentPath.GetContentPath(StorageId.BuiltInSystem); ContentPath.TryGetContentPath(StorageId.BuiltInSystem, out var contentPathString);
string contentDirectory = ContentPath.GetRealPath(contentPathString); ContentPath.TryGetRealPath(contentPathString, out var contentDirectory);
string registeredDirectory = Path.Combine(contentDirectory, "registered"); string registeredDirectory = Path.Combine(contentDirectory, "registered");
string temporaryDirectory = Path.Combine(contentDirectory, "temp"); string temporaryDirectory = Path.Combine(contentDirectory, "temp");

View file

@ -26,18 +26,31 @@ namespace Ryujinx.HLE.FileSystem
public const string Nintendo = "Nintendo"; public const string Nintendo = "Nintendo";
public const string Contents = "Contents"; public const string Contents = "Contents";
public static string GetRealPath(string switchContentPath) public static bool TryGetRealPath(string switchContentPath, out string realPath)
{ {
return switchContentPath switch switch (switchContentPath)
{ {
SystemContent => Path.Combine(AppDataManager.BaseDirPath, SystemNandPath, Contents), case SystemContent:
UserContent => Path.Combine(AppDataManager.BaseDirPath, UserNandPath, Contents), realPath = Path.Combine(AppDataManager.BaseDirPath, SystemNandPath, Contents);
SdCardContent => Path.Combine(GetSdCardPath(), Nintendo, Contents), return true;
System => Path.Combine(AppDataManager.BaseDirPath, SystemNandPath), case UserContent:
User => Path.Combine(AppDataManager.BaseDirPath, UserNandPath), realPath = Path.Combine(AppDataManager.BaseDirPath, UserNandPath, Contents);
_ => throw new NotSupportedException($"Content Path \"`{switchContentPath}`\" is not supported."), return true;
}; case SdCardContent:
realPath = Path.Combine(GetSdCardPath(), Nintendo, Contents);
return true;
case System:
realPath = Path.Combine(AppDataManager.BaseDirPath, SystemNandPath);
return true;
case User:
realPath = Path.Combine(AppDataManager.BaseDirPath, UserNandPath);
return true;
default:
realPath = null;
return false;
} }
}
public static string GetContentPath(ContentStorageId contentStorageId) public static string GetContentPath(ContentStorageId contentStorageId)
{ {
@ -50,15 +63,23 @@ namespace Ryujinx.HLE.FileSystem
}; };
} }
public static string GetContentPath(StorageId storageId) public static bool TryGetContentPath(StorageId storageId, out string contentPath)
{ {
return storageId switch switch (storageId)
{ {
StorageId.BuiltInSystem => SystemContent, case StorageId.BuiltInSystem:
StorageId.BuiltInUser => UserContent, contentPath = SystemContent;
StorageId.SdCard => SdCardContent, return true;
_ => throw new NotSupportedException($"Storage Id \"`{storageId}`\" is not supported."), case StorageId.BuiltInUser:
}; contentPath = UserContent;
return true;
case StorageId.SdCard:
contentPath = SdCardContent;
return true;
default:
contentPath = null;
return false;
}
} }
public static StorageId GetStorageId(string contentPathString) public static StorageId GetStorageId(string contentPathString)