Program.cs 6.2 KB

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