VirtualFileSystem.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Ryujinx.HLE.HOS;
  2. using System;
  3. using System.IO;
  4. namespace Ryujinx.HLE.FileSystem
  5. {
  6. class VirtualFileSystem : IDisposable
  7. {
  8. public const string BasePath = "RyuFs";
  9. public const string NandPath = "nand";
  10. public const string SdCardPath = "sdmc";
  11. public const string SystemPath = "system";
  12. public static string SystemNandPath = Path.Combine(NandPath, "system");
  13. public static string UserNandPath = Path.Combine(NandPath, "user");
  14. public Stream RomFs { get; private set; }
  15. public void LoadRomFs(string FileName)
  16. {
  17. RomFs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
  18. }
  19. public void SetRomFs(Stream RomfsStream)
  20. {
  21. RomFs?.Close();
  22. RomFs = RomfsStream;
  23. }
  24. public string GetFullPath(string BasePath, string FileName)
  25. {
  26. if (FileName.StartsWith("//"))
  27. {
  28. FileName = FileName.Substring(2);
  29. }
  30. else if (FileName.StartsWith('/'))
  31. {
  32. FileName = FileName.Substring(1);
  33. }
  34. else
  35. {
  36. return null;
  37. }
  38. string FullPath = Path.GetFullPath(Path.Combine(BasePath, FileName));
  39. if (!FullPath.StartsWith(GetBasePath()))
  40. {
  41. return null;
  42. }
  43. return FullPath;
  44. }
  45. public string GetSdCardPath() => MakeDirAndGetFullPath(SdCardPath);
  46. public string GetNandPath() => MakeDirAndGetFullPath(NandPath);
  47. public string GetSystemPath() => MakeDirAndGetFullPath(SystemPath);
  48. public string GetGameSavePath(SaveInfo Save, ServiceCtx Context)
  49. {
  50. return MakeDirAndGetFullPath(SaveHelper.GetSavePath(Save, Context));
  51. }
  52. public string SwitchPathToSystemPath(string SwitchPath)
  53. {
  54. string[] Parts = SwitchPath.Split(":");
  55. if (Parts.Length != 2)
  56. {
  57. return null;
  58. }
  59. return GetFullPath(MakeDirAndGetFullPath(Parts[0]), Parts[1]);
  60. }
  61. public string SystemPathToSwitchPath(string SystemPath)
  62. {
  63. string BaseSystemPath = GetBasePath() + Path.DirectorySeparatorChar;
  64. if (SystemPath.StartsWith(BaseSystemPath))
  65. {
  66. string RawPath = SystemPath.Replace(BaseSystemPath, "");
  67. int FirstSeparatorOffset = RawPath.IndexOf(Path.DirectorySeparatorChar);
  68. if (FirstSeparatorOffset == -1)
  69. {
  70. return $"{RawPath}:/";
  71. }
  72. string BasePath = RawPath.Substring(0, FirstSeparatorOffset);
  73. string FileName = RawPath.Substring(FirstSeparatorOffset + 1);
  74. return $"{BasePath}:/{FileName}";
  75. }
  76. return null;
  77. }
  78. private string MakeDirAndGetFullPath(string Dir)
  79. {
  80. string FullPath = Path.Combine(GetBasePath(), Dir);
  81. if (!Directory.Exists(FullPath))
  82. {
  83. Directory.CreateDirectory(FullPath);
  84. }
  85. return FullPath;
  86. }
  87. public DriveInfo GetDrive()
  88. {
  89. return new DriveInfo(Path.GetPathRoot(GetBasePath()));
  90. }
  91. public string GetBasePath()
  92. {
  93. string AppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  94. return Path.Combine(AppDataPath, BasePath);
  95. }
  96. public void Dispose()
  97. {
  98. Dispose(true);
  99. }
  100. protected virtual void Dispose(bool disposing)
  101. {
  102. if (disposing)
  103. {
  104. RomFs?.Dispose();
  105. }
  106. }
  107. }
  108. }