Program.cs 8.3 KB

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