Program.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. static void Main(string[] args)
  30. {
  31. // Parse Arguments.
  32. string launchPathArg = null;
  33. string baseDirPathArg = null;
  34. bool startFullscreenArg = false;
  35. for (int i = 0; i < args.Length; ++i)
  36. {
  37. string arg = args[i];
  38. if (arg == "-r" || arg == "--root-data-dir")
  39. {
  40. if (i + 1 >= args.Length)
  41. {
  42. Logger.Error?.Print(LogClass.Application, $"Invalid option '{arg}'");
  43. continue;
  44. }
  45. baseDirPathArg = args[++i];
  46. }
  47. else if (arg == "-p" || arg == "--profile")
  48. {
  49. if (i + 1 >= args.Length)
  50. {
  51. Logger.Error?.Print(LogClass.Application, $"Invalid option '{arg}'");
  52. continue;
  53. }
  54. CommandLineProfile = args[++i];
  55. }
  56. else if (arg == "-f" || arg == "--fullscreen")
  57. {
  58. startFullscreenArg = true;
  59. }
  60. else if (launchPathArg == null)
  61. {
  62. launchPathArg = arg;
  63. }
  64. }
  65. // Make process DPI aware for proper window sizing on high-res screens.
  66. ForceDpiAware.Windows();
  67. WindowScaleFactor = ForceDpiAware.GetWindowScaleFactor();
  68. // Delete backup files after updating.
  69. Task.Run(Updater.CleanupUpdate);
  70. Version = ReleaseInformations.GetVersion();
  71. Console.Title = $"Ryujinx Console {Version}";
  72. // NOTE: GTK3 doesn't init X11 in a multi threaded way.
  73. // This ends up causing race condition and abort of XCB when a context is created by SPB (even if SPB do call XInitThreads).
  74. if (OperatingSystem.IsLinux())
  75. {
  76. XInitThreads();
  77. }
  78. string systemPath = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine);
  79. Environment.SetEnvironmentVariable("Path", $"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin")};{systemPath}");
  80. // Hook unhandled exception and process exit events.
  81. GLib.ExceptionManager.UnhandledException += (GLib.UnhandledExceptionArgs e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  82. AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  83. AppDomain.CurrentDomain.ProcessExit += (object sender, EventArgs e) => Exit();
  84. // Setup base data directory.
  85. AppDataManager.Initialize(baseDirPathArg);
  86. // Initialize the configuration.
  87. ConfigurationState.Initialize();
  88. // Initialize the logger system.
  89. LoggerModule.Initialize();
  90. // Initialize Discord integration.
  91. DiscordIntegrationModule.Initialize();
  92. // Sets ImageSharp Jpeg Encoder Quality.
  93. SixLabors.ImageSharp.Configuration.Default.ImageFormatsManager.SetEncoder(JpegFormat.Instance, new JpegEncoder()
  94. {
  95. Quality = 100
  96. });
  97. string localConfigurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
  98. string appDataConfigurationPath = Path.Combine(AppDataManager.BaseDirPath, "Config.json");
  99. // Now load the configuration as the other subsystems are now registered
  100. ConfigurationPath = File.Exists(localConfigurationPath)
  101. ? localConfigurationPath
  102. : File.Exists(appDataConfigurationPath)
  103. ? appDataConfigurationPath
  104. : null;
  105. if (ConfigurationPath == null)
  106. {
  107. // No configuration, we load the default values and save it to disk
  108. ConfigurationPath = appDataConfigurationPath;
  109. ConfigurationState.Instance.LoadDefault();
  110. ConfigurationState.Instance.ToFileFormat().SaveConfig(ConfigurationPath);
  111. }
  112. else
  113. {
  114. if (ConfigurationFileFormat.TryLoad(ConfigurationPath, out ConfigurationFileFormat configurationFileFormat))
  115. {
  116. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  117. }
  118. else
  119. {
  120. ConfigurationState.Instance.LoadDefault();
  121. Logger.Warning?.PrintMsg(LogClass.Application, $"Failed to load config! Loading the default config instead.\nFailed config location {ConfigurationPath}");
  122. }
  123. }
  124. // Logging system information.
  125. PrintSystemInfo();
  126. // Enable OGL multithreading on the driver, when available.
  127. BackendThreading threadingMode = ConfigurationState.Instance.Graphics.BackendThreading;
  128. DriverUtilities.ToggleOGLThreading(threadingMode == BackendThreading.Off);
  129. // Initialize Gtk.
  130. Application.Init();
  131. // Check if keys exists.
  132. bool hasSystemProdKeys = File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys"));
  133. bool hasCommonProdKeys = AppDataManager.Mode == AppDataManager.LaunchMode.UserProfile && File.Exists(Path.Combine(AppDataManager.KeysDirPathUser, "prod.keys"));
  134. if (!hasSystemProdKeys && !hasCommonProdKeys)
  135. {
  136. UserErrorDialog.CreateUserErrorDialog(UserError.NoKeys);
  137. }
  138. // Show the main window UI.
  139. MainWindow mainWindow = new MainWindow();
  140. mainWindow.Show();
  141. if (launchPathArg != null)
  142. {
  143. mainWindow.LoadApplication(launchPathArg, startFullscreenArg);
  144. }
  145. if (ConfigurationState.Instance.CheckUpdatesOnStart.Value && Updater.CanUpdate(false))
  146. {
  147. Updater.BeginParse(mainWindow, false).ContinueWith(task =>
  148. {
  149. Logger.Error?.Print(LogClass.Application, $"Updater Error: {task.Exception}");
  150. }, TaskContinuationOptions.OnlyOnFaulted);
  151. }
  152. Application.Run();
  153. }
  154. private static void PrintSystemInfo()
  155. {
  156. Logger.Notice.Print(LogClass.Application, $"Ryujinx Version: {Version}");
  157. SystemInfo.Gather().Print();
  158. var enabledLogs = Logger.GetEnabledLevels();
  159. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {(enabledLogs.Count == 0 ? "<None>" : string.Join(", ", enabledLogs))}");
  160. if (AppDataManager.Mode == AppDataManager.LaunchMode.Custom)
  161. {
  162. Logger.Notice.Print(LogClass.Application, $"Launch Mode: Custom Path {AppDataManager.BaseDirPath}");
  163. }
  164. else
  165. {
  166. Logger.Notice.Print(LogClass.Application, $"Launch Mode: {AppDataManager.Mode}");
  167. }
  168. }
  169. private static void ProcessUnhandledException(Exception ex, bool isTerminating)
  170. {
  171. Ptc.Close();
  172. PtcProfiler.Stop();
  173. string message = $"Unhandled exception caught: {ex}";
  174. Logger.Error?.PrintMsg(LogClass.Application, message);
  175. if (Logger.Error == null)
  176. {
  177. Logger.Notice.PrintMsg(LogClass.Application, message);
  178. }
  179. if (isTerminating)
  180. {
  181. Exit();
  182. }
  183. }
  184. public static void Exit()
  185. {
  186. DiscordIntegrationModule.Exit();
  187. Ptc.Dispose();
  188. PtcProfiler.Dispose();
  189. Logger.Shutdown();
  190. }
  191. }
  192. }