Program.cs 9.6 KB

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