Program.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 = 8.0f,
  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. DriverUtilities.ToggleOGLThreading(ConfigurationState.Instance.Graphics.BackendThreading == BackendThreading.Off);
  101. // Check if keys exists.
  102. if (!File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys")))
  103. {
  104. if (!(AppDataManager.Mode == AppDataManager.LaunchMode.UserProfile && File.Exists(Path.Combine(AppDataManager.KeysDirPathUser, "prod.keys"))))
  105. {
  106. MainWindow.ShowKeyErrorOnLoad = true;
  107. }
  108. }
  109. if (CommandLineState.LaunchPathArg != null)
  110. {
  111. MainWindow.DeferLoadApplication(CommandLineState.LaunchPathArg, CommandLineState.StartFullscreenArg);
  112. }
  113. }
  114. public static void ReloadConfig()
  115. {
  116. string localConfigurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
  117. string appDataConfigurationPath = Path.Combine(AppDataManager.BaseDirPath, "Config.json");
  118. // Now load the configuration as the other subsystems are now registered
  119. if (File.Exists(localConfigurationPath))
  120. {
  121. ConfigurationPath = localConfigurationPath;
  122. }
  123. else if (File.Exists(appDataConfigurationPath))
  124. {
  125. ConfigurationPath = appDataConfigurationPath;
  126. }
  127. if (ConfigurationPath == null)
  128. {
  129. // No configuration, we load the default values and save it to disk
  130. ConfigurationPath = appDataConfigurationPath;
  131. ConfigurationState.Instance.LoadDefault();
  132. ConfigurationState.Instance.ToFileFormat().SaveConfig(ConfigurationPath);
  133. }
  134. else
  135. {
  136. if (ConfigurationFileFormat.TryLoad(ConfigurationPath, out ConfigurationFileFormat configurationFileFormat))
  137. {
  138. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  139. }
  140. else
  141. {
  142. ConfigurationState.Instance.LoadDefault();
  143. Logger.Warning?.PrintMsg(LogClass.Application, $"Failed to load config! Loading the default config instead.\nFailed config location {ConfigurationPath}");
  144. }
  145. }
  146. // Check if graphics backend was overridden
  147. if (CommandLineState.OverrideGraphicsBackend != null)
  148. {
  149. if (CommandLineState.OverrideGraphicsBackend.ToLower() == "opengl")
  150. {
  151. ConfigurationState.Instance.Graphics.GraphicsBackend.Value = GraphicsBackend.OpenGl;
  152. }
  153. else if (CommandLineState.OverrideGraphicsBackend.ToLower() == "vulkan")
  154. {
  155. ConfigurationState.Instance.Graphics.GraphicsBackend.Value = GraphicsBackend.Vulkan;
  156. }
  157. }
  158. }
  159. private static void PrintSystemInfo()
  160. {
  161. Logger.Notice.Print(LogClass.Application, $"Ryujinx Version: {Version}");
  162. SystemInfo.Gather().Print();
  163. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {(Logger.GetEnabledLevels().Count == 0 ? "<None>" : string.Join(", ", Logger.GetEnabledLevels()))}");
  164. if (AppDataManager.Mode == AppDataManager.LaunchMode.Custom)
  165. {
  166. Logger.Notice.Print(LogClass.Application, $"Launch Mode: Custom Path {AppDataManager.BaseDirPath}");
  167. }
  168. else
  169. {
  170. Logger.Notice.Print(LogClass.Application, $"Launch Mode: {AppDataManager.Mode}");
  171. }
  172. }
  173. private static void ProcessUnhandledException(Exception ex, bool isTerminating)
  174. {
  175. Ptc.Close();
  176. PtcProfiler.Stop();
  177. string message = $"Unhandled exception caught: {ex}";
  178. Logger.Error?.PrintMsg(LogClass.Application, message);
  179. if (Logger.Error == null)
  180. {
  181. Logger.Notice.PrintMsg(LogClass.Application, message);
  182. }
  183. if (isTerminating)
  184. {
  185. Exit();
  186. }
  187. }
  188. public static void Exit()
  189. {
  190. DiscordIntegrationModule.Exit();
  191. Ptc.Dispose();
  192. PtcProfiler.Dispose();
  193. Logger.Shutdown();
  194. }
  195. }
  196. }