Program.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using ARMeilleure.Translation.PTC;
  2. using Gtk;
  3. using OpenTK;
  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 System;
  13. using System.IO;
  14. using System.Reflection;
  15. using System.Threading.Tasks;
  16. namespace Ryujinx
  17. {
  18. class Program
  19. {
  20. public static string Version { get; private set; }
  21. public static string ConfigurationPath { get; set; }
  22. static void Main(string[] args)
  23. {
  24. // Parse Arguments.
  25. string launchPathArg = null;
  26. string baseDirPathArg = null;
  27. bool startFullscreenArg = false;
  28. for (int i = 0; i < args.Length; ++i)
  29. {
  30. string arg = args[i];
  31. if (arg == "-r" || arg == "--root-data-dir")
  32. {
  33. if (i + 1 >= args.Length)
  34. {
  35. Logger.Error?.Print(LogClass.Application, $"Invalid option '{arg}'");
  36. continue;
  37. }
  38. baseDirPathArg = args[++i];
  39. }
  40. else if (arg == "-f" || arg == "--fullscreen")
  41. {
  42. startFullscreenArg = true;
  43. }
  44. else if (launchPathArg == null)
  45. {
  46. launchPathArg = arg;
  47. }
  48. }
  49. // Delete backup files after updating.
  50. Task.Run(Updater.CleanupUpdate);
  51. Toolkit.Init(new ToolkitOptions
  52. {
  53. Backend = PlatformBackend.PreferNative
  54. });
  55. Version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
  56. Console.Title = $"Ryujinx Console {Version}";
  57. string systemPath = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine);
  58. Environment.SetEnvironmentVariable("Path", $"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin")};{systemPath}");
  59. // Hook unhandled exception and process exit events.
  60. GLib.ExceptionManager.UnhandledException += (GLib.UnhandledExceptionArgs e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  61. AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  62. AppDomain.CurrentDomain.ProcessExit += (object sender, EventArgs e) => Exit();
  63. // Setup base data directory.
  64. AppDataManager.Initialize(baseDirPathArg);
  65. // Initialize the configuration.
  66. ConfigurationState.Initialize();
  67. // Initialize the logger system.
  68. LoggerModule.Initialize();
  69. // Initialize Discord integration.
  70. DiscordIntegrationModule.Initialize();
  71. string localConfigurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
  72. string appDataConfigurationPath = Path.Combine(AppDataManager.BaseDirPath, "Config.json");
  73. // Now load the configuration as the other subsystems are now registered.
  74. if (File.Exists(localConfigurationPath))
  75. {
  76. ConfigurationPath = localConfigurationPath;
  77. ConfigurationFileFormat configurationFileFormat = ConfigurationFileFormat.Load(localConfigurationPath);
  78. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  79. }
  80. else if (File.Exists(appDataConfigurationPath))
  81. {
  82. ConfigurationPath = appDataConfigurationPath;
  83. ConfigurationFileFormat configurationFileFormat = ConfigurationFileFormat.Load(appDataConfigurationPath);
  84. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  85. }
  86. else
  87. {
  88. // No configuration, we load the default values and save it on disk.
  89. ConfigurationPath = appDataConfigurationPath;
  90. ConfigurationState.Instance.LoadDefault();
  91. ConfigurationState.Instance.ToFileFormat().SaveConfig(appDataConfigurationPath);
  92. }
  93. if (startFullscreenArg)
  94. {
  95. ConfigurationState.Instance.Ui.StartFullscreen.Value = true;
  96. }
  97. // Logging system informations.
  98. PrintSystemInfo();
  99. // Initialize Gtk.
  100. Application.Init();
  101. // Check if keys exists.
  102. bool hasGlobalProdKeys = File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys"));
  103. bool hasAltProdKeys = !AppDataManager.IsCustomBasePath && File.Exists(Path.Combine(AppDataManager.KeysDirPathAlt, "prod.keys"));
  104. if (!hasGlobalProdKeys && !hasAltProdKeys)
  105. {
  106. UserErrorDialog.CreateUserErrorDialog(UserError.NoKeys);
  107. }
  108. // Force dedicated GPU if we can.
  109. ForceDedicatedGpu.Nvidia();
  110. // Show the main window UI.
  111. MainWindow mainWindow = new MainWindow();
  112. mainWindow.Show();
  113. if (launchPathArg != null)
  114. {
  115. mainWindow.LoadApplication(launchPathArg);
  116. }
  117. if (ConfigurationState.Instance.CheckUpdatesOnStart.Value && Updater.CanUpdate(false))
  118. {
  119. _ = Updater.BeginParse(mainWindow, false);
  120. }
  121. Application.Run();
  122. }
  123. private static void PrintSystemInfo()
  124. {
  125. Logger.Notice.Print(LogClass.Application, $"Ryujinx Version: {Version}");
  126. Logger.Notice.Print(LogClass.Application, $"Operating System: {SystemInfo.Instance.OsDescription}");
  127. Logger.Notice.Print(LogClass.Application, $"CPU: {SystemInfo.Instance.CpuName}");
  128. Logger.Notice.Print(LogClass.Application, $"Total RAM: {SystemInfo.Instance.RamSizeInMB}");
  129. var enabledLogs = Logger.GetEnabledLevels();
  130. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {(enabledLogs.Count == 0 ? "<None>" : string.Join(", ", enabledLogs))}");
  131. if (AppDataManager.IsCustomBasePath)
  132. {
  133. Logger.Notice.Print(LogClass.Application, $"Custom Data Directory: {AppDataManager.BaseDirPath}");
  134. }
  135. }
  136. private static void ProcessUnhandledException(Exception ex, bool isTerminating)
  137. {
  138. Ptc.Close();
  139. PtcProfiler.Stop();
  140. string message = $"Unhandled exception caught: {ex}";
  141. Logger.Error?.PrintMsg(LogClass.Application, message);
  142. if (Logger.Error == null)
  143. {
  144. Logger.Notice.PrintMsg(LogClass.Application, message);
  145. }
  146. if (isTerminating)
  147. {
  148. Exit();
  149. }
  150. }
  151. public static void Exit()
  152. {
  153. DiscordIntegrationModule.Exit();
  154. Ptc.Dispose();
  155. PtcProfiler.Dispose();
  156. Logger.Shutdown();
  157. }
  158. }
  159. }