AppDataManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Common.Utilities;
  3. using System;
  4. using System.IO;
  5. using System.Runtime.Versioning;
  6. namespace Ryujinx.Common.Configuration
  7. {
  8. public static class AppDataManager
  9. {
  10. private const string DefaultBaseDir = "Ryujinx";
  11. private const string DefaultPortableDir = "portable";
  12. // The following 3 are always part of Base Directory
  13. private const string GamesDir = "games";
  14. private const string ProfilesDir = "profiles";
  15. private const string KeysDir = "system";
  16. public enum LaunchMode
  17. {
  18. UserProfile,
  19. Portable,
  20. Custom,
  21. }
  22. public static LaunchMode Mode { get; private set; }
  23. public static string BaseDirPath { get; private set; }
  24. public static string GamesDirPath { get; private set; }
  25. public static string ProfilesDirPath { get; private set; }
  26. public static string KeysDirPath { get; private set; }
  27. public static string KeysDirPathUser { get; }
  28. public static string LogsDirPath { get; private set; }
  29. public const string DefaultNandDir = "bis";
  30. public const string DefaultSdcardDir = "sdcard";
  31. private const string DefaultModsDir = "mods";
  32. public static string CustomModsPath { get; set; }
  33. public static string CustomSdModsPath { get; set; }
  34. public static string CustomNandPath { get; set; } // TODO: Actually implement this into VFS
  35. public static string CustomSdCardPath { get; set; } // TODO: Actually implement this into VFS
  36. static AppDataManager()
  37. {
  38. KeysDirPathUser = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".switch");
  39. }
  40. public static void Initialize(string baseDirPath)
  41. {
  42. string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  43. if (appDataPath.Length == 0)
  44. {
  45. appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
  46. }
  47. string userProfilePath = Path.Combine(appDataPath, DefaultBaseDir);
  48. string portablePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DefaultPortableDir);
  49. // On macOS, check for a portable directory next to the app bundle as well.
  50. if (OperatingSystem.IsMacOS() && !Directory.Exists(portablePath))
  51. {
  52. string bundlePath = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", ".."));
  53. // Make sure we're actually running within an app bundle.
  54. if (bundlePath.EndsWith(".app"))
  55. {
  56. portablePath = Path.GetFullPath(Path.Combine(bundlePath, "..", DefaultPortableDir));
  57. }
  58. }
  59. if (Directory.Exists(portablePath))
  60. {
  61. BaseDirPath = portablePath;
  62. Mode = LaunchMode.Portable;
  63. }
  64. else
  65. {
  66. BaseDirPath = userProfilePath;
  67. Mode = LaunchMode.UserProfile;
  68. }
  69. if (baseDirPath != null && baseDirPath != userProfilePath)
  70. {
  71. if (!Directory.Exists(baseDirPath))
  72. {
  73. Logger.Error?.Print(LogClass.Application, $"Custom Data Directory '{baseDirPath}' does not exist. Falling back to {Mode}...");
  74. }
  75. else
  76. {
  77. BaseDirPath = baseDirPath;
  78. Mode = LaunchMode.Custom;
  79. }
  80. }
  81. BaseDirPath = Path.GetFullPath(BaseDirPath); // convert relative paths
  82. if (IsPathSymlink(BaseDirPath))
  83. {
  84. Logger.Warning?.Print(LogClass.Application, $"Application data directory is a symlink. This may be unintended.");
  85. }
  86. SetupBasePaths();
  87. }
  88. public static string GetOrCreateLogsDir()
  89. {
  90. if (Directory.Exists(LogsDirPath))
  91. {
  92. return LogsDirPath;
  93. }
  94. Logger.Notice.Print(LogClass.Application, "Logging directory not found; attempting to create new logging directory.");
  95. LogsDirPath = SetUpLogsDir();
  96. return LogsDirPath;
  97. }
  98. private static string SetUpLogsDir()
  99. {
  100. string logDir = "";
  101. if (Mode == LaunchMode.Portable)
  102. {
  103. logDir = Path.Combine(BaseDirPath, "Logs");
  104. try
  105. {
  106. Directory.CreateDirectory(logDir);
  107. }
  108. catch
  109. {
  110. Logger.Warning?.Print(LogClass.Application, $"Logging directory could not be created '{logDir}'");
  111. return null;
  112. }
  113. }
  114. else
  115. {
  116. if (OperatingSystem.IsMacOS())
  117. {
  118. // NOTE: Should evaluate to "~/Library/Logs/Ryujinx/".
  119. logDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library", "Logs", DefaultBaseDir);
  120. try
  121. {
  122. Directory.CreateDirectory(logDir);
  123. }
  124. catch
  125. {
  126. Logger.Warning?.Print(LogClass.Application, $"Logging directory could not be created '{logDir}'");
  127. logDir = "";
  128. }
  129. if (string.IsNullOrEmpty(logDir))
  130. {
  131. // NOTE: Should evaluate to "~/Library/Application Support/Ryujinx/Logs".
  132. logDir = Path.Combine(BaseDirPath, "Logs");
  133. try
  134. {
  135. Directory.CreateDirectory(logDir);
  136. }
  137. catch
  138. {
  139. Logger.Warning?.Print(LogClass.Application, $"Logging directory could not be created '{logDir}'");
  140. return null;
  141. }
  142. }
  143. }
  144. else if (OperatingSystem.IsWindows())
  145. {
  146. // NOTE: Should evaluate to a "Logs" directory in whatever directory Ryujinx was launched from.
  147. logDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
  148. try
  149. {
  150. Directory.CreateDirectory(logDir);
  151. }
  152. catch
  153. {
  154. Logger.Warning?.Print(LogClass.Application, $"Logging directory could not be created '{logDir}'");
  155. logDir = "";
  156. }
  157. if (string.IsNullOrEmpty(logDir))
  158. {
  159. // NOTE: Should evaluate to "C:\Users\user\AppData\Roaming\Ryujinx\Logs".
  160. logDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), DefaultBaseDir, "Logs");
  161. try
  162. {
  163. Directory.CreateDirectory(logDir);
  164. }
  165. catch
  166. {
  167. Logger.Warning?.Print(LogClass.Application, $"Logging directory could not be created '{logDir}'");
  168. return null;
  169. }
  170. }
  171. }
  172. else if (OperatingSystem.IsLinux())
  173. {
  174. // NOTE: Should evaluate to "~/.config/Ryujinx/Logs".
  175. logDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), DefaultBaseDir, "Logs");
  176. try
  177. {
  178. Directory.CreateDirectory(logDir);
  179. }
  180. catch
  181. {
  182. Logger.Warning?.Print(LogClass.Application, $"Logging directory could not be created '{logDir}'");
  183. return null;
  184. }
  185. }
  186. }
  187. return logDir;
  188. }
  189. private static void SetupBasePaths()
  190. {
  191. Directory.CreateDirectory(BaseDirPath);
  192. LogsDirPath = SetUpLogsDir();
  193. Directory.CreateDirectory(GamesDirPath = Path.Combine(BaseDirPath, GamesDir));
  194. Directory.CreateDirectory(ProfilesDirPath = Path.Combine(BaseDirPath, ProfilesDir));
  195. Directory.CreateDirectory(KeysDirPath = Path.Combine(BaseDirPath, KeysDir));
  196. }
  197. // Check if existing old baseDirPath is a symlink, to prevent possible errors.
  198. // Should be removed, when the existence of the old directory isn't checked anymore.
  199. private static bool IsPathSymlink(string path)
  200. {
  201. try
  202. {
  203. FileAttributes attributes = File.GetAttributes(path);
  204. return (attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint;
  205. }
  206. catch
  207. {
  208. return false;
  209. }
  210. }
  211. [SupportedOSPlatform("macos")]
  212. public static void FixMacOSConfigurationFolders()
  213. {
  214. string oldConfigPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
  215. ".config", DefaultBaseDir);
  216. if (Path.Exists(oldConfigPath) && !IsPathSymlink(oldConfigPath) && !Path.Exists(BaseDirPath))
  217. {
  218. FileSystemUtils.MoveDirectory(oldConfigPath, BaseDirPath);
  219. Directory.CreateSymbolicLink(oldConfigPath, BaseDirPath);
  220. }
  221. string correctApplicationDataDirectoryPath =
  222. Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), DefaultBaseDir);
  223. if (IsPathSymlink(correctApplicationDataDirectoryPath))
  224. {
  225. //copy the files somewhere temporarily
  226. string tempPath = Path.Combine(Path.GetTempPath(), DefaultBaseDir);
  227. try
  228. {
  229. FileSystemUtils.CopyDirectory(correctApplicationDataDirectoryPath, tempPath, true);
  230. }
  231. catch (Exception exception)
  232. {
  233. Logger.Error?.Print(LogClass.Application,
  234. $"Critical error copying Ryujinx application data into the temp folder. {exception}");
  235. try
  236. {
  237. FileSystemInfo resolvedDirectoryInfo =
  238. Directory.ResolveLinkTarget(correctApplicationDataDirectoryPath, true);
  239. string resolvedPath = resolvedDirectoryInfo.FullName;
  240. Logger.Error?.Print(LogClass.Application, $"Please manually move your Ryujinx data from {resolvedPath} to {correctApplicationDataDirectoryPath}, and remove the symlink.");
  241. }
  242. catch (Exception symlinkException)
  243. {
  244. Logger.Error?.Print(LogClass.Application, $"Unable to resolve the symlink for Ryujinx application data: {symlinkException}. Follow the symlink at {correctApplicationDataDirectoryPath} and move your data back to the Application Support folder.");
  245. }
  246. return;
  247. }
  248. //delete the symlink
  249. try
  250. {
  251. //This will fail if this is an actual directory, so there is no way we can actually delete user data here.
  252. File.Delete(correctApplicationDataDirectoryPath);
  253. }
  254. catch (Exception exception)
  255. {
  256. Logger.Error?.Print(LogClass.Application,
  257. $"Critical error deleting the Ryujinx application data folder symlink at {correctApplicationDataDirectoryPath}. {exception}");
  258. try
  259. {
  260. FileSystemInfo resolvedDirectoryInfo =
  261. Directory.ResolveLinkTarget(correctApplicationDataDirectoryPath, true);
  262. string resolvedPath = resolvedDirectoryInfo.FullName;
  263. Logger.Error?.Print(LogClass.Application, $"Please manually move your Ryujinx data from {resolvedPath} to {correctApplicationDataDirectoryPath}, and remove the symlink.");
  264. }
  265. catch (Exception symlinkException)
  266. {
  267. Logger.Error?.Print(LogClass.Application, $"Unable to resolve the symlink for Ryujinx application data: {symlinkException}. Follow the symlink at {correctApplicationDataDirectoryPath} and move your data back to the Application Support folder.");
  268. }
  269. return;
  270. }
  271. //put the files back
  272. try
  273. {
  274. FileSystemUtils.CopyDirectory(tempPath, correctApplicationDataDirectoryPath, true);
  275. }
  276. catch (Exception exception)
  277. {
  278. Logger.Error?.Print(LogClass.Application,
  279. $"Critical error copying Ryujinx application data into the correct location. {exception}. Please manually move your application data from {tempPath} to {correctApplicationDataDirectoryPath}.");
  280. }
  281. }
  282. }
  283. public static string GetModsPath() => CustomModsPath ?? Directory.CreateDirectory(Path.Combine(BaseDirPath, DefaultModsDir)).FullName;
  284. public static string GetSdModsPath() => CustomSdModsPath ?? Directory.CreateDirectory(Path.Combine(BaseDirPath, DefaultSdcardDir, "atmosphere")).FullName;
  285. }
  286. }