Program.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using ARMeilleure.Translation.PTC;
  2. using Gtk;
  3. using Ryujinx.Common;
  4. using Ryujinx.Common.Configuration;
  5. using Ryujinx.Common.GraphicsDriver;
  6. using Ryujinx.Common.Logging;
  7. using Ryujinx.Common.System;
  8. using Ryujinx.Common.SystemInfo;
  9. using Ryujinx.Configuration;
  10. using Ryujinx.Modules;
  11. using Ryujinx.Ui;
  12. using Ryujinx.Ui.Widgets;
  13. using SixLabors.ImageSharp.Formats.Jpeg;
  14. using System;
  15. using System.IO;
  16. using System.Reflection;
  17. using System.Runtime.InteropServices;
  18. using System.Threading.Tasks;
  19. namespace Ryujinx
  20. {
  21. class Program
  22. {
  23. public static double WindowScaleFactor { get; private set; }
  24. public static string Version { get; private set; }
  25. public static string ConfigurationPath { get; set; }
  26. public static string CommandLineProfile { get; set; }
  27. [DllImport("libX11")]
  28. private extern static int XInitThreads();
  29. [DllImport("user32.dll", SetLastError = true)]
  30. public static extern int MessageBoxA(IntPtr hWnd, string text, string caption, uint type);
  31. private const uint MB_ICONWARNING = 0x30;
  32. static void Main(string[] args)
  33. {
  34. Version = ReleaseInformations.GetVersion();
  35. if (OperatingSystem.IsWindows() && !OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17134))
  36. {
  37. 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);
  38. }
  39. // Parse Arguments.
  40. string launchPathArg = null;
  41. string baseDirPathArg = null;
  42. bool startFullscreenArg = false;
  43. for (int i = 0; i < args.Length; ++i)
  44. {
  45. string arg = args[i];
  46. if (arg == "-r" || arg == "--root-data-dir")
  47. {
  48. if (i + 1 >= args.Length)
  49. {
  50. Logger.Error?.Print(LogClass.Application, $"Invalid option '{arg}'");
  51. continue;
  52. }
  53. baseDirPathArg = args[++i];
  54. }
  55. else if (arg == "-p" || arg == "--profile")
  56. {
  57. if (i + 1 >= args.Length)
  58. {
  59. Logger.Error?.Print(LogClass.Application, $"Invalid option '{arg}'");
  60. continue;
  61. }
  62. CommandLineProfile = args[++i];
  63. }
  64. else if (arg == "-f" || arg == "--fullscreen")
  65. {
  66. startFullscreenArg = true;
  67. }
  68. else if (launchPathArg == null)
  69. {
  70. launchPathArg = arg;
  71. }
  72. }
  73. // Make process DPI aware for proper window sizing on high-res screens.
  74. ForceDpiAware.Windows();
  75. WindowScaleFactor = ForceDpiAware.GetWindowScaleFactor();
  76. // Delete backup files after updating.
  77. Task.Run(Updater.CleanupUpdate);
  78. Console.Title = $"Ryujinx Console {Version}";
  79. // NOTE: GTK3 doesn't init X11 in a multi threaded way.
  80. // This ends up causing race condition and abort of XCB when a context is created by SPB (even if SPB do call XInitThreads).
  81. if (OperatingSystem.IsLinux())
  82. {
  83. XInitThreads();
  84. }
  85. string systemPath = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine);
  86. Environment.SetEnvironmentVariable("Path", $"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin")};{systemPath}");
  87. // Hook unhandled exception and process exit events.
  88. GLib.ExceptionManager.UnhandledException += (GLib.UnhandledExceptionArgs e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  89. AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  90. AppDomain.CurrentDomain.ProcessExit += (object sender, EventArgs e) => Exit();
  91. // Setup base data directory.
  92. AppDataManager.Initialize(baseDirPathArg);
  93. // Initialize the configuration.
  94. ConfigurationState.Initialize();
  95. // Initialize the logger system.
  96. LoggerModule.Initialize();
  97. // Initialize Discord integration.
  98. DiscordIntegrationModule.Initialize();
  99. // Sets ImageSharp Jpeg Encoder Quality.
  100. SixLabors.ImageSharp.Configuration.Default.ImageFormatsManager.SetEncoder(JpegFormat.Instance, new JpegEncoder()
  101. {
  102. Quality = 100
  103. });
  104. string localConfigurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
  105. string appDataConfigurationPath = Path.Combine(AppDataManager.BaseDirPath, "Config.json");
  106. // Now load the configuration as the other subsystems are now registered
  107. ConfigurationPath = File.Exists(localConfigurationPath)
  108. ? localConfigurationPath
  109. : File.Exists(appDataConfigurationPath)
  110. ? appDataConfigurationPath
  111. : null;
  112. if (ConfigurationPath == null)
  113. {
  114. // No configuration, we load the default values and save it to disk
  115. ConfigurationPath = appDataConfigurationPath;
  116. ConfigurationState.Instance.LoadDefault();
  117. ConfigurationState.Instance.ToFileFormat().SaveConfig(ConfigurationPath);
  118. }
  119. else
  120. {
  121. if (ConfigurationFileFormat.TryLoad(ConfigurationPath, out ConfigurationFileFormat configurationFileFormat))
  122. {
  123. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  124. }
  125. else
  126. {
  127. ConfigurationState.Instance.LoadDefault();
  128. Logger.Warning?.PrintMsg(LogClass.Application, $"Failed to load config! Loading the default config instead.\nFailed config location {ConfigurationPath}");
  129. }
  130. }
  131. // Logging system information.
  132. PrintSystemInfo();
  133. // Enable OGL multithreading on the driver, when available.
  134. BackendThreading threadingMode = ConfigurationState.Instance.Graphics.BackendThreading;
  135. DriverUtilities.ToggleOGLThreading(threadingMode == BackendThreading.Off);
  136. // Initialize Gtk.
  137. Application.Init();
  138. // Check if keys exists.
  139. bool hasSystemProdKeys = File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys"));
  140. bool hasCommonProdKeys = AppDataManager.Mode == AppDataManager.LaunchMode.UserProfile && File.Exists(Path.Combine(AppDataManager.KeysDirPathUser, "prod.keys"));
  141. if (!hasSystemProdKeys && !hasCommonProdKeys)
  142. {
  143. UserErrorDialog.CreateUserErrorDialog(UserError.NoKeys);
  144. }
  145. // Show the main window UI.
  146. MainWindow mainWindow = new MainWindow();
  147. mainWindow.Show();
  148. if (launchPathArg != null)
  149. {
  150. mainWindow.LoadApplication(launchPathArg, startFullscreenArg);
  151. }
  152. if (ConfigurationState.Instance.CheckUpdatesOnStart.Value && Updater.CanUpdate(false))
  153. {
  154. Updater.BeginParse(mainWindow, false).ContinueWith(task =>
  155. {
  156. Logger.Error?.Print(LogClass.Application, $"Updater Error: {task.Exception}");
  157. }, TaskContinuationOptions.OnlyOnFaulted);
  158. }
  159. Application.Run();
  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. }