Program.cs 8.0 KB

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