Program.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using ARMeilleure.Translation.PTC;
  2. using FFmpeg.AutoGen;
  3. using Gtk;
  4. using Ryujinx.Common.Configuration;
  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. // Configure FFmpeg search path
  67. ffmpeg.RootPath = "/lib";
  68. }
  69. string systemPath = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine);
  70. Environment.SetEnvironmentVariable("Path", $"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin")};{systemPath}");
  71. // Hook unhandled exception and process exit events.
  72. GLib.ExceptionManager.UnhandledException += (GLib.UnhandledExceptionArgs e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  73. AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  74. AppDomain.CurrentDomain.ProcessExit += (object sender, EventArgs e) => Exit();
  75. // Setup base data directory.
  76. AppDataManager.Initialize(baseDirPathArg);
  77. // Initialize the configuration.
  78. ConfigurationState.Initialize();
  79. // Initialize the logger system.
  80. LoggerModule.Initialize();
  81. // Initialize Discord integration.
  82. DiscordIntegrationModule.Initialize();
  83. // Sets ImageSharp Jpeg Encoder Quality.
  84. SixLabors.ImageSharp.Configuration.Default.ImageFormatsManager.SetEncoder(JpegFormat.Instance, new JpegEncoder()
  85. {
  86. Quality = 100
  87. });
  88. string localConfigurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
  89. string appDataConfigurationPath = Path.Combine(AppDataManager.BaseDirPath, "Config.json");
  90. // Now load the configuration as the other subsystems are now registered
  91. ConfigurationPath = File.Exists(localConfigurationPath)
  92. ? localConfigurationPath
  93. : File.Exists(appDataConfigurationPath)
  94. ? appDataConfigurationPath
  95. : null;
  96. if (ConfigurationPath == null)
  97. {
  98. // No configuration, we load the default values and save it to disk
  99. ConfigurationPath = appDataConfigurationPath;
  100. ConfigurationState.Instance.LoadDefault();
  101. ConfigurationState.Instance.ToFileFormat().SaveConfig(ConfigurationPath);
  102. }
  103. else
  104. {
  105. if (ConfigurationFileFormat.TryLoad(ConfigurationPath, out ConfigurationFileFormat configurationFileFormat))
  106. {
  107. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  108. }
  109. else
  110. {
  111. ConfigurationState.Instance.LoadDefault();
  112. Logger.Warning?.PrintMsg(LogClass.Application, $"Failed to load config! Loading the default config instead.\nFailed config location {ConfigurationPath}");
  113. }
  114. }
  115. if (startFullscreenArg)
  116. {
  117. ConfigurationState.Instance.Ui.StartFullscreen.Value = true;
  118. }
  119. // Logging system information.
  120. PrintSystemInfo();
  121. // Initialize Gtk.
  122. Application.Init();
  123. // Check if keys exists.
  124. bool hasSystemProdKeys = File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys"));
  125. bool hasCommonProdKeys = AppDataManager.Mode == AppDataManager.LaunchMode.UserProfile && File.Exists(Path.Combine(AppDataManager.KeysDirPathUser, "prod.keys"));
  126. if (!hasSystemProdKeys && !hasCommonProdKeys)
  127. {
  128. UserErrorDialog.CreateUserErrorDialog(UserError.NoKeys);
  129. }
  130. // Force dedicated GPU if we can.
  131. ForceDedicatedGpu.Nvidia();
  132. // Show the main window UI.
  133. MainWindow mainWindow = new MainWindow();
  134. mainWindow.Show();
  135. if (launchPathArg != null)
  136. {
  137. mainWindow.LoadApplication(launchPathArg);
  138. }
  139. if (ConfigurationState.Instance.CheckUpdatesOnStart.Value && Updater.CanUpdate(false))
  140. {
  141. Updater.BeginParse(mainWindow, false).ContinueWith(task =>
  142. {
  143. Logger.Error?.Print(LogClass.Application, $"Updater Error: {task.Exception}");
  144. }, TaskContinuationOptions.OnlyOnFaulted);
  145. }
  146. Application.Run();
  147. }
  148. private static void PrintSystemInfo()
  149. {
  150. Logger.Notice.Print(LogClass.Application, $"Ryujinx Version: {Version}");
  151. SystemInfo.Gather().Print();
  152. var enabledLogs = Logger.GetEnabledLevels();
  153. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {(enabledLogs.Count == 0 ? "<None>" : string.Join(", ", enabledLogs))}");
  154. if (AppDataManager.Mode == AppDataManager.LaunchMode.Custom)
  155. {
  156. Logger.Notice.Print(LogClass.Application, $"Launch Mode: Custom Path {AppDataManager.BaseDirPath}");
  157. }
  158. else
  159. {
  160. Logger.Notice.Print(LogClass.Application, $"Launch Mode: {AppDataManager.Mode}");
  161. }
  162. }
  163. private static void ProcessUnhandledException(Exception ex, bool isTerminating)
  164. {
  165. Ptc.Close();
  166. PtcProfiler.Stop();
  167. string message = $"Unhandled exception caught: {ex}";
  168. Logger.Error?.PrintMsg(LogClass.Application, message);
  169. if (Logger.Error == null)
  170. {
  171. Logger.Notice.PrintMsg(LogClass.Application, message);
  172. }
  173. if (isTerminating)
  174. {
  175. Exit();
  176. }
  177. }
  178. public static void Exit()
  179. {
  180. DiscordIntegrationModule.Exit();
  181. Ptc.Dispose();
  182. PtcProfiler.Dispose();
  183. Logger.Shutdown();
  184. }
  185. }
  186. }