Program.cs 8.2 KB

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