VirtualFileSystem.cs 8.9 KB

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