VirtualFileSystem.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 string GetFullPath(string BasePath, string FileName)
  17. {
  18. if (FileName.StartsWith("//"))
  19. {
  20. FileName = FileName.Substring(2);
  21. }
  22. else if (FileName.StartsWith('/'))
  23. {
  24. FileName = FileName.Substring(1);
  25. }
  26. else
  27. {
  28. return null;
  29. }
  30. string FullPath = Path.GetFullPath(Path.Combine(BasePath, FileName));
  31. if (!FullPath.StartsWith(GetBasePath()))
  32. {
  33. return null;
  34. }
  35. return FullPath;
  36. }
  37. public string GetSdCardPath() => MakeDirAndGetFullPath(SdCardPath);
  38. public string GetGameSavesPath() => MakeDirAndGetFullPath(NandPath);
  39. public string GetSystemPath() => MakeDirAndGetFullPath(SystemPath);
  40. public string SwitchPathToSystemPath(string SwitchPath)
  41. {
  42. string[] Parts = SwitchPath.Split(":");
  43. if (Parts.Length != 2)
  44. {
  45. return null;
  46. }
  47. return GetFullPath(MakeDirAndGetFullPath(Parts[0]), Parts[1]);
  48. }
  49. public string SystemPathToSwitchPath(string SystemPath)
  50. {
  51. string BaseSystemPath = GetBasePath() + Path.DirectorySeparatorChar;
  52. if (SystemPath.StartsWith(BaseSystemPath))
  53. {
  54. string RawPath = SystemPath.Replace(BaseSystemPath, "");
  55. int FirstSeparatorOffset = RawPath.IndexOf(Path.DirectorySeparatorChar);
  56. if (FirstSeparatorOffset == -1)
  57. {
  58. return $"{RawPath}:/";
  59. }
  60. string BasePath = RawPath.Substring(0, FirstSeparatorOffset);
  61. string FileName = RawPath.Substring(FirstSeparatorOffset + 1);
  62. return $"{BasePath}:/{FileName}";
  63. }
  64. return null;
  65. }
  66. private string MakeDirAndGetFullPath(string Dir)
  67. {
  68. string FullPath = Path.Combine(GetBasePath(), Dir);
  69. if (!Directory.Exists(FullPath))
  70. {
  71. Directory.CreateDirectory(FullPath);
  72. }
  73. return FullPath;
  74. }
  75. public DriveInfo GetDrive()
  76. {
  77. return new DriveInfo(Path.GetPathRoot(GetBasePath()));
  78. }
  79. public string GetBasePath()
  80. {
  81. string AppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  82. return Path.Combine(AppDataPath, BasePath);
  83. }
  84. public void Dispose()
  85. {
  86. Dispose(true);
  87. }
  88. protected virtual void Dispose(bool disposing)
  89. {
  90. if (disposing)
  91. {
  92. RomFs?.Dispose();
  93. }
  94. }
  95. }
  96. }