Program.cs 8.5 KB

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