VirtualFileSystem.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using LibHac;
  2. using LibHac.Common;
  3. using LibHac.Fs;
  4. using LibHac.FsService;
  5. using LibHac.FsSystem;
  6. using LibHac.Spl;
  7. using Ryujinx.Common.Configuration;
  8. using Ryujinx.HLE.FileSystem.Content;
  9. using Ryujinx.HLE.HOS;
  10. using System;
  11. using System.IO;
  12. namespace Ryujinx.HLE.FileSystem
  13. {
  14. public class VirtualFileSystem : IDisposable
  15. {
  16. public const string NandPath = AppDataManager.DefaultNandDir;
  17. public const string SdCardPath = AppDataManager.DefaultSdcardDir;
  18. public static string SafeNandPath = Path.Combine(NandPath, "safe");
  19. public static string SystemNandPath = Path.Combine(NandPath, "system");
  20. public static string UserNandPath = Path.Combine(NandPath, "user");
  21. private static bool _isInitialized = false;
  22. public Keyset KeySet { get; private set; }
  23. public FileSystemServer FsServer { get; private set; }
  24. public FileSystemClient FsClient { get; private set; }
  25. public EmulatedGameCard GameCard { get; private set; }
  26. public EmulatedSdCard SdCard { get; private set; }
  27. public ModLoader ModLoader {get; private set;}
  28. private VirtualFileSystem()
  29. {
  30. Reload();
  31. ModLoader = new ModLoader(); // Should only be created once
  32. }
  33. public Stream RomFs { get; private set; }
  34. public void LoadRomFs(string fileName)
  35. {
  36. RomFs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
  37. }
  38. public void SetRomFs(Stream romfsStream)
  39. {
  40. RomFs?.Close();
  41. RomFs = romfsStream;
  42. }
  43. public string GetFullPath(string basePath, string fileName)
  44. {
  45. if (fileName.StartsWith("//"))
  46. {
  47. fileName = fileName.Substring(2);
  48. }
  49. else if (fileName.StartsWith('/'))
  50. {
  51. fileName = fileName.Substring(1);
  52. }
  53. else
  54. {
  55. return null;
  56. }
  57. string fullPath = Path.GetFullPath(Path.Combine(basePath, fileName));
  58. if (!fullPath.StartsWith(GetBasePath()))
  59. {
  60. return null;
  61. }
  62. return fullPath;
  63. }
  64. internal string GetBasePath() => AppDataManager.BaseDirPath;
  65. internal string GetSdCardPath() => MakeFullPath(SdCardPath);
  66. public string GetNandPath() => MakeFullPath(NandPath);
  67. internal string GetSavePath(ServiceCtx context, SaveInfo saveInfo, bool isDirectory = true)
  68. {
  69. string saveUserPath = "";
  70. string baseSavePath = NandPath;
  71. ulong currentTitleId = saveInfo.TitleId;
  72. switch (saveInfo.SaveSpaceId)
  73. {
  74. case SaveSpaceId.NandUser: baseSavePath = UserNandPath; break;
  75. case SaveSpaceId.NandSystem: baseSavePath = SystemNandPath; break;
  76. case SaveSpaceId.SdCard: baseSavePath = Path.Combine(SdCardPath, "Nintendo"); break;
  77. }
  78. baseSavePath = Path.Combine(baseSavePath, "save");
  79. if (saveInfo.TitleId == 0 && saveInfo.SaveDataType == SaveDataType.SaveData)
  80. {
  81. currentTitleId = context.Process.TitleId;
  82. }
  83. if (saveInfo.SaveSpaceId == SaveSpaceId.NandUser)
  84. {
  85. saveUserPath = saveInfo.UserId.IsNull ? "savecommon" : saveInfo.UserId.ToString();
  86. }
  87. string savePath = Path.Combine(baseSavePath,
  88. saveInfo.SaveId.ToString("x16"),
  89. saveUserPath,
  90. saveInfo.SaveDataType == SaveDataType.SaveData ? currentTitleId.ToString("x16") : string.Empty);
  91. return MakeFullPath(savePath, isDirectory);
  92. }
  93. public string GetFullPartitionPath(string partitionPath)
  94. {
  95. return MakeFullPath(partitionPath);
  96. }
  97. public string SwitchPathToSystemPath(string switchPath)
  98. {
  99. string[] parts = switchPath.Split(":");
  100. if (parts.Length != 2)
  101. {
  102. return null;
  103. }
  104. return GetFullPath(MakeFullPath(parts[0]), parts[1]);
  105. }
  106. public string SystemPathToSwitchPath(string systemPath)
  107. {
  108. string baseSystemPath = GetBasePath() + Path.DirectorySeparatorChar;
  109. if (systemPath.StartsWith(baseSystemPath))
  110. {
  111. string rawPath = systemPath.Replace(baseSystemPath, "");
  112. int firstSeparatorOffset = rawPath.IndexOf(Path.DirectorySeparatorChar);
  113. if (firstSeparatorOffset == -1)
  114. {
  115. return $"{rawPath}:/";
  116. }
  117. string basePath = rawPath.Substring(0, firstSeparatorOffset);
  118. string fileName = rawPath.Substring(firstSeparatorOffset + 1);
  119. return $"{basePath}:/{fileName}";
  120. }
  121. return null;
  122. }
  123. private string MakeFullPath(string path, bool isDirectory = true)
  124. {
  125. // Handles Common Switch Content Paths
  126. switch (path)
  127. {
  128. case ContentPath.SdCard:
  129. case "@Sdcard":
  130. path = SdCardPath;
  131. break;
  132. case ContentPath.User:
  133. path = UserNandPath;
  134. break;
  135. case ContentPath.System:
  136. path = SystemNandPath;
  137. break;
  138. case ContentPath.SdCardContent:
  139. path = Path.Combine(SdCardPath, "Nintendo", "Contents");
  140. break;
  141. case ContentPath.UserContent:
  142. path = Path.Combine(UserNandPath, "Contents");
  143. break;
  144. case ContentPath.SystemContent:
  145. path = Path.Combine(SystemNandPath, "Contents");
  146. break;
  147. }
  148. string fullPath = Path.Combine(GetBasePath(), path);
  149. if (isDirectory)
  150. {
  151. if (!Directory.Exists(fullPath))
  152. {
  153. Directory.CreateDirectory(fullPath);
  154. }
  155. }
  156. return fullPath;
  157. }
  158. public DriveInfo GetDrive()
  159. {
  160. return new DriveInfo(Path.GetPathRoot(GetBasePath()));
  161. }
  162. public void Reload()
  163. {
  164. ReloadKeySet();
  165. LocalFileSystem serverBaseFs = new LocalFileSystem(GetBasePath());
  166. DefaultFsServerObjects fsServerObjects = DefaultFsServerObjects.GetDefaultEmulatedCreators(serverBaseFs, KeySet);
  167. GameCard = fsServerObjects.GameCard;
  168. SdCard = fsServerObjects.SdCard;
  169. SdCard.SetSdCardInsertionStatus(true);
  170. FileSystemServerConfig fsServerConfig = new FileSystemServerConfig
  171. {
  172. FsCreators = fsServerObjects.FsCreators,
  173. DeviceOperator = fsServerObjects.DeviceOperator,
  174. ExternalKeySet = KeySet.ExternalKeySet
  175. };
  176. FsServer = new FileSystemServer(fsServerConfig);
  177. FsClient = FsServer.CreateFileSystemClient();
  178. }
  179. private void ReloadKeySet()
  180. {
  181. string keyFile = null;
  182. string titleKeyFile = null;
  183. string consoleKeyFile = null;
  184. if (!AppDataManager.IsCustomBasePath)
  185. {
  186. LoadSetAtPath(AppDataManager.KeysDirPathAlt);
  187. }
  188. LoadSetAtPath(AppDataManager.KeysDirPath);
  189. void LoadSetAtPath(string basePath)
  190. {
  191. string localKeyFile = Path.Combine(basePath, "prod.keys");
  192. string localTitleKeyFile = Path.Combine(basePath, "title.keys");
  193. string localConsoleKeyFile = Path.Combine(basePath, "console.keys");
  194. if (File.Exists(localKeyFile))
  195. {
  196. keyFile = localKeyFile;
  197. }
  198. if (File.Exists(localTitleKeyFile))
  199. {
  200. titleKeyFile = localTitleKeyFile;
  201. }
  202. if (File.Exists(localConsoleKeyFile))
  203. {
  204. consoleKeyFile = localConsoleKeyFile;
  205. }
  206. }
  207. KeySet = ExternalKeyReader.ReadKeyFile(keyFile, titleKeyFile, consoleKeyFile);
  208. }
  209. public void ImportTickets(IFileSystem fs)
  210. {
  211. foreach (DirectoryEntryEx ticketEntry in fs.EnumerateEntries("/", "*.tik"))
  212. {
  213. Result result = fs.OpenFile(out IFile ticketFile, ticketEntry.FullPath.ToU8Span(), OpenMode.Read);
  214. if (result.IsSuccess())
  215. {
  216. Ticket ticket = new Ticket(ticketFile.AsStream());
  217. KeySet.ExternalKeySet.Add(new RightsId(ticket.RightsId), new AccessKey(ticket.GetTitleKey(KeySet)));
  218. }
  219. }
  220. }
  221. public void Unload()
  222. {
  223. RomFs?.Dispose();
  224. }
  225. public void Dispose()
  226. {
  227. Dispose(true);
  228. }
  229. protected virtual void Dispose(bool disposing)
  230. {
  231. if (disposing)
  232. {
  233. Unload();
  234. }
  235. }
  236. public static VirtualFileSystem CreateInstance()
  237. {
  238. if (_isInitialized)
  239. {
  240. throw new InvalidOperationException($"VirtualFileSystem can only be instantiated once!");
  241. }
  242. _isInitialized = true;
  243. return new VirtualFileSystem();
  244. }
  245. }
  246. }