VirtualFileSystem.cs 9.6 KB

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