Program.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using ARMeilleure.Translation.PTC;
  2. using Avalonia;
  3. using Avalonia.Threading;
  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.System;
  10. using Ryujinx.Common.SystemInfo;
  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 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. [DllImport("user32.dll", SetLastError = true)]
  30. public static extern int MessageBoxA(IntPtr hWnd, string text, string caption, uint type);
  31. private const uint MB_ICONWARNING = 0x30;
  32. public static void Main(string[] args)
  33. {
  34. Version = ReleaseInformations.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. BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
  42. }
  43. public static AppBuilder BuildAvaloniaApp()
  44. {
  45. return AppBuilder.Configure<App>()
  46. .UsePlatformDetect()
  47. .With(new X11PlatformOptions
  48. {
  49. EnableMultiTouch = true,
  50. EnableIme = true,
  51. UseEGL = false,
  52. UseGpu = true
  53. })
  54. .With(new Win32PlatformOptions
  55. {
  56. EnableMultitouch = true,
  57. UseWgl = false,
  58. AllowEglInitialization = false,
  59. CompositionBackdropCornerRadius = 8.0f,
  60. })
  61. .UseSkia()
  62. .LogToTrace();
  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 += (object sender, UnhandledExceptionEventArgs e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  73. AppDomain.CurrentDomain.ProcessExit += (object sender, EventArgs 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. }
  149. private static void PrintSystemInfo()
  150. {
  151. Logger.Notice.Print(LogClass.Application, $"Ryujinx Version: {Version}");
  152. SystemInfo.Gather().Print();
  153. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {(Logger.GetEnabledLevels().Count == 0 ? "<None>" : string.Join(", ", Logger.GetEnabledLevels()))}");
  154. if (AppDataManager.Mode == AppDataManager.LaunchMode.Custom)
  155. {
  156. Logger.Notice.Print(LogClass.Application, $"Launch Mode: Custom Path {AppDataManager.BaseDirPath}");
  157. }
  158. else
  159. {
  160. Logger.Notice.Print(LogClass.Application, $"Launch Mode: {AppDataManager.Mode}");
  161. }
  162. }
  163. private static void ProcessUnhandledException(Exception ex, bool isTerminating)
  164. {
  165. Ptc.Close();
  166. PtcProfiler.Stop();
  167. string message = $"Unhandled exception caught: {ex}";
  168. Logger.Error?.PrintMsg(LogClass.Application, message);
  169. if (Logger.Error == null)
  170. {
  171. Logger.Notice.PrintMsg(LogClass.Application, message);
  172. }
  173. if (isTerminating)
  174. {
  175. Exit();
  176. }
  177. }
  178. public static void Exit()
  179. {
  180. DiscordIntegrationModule.Exit();
  181. Ptc.Dispose();
  182. PtcProfiler.Dispose();
  183. Logger.Shutdown();
  184. }
  185. }
  186. }