VirtualFileSystem.cs 9.0 KB

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