Program.cs 8.0 KB

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