VirtualFileSystem.cs 9.6 KB

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