Program.cs 9.9 KB

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