Program.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using ARMeilleure.Translation.PTC;
  2. using Avalonia;
  3. using Avalonia.OpenGL;
  4. using Avalonia.Rendering;
  5. using Avalonia.Threading;
  6. using Ryujinx.Ava.Ui.Backend;
  7. using Ryujinx.Ava.Ui.Controls;
  8. using Ryujinx.Ava.Ui.Windows;
  9. using Ryujinx.Common;
  10. using Ryujinx.Common.Configuration;
  11. using Ryujinx.Common.GraphicsDriver;
  12. using Ryujinx.Common.Logging;
  13. using Ryujinx.Common.System;
  14. using Ryujinx.Common.SystemInfo;
  15. using Ryujinx.Graphics.Vulkan;
  16. using Ryujinx.Modules;
  17. using Ryujinx.Ui.Common;
  18. using Ryujinx.Ui.Common.Configuration;
  19. using Silk.NET.Vulkan.Extensions.EXT;
  20. using Silk.NET.Vulkan.Extensions.KHR;
  21. using System;
  22. using System.Collections.Generic;
  23. using System.IO;
  24. using System.Runtime.InteropServices;
  25. using System.Threading.Tasks;
  26. namespace Ryujinx.Ava
  27. {
  28. internal class Program
  29. {
  30. public static double WindowScaleFactor { get; set; }
  31. public static double ActualScaleFactor { get; set; }
  32. public static string Version { get; private set; }
  33. public static string ConfigurationPath { get; private set; }
  34. public static string CommandLineProfile { get; set; }
  35. public static bool PreviewerDetached { get; private set; }
  36. public static RenderTimer RenderTimer { get; private set; }
  37. public static bool UseVulkan { get; private set; }
  38. [DllImport("user32.dll", SetLastError = true)]
  39. public static extern int MessageBoxA(IntPtr hWnd, string text, string caption, uint type);
  40. private const uint MB_ICONWARNING = 0x30;
  41. private const int BaseDpi = 96;
  42. public static void Main(string[] args)
  43. {
  44. Version = ReleaseInformations.GetVersion();
  45. if (OperatingSystem.IsWindows() && !OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17134))
  46. {
  47. 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);
  48. }
  49. PreviewerDetached = true;
  50. Initialize(args);
  51. RenderTimer = new RenderTimer();
  52. BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
  53. RenderTimer.Dispose();
  54. }
  55. public static AppBuilder BuildAvaloniaApp()
  56. {
  57. return AppBuilder.Configure<App>()
  58. .UsePlatformDetect()
  59. .With(new X11PlatformOptions
  60. {
  61. EnableMultiTouch = true,
  62. EnableIme = true,
  63. UseEGL = false,
  64. UseGpu = !UseVulkan,
  65. GlProfiles = new List<GlVersion>()
  66. {
  67. new GlVersion(GlProfileType.OpenGL, 4, 3)
  68. }
  69. })
  70. .With(new Win32PlatformOptions
  71. {
  72. EnableMultitouch = true,
  73. UseWgl = !UseVulkan,
  74. WglProfiles = new List<GlVersion>()
  75. {
  76. new GlVersion(GlProfileType.OpenGL, 4, 3)
  77. },
  78. AllowEglInitialization = false,
  79. CompositionBackdropCornerRadius = 8f,
  80. })
  81. .UseSkia()
  82. .With(new Ui.Vulkan.VulkanOptions()
  83. {
  84. ApplicationName = "Ryujinx.Graphics.Vulkan",
  85. VulkanVersion = new Version(1, 2),
  86. MaxQueueCount = 2,
  87. PreferDiscreteGpu = true,
  88. PreferredDevice = !PreviewerDetached ? "" : ConfigurationState.Instance.Graphics.PreferredGpu.Value,
  89. UseDebug = !PreviewerDetached ? false : ConfigurationState.Instance.Logger.GraphicsDebugLevel.Value != GraphicsDebugLevel.None,
  90. })
  91. .With(new SkiaOptions()
  92. {
  93. CustomGpuFactory = UseVulkan ? SkiaGpuFactory.CreateVulkanGpu : null
  94. })
  95. .AfterSetup(_ =>
  96. {
  97. AvaloniaLocator.CurrentMutable
  98. .Bind<IRenderTimer>().ToConstant(RenderTimer)
  99. .Bind<IRenderLoop>().ToConstant(new RenderLoop(RenderTimer, Dispatcher.UIThread));
  100. })
  101. .LogToTrace();
  102. }
  103. private static void Initialize(string[] args)
  104. {
  105. // Parse Arguments.
  106. string launchPathArg = null;
  107. string baseDirPathArg = null;
  108. bool startFullscreenArg = false;
  109. for (int i = 0; i < args.Length; ++i)
  110. {
  111. string arg = args[i];
  112. if (arg == "-r" || arg == "--root-data-dir")
  113. {
  114. if (i + 1 >= args.Length)
  115. {
  116. Logger.Error?.Print(LogClass.Application, $"Invalid option '{arg}'");
  117. continue;
  118. }
  119. baseDirPathArg = args[++i];
  120. }
  121. else if (arg == "-p" || arg == "--profile")
  122. {
  123. if (i + 1 >= args.Length)
  124. {
  125. Logger.Error?.Print(LogClass.Application, $"Invalid option '{arg}'");
  126. continue;
  127. }
  128. CommandLineProfile = args[++i];
  129. }
  130. else if (arg == "-f" || arg == "--fullscreen")
  131. {
  132. startFullscreenArg = true;
  133. }
  134. else
  135. {
  136. launchPathArg = arg;
  137. }
  138. }
  139. // Delete backup files after updating.
  140. Task.Run(Updater.CleanupUpdate);
  141. Console.Title = $"Ryujinx Console {Version}";
  142. // Hook unhandled exception and process exit events.
  143. AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  144. AppDomain.CurrentDomain.ProcessExit += (object sender, EventArgs e) => Exit();
  145. // Setup base data directory.
  146. AppDataManager.Initialize(baseDirPathArg);
  147. // Initialize the configuration.
  148. ConfigurationState.Initialize();
  149. // Initialize the logger system.
  150. LoggerModule.Initialize();
  151. // Initialize Discord integration.
  152. DiscordIntegrationModule.Initialize();
  153. ReloadConfig();
  154. UseVulkan = PreviewerDetached ? ConfigurationState.Instance.Graphics.GraphicsBackend.Value == GraphicsBackend.Vulkan : false;
  155. if (UseVulkan)
  156. {
  157. // With a custom gpu backend, avalonia doesn't enable dpi awareness, so the backend must handle it. This isn't so for the opengl backed,
  158. // as that uses avalonia's gpu backend and it's enabled there.
  159. ForceDpiAware.Windows();
  160. }
  161. WindowScaleFactor = ForceDpiAware.GetWindowScaleFactor();
  162. ActualScaleFactor = ForceDpiAware.GetActualScaleFactor() / BaseDpi;
  163. // Logging system information.
  164. PrintSystemInfo();
  165. // Enable OGL multithreading on the driver, when available.
  166. BackendThreading threadingMode = ConfigurationState.Instance.Graphics.BackendThreading;
  167. DriverUtilities.ToggleOGLThreading(threadingMode == BackendThreading.Off);
  168. // Check if keys exists.
  169. bool hasSystemProdKeys = File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys"));
  170. if (!hasSystemProdKeys)
  171. {
  172. if (!(AppDataManager.Mode == AppDataManager.LaunchMode.UserProfile && File.Exists(Path.Combine(AppDataManager.KeysDirPathUser, "prod.keys"))))
  173. {
  174. MainWindow.ShowKeyErrorOnLoad = true;
  175. }
  176. }
  177. if (launchPathArg != null)
  178. {
  179. MainWindow.DeferLoadApplication(launchPathArg, startFullscreenArg);
  180. }
  181. }
  182. public static void ReloadConfig()
  183. {
  184. string localConfigurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
  185. string appDataConfigurationPath = Path.Combine(AppDataManager.BaseDirPath, "Config.json");
  186. // Now load the configuration as the other subsystems are now registered
  187. if (File.Exists(localConfigurationPath))
  188. {
  189. ConfigurationPath = localConfigurationPath;
  190. }
  191. else if (File.Exists(appDataConfigurationPath))
  192. {
  193. ConfigurationPath = appDataConfigurationPath;
  194. }
  195. if (ConfigurationPath == null)
  196. {
  197. // No configuration, we load the default values and save it to disk
  198. ConfigurationPath = appDataConfigurationPath;
  199. ConfigurationState.Instance.LoadDefault();
  200. ConfigurationState.Instance.ToFileFormat().SaveConfig(ConfigurationPath);
  201. }
  202. else
  203. {
  204. if (ConfigurationFileFormat.TryLoad(ConfigurationPath, out ConfigurationFileFormat configurationFileFormat))
  205. {
  206. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  207. }
  208. else
  209. {
  210. ConfigurationState.Instance.LoadDefault();
  211. Logger.Warning?.PrintMsg(LogClass.Application, $"Failed to load config! Loading the default config instead.\nFailed config location {ConfigurationPath}");
  212. }
  213. }
  214. }
  215. private static void PrintSystemInfo()
  216. {
  217. Logger.Notice.Print(LogClass.Application, $"Ryujinx Version: {Version}");
  218. SystemInfo.Gather().Print();
  219. var enabledLogs = Logger.GetEnabledLevels();
  220. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {(enabledLogs.Count == 0 ? "<None>" : string.Join(", ", enabledLogs))}");
  221. if (AppDataManager.Mode == AppDataManager.LaunchMode.Custom)
  222. {
  223. Logger.Notice.Print(LogClass.Application, $"Launch Mode: Custom Path {AppDataManager.BaseDirPath}");
  224. }
  225. else
  226. {
  227. Logger.Notice.Print(LogClass.Application, $"Launch Mode: {AppDataManager.Mode}");
  228. }
  229. }
  230. private static void ProcessUnhandledException(Exception ex, bool isTerminating)
  231. {
  232. Ptc.Close();
  233. PtcProfiler.Stop();
  234. string message = $"Unhandled exception caught: {ex}";
  235. Logger.Error?.PrintMsg(LogClass.Application, message);
  236. if (Logger.Error == null)
  237. {
  238. Logger.Notice.PrintMsg(LogClass.Application, message);
  239. }
  240. if (isTerminating)
  241. {
  242. Exit();
  243. }
  244. }
  245. public static void Exit()
  246. {
  247. DiscordIntegrationModule.Exit();
  248. Ptc.Dispose();
  249. PtcProfiler.Dispose();
  250. Logger.Shutdown();
  251. }
  252. }
  253. }