Program.cs 6.5 KB

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