AppDataManager.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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;
  40. if (OperatingSystem.IsMacOS())
  41. {
  42. appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Library", "Application Support");
  43. }
  44. else
  45. {
  46. appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  47. }
  48. if (appDataPath.Length == 0)
  49. {
  50. appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
  51. }
  52. string userProfilePath = Path.Combine(appDataPath, DefaultBaseDir);
  53. string portablePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DefaultPortableDir);
  54. if (Directory.Exists(portablePath))
  55. {
  56. BaseDirPath = portablePath;
  57. Mode = LaunchMode.Portable;
  58. }
  59. else
  60. {
  61. BaseDirPath = userProfilePath;
  62. Mode = LaunchMode.UserProfile;
  63. }
  64. if (baseDirPath != null && baseDirPath != userProfilePath)
  65. {
  66. if (!Directory.Exists(baseDirPath))
  67. {
  68. Logger.Error?.Print(LogClass.Application, $"Custom Data Directory '{baseDirPath}' does not exist. Falling back to {Mode}...");
  69. }
  70. else
  71. {
  72. BaseDirPath = baseDirPath;
  73. Mode = LaunchMode.Custom;
  74. }
  75. }
  76. BaseDirPath = Path.GetFullPath(BaseDirPath); // convert relative paths
  77. // NOTE: Moves the Ryujinx folder in `~/.config` to `~/Library/Application Support` if one is found
  78. // and a Ryujinx folder does not already exist in Application Support.
  79. // Also creates a symlink from `~/.config/Ryujinx` to `~/Library/Application Support/Ryujinx` to preserve backwards compatibility.
  80. // This should be removed in the future.
  81. if (OperatingSystem.IsMacOS() && Mode == LaunchMode.UserProfile)
  82. {
  83. string oldConfigPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), DefaultBaseDir);
  84. if (Path.Exists(oldConfigPath) && !Path.Exists(BaseDirPath))
  85. {
  86. CopyDirectory(oldConfigPath, BaseDirPath);
  87. Directory.Delete(oldConfigPath, true);
  88. Directory.CreateSymbolicLink(oldConfigPath, BaseDirPath);
  89. }
  90. }
  91. SetupBasePaths();
  92. }
  93. private static void SetupBasePaths()
  94. {
  95. Directory.CreateDirectory(BaseDirPath);
  96. Directory.CreateDirectory(GamesDirPath = Path.Combine(BaseDirPath, GamesDir));
  97. Directory.CreateDirectory(ProfilesDirPath = Path.Combine(BaseDirPath, ProfilesDir));
  98. Directory.CreateDirectory(KeysDirPath = Path.Combine(BaseDirPath, KeysDir));
  99. }
  100. private static void CopyDirectory(string sourceDir, string destinationDir)
  101. {
  102. var dir = new DirectoryInfo(sourceDir);
  103. if (!dir.Exists)
  104. {
  105. throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
  106. }
  107. DirectoryInfo[] subDirs = dir.GetDirectories();
  108. Directory.CreateDirectory(destinationDir);
  109. foreach (FileInfo file in dir.GetFiles())
  110. {
  111. if (file.Name == ".DS_Store")
  112. {
  113. continue;
  114. }
  115. file.CopyTo(Path.Combine(destinationDir, file.Name));
  116. }
  117. foreach (DirectoryInfo subDir in subDirs)
  118. {
  119. CopyDirectory(subDir.FullName, Path.Combine(destinationDir, subDir.Name));
  120. }
  121. }
  122. public static string GetModsPath() => CustomModsPath ?? Directory.CreateDirectory(Path.Combine(BaseDirPath, DefaultModsDir)).FullName;
  123. public static string GetSdModsPath() => CustomSdModsPath ?? Directory.CreateDirectory(Path.Combine(BaseDirPath, DefaultSdcardDir, "atmosphere")).FullName;
  124. }
  125. }