Program.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 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. string localConfigurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
  81. string appDataConfigurationPath = Path.Combine(AppDataManager.BaseDirPath, "Config.json");
  82. // Now load the configuration as the other subsystems are now registered
  83. ConfigurationPath = File.Exists(localConfigurationPath)
  84. ? localConfigurationPath
  85. : File.Exists(appDataConfigurationPath)
  86. ? appDataConfigurationPath
  87. : null;
  88. if (ConfigurationPath == null)
  89. {
  90. // No configuration, we load the default values and save it to disk
  91. ConfigurationPath = appDataConfigurationPath;
  92. ConfigurationState.Instance.LoadDefault();
  93. ConfigurationState.Instance.ToFileFormat().SaveConfig(ConfigurationPath);
  94. }
  95. else
  96. {
  97. if (ConfigurationFileFormat.TryLoad(ConfigurationPath, out ConfigurationFileFormat configurationFileFormat))
  98. {
  99. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  100. }
  101. else
  102. {
  103. ConfigurationState.Instance.LoadDefault();
  104. Logger.Warning?.PrintMsg(LogClass.Application, $"Failed to load config! Loading the default config instead.\nFailed config location {ConfigurationPath}");
  105. }
  106. }
  107. if (startFullscreenArg)
  108. {
  109. ConfigurationState.Instance.Ui.StartFullscreen.Value = true;
  110. }
  111. // Logging system information.
  112. PrintSystemInfo();
  113. // Force dedicated GPU if we can.
  114. ForceDedicatedGpu.Nvidia();
  115. // Enable OGL multithreading on the driver, when available.
  116. DriverUtilities.ToggleOGLThreading(true);
  117. // Initialize Gtk.
  118. Application.Init();
  119. // Check if keys exists.
  120. bool hasSystemProdKeys = File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys"));
  121. bool hasCommonProdKeys = AppDataManager.Mode == AppDataManager.LaunchMode.UserProfile && File.Exists(Path.Combine(AppDataManager.KeysDirPathUser, "prod.keys"));
  122. if (!hasSystemProdKeys && !hasCommonProdKeys)
  123. {
  124. UserErrorDialog.CreateUserErrorDialog(UserError.NoKeys);
  125. }
  126. // Show the main window UI.
  127. MainWindow mainWindow = new MainWindow();
  128. mainWindow.Show();
  129. if (launchPathArg != null)
  130. {
  131. mainWindow.LoadApplication(launchPathArg);
  132. }
  133. if (ConfigurationState.Instance.CheckUpdatesOnStart.Value && Updater.CanUpdate(false))
  134. {
  135. Updater.BeginParse(mainWindow, false).ContinueWith(task =>
  136. {
  137. Logger.Error?.Print(LogClass.Application, $"Updater Error: {task.Exception}");
  138. }, TaskContinuationOptions.OnlyOnFaulted);
  139. }
  140. Application.Run();
  141. }
  142. private static void PrintSystemInfo()
  143. {
  144. Logger.Notice.Print(LogClass.Application, $"Ryujinx Version: {Version}");
  145. SystemInfo.Gather().Print();
  146. var enabledLogs = Logger.GetEnabledLevels();
  147. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {(enabledLogs.Count == 0 ? "<None>" : string.Join(", ", enabledLogs))}");
  148. if (AppDataManager.Mode == AppDataManager.LaunchMode.Custom)
  149. {
  150. Logger.Notice.Print(LogClass.Application, $"Launch Mode: Custom Path {AppDataManager.BaseDirPath}");
  151. }
  152. else
  153. {
  154. Logger.Notice.Print(LogClass.Application, $"Launch Mode: {AppDataManager.Mode}");
  155. }
  156. }
  157. private static void ProcessUnhandledException(Exception ex, bool isTerminating)
  158. {
  159. Ptc.Close();
  160. PtcProfiler.Stop();
  161. string message = $"Unhandled exception caught: {ex}";
  162. Logger.Error?.PrintMsg(LogClass.Application, message);
  163. if (Logger.Error == null)
  164. {
  165. Logger.Notice.PrintMsg(LogClass.Application, message);
  166. }
  167. if (isTerminating)
  168. {
  169. Exit();
  170. }
  171. }
  172. public static void Exit()
  173. {
  174. DiscordIntegrationModule.Exit();
  175. Ptc.Dispose();
  176. PtcProfiler.Dispose();
  177. Logger.Shutdown();
  178. }
  179. }
  180. }