Program.cs 8.3 KB

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