Program.cs 11 KB

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