Program.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using ARMeilleure.Translation.PTC;
  2. using Avalonia;
  3. using Ryujinx.Ava.Ui.Windows;
  4. using Ryujinx.Common;
  5. using Ryujinx.Common.Configuration;
  6. using Ryujinx.Common.GraphicsDriver;
  7. using Ryujinx.Common.Logging;
  8. using Ryujinx.Common.System;
  9. using Ryujinx.Common.SystemInfo;
  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 System;
  16. using System.IO;
  17. using System.Runtime.InteropServices;
  18. using System.Threading.Tasks;
  19. namespace Ryujinx.Ava
  20. {
  21. internal class Program
  22. {
  23. public static double WindowScaleFactor { get; set; }
  24. public static string Version { get; private set; }
  25. public static string ConfigurationPath { get; private set; }
  26. public static bool PreviewerDetached { get; private set; }
  27. [DllImport("user32.dll", SetLastError = true)]
  28. public static extern int MessageBoxA(IntPtr hWnd, string text, string caption, uint type);
  29. private const uint MB_ICONWARNING = 0x30;
  30. public static void Main(string[] args)
  31. {
  32. Version = ReleaseInformations.GetVersion();
  33. if (OperatingSystem.IsWindows() && !OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17134))
  34. {
  35. _ = 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);
  36. }
  37. PreviewerDetached = true;
  38. Initialize(args);
  39. BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
  40. }
  41. public static AppBuilder BuildAvaloniaApp()
  42. {
  43. return AppBuilder.Configure<App>()
  44. .UsePlatformDetect()
  45. .With(new X11PlatformOptions
  46. {
  47. EnableMultiTouch = true,
  48. EnableIme = true,
  49. UseEGL = false,
  50. UseGpu = true
  51. })
  52. .With(new Win32PlatformOptions
  53. {
  54. EnableMultitouch = true,
  55. UseWgl = false,
  56. AllowEglInitialization = false,
  57. CompositionBackdropCornerRadius = 8.0f,
  58. })
  59. .UseSkia()
  60. .LogToTrace();
  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 += (object sender, UnhandledExceptionEventArgs e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  71. AppDomain.CurrentDomain.ProcessExit += (object sender, EventArgs 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. ForceDpiAware.Windows();
  84. WindowScaleFactor = ForceDpiAware.GetWindowScaleFactor();
  85. // Logging system information.
  86. PrintSystemInfo();
  87. // Enable OGL multithreading on the driver, when available.
  88. DriverUtilities.ToggleOGLThreading(ConfigurationState.Instance.Graphics.BackendThreading == BackendThreading.Off);
  89. // Check if keys exists.
  90. if (!File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys")))
  91. {
  92. if (!(AppDataManager.Mode == AppDataManager.LaunchMode.UserProfile && File.Exists(Path.Combine(AppDataManager.KeysDirPathUser, "prod.keys"))))
  93. {
  94. MainWindow.ShowKeyErrorOnLoad = true;
  95. }
  96. }
  97. if (CommandLineState.LaunchPathArg != null)
  98. {
  99. MainWindow.DeferLoadApplication(CommandLineState.LaunchPathArg, CommandLineState.StartFullscreenArg);
  100. }
  101. }
  102. public static void ReloadConfig()
  103. {
  104. string localConfigurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
  105. string appDataConfigurationPath = Path.Combine(AppDataManager.BaseDirPath, "Config.json");
  106. // Now load the configuration as the other subsystems are now registered
  107. if (File.Exists(localConfigurationPath))
  108. {
  109. ConfigurationPath = localConfigurationPath;
  110. }
  111. else if (File.Exists(appDataConfigurationPath))
  112. {
  113. ConfigurationPath = appDataConfigurationPath;
  114. }
  115. if (ConfigurationPath == null)
  116. {
  117. // No configuration, we load the default values and save it to disk
  118. ConfigurationPath = appDataConfigurationPath;
  119. ConfigurationState.Instance.LoadDefault();
  120. ConfigurationState.Instance.ToFileFormat().SaveConfig(ConfigurationPath);
  121. }
  122. else
  123. {
  124. if (ConfigurationFileFormat.TryLoad(ConfigurationPath, out ConfigurationFileFormat configurationFileFormat))
  125. {
  126. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  127. }
  128. else
  129. {
  130. ConfigurationState.Instance.LoadDefault();
  131. Logger.Warning?.PrintMsg(LogClass.Application, $"Failed to load config! Loading the default config instead.\nFailed config location {ConfigurationPath}");
  132. }
  133. }
  134. // Check if graphics backend was overridden
  135. if (CommandLineState.OverrideGraphicsBackend != null)
  136. {
  137. if (CommandLineState.OverrideGraphicsBackend.ToLower() == "opengl")
  138. {
  139. ConfigurationState.Instance.Graphics.GraphicsBackend.Value = GraphicsBackend.OpenGl;
  140. }
  141. else if (CommandLineState.OverrideGraphicsBackend.ToLower() == "vulkan")
  142. {
  143. ConfigurationState.Instance.Graphics.GraphicsBackend.Value = GraphicsBackend.Vulkan;
  144. }
  145. }
  146. }
  147. private static void PrintSystemInfo()
  148. {
  149. Logger.Notice.Print(LogClass.Application, $"Ryujinx Version: {Version}");
  150. SystemInfo.Gather().Print();
  151. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {(Logger.GetEnabledLevels().Count == 0 ? "<None>" : string.Join(", ", Logger.GetEnabledLevels()))}");
  152. if (AppDataManager.Mode == AppDataManager.LaunchMode.Custom)
  153. {
  154. Logger.Notice.Print(LogClass.Application, $"Launch Mode: Custom Path {AppDataManager.BaseDirPath}");
  155. }
  156. else
  157. {
  158. Logger.Notice.Print(LogClass.Application, $"Launch Mode: {AppDataManager.Mode}");
  159. }
  160. }
  161. private static void ProcessUnhandledException(Exception ex, bool isTerminating)
  162. {
  163. Ptc.Close();
  164. PtcProfiler.Stop();
  165. string message = $"Unhandled exception caught: {ex}";
  166. Logger.Error?.PrintMsg(LogClass.Application, message);
  167. if (Logger.Error == null)
  168. {
  169. Logger.Notice.PrintMsg(LogClass.Application, message);
  170. }
  171. if (isTerminating)
  172. {
  173. Exit();
  174. }
  175. }
  176. public static void Exit()
  177. {
  178. DiscordIntegrationModule.Exit();
  179. Ptc.Dispose();
  180. PtcProfiler.Dispose();
  181. Logger.Shutdown();
  182. }
  183. }
  184. }