VirtualFs.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.IO;
  3. namespace Ryujinx.Core
  4. {
  5. class VirtualFs : IDisposable
  6. {
  7. private const string BasePath = "Fs";
  8. private const string SavesPath = "Saves";
  9. private const string SdCardPath = "SdCard";
  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(1);
  20. }
  21. string FullPath = Path.GetFullPath(Path.Combine(BasePath, FileName));
  22. if (!FullPath.StartsWith(GetBasePath()))
  23. {
  24. return null;
  25. }
  26. return FullPath;
  27. }
  28. public string GetSdCardPath() => MakeDirAndGetFullPath(SdCardPath);
  29. public string GetGameSavesPath() => MakeDirAndGetFullPath(SavesPath);
  30. private string MakeDirAndGetFullPath(string Dir)
  31. {
  32. string FullPath = Path.Combine(GetBasePath(), Dir);
  33. if (!Directory.Exists(FullPath))
  34. {
  35. Directory.CreateDirectory(FullPath);
  36. }
  37. return FullPath;
  38. }
  39. public DriveInfo GetDrive()
  40. {
  41. return new DriveInfo(Path.GetPathRoot(GetBasePath()));
  42. }
  43. public string GetBasePath()
  44. {
  45. return Path.Combine(Directory.GetCurrentDirectory(), BasePath);
  46. }
  47. public void Dispose()
  48. {
  49. Dispose(true);
  50. }
  51. protected virtual void Dispose(bool disposing)
  52. {
  53. if (disposing)
  54. {
  55. RomFs?.Dispose();
  56. }
  57. }
  58. }
  59. }