Program.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using Avalonia;
  2. using Avalonia.Threading;
  3. using DiscordRPC;
  4. using Gommon;
  5. using Projektanker.Icons.Avalonia;
  6. using Projektanker.Icons.Avalonia.FontAwesome;
  7. using Projektanker.Icons.Avalonia.MaterialDesign;
  8. using Ryujinx.Ava.Common.Locale;
  9. using Ryujinx.Ava.UI.Helpers;
  10. using Ryujinx.Ava.UI.Windows;
  11. using Ryujinx.Ava.Utilities;
  12. using Ryujinx.Ava.Utilities.AppLibrary;
  13. using Ryujinx.Ava.Utilities.Configuration;
  14. using Ryujinx.Ava.Utilities.SystemInfo;
  15. using Ryujinx.Common;
  16. using Ryujinx.Common.Configuration;
  17. using Ryujinx.Common.GraphicsDriver;
  18. using Ryujinx.Common.Logging;
  19. using Ryujinx.Common.SystemInterop;
  20. using Ryujinx.Graphics.Vulkan.MoltenVK;
  21. using Ryujinx.Headless;
  22. using Ryujinx.SDL2.Common;
  23. using System;
  24. using System.Collections.Generic;
  25. using System.IO;
  26. using System.Linq;
  27. using System.Runtime.InteropServices;
  28. using System.Threading.Tasks;
  29. namespace Ryujinx.Ava
  30. {
  31. internal partial class Program
  32. {
  33. public static double WindowScaleFactor { get; set; }
  34. public static double DesktopScaleFactor { get; set; } = 1.0;
  35. public static string Version { get; private set; }
  36. public static string ConfigurationPath { get; private set; }
  37. public static bool PreviewerDetached { get; private set; }
  38. public static bool UseHardwareAcceleration { get; private set; }
  39. [LibraryImport("user32.dll", SetLastError = true)]
  40. public static partial int MessageBoxA(nint hWnd, [MarshalAs(UnmanagedType.LPStr)] string text, [MarshalAs(UnmanagedType.LPStr)] string caption, uint type);
  41. private const uint MbIconwarning = 0x30;
  42. public static int Main(string[] args)
  43. {
  44. Version = ReleaseInformation.Version;
  45. if (OperatingSystem.IsWindows() && !OperatingSystem.IsWindowsVersionAtLeast(10, 0, 19041))
  46. {
  47. _ = MessageBoxA(nint.Zero, "You are running an outdated version of Windows.\n\nRyujinx supports Windows 10 version 20H1 and newer.\n", $"Ryujinx {Version}", MbIconwarning);
  48. }
  49. PreviewerDetached = true;
  50. if (args.Length > 0 && args[0] is "--no-gui" or "nogui")
  51. {
  52. HeadlessRyujinx.Entrypoint(args[1..]);
  53. return 0;
  54. }
  55. Initialize(args);
  56. LoggerAdapter.Register();
  57. IconProvider.Current
  58. .Register<FontAwesomeIconProvider>()
  59. .Register<MaterialDesignIconProvider>();
  60. return BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
  61. }
  62. public static AppBuilder BuildAvaloniaApp() =>
  63. AppBuilder.Configure<RyujinxApp>()
  64. .UsePlatformDetect()
  65. .With(new X11PlatformOptions
  66. {
  67. EnableMultiTouch = true,
  68. EnableIme = true,
  69. EnableInputFocusProxy = Environment.GetEnvironmentVariable("XDG_CURRENT_DESKTOP") == "gamescope",
  70. RenderingMode = UseHardwareAcceleration
  71. ? [X11RenderingMode.Glx, X11RenderingMode.Software]
  72. : [X11RenderingMode.Software]
  73. })
  74. .With(new Win32PlatformOptions
  75. {
  76. WinUICompositionBackdropCornerRadius = 8.0f,
  77. RenderingMode = UseHardwareAcceleration
  78. ? [Win32RenderingMode.AngleEgl, Win32RenderingMode.Software]
  79. : [Win32RenderingMode.Software]
  80. });
  81. private static void Initialize(string[] args)
  82. {
  83. // Ensure Discord presence timestamp begins at the absolute start of when Ryujinx is launched
  84. DiscordIntegrationModule.EmulatorStartedAt = Timestamps.Now;
  85. // Parse arguments
  86. CommandLineState.ParseArguments(args);
  87. if (OperatingSystem.IsMacOS())
  88. {
  89. MVKInitialization.InitializeResolver();
  90. }
  91. // Delete backup files after updating.
  92. Task.Run(Updater.CleanupUpdate);
  93. Console.Title = $"{RyujinxApp.FullAppName} Console {Version}";
  94. // Hook unhandled exception and process exit events.
  95. AppDomain.CurrentDomain.UnhandledException += (sender, e)
  96. => ProcessUnhandledException(sender, e.ExceptionObject as Exception, e.IsTerminating);
  97. TaskScheduler.UnobservedTaskException += (sender, e)
  98. => ProcessUnhandledException(sender, e.Exception, false);
  99. AppDomain.CurrentDomain.ProcessExit += (_, _) => Exit();
  100. // Setup base data directory.
  101. AppDataManager.Initialize(CommandLineState.BaseDirPathArg);
  102. // Initialize the configuration.
  103. ConfigurationState.Initialize();
  104. // Initialize the logger system.
  105. LoggerModule.Initialize();
  106. // Initialize Discord integration.
  107. DiscordIntegrationModule.Initialize();
  108. // Initialize SDL2 driver
  109. SDL2Driver.MainThreadDispatcher = action => Dispatcher.UIThread.InvokeAsync(action, DispatcherPriority.Input);
  110. ReloadConfig();
  111. WindowScaleFactor = ForceDpiAware.GetWindowScaleFactor();
  112. // Logging system information.
  113. PrintSystemInfo();
  114. // Enable OGL multithreading on the driver, and some other flags.
  115. DriverUtilities.InitDriverConfig(ConfigurationState.Instance.Graphics.BackendThreading == BackendThreading.Off);
  116. // Check if keys exists.
  117. if (!File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys")))
  118. {
  119. if (!(AppDataManager.Mode == AppDataManager.LaunchMode.UserProfile && File.Exists(Path.Combine(AppDataManager.KeysDirPathUser, "prod.keys"))))
  120. {
  121. MainWindow.ShowKeyErrorOnLoad = true;
  122. }
  123. }
  124. if (CommandLineState.LaunchPathArg != null)
  125. {
  126. MainWindow.DeferLoadApplication(CommandLineState.LaunchPathArg, CommandLineState.LaunchApplicationId, CommandLineState.StartFullscreenArg);
  127. }
  128. }
  129. public static void ReloadConfig()
  130. {
  131. string localConfigurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ReleaseInformation.ConfigName);
  132. string appDataConfigurationPath = Path.Combine(AppDataManager.BaseDirPath, ReleaseInformation.ConfigName);
  133. // Now load the configuration as the other subsystems are now registered
  134. if (File.Exists(localConfigurationPath))
  135. {
  136. ConfigurationPath = localConfigurationPath;
  137. }
  138. else if (File.Exists(appDataConfigurationPath))
  139. {
  140. ConfigurationPath = appDataConfigurationPath;
  141. }
  142. if (ConfigurationPath == null)
  143. {
  144. // No configuration, we load the default values and save it to disk
  145. ConfigurationPath = appDataConfigurationPath;
  146. Logger.Notice.Print(LogClass.Application, $"No configuration file found. Saving default configuration to: {ConfigurationPath}");
  147. ConfigurationState.Instance.LoadDefault();
  148. ConfigurationState.Instance.ToFileFormat().SaveConfig(ConfigurationPath);
  149. }
  150. else
  151. {
  152. Logger.Notice.Print(LogClass.Application, $"Loading configuration from: {ConfigurationPath}");
  153. if (ConfigurationFileFormat.TryLoad(ConfigurationPath, out ConfigurationFileFormat configurationFileFormat))
  154. {
  155. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  156. }
  157. else
  158. {
  159. Logger.Warning?.PrintMsg(LogClass.Application, $"Failed to load config! Loading the default config instead.\nFailed config location: {ConfigurationPath}");
  160. ConfigurationState.Instance.LoadDefault();
  161. }
  162. }
  163. UseHardwareAcceleration = ConfigurationState.Instance.EnableHardwareAcceleration;
  164. // Check if graphics backend was overridden
  165. if (CommandLineState.OverrideGraphicsBackend is not null)
  166. ConfigurationState.Instance.Graphics.GraphicsBackend.Value = CommandLineState.OverrideGraphicsBackend.ToLower() switch
  167. {
  168. "opengl" => GraphicsBackend.OpenGl,
  169. "vulkan" => GraphicsBackend.Vulkan,
  170. "metal" => GraphicsBackend.Metal,
  171. _ => ConfigurationState.Instance.Graphics.GraphicsBackend
  172. };
  173. // Check if backend threading was overridden
  174. if (CommandLineState.OverrideBackendThreading is not null)
  175. ConfigurationState.Instance.Graphics.BackendThreading.Value = CommandLineState.OverrideBackendThreading.ToLower() switch
  176. {
  177. "auto" => BackendThreading.Auto,
  178. "off" => BackendThreading.Off,
  179. "on" => BackendThreading.On,
  180. _ => ConfigurationState.Instance.Graphics.BackendThreading
  181. };
  182. // Check if docked mode was overriden.
  183. if (CommandLineState.OverrideDockedMode.HasValue)
  184. ConfigurationState.Instance.System.EnableDockedMode.Value = CommandLineState.OverrideDockedMode.Value;
  185. // Check if HideCursor was overridden.
  186. if (CommandLineState.OverrideHideCursor is not null)
  187. ConfigurationState.Instance.HideCursor.Value = CommandLineState.OverrideHideCursor.ToLower() switch
  188. {
  189. "never" => HideCursorMode.Never,
  190. "onidle" => HideCursorMode.OnIdle,
  191. "always" => HideCursorMode.Always,
  192. _ => ConfigurationState.Instance.HideCursor,
  193. };
  194. // Check if hardware-acceleration was overridden.
  195. if (CommandLineState.OverrideHardwareAcceleration != null)
  196. UseHardwareAcceleration = CommandLineState.OverrideHardwareAcceleration.Value;
  197. }
  198. internal static void PrintSystemInfo()
  199. {
  200. Logger.Notice.Print(LogClass.Application, $"{RyujinxApp.FullAppName} Version: {Version}");
  201. Logger.Notice.Print(LogClass.Application, $".NET Runtime: {RuntimeInformation.FrameworkDescription}");
  202. SystemInfo.Gather().Print();
  203. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {
  204. Logger.GetEnabledLevels()
  205. .FormatCollection(
  206. x => x.ToString(),
  207. separator: ", ",
  208. emptyCollectionFallback: "<None>")
  209. }");
  210. Logger.Notice.Print(LogClass.Application,
  211. AppDataManager.Mode == AppDataManager.LaunchMode.Custom
  212. ? $"Launch Mode: Custom Path {AppDataManager.BaseDirPath}"
  213. : $"Launch Mode: {AppDataManager.Mode}");
  214. }
  215. internal static void ProcessUnhandledException(object sender, Exception initialException, bool isTerminating)
  216. {
  217. Logger.Log log = Logger.Error ?? Logger.Notice;
  218. List<Exception> exceptions = [];
  219. if (initialException is AggregateException ae)
  220. {
  221. exceptions.AddRange(ae.InnerExceptions);
  222. }
  223. else
  224. {
  225. exceptions.Add(initialException);
  226. }
  227. foreach (Exception e in exceptions)
  228. {
  229. string message = $"Unhandled exception caught: {e}";
  230. // ReSharper disable once ConstantConditionalAccessQualifier
  231. if (sender?.GetType()?.AsPrettyString() is { } senderName)
  232. log.Print(LogClass.Application, message, senderName);
  233. else
  234. log.PrintMsg(LogClass.Application, message);
  235. }
  236. if (isTerminating)
  237. Exit();
  238. }
  239. internal static void Exit()
  240. {
  241. DiscordIntegrationModule.Exit();
  242. Logger.Shutdown();
  243. }
  244. }
  245. }