Implement basic support of SystemSaveData and Cleanup IFileSystemProxy (#767)

* Implement basic support of SystemSaveData and Cleanup IFileSystemProxy

- Implement `OpenSystemSaveData` as a `IFileSystem` in `SaveHelper`:
  On real device, system saves data are stored encrypted, and we can't create an empty system save data for now. That's why if a user put his own dump of system save in `RyuFs\nand\system\save\`, we extract content in associated folder and open it as a `IFileSystem`. If the system save data don't exist, a folder is created.

- Cleanup `IFileSystemProxy` by adding a Helper class.

- Implement `GetSavePath` in `VirtualFileSystem` and remove `GetGameSavePath` in `SaveHelper`.

* remove the forgotten I

* Fix align
This commit is contained in:
Ac_K 2019-09-08 23:33:40 +02:00 committed by Thomas Guillemard
parent 9afb8ad485
commit 1ff89d6482
6 changed files with 280 additions and 195 deletions

View file

@ -9,4 +9,4 @@
TemporaryStorage,
CacheStorage
}
}
}

View file

@ -1,45 +1,44 @@
using Ryujinx.HLE.HOS;
using LibHac.Fs;
using Ryujinx.HLE.HOS;
using System.IO;
using static Ryujinx.HLE.FileSystem.VirtualFileSystem;
namespace Ryujinx.HLE.FileSystem
{
static class SaveHelper
{
public static string GetSavePath(SaveInfo saveMetaData, ServiceCtx context)
public static IFileSystem OpenSystemSaveData(ServiceCtx context, ulong saveId)
{
string baseSavePath = NandPath;
ulong currentTitleId = saveMetaData.TitleId;
SaveInfo saveInfo = new SaveInfo(0, (long)saveId, SaveDataType.SystemSaveData, SaveSpaceId.NandSystem);
string savePath = context.Device.FileSystem.GetSavePath(context, saveInfo, false);
switch (saveMetaData.SaveSpaceId)
if (File.Exists(savePath))
{
case SaveSpaceId.NandUser:
baseSavePath = UserNandPath;
break;
case SaveSpaceId.NandSystem:
baseSavePath = SystemNandPath;
break;
case SaveSpaceId.SdCard:
baseSavePath = Path.Combine(SdCardPath, "Nintendo");
break;
string tempDirectoryPath = $"{savePath}_temp";
Directory.CreateDirectory(tempDirectoryPath);
IFileSystem outputFolder = new LocalFileSystem(tempDirectoryPath);
using (LocalStorage systemSaveData = new LocalStorage(savePath, FileAccess.Read, FileMode.Open))
{
IFileSystem saveFs = new LibHac.Fs.Save.SaveDataFileSystem(context.Device.System.KeySet, systemSaveData, IntegrityCheckLevel.None, false);
saveFs.CopyFileSystem(outputFolder);
}
File.Delete(savePath);
Directory.Move(tempDirectoryPath, savePath);
}
else
{
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}
}
baseSavePath = Path.Combine(baseSavePath, "save");
if (saveMetaData.TitleId == 0 && saveMetaData.SaveDataType == SaveDataType.SaveData)
{
currentTitleId = context.Process.TitleId;
}
string saveAccount = saveMetaData.UserId.IsNull ? "savecommon" : saveMetaData.UserId.ToString();
string savePath = Path.Combine(baseSavePath,
saveMetaData.SaveId.ToString("x16"),
saveAccount,
saveMetaData.SaveDataType == SaveDataType.SaveData ? currentTitleId.ToString("x16") : string.Empty);
return savePath;
return new LocalFileSystem(savePath);
}
}
}
}

View file

@ -4,25 +4,24 @@ namespace Ryujinx.HLE.FileSystem
{
struct SaveInfo
{
public ulong TitleId { get; private set; }
public long SaveId { get; private set; }
public UInt128 UserId { get; private set; }
public ulong TitleId { get; private set; }
public long SaveId { get; private set; }
public SaveDataType SaveDataType { get; private set; }
public SaveSpaceId SaveSpaceId { get; private set; }
public UInt128 UserId { get; private set; }
public SaveInfo(
ulong titleId,
long saveId,
SaveDataType saveDataType,
UInt128 userId,
SaveSpaceId saveSpaceId)
SaveSpaceId saveSpaceId,
UInt128 userId = new UInt128())
{
TitleId = titleId;
UserId = userId;
SaveId = saveId;
SaveDataType = saveDataType;
SaveSpaceId = saveSpaceId;
UserId = userId;
}
}
}
}

View file

@ -54,20 +54,48 @@ namespace Ryujinx.HLE.FileSystem
return fullPath;
}
public string GetSdCardPath() => MakeDirAndGetFullPath(SdCardPath);
public string GetSdCardPath() => MakeFullPath(SdCardPath);
public string GetNandPath() => MakeDirAndGetFullPath(NandPath);
public string GetNandPath() => MakeFullPath(NandPath);
public string GetSystemPath() => MakeDirAndGetFullPath(SystemPath);
public string GetSystemPath() => MakeFullPath(SystemPath);
internal string GetGameSavePath(SaveInfo save, ServiceCtx context)
internal string GetSavePath(ServiceCtx context, SaveInfo saveInfo, bool isDirectory = true)
{
return MakeDirAndGetFullPath(SaveHelper.GetSavePath(save, context));
string saveUserPath = "";
string baseSavePath = NandPath;
ulong currentTitleId = saveInfo.TitleId;
switch (saveInfo.SaveSpaceId)
{
case SaveSpaceId.NandUser: baseSavePath = UserNandPath; break;
case SaveSpaceId.NandSystem: baseSavePath = SystemNandPath; break;
case SaveSpaceId.SdCard: baseSavePath = Path.Combine(SdCardPath, "Nintendo"); break;
}
baseSavePath = Path.Combine(baseSavePath, "save");
if (saveInfo.TitleId == 0 && saveInfo.SaveDataType == SaveDataType.SaveData)
{
currentTitleId = context.Process.TitleId;
}
if (saveInfo.SaveSpaceId == SaveSpaceId.NandUser)
{
saveUserPath = saveInfo.UserId.IsNull ? "savecommon" : saveInfo.UserId.ToString();
}
string savePath = Path.Combine(baseSavePath,
saveInfo.SaveId.ToString("x16"),
saveUserPath,
saveInfo.SaveDataType == SaveDataType.SaveData ? currentTitleId.ToString("x16") : string.Empty);
return MakeFullPath(savePath, isDirectory);
}
public string GetFullPartitionPath(string partitionPath)
{
return MakeDirAndGetFullPath(partitionPath);
return MakeFullPath(partitionPath);
}
public string SwitchPathToSystemPath(string switchPath)
@ -79,7 +107,7 @@ namespace Ryujinx.HLE.FileSystem
return null;
}
return GetFullPath(MakeDirAndGetFullPath(parts[0]), parts[1]);
return GetFullPath(MakeFullPath(parts[0]), parts[1]);
}
public string SystemPathToSwitchPath(string systemPath)
@ -104,37 +132,40 @@ namespace Ryujinx.HLE.FileSystem
return null;
}
private string MakeDirAndGetFullPath(string dir)
private string MakeFullPath(string path, bool isDirectory = true)
{
// Handles Common Switch Content Paths
switch (dir)
switch (path)
{
case ContentPath.SdCard:
case "@Sdcard":
dir = SdCardPath;
path = SdCardPath;
break;
case ContentPath.User:
dir = UserNandPath;
path = UserNandPath;
break;
case ContentPath.System:
dir = SystemNandPath;
path = SystemNandPath;
break;
case ContentPath.SdCardContent:
dir = Path.Combine(SdCardPath, "Nintendo", "Contents");
path = Path.Combine(SdCardPath, "Nintendo", "Contents");
break;
case ContentPath.UserContent:
dir = Path.Combine(UserNandPath, "Contents");
path = Path.Combine(UserNandPath, "Contents");
break;
case ContentPath.SystemContent:
dir = Path.Combine(SystemNandPath, "Contents");
path = Path.Combine(SystemNandPath, "Contents");
break;
}
string fullPath = Path.Combine(GetBasePath(), dir);
string fullPath = Path.Combine(GetBasePath(), path);
if (!Directory.Exists(fullPath))
if (isDirectory)
{
Directory.CreateDirectory(fullPath);
if (!Directory.Exists(fullPath))
{
Directory.CreateDirectory(fullPath);
}
}
return fullPath;