AppDataManager.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using Ryujinx.Common.Logging;
  2. using System;
  3. using System.IO;
  4. namespace Ryujinx.Common.Configuration
  5. {
  6. public static class AppDataManager
  7. {
  8. public const string DefaultBaseDir = "Ryujinx";
  9. public const string DefaultPortableDir = "portable";
  10. // The following 3 are always part of Base Directory
  11. private const string GamesDir = "games";
  12. private const string ProfilesDir = "profiles";
  13. private const string KeysDir = "system";
  14. public enum LaunchMode
  15. {
  16. UserProfile,
  17. Portable,
  18. Custom
  19. }
  20. public static LaunchMode Mode { get; private set; }
  21. public static string BaseDirPath { get; private set; }
  22. public static string GamesDirPath { get; private set; }
  23. public static string ProfilesDirPath { get; private set; }
  24. public static string KeysDirPath { get; private set; }
  25. public static string KeysDirPathUser { get; }
  26. public const string DefaultNandDir = "bis";
  27. public const string DefaultSdcardDir = "sdcard";
  28. private const string DefaultModsDir = "mods";
  29. public static string CustomModsPath { get; set; }
  30. public static string CustomSdModsPath {get; set; }
  31. public static string CustomNandPath { get; set; } // TODO: Actually implement this into VFS
  32. public static string CustomSdCardPath { get; set; } // TODO: Actually implement this into VFS
  33. static AppDataManager()
  34. {
  35. KeysDirPathUser = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".switch");
  36. }
  37. public static void Initialize(string baseDirPath)
  38. {
  39. string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  40. if (appDataPath.Length == 0)
  41. {
  42. appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
  43. }
  44. string userProfilePath = Path.Combine(appDataPath, DefaultBaseDir);
  45. string portablePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DefaultPortableDir);
  46. if (Directory.Exists(portablePath))
  47. {
  48. BaseDirPath = portablePath;
  49. Mode = LaunchMode.Portable;
  50. }
  51. else
  52. {
  53. BaseDirPath = userProfilePath;
  54. Mode = LaunchMode.UserProfile;
  55. }
  56. if (baseDirPath != null && baseDirPath != userProfilePath)
  57. {
  58. if (!Directory.Exists(baseDirPath))
  59. {
  60. Logger.Error?.Print(LogClass.Application, $"Custom Data Directory '{baseDirPath}' does not exist. Falling back to {Mode}...");
  61. }
  62. else
  63. {
  64. BaseDirPath = baseDirPath;
  65. Mode = LaunchMode.Custom;
  66. }
  67. }
  68. BaseDirPath = Path.GetFullPath(BaseDirPath); // convert relative paths
  69. SetupBasePaths();
  70. }
  71. private static void SetupBasePaths()
  72. {
  73. Directory.CreateDirectory(BaseDirPath);
  74. Directory.CreateDirectory(GamesDirPath = Path.Combine(BaseDirPath, GamesDir));
  75. Directory.CreateDirectory(ProfilesDirPath = Path.Combine(BaseDirPath, ProfilesDir));
  76. Directory.CreateDirectory(KeysDirPath = Path.Combine(BaseDirPath, KeysDir));
  77. }
  78. public static string GetModsPath() => CustomModsPath ?? Directory.CreateDirectory(Path.Combine(BaseDirPath, DefaultModsDir)).FullName;
  79. public static string GetSdModsPath() => CustomSdModsPath ?? Directory.CreateDirectory(Path.Combine(BaseDirPath, DefaultSdcardDir, "atmosphere")).FullName;
  80. }
  81. }