FileSystemHelper.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using LibHac;
  2. using LibHac.Fs;
  3. using LibHac.Fs.NcaUtils;
  4. using Ryujinx.Common;
  5. using Ryujinx.HLE.FileSystem;
  6. using Ryujinx.HLE.Utilities;
  7. using System.IO;
  8. namespace Ryujinx.HLE.HOS.Services.FspSrv
  9. {
  10. static class FileSystemHelper
  11. {
  12. public static ResultCode LoadSaveDataFileSystem(ServiceCtx context, bool readOnly, out IFileSystem loadedFileSystem)
  13. {
  14. loadedFileSystem = null;
  15. SaveSpaceId saveSpaceId = (SaveSpaceId)context.RequestData.ReadInt64();
  16. ulong titleId = context.RequestData.ReadUInt64();
  17. UInt128 userId = context.RequestData.ReadStruct<UInt128>();
  18. long saveId = context.RequestData.ReadInt64();
  19. SaveDataType saveDataType = (SaveDataType)context.RequestData.ReadByte();
  20. SaveInfo saveInfo = new SaveInfo(titleId, saveId, saveDataType, saveSpaceId, userId);
  21. string savePath = context.Device.FileSystem.GetSavePath(context, saveInfo);
  22. try
  23. {
  24. LocalFileSystem fileSystem = new LocalFileSystem(savePath);
  25. LibHac.Fs.IFileSystem saveFileSystem = new DirectorySaveDataFileSystem(fileSystem);
  26. if (readOnly)
  27. {
  28. saveFileSystem = new ReadOnlyFileSystem(saveFileSystem);
  29. }
  30. loadedFileSystem = new IFileSystem(saveFileSystem);
  31. }
  32. catch (HorizonResultException ex)
  33. {
  34. return (ResultCode)ex.ResultValue.Value;
  35. }
  36. return ResultCode.Success;
  37. }
  38. public static ResultCode OpenNsp(ServiceCtx context, string pfsPath, out IFileSystem openedFileSystem)
  39. {
  40. openedFileSystem = null;
  41. try
  42. {
  43. LocalStorage storage = new LocalStorage(pfsPath, FileAccess.Read, FileMode.Open);
  44. PartitionFileSystem nsp = new PartitionFileSystem(storage);
  45. ImportTitleKeysFromNsp(nsp, context.Device.System.KeySet);
  46. openedFileSystem = new IFileSystem(nsp);
  47. }
  48. catch (HorizonResultException ex)
  49. {
  50. return (ResultCode)ex.ResultValue.Value;
  51. }
  52. return ResultCode.Success;
  53. }
  54. public static ResultCode OpenNcaFs(ServiceCtx context, string ncaPath, LibHac.Fs.IStorage ncaStorage, out IFileSystem openedFileSystem)
  55. {
  56. openedFileSystem = null;
  57. try
  58. {
  59. Nca nca = new Nca(context.Device.System.KeySet, ncaStorage);
  60. if (!nca.SectionExists(NcaSectionType.Data))
  61. {
  62. return ResultCode.PartitionNotFound;
  63. }
  64. LibHac.Fs.IFileSystem fileSystem = nca.OpenFileSystem(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel);
  65. openedFileSystem = new IFileSystem(fileSystem);
  66. }
  67. catch (HorizonResultException ex)
  68. {
  69. return (ResultCode)ex.ResultValue.Value;
  70. }
  71. return ResultCode.Success;
  72. }
  73. public static ResultCode OpenFileSystemFromInternalFile(ServiceCtx context, string fullPath, out IFileSystem openedFileSystem)
  74. {
  75. openedFileSystem = null;
  76. DirectoryInfo archivePath = new DirectoryInfo(fullPath).Parent;
  77. while (string.IsNullOrWhiteSpace(archivePath.Extension))
  78. {
  79. archivePath = archivePath.Parent;
  80. }
  81. if (archivePath.Extension == ".nsp" && File.Exists(archivePath.FullName))
  82. {
  83. FileStream pfsFile = new FileStream(
  84. archivePath.FullName.TrimEnd(Path.DirectorySeparatorChar),
  85. FileMode.Open,
  86. FileAccess.Read);
  87. try
  88. {
  89. PartitionFileSystem nsp = new PartitionFileSystem(pfsFile.AsStorage());
  90. ImportTitleKeysFromNsp(nsp, context.Device.System.KeySet);
  91. string filename = fullPath.Replace(archivePath.FullName, string.Empty).TrimStart('\\');
  92. if (nsp.FileExists(filename))
  93. {
  94. return OpenNcaFs(context, fullPath, nsp.OpenFile(filename, OpenMode.Read).AsStorage(), out openedFileSystem);
  95. }
  96. }
  97. catch (HorizonResultException ex)
  98. {
  99. return (ResultCode)ex.ResultValue.Value;
  100. }
  101. }
  102. return ResultCode.PathDoesNotExist;
  103. }
  104. public static void ImportTitleKeysFromNsp(LibHac.Fs.IFileSystem nsp, Keyset keySet)
  105. {
  106. foreach (DirectoryEntry ticketEntry in nsp.EnumerateEntries("*.tik"))
  107. {
  108. Ticket ticket = new Ticket(nsp.OpenFile(ticketEntry.FullPath, OpenMode.Read).AsStream());
  109. if (!keySet.TitleKeys.ContainsKey(ticket.RightsId))
  110. {
  111. keySet.TitleKeys.Add(ticket.RightsId, ticket.GetTitleKey(keySet));
  112. }
  113. }
  114. }
  115. }
  116. }