VirtualFs.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.IO;
  3. namespace Ryujinx.Core
  4. {
  5. class VirtualFs : 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(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(NandPath);
  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. string AppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  46. return Path.Combine(AppDataPath, BasePath);
  47. }
  48. public void Dispose()
  49. {
  50. Dispose(true);
  51. }
  52. protected virtual void Dispose(bool disposing)
  53. {
  54. if (disposing)
  55. {
  56. RomFs?.Dispose();
  57. }
  58. }
  59. }
  60. }