Program.cs 8.3 KB

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