Program.cs 8.4 KB

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