Program.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. // Enforce loading of Mono.Posix.NETStandard to avoid .NET runtime lazy loading it during an update.
  55. Assembly.Load("Mono.Posix.NETStandard");
  56. // Make process DPI aware for proper window sizing on high-res screens.
  57. ForceDpiAware.Windows();
  58. WindowScaleFactor = ForceDpiAware.GetWindowScaleFactor();
  59. // Delete backup files after updating.
  60. Task.Run(Updater.CleanupUpdate);
  61. Version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
  62. Console.Title = $"Ryujinx Console {Version}";
  63. // NOTE: GTK3 doesn't init X11 in a multi threaded way.
  64. // This ends up causing race condition and abort of XCB when a context is created by SPB (even if SPB do call XInitThreads).
  65. if (OperatingSystem.IsLinux())
  66. {
  67. XInitThreads();
  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. // Logging system information.
  116. PrintSystemInfo();
  117. // Enable OGL multithreading on the driver, when available.
  118. BackendThreading threadingMode = ConfigurationState.Instance.Graphics.BackendThreading;
  119. DriverUtilities.ToggleOGLThreading(threadingMode == BackendThreading.Off);
  120. // Initialize Gtk.
  121. Application.Init();
  122. // Check if keys exists.
  123. bool hasSystemProdKeys = File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys"));
  124. bool hasCommonProdKeys = AppDataManager.Mode == AppDataManager.LaunchMode.UserProfile && File.Exists(Path.Combine(AppDataManager.KeysDirPathUser, "prod.keys"));
  125. if (!hasSystemProdKeys && !hasCommonProdKeys)
  126. {
  127. UserErrorDialog.CreateUserErrorDialog(UserError.NoKeys);
  128. }
  129. // Show the main window UI.
  130. MainWindow mainWindow = new MainWindow();
  131. mainWindow.Show();
  132. if (launchPathArg != null)
  133. {
  134. mainWindow.LoadApplication(launchPathArg, startFullscreenArg);
  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. }