Program.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using Avalonia;
  2. using Avalonia.Threading;
  3. using Ryujinx.Ava.UI.Helpers;
  4. using Ryujinx.Ava.UI.Windows;
  5. using Ryujinx.Common;
  6. using Ryujinx.Common.Configuration;
  7. using Ryujinx.Common.GraphicsDriver;
  8. using Ryujinx.Common.Logging;
  9. using Ryujinx.Common.SystemInfo;
  10. using Ryujinx.Common.SystemInterop;
  11. using Ryujinx.Modules;
  12. using Ryujinx.SDL2.Common;
  13. using Ryujinx.Ui.Common;
  14. using Ryujinx.Ui.Common.Configuration;
  15. using Ryujinx.Ui.Common.Helper;
  16. using System;
  17. using System.Diagnostics;
  18. using System.IO;
  19. using System.Runtime.InteropServices;
  20. using System.Runtime.Versioning;
  21. using System.Threading.Tasks;
  22. namespace Ryujinx.Ava
  23. {
  24. internal partial class Program
  25. {
  26. public static double WindowScaleFactor { get; set; }
  27. public static double DesktopScaleFactor { get; set; } = 1.0;
  28. public static string Version { get; private set; }
  29. public static string ConfigurationPath { get; private set; }
  30. public static bool PreviewerDetached { get; private set; }
  31. [LibraryImport("user32.dll", SetLastError = true)]
  32. public static partial int MessageBoxA(IntPtr hWnd, [MarshalAs(UnmanagedType.LPStr)] string text, [MarshalAs(UnmanagedType.LPStr)] string caption, uint type);
  33. private const uint MB_ICONWARNING = 0x30;
  34. [SupportedOSPlatform("linux")]
  35. static void RegisterMimeTypes()
  36. {
  37. if (ReleaseInformation.IsFlatHubBuild())
  38. {
  39. return;
  40. }
  41. string mimeDbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".local", "share", "mime");
  42. if (!File.Exists(Path.Combine(mimeDbPath, "packages", "Ryujinx.xml")))
  43. {
  44. string mimeTypesFile = Path.Combine(ReleaseInformation.GetBaseApplicationDirectory(), "mime", "Ryujinx.xml");
  45. using Process mimeProcess = new();
  46. mimeProcess.StartInfo.FileName = "xdg-mime";
  47. mimeProcess.StartInfo.Arguments = $"install --novendor --mode user {mimeTypesFile}";
  48. mimeProcess.Start();
  49. mimeProcess.WaitForExit();
  50. if (mimeProcess.ExitCode != 0)
  51. {
  52. Logger.Error?.PrintMsg(LogClass.Application, $"Unable to install mime types. Make sure xdg-utils is installed. Process exited with code: {mimeProcess.ExitCode}");
  53. return;
  54. }
  55. using Process updateMimeProcess = new();
  56. updateMimeProcess.StartInfo.FileName = "update-mime-database";
  57. updateMimeProcess.StartInfo.Arguments = mimeDbPath;
  58. updateMimeProcess.Start();
  59. updateMimeProcess.WaitForExit();
  60. if (updateMimeProcess.ExitCode != 0)
  61. {
  62. Logger.Error?.PrintMsg(LogClass.Application, $"Could not update local mime database. Process exited with code: {updateMimeProcess.ExitCode}");
  63. }
  64. }
  65. }
  66. public static void Main(string[] args)
  67. {
  68. Version = ReleaseInformation.GetVersion();
  69. if (OperatingSystem.IsWindows() && !OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17134))
  70. {
  71. _ = MessageBoxA(IntPtr.Zero, "You are running an outdated version of Windows.\n\nStarting on June 1st 2022, Ryujinx will only support Windows 10 1803 and newer.\n", $"Ryujinx {Version}", MB_ICONWARNING);
  72. }
  73. PreviewerDetached = true;
  74. Initialize(args);
  75. LoggerAdapter.Register();
  76. BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
  77. }
  78. public static AppBuilder BuildAvaloniaApp()
  79. {
  80. return AppBuilder.Configure<App>()
  81. .UsePlatformDetect()
  82. .With(new X11PlatformOptions
  83. {
  84. EnableMultiTouch = true,
  85. EnableIme = true,
  86. UseEGL = false,
  87. UseGpu = true
  88. })
  89. .With(new Win32PlatformOptions
  90. {
  91. EnableMultitouch = true,
  92. UseWgl = false,
  93. AllowEglInitialization = false,
  94. CompositionBackdropCornerRadius = 8.0f,
  95. })
  96. .UseSkia();
  97. }
  98. private static void Initialize(string[] args)
  99. {
  100. // Parse arguments
  101. CommandLineState.ParseArguments(args);
  102. // Delete backup files after updating.
  103. Task.Run(Updater.CleanupUpdate);
  104. Console.Title = $"Ryujinx Console {Version}";
  105. // Hook unhandled exception and process exit events.
  106. AppDomain.CurrentDomain.UnhandledException += (sender, e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  107. AppDomain.CurrentDomain.ProcessExit += (sender, e) => Exit();
  108. // Setup base data directory.
  109. AppDataManager.Initialize(CommandLineState.BaseDirPathArg);
  110. // Initialize the configuration.
  111. ConfigurationState.Initialize();
  112. // Initialize the logger system.
  113. LoggerModule.Initialize();
  114. // Register mime types on linux.
  115. if (OperatingSystem.IsLinux())
  116. {
  117. RegisterMimeTypes();
  118. }
  119. // Initialize Discord integration.
  120. DiscordIntegrationModule.Initialize();
  121. // Initialize SDL2 driver
  122. SDL2Driver.MainThreadDispatcher = action => Dispatcher.UIThread.InvokeAsync(action, DispatcherPriority.Input);
  123. ReloadConfig();
  124. ForceDpiAware.Windows();
  125. WindowScaleFactor = ForceDpiAware.GetWindowScaleFactor();
  126. // Logging system information.
  127. PrintSystemInfo();
  128. // Enable OGL multithreading on the driver, when available.
  129. DriverUtilities.ToggleOGLThreading(ConfigurationState.Instance.Graphics.BackendThreading == BackendThreading.Off);
  130. // Check if keys exists.
  131. if (!File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys")))
  132. {
  133. if (!(AppDataManager.Mode == AppDataManager.LaunchMode.UserProfile && File.Exists(Path.Combine(AppDataManager.KeysDirPathUser, "prod.keys"))))
  134. {
  135. MainWindow.ShowKeyErrorOnLoad = true;
  136. }
  137. }
  138. if (CommandLineState.LaunchPathArg != null)
  139. {
  140. MainWindow.DeferLoadApplication(CommandLineState.LaunchPathArg, CommandLineState.StartFullscreenArg);
  141. }
  142. }
  143. public static void ReloadConfig()
  144. {
  145. string localConfigurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
  146. string appDataConfigurationPath = Path.Combine(AppDataManager.BaseDirPath, "Config.json");
  147. // Now load the configuration as the other subsystems are now registered
  148. if (File.Exists(localConfigurationPath))
  149. {
  150. ConfigurationPath = localConfigurationPath;
  151. }
  152. else if (File.Exists(appDataConfigurationPath))
  153. {
  154. ConfigurationPath = appDataConfigurationPath;
  155. }
  156. if (ConfigurationPath == null)
  157. {
  158. // No configuration, we load the default values and save it to disk
  159. ConfigurationPath = appDataConfigurationPath;
  160. ConfigurationState.Instance.LoadDefault();
  161. ConfigurationState.Instance.ToFileFormat().SaveConfig(ConfigurationPath);
  162. }
  163. else
  164. {
  165. if (ConfigurationFileFormat.TryLoad(ConfigurationPath, out ConfigurationFileFormat configurationFileFormat))
  166. {
  167. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  168. }
  169. else
  170. {
  171. ConfigurationState.Instance.LoadDefault();
  172. Logger.Warning?.PrintMsg(LogClass.Application, $"Failed to load config! Loading the default config instead.\nFailed config location {ConfigurationPath}");
  173. }
  174. }
  175. // Check if graphics backend was overridden
  176. if (CommandLineState.OverrideGraphicsBackend != null)
  177. {
  178. if (CommandLineState.OverrideGraphicsBackend.ToLower() == "opengl")
  179. {
  180. ConfigurationState.Instance.Graphics.GraphicsBackend.Value = GraphicsBackend.OpenGl;
  181. }
  182. else if (CommandLineState.OverrideGraphicsBackend.ToLower() == "vulkan")
  183. {
  184. ConfigurationState.Instance.Graphics.GraphicsBackend.Value = GraphicsBackend.Vulkan;
  185. }
  186. }
  187. // Check if docked mode was overriden.
  188. if (CommandLineState.OverrideDockedMode.HasValue)
  189. {
  190. ConfigurationState.Instance.System.EnableDockedMode.Value = CommandLineState.OverrideDockedMode.Value;
  191. }
  192. }
  193. private static void PrintSystemInfo()
  194. {
  195. Logger.Notice.Print(LogClass.Application, $"Ryujinx Version: {Version}");
  196. SystemInfo.Gather().Print();
  197. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {(Logger.GetEnabledLevels().Count == 0 ? "<None>" : string.Join(", ", Logger.GetEnabledLevels()))}");
  198. if (AppDataManager.Mode == AppDataManager.LaunchMode.Custom)
  199. {
  200. Logger.Notice.Print(LogClass.Application, $"Launch Mode: Custom Path {AppDataManager.BaseDirPath}");
  201. }
  202. else
  203. {
  204. Logger.Notice.Print(LogClass.Application, $"Launch Mode: {AppDataManager.Mode}");
  205. }
  206. }
  207. private static void ProcessUnhandledException(Exception ex, bool isTerminating)
  208. {
  209. string message = $"Unhandled exception caught: {ex}";
  210. Logger.Error?.PrintMsg(LogClass.Application, message);
  211. if (Logger.Error == null)
  212. {
  213. Logger.Notice.PrintMsg(LogClass.Application, message);
  214. }
  215. if (isTerminating)
  216. {
  217. Exit();
  218. }
  219. }
  220. public static void Exit()
  221. {
  222. DiscordIntegrationModule.Exit();
  223. Logger.Shutdown();
  224. }
  225. }
  226. }