Program.cs 8.8 KB

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