VirtualFileSystem.cs 3.4 KB

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