Program.cs 9.0 KB

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