Program.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. ConfigurationPath = File.Exists(localConfigurationPath)
  75. ? localConfigurationPath
  76. : File.Exists(appDataConfigurationPath)
  77. ? appDataConfigurationPath
  78. : null;
  79. if (ConfigurationPath == null)
  80. {
  81. // No configuration, we load the default values and save it to disk
  82. ConfigurationPath = appDataConfigurationPath;
  83. ConfigurationState.Instance.LoadDefault();
  84. ConfigurationState.Instance.ToFileFormat().SaveConfig(ConfigurationPath);
  85. }
  86. else
  87. {
  88. if (ConfigurationFileFormat.TryLoad(ConfigurationPath, out ConfigurationFileFormat configurationFileFormat))
  89. {
  90. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  91. }
  92. else
  93. {
  94. ConfigurationState.Instance.LoadDefault();
  95. Logger.Warning?.PrintMsg(LogClass.Application, $"Failed to load config! Loading the default config instead.\nFailed config location {ConfigurationPath}");
  96. }
  97. }
  98. if (startFullscreenArg)
  99. {
  100. ConfigurationState.Instance.Ui.StartFullscreen.Value = true;
  101. }
  102. // Logging system information.
  103. PrintSystemInfo();
  104. // Initialize Gtk.
  105. Application.Init();
  106. // Check if keys exists.
  107. bool hasGlobalProdKeys = File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys"));
  108. bool hasAltProdKeys = !AppDataManager.IsCustomBasePath && File.Exists(Path.Combine(AppDataManager.KeysDirPathAlt, "prod.keys"));
  109. if (!hasGlobalProdKeys && !hasAltProdKeys)
  110. {
  111. UserErrorDialog.CreateUserErrorDialog(UserError.NoKeys);
  112. }
  113. // Force dedicated GPU if we can.
  114. ForceDedicatedGpu.Nvidia();
  115. // Show the main window UI.
  116. MainWindow mainWindow = new MainWindow();
  117. mainWindow.Show();
  118. if (launchPathArg != null)
  119. {
  120. mainWindow.LoadApplication(launchPathArg);
  121. }
  122. if (ConfigurationState.Instance.CheckUpdatesOnStart.Value && Updater.CanUpdate(false))
  123. {
  124. _ = Updater.BeginParse(mainWindow, false);
  125. }
  126. Application.Run();
  127. }
  128. private static void PrintSystemInfo()
  129. {
  130. Logger.Notice.Print(LogClass.Application, $"Ryujinx Version: {Version}");
  131. Logger.Notice.Print(LogClass.Application, $"Operating System: {SystemInfo.Instance.OsDescription}");
  132. Logger.Notice.Print(LogClass.Application, $"CPU: {SystemInfo.Instance.CpuName}");
  133. Logger.Notice.Print(LogClass.Application, $"Total RAM: {SystemInfo.Instance.RamSizeInMB}");
  134. var enabledLogs = Logger.GetEnabledLevels();
  135. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {(enabledLogs.Count == 0 ? "<None>" : string.Join(", ", enabledLogs))}");
  136. if (AppDataManager.IsCustomBasePath)
  137. {
  138. Logger.Notice.Print(LogClass.Application, $"Custom Data Directory: {AppDataManager.BaseDirPath}");
  139. }
  140. }
  141. private static void ProcessUnhandledException(Exception ex, bool isTerminating)
  142. {
  143. Ptc.Close();
  144. PtcProfiler.Stop();
  145. string message = $"Unhandled exception caught: {ex}";
  146. Logger.Error?.PrintMsg(LogClass.Application, message);
  147. if (Logger.Error == null)
  148. {
  149. Logger.Notice.PrintMsg(LogClass.Application, message);
  150. }
  151. if (isTerminating)
  152. {
  153. Exit();
  154. }
  155. }
  156. public static void Exit()
  157. {
  158. DiscordIntegrationModule.Exit();
  159. Ptc.Dispose();
  160. PtcProfiler.Dispose();
  161. Logger.Shutdown();
  162. }
  163. }
  164. }