VirtualFileSystem.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.IO;
  3. namespace Ryujinx.Core
  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. private string MakeDirAndGetFullPath(string Dir)
  39. {
  40. string FullPath = Path.Combine(GetBasePath(), Dir);
  41. if (!Directory.Exists(FullPath))
  42. {
  43. Directory.CreateDirectory(FullPath);
  44. }
  45. return FullPath;
  46. }
  47. public DriveInfo GetDrive()
  48. {
  49. return new DriveInfo(Path.GetPathRoot(GetBasePath()));
  50. }
  51. public string GetBasePath()
  52. {
  53. string AppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  54. return Path.Combine(AppDataPath, BasePath);
  55. }
  56. public void Dispose()
  57. {
  58. Dispose(true);
  59. }
  60. protected virtual void Dispose(bool disposing)
  61. {
  62. if (disposing)
  63. {
  64. RomFs?.Dispose();
  65. }
  66. }
  67. }
  68. }