VirtualFileSystem.cs 10 KB

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