Program.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 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 double ActualScaleFactor { get; set; }
  26. public static string Version { get; private set; }
  27. public static string ConfigurationPath { get; private set; }
  28. public static string CommandLineProfile { get; 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. string launchPathArg = null;
  79. string baseDirPathArg = null;
  80. bool startFullscreenArg = false;
  81. for (int i = 0; i < args.Length; ++i)
  82. {
  83. string arg = args[i];
  84. if (arg == "-r" || arg == "--root-data-dir")
  85. {
  86. if (i + 1 >= args.Length)
  87. {
  88. Logger.Error?.Print(LogClass.Application, $"Invalid option '{arg}'");
  89. continue;
  90. }
  91. baseDirPathArg = args[++i];
  92. }
  93. else if (arg == "-p" || arg == "--profile")
  94. {
  95. if (i + 1 >= args.Length)
  96. {
  97. Logger.Error?.Print(LogClass.Application, $"Invalid option '{arg}'");
  98. continue;
  99. }
  100. CommandLineProfile = args[++i];
  101. }
  102. else if (arg == "-f" || arg == "--fullscreen")
  103. {
  104. startFullscreenArg = true;
  105. }
  106. else
  107. {
  108. launchPathArg = arg;
  109. }
  110. }
  111. // Delete backup files after updating.
  112. Task.Run(Updater.CleanupUpdate);
  113. Console.Title = $"Ryujinx Console {Version}";
  114. // Hook unhandled exception and process exit events.
  115. AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  116. AppDomain.CurrentDomain.ProcessExit += (object sender, EventArgs e) => Exit();
  117. // Setup base data directory.
  118. AppDataManager.Initialize(baseDirPathArg);
  119. // Initialize the configuration.
  120. ConfigurationState.Initialize();
  121. // Initialize the logger system.
  122. LoggerModule.Initialize();
  123. // Initialize Discord integration.
  124. DiscordIntegrationModule.Initialize();
  125. ReloadConfig();
  126. ForceDpiAware.Windows();
  127. WindowScaleFactor = ForceDpiAware.GetWindowScaleFactor();
  128. ActualScaleFactor = ForceDpiAware.GetActualScaleFactor() / BaseDpi;
  129. // Logging system information.
  130. PrintSystemInfo();
  131. // Enable OGL multithreading on the driver, when available.
  132. BackendThreading threadingMode = ConfigurationState.Instance.Graphics.BackendThreading;
  133. DriverUtilities.ToggleOGLThreading(threadingMode == BackendThreading.Off);
  134. // Check if keys exists.
  135. bool hasSystemProdKeys = File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys"));
  136. if (!hasSystemProdKeys)
  137. {
  138. if (!(AppDataManager.Mode == AppDataManager.LaunchMode.UserProfile && File.Exists(Path.Combine(AppDataManager.KeysDirPathUser, "prod.keys"))))
  139. {
  140. MainWindow.ShowKeyErrorOnLoad = true;
  141. }
  142. }
  143. if (launchPathArg != null)
  144. {
  145. MainWindow.DeferLoadApplication(launchPathArg, startFullscreenArg);
  146. }
  147. }
  148. public static void ReloadConfig()
  149. {
  150. string localConfigurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
  151. string appDataConfigurationPath = Path.Combine(AppDataManager.BaseDirPath, "Config.json");
  152. // Now load the configuration as the other subsystems are now registered
  153. if (File.Exists(localConfigurationPath))
  154. {
  155. ConfigurationPath = localConfigurationPath;
  156. }
  157. else if (File.Exists(appDataConfigurationPath))
  158. {
  159. ConfigurationPath = appDataConfigurationPath;
  160. }
  161. if (ConfigurationPath == null)
  162. {
  163. // No configuration, we load the default values and save it to disk
  164. ConfigurationPath = appDataConfigurationPath;
  165. ConfigurationState.Instance.LoadDefault();
  166. ConfigurationState.Instance.ToFileFormat().SaveConfig(ConfigurationPath);
  167. }
  168. else
  169. {
  170. if (ConfigurationFileFormat.TryLoad(ConfigurationPath, out ConfigurationFileFormat configurationFileFormat))
  171. {
  172. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  173. }
  174. else
  175. {
  176. ConfigurationState.Instance.LoadDefault();
  177. Logger.Warning?.PrintMsg(LogClass.Application, $"Failed to load config! Loading the default config instead.\nFailed config location {ConfigurationPath}");
  178. }
  179. }
  180. }
  181. private static void PrintSystemInfo()
  182. {
  183. Logger.Notice.Print(LogClass.Application, $"Ryujinx Version: {Version}");
  184. SystemInfo.Gather().Print();
  185. var enabledLogs = Logger.GetEnabledLevels();
  186. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {(enabledLogs.Count == 0 ? "<None>" : string.Join(", ", enabledLogs))}");
  187. if (AppDataManager.Mode == AppDataManager.LaunchMode.Custom)
  188. {
  189. Logger.Notice.Print(LogClass.Application, $"Launch Mode: Custom Path {AppDataManager.BaseDirPath}");
  190. }
  191. else
  192. {
  193. Logger.Notice.Print(LogClass.Application, $"Launch Mode: {AppDataManager.Mode}");
  194. }
  195. }
  196. private static void ProcessUnhandledException(Exception ex, bool isTerminating)
  197. {
  198. Ptc.Close();
  199. PtcProfiler.Stop();
  200. string message = $"Unhandled exception caught: {ex}";
  201. Logger.Error?.PrintMsg(LogClass.Application, message);
  202. if (Logger.Error == null)
  203. {
  204. Logger.Notice.PrintMsg(LogClass.Application, message);
  205. }
  206. if (isTerminating)
  207. {
  208. Exit();
  209. }
  210. }
  211. public static void Exit()
  212. {
  213. DiscordIntegrationModule.Exit();
  214. Ptc.Dispose();
  215. PtcProfiler.Dispose();
  216. Logger.Shutdown();
  217. }
  218. }
  219. }