Enhance Error Handling with Try-Pattern Refactoring

This commit is contained in:
Marco Carvalho 2024-04-06 11:57:33 -03:00
parent 66b1d59c66
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>())
{
string contentDirectory = null;
string contentPathString = null;
string registeredDirectory = null;
try
{
contentPathString = ContentPath.GetContentPath(storageId);
contentDirectory = ContentPath.GetRealPath(contentPathString);
registeredDirectory = Path.Combine(contentDirectory, "registered");
}
catch (NotSupportedException)
if (!ContentPath.TryGetContentPath(storageId, out var contentPathString))
{
continue;
}
if (!ContentPath.TryGetRealPath(contentPathString, out var contentDirectory))
{
continue;
}
var registeredDirectory = Path.Combine(contentDirectory, "registered");
Directory.CreateDirectory(registeredDirectory);
@ -471,8 +466,8 @@ namespace Ryujinx.HLE.FileSystem
public void InstallFirmware(string firmwareSource)
{
string contentPathString = ContentPath.GetContentPath(StorageId.BuiltInSystem);
string contentDirectory = ContentPath.GetRealPath(contentPathString);
ContentPath.TryGetContentPath(StorageId.BuiltInSystem, out var contentPathString);
ContentPath.TryGetRealPath(contentPathString, out var contentDirectory);
string registeredDirectory = Path.Combine(contentDirectory, "registered");
string temporaryDirectory = Path.Combine(contentDirectory, "temp");

View file

@ -26,19 +26,32 @@ namespace Ryujinx.HLE.FileSystem
public const string Nintendo = "Nintendo";
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),
UserContent => Path.Combine(AppDataManager.BaseDirPath, UserNandPath, Contents),
SdCardContent => Path.Combine(GetSdCardPath(), Nintendo, Contents),
System => Path.Combine(AppDataManager.BaseDirPath, SystemNandPath),
User => Path.Combine(AppDataManager.BaseDirPath, UserNandPath),
_ => throw new NotSupportedException($"Content Path \"`{switchContentPath}`\" is not supported."),
};
case SystemContent:
realPath = Path.Combine(AppDataManager.BaseDirPath, SystemNandPath, Contents);
return true;
case UserContent:
realPath = Path.Combine(AppDataManager.BaseDirPath, UserNandPath, Contents);
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)
{
return contentStorageId switch
@ -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,
StorageId.BuiltInUser => UserContent,
StorageId.SdCard => SdCardContent,
_ => throw new NotSupportedException($"Storage Id \"`{storageId}`\" is not supported."),
};
case StorageId.BuiltInSystem:
contentPath = SystemContent;
return true;
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)