Program.cs 8.9 KB

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