Program.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.Controls;
  7. using Ryujinx.Ava.Ui.Windows;
  8. using Ryujinx.Common;
  9. using Ryujinx.Common.Configuration;
  10. using Ryujinx.Common.GraphicsDriver;
  11. using Ryujinx.Common.Logging;
  12. using Ryujinx.Common.System;
  13. using Ryujinx.Common.SystemInfo;
  14. using Ryujinx.Modules;
  15. using Ryujinx.Ui.Common;
  16. using Ryujinx.Ui.Common.Configuration;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.IO;
  20. using System.Runtime.InteropServices;
  21. using System.Threading.Tasks;
  22. namespace Ryujinx.Ava
  23. {
  24. internal class Program
  25. {
  26. public static double WindowScaleFactor { get; set; }
  27. public static string Version { get; private set; }
  28. public static string ConfigurationPath { get; private set; }
  29. public static string CommandLineProfile { get; set; }
  30. public static bool PreviewerDetached { get; private set; }
  31. public static RenderTimer RenderTimer { get; private set; }
  32. [DllImport("user32.dll", SetLastError = true)]
  33. public static extern int MessageBoxA(IntPtr hWnd, string text, string caption, uint type);
  34. private const uint MB_ICONWARNING = 0x30;
  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 = true,
  58. GlProfiles = new List<GlVersion>()
  59. {
  60. new GlVersion(GlProfileType.OpenGL, 4, 3)
  61. }
  62. })
  63. .With(new Win32PlatformOptions
  64. {
  65. EnableMultitouch = true,
  66. UseWgl = true,
  67. WglProfiles = new List<GlVersion>()
  68. {
  69. new GlVersion(GlProfileType.OpenGL, 4, 3)
  70. },
  71. AllowEglInitialization = false,
  72. CompositionBackdropCornerRadius = 8f,
  73. })
  74. .UseSkia()
  75. .AfterSetup(_ =>
  76. {
  77. AvaloniaLocator.CurrentMutable
  78. .Bind<IRenderTimer>().ToConstant(RenderTimer)
  79. .Bind<IRenderLoop>().ToConstant(new RenderLoop(RenderTimer, Dispatcher.UIThread));
  80. })
  81. .LogToTrace();
  82. }
  83. private static void Initialize(string[] args)
  84. {
  85. // Parse Arguments.
  86. string launchPathArg = null;
  87. string baseDirPathArg = null;
  88. bool startFullscreenArg = false;
  89. for (int i = 0; i < args.Length; ++i)
  90. {
  91. string arg = args[i];
  92. if (arg == "-r" || arg == "--root-data-dir")
  93. {
  94. if (i + 1 >= args.Length)
  95. {
  96. Logger.Error?.Print(LogClass.Application, $"Invalid option '{arg}'");
  97. continue;
  98. }
  99. baseDirPathArg = args[++i];
  100. }
  101. else if (arg == "-p" || arg == "--profile")
  102. {
  103. if (i + 1 >= args.Length)
  104. {
  105. Logger.Error?.Print(LogClass.Application, $"Invalid option '{arg}'");
  106. continue;
  107. }
  108. CommandLineProfile = args[++i];
  109. }
  110. else if (arg == "-f" || arg == "--fullscreen")
  111. {
  112. startFullscreenArg = true;
  113. }
  114. else
  115. {
  116. launchPathArg = arg;
  117. }
  118. }
  119. // Make process DPI aware for proper window sizing on high-res screens.
  120. ForceDpiAware.Windows();
  121. WindowScaleFactor = ForceDpiAware.GetWindowScaleFactor();
  122. // Delete backup files after updating.
  123. Task.Run(Updater.CleanupUpdate);
  124. Console.Title = $"Ryujinx Console {Version}";
  125. // Hook unhandled exception and process exit events.
  126. AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  127. AppDomain.CurrentDomain.ProcessExit += (object sender, EventArgs e) => Exit();
  128. // Setup base data directory.
  129. AppDataManager.Initialize(baseDirPathArg);
  130. // Initialize the configuration.
  131. ConfigurationState.Initialize();
  132. // Initialize the logger system.
  133. LoggerModule.Initialize();
  134. // Initialize Discord integration.
  135. DiscordIntegrationModule.Initialize();
  136. ReloadConfig();
  137. // Logging system information.
  138. PrintSystemInfo();
  139. // Enable OGL multithreading on the driver, when available.
  140. BackendThreading threadingMode = ConfigurationState.Instance.Graphics.BackendThreading;
  141. DriverUtilities.ToggleOGLThreading(threadingMode == BackendThreading.Off);
  142. // Check if keys exists.
  143. bool hasSystemProdKeys = File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys"));
  144. if (!hasSystemProdKeys)
  145. {
  146. if (!(AppDataManager.Mode == AppDataManager.LaunchMode.UserProfile && File.Exists(Path.Combine(AppDataManager.KeysDirPathUser, "prod.keys"))))
  147. {
  148. MainWindow.ShowKeyErrorOnLoad = true;
  149. }
  150. }
  151. if (launchPathArg != null)
  152. {
  153. MainWindow.DeferLoadApplication(launchPathArg, startFullscreenArg);
  154. }
  155. }
  156. private static void ReloadConfig()
  157. {
  158. string localConfigurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
  159. string appDataConfigurationPath = Path.Combine(AppDataManager.BaseDirPath, "Config.json");
  160. // Now load the configuration as the other subsystems are now registered
  161. if (File.Exists(localConfigurationPath))
  162. {
  163. ConfigurationPath = localConfigurationPath;
  164. }
  165. else if (File.Exists(appDataConfigurationPath))
  166. {
  167. ConfigurationPath = appDataConfigurationPath;
  168. }
  169. if (ConfigurationPath == null)
  170. {
  171. // No configuration, we load the default values and save it to disk
  172. ConfigurationPath = appDataConfigurationPath;
  173. ConfigurationState.Instance.LoadDefault();
  174. ConfigurationState.Instance.ToFileFormat().SaveConfig(ConfigurationPath);
  175. }
  176. else
  177. {
  178. if (ConfigurationFileFormat.TryLoad(ConfigurationPath, out ConfigurationFileFormat configurationFileFormat))
  179. {
  180. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  181. }
  182. else
  183. {
  184. ConfigurationState.Instance.LoadDefault();
  185. Logger.Warning?.PrintMsg(LogClass.Application, $"Failed to load config! Loading the default config instead.\nFailed config location {ConfigurationPath}");
  186. }
  187. }
  188. }
  189. private static void PrintSystemInfo()
  190. {
  191. Logger.Notice.Print(LogClass.Application, $"Ryujinx Version: {Version}");
  192. SystemInfo.Gather().Print();
  193. var enabledLogs = Logger.GetEnabledLevels();
  194. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {(enabledLogs.Count == 0 ? "<None>" : string.Join(", ", enabledLogs))}");
  195. if (AppDataManager.Mode == AppDataManager.LaunchMode.Custom)
  196. {
  197. Logger.Notice.Print(LogClass.Application, $"Launch Mode: Custom Path {AppDataManager.BaseDirPath}");
  198. }
  199. else
  200. {
  201. Logger.Notice.Print(LogClass.Application, $"Launch Mode: {AppDataManager.Mode}");
  202. }
  203. }
  204. private static void ProcessUnhandledException(Exception ex, bool isTerminating)
  205. {
  206. Ptc.Close();
  207. PtcProfiler.Stop();
  208. string message = $"Unhandled exception caught: {ex}";
  209. Logger.Error?.PrintMsg(LogClass.Application, message);
  210. if (Logger.Error == null)
  211. {
  212. Logger.Notice.PrintMsg(LogClass.Application, message);
  213. }
  214. if (isTerminating)
  215. {
  216. Exit();
  217. }
  218. }
  219. public static void Exit()
  220. {
  221. DiscordIntegrationModule.Exit();
  222. Ptc.Dispose();
  223. PtcProfiler.Dispose();
  224. Logger.Shutdown();
  225. }
  226. }
  227. }