AppDataManager.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 CustomNandPath { get; set; } // TODO: Actually implement this into VFS
  31. public static string CustomSdCardPath { get; set; } // TODO: Actually implement this into VFS
  32. static AppDataManager()
  33. {
  34. KeysDirPathUser = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".switch");
  35. }
  36. public static void Initialize(string baseDirPath)
  37. {
  38. string userProfilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), DefaultBaseDir);
  39. string portablePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DefaultPortableDir);
  40. if (Directory.Exists(portablePath))
  41. {
  42. BaseDirPath = portablePath;
  43. Mode = LaunchMode.Portable;
  44. }
  45. else
  46. {
  47. BaseDirPath = userProfilePath;
  48. Mode = LaunchMode.UserProfile;
  49. }
  50. if (baseDirPath != null && baseDirPath != userProfilePath)
  51. {
  52. if (!Directory.Exists(baseDirPath))
  53. {
  54. Logger.Error?.Print(LogClass.Application, $"Custom Data Directory '{baseDirPath}' does not exist. Falling back to {Mode}...");
  55. }
  56. else
  57. {
  58. BaseDirPath = baseDirPath;
  59. Mode = LaunchMode.Custom;
  60. }
  61. }
  62. BaseDirPath = Path.GetFullPath(BaseDirPath); // convert relative paths
  63. SetupBasePaths();
  64. }
  65. private static void SetupBasePaths()
  66. {
  67. Directory.CreateDirectory(BaseDirPath);
  68. Directory.CreateDirectory(GamesDirPath = Path.Combine(BaseDirPath, GamesDir));
  69. Directory.CreateDirectory(ProfilesDirPath = Path.Combine(BaseDirPath, ProfilesDir));
  70. Directory.CreateDirectory(KeysDirPath = Path.Combine(BaseDirPath, KeysDir));
  71. }
  72. public static string GetModsPath() => CustomModsPath ?? Directory.CreateDirectory(Path.Combine(BaseDirPath, DefaultModsDir)).FullName;
  73. }
  74. }