VirtualFileSystem.cs 3.1 KB

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