Program.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using ARMeilleure.Translation.PTC;
  2. using Gtk;
  3. using Ryujinx.Common.Logging;
  4. using Ryujinx.Common.SystemInfo;
  5. using Ryujinx.Configuration;
  6. using Ryujinx.Debugger.Profiler;
  7. using Ryujinx.Ui;
  8. using OpenTK;
  9. using System;
  10. using System.IO;
  11. using System.Reflection;
  12. namespace Ryujinx
  13. {
  14. class Program
  15. {
  16. public static string Version { get; private set; }
  17. public static string ConfigurationPath { get; set; }
  18. static void Main(string[] args)
  19. {
  20. Toolkit.Init(new ToolkitOptions
  21. {
  22. Backend = PlatformBackend.PreferNative,
  23. EnableHighResolution = true
  24. });
  25. Version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
  26. Console.Title = $"Ryujinx Console {Version}";
  27. string systemPath = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine);
  28. Environment.SetEnvironmentVariable("Path", $"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin")};{systemPath}");
  29. // Hook unhandled exception and process exit events
  30. GLib.ExceptionManager.UnhandledException += (GLib.UnhandledExceptionArgs e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  31. AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  32. AppDomain.CurrentDomain.ProcessExit += (object sender, EventArgs e) => ProgramExit();
  33. // Initialize the configuration
  34. ConfigurationState.Initialize();
  35. // Initialize the logger system
  36. LoggerModule.Initialize();
  37. // Initialize Discord integration
  38. DiscordIntegrationModule.Initialize();
  39. string localConfigurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
  40. string globalBasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Ryujinx");
  41. string globalConfigurationPath = Path.Combine(globalBasePath, "Config.json");
  42. // Now load the configuration as the other subsystems are now registered
  43. if (File.Exists(localConfigurationPath))
  44. {
  45. ConfigurationPath = localConfigurationPath;
  46. ConfigurationFileFormat configurationFileFormat = ConfigurationFileFormat.Load(localConfigurationPath);
  47. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  48. }
  49. else if (File.Exists(globalConfigurationPath))
  50. {
  51. ConfigurationPath = globalConfigurationPath;
  52. ConfigurationFileFormat configurationFileFormat = ConfigurationFileFormat.Load(globalConfigurationPath);
  53. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  54. }
  55. else
  56. {
  57. // No configuration, we load the default values and save it on disk
  58. ConfigurationPath = globalConfigurationPath;
  59. // Make sure to create the Ryujinx directory if needed.
  60. Directory.CreateDirectory(globalBasePath);
  61. ConfigurationState.Instance.LoadDefault();
  62. ConfigurationState.Instance.ToFileFormat().SaveConfig(globalConfigurationPath);
  63. }
  64. PrintSystemInfo();
  65. Profile.Initialize();
  66. Application.Init();
  67. string globalProdKeysPath = Path.Combine(globalBasePath, "system", "prod.keys");
  68. string userProfilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".switch", "prod.keys");
  69. if (!File.Exists(globalProdKeysPath) && !File.Exists(userProfilePath) && !Migration.IsMigrationNeeded())
  70. {
  71. GtkDialog.CreateWarningDialog("Key file was not found", "Please refer to `KEYS.md` for more info");
  72. }
  73. MainWindow mainWindow = new MainWindow();
  74. mainWindow.Show();
  75. if (args.Length == 1)
  76. {
  77. mainWindow.LoadApplication(args[0]);
  78. }
  79. Application.Run();
  80. }
  81. private static void PrintSystemInfo()
  82. {
  83. Logger.Notice.Print(LogClass.Application, $"Ryujinx Version: {Version}");
  84. Logger.Notice.Print(LogClass.Application, $"Operating System: {SystemInfo.Instance.OsDescription}");
  85. Logger.Notice.Print(LogClass.Application, $"CPU: {SystemInfo.Instance.CpuName}");
  86. Logger.Notice.Print(LogClass.Application, $"Total RAM: {SystemInfo.Instance.RamSizeInMB}");
  87. var enabledLogs = Logger.GetEnabledLevels();
  88. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {(enabledLogs.Count == 0 ? "<None>" : string.Join(", ", enabledLogs))}");
  89. }
  90. private static void ProcessUnhandledException(Exception e, bool isTerminating)
  91. {
  92. Ptc.Close();
  93. PtcProfiler.Stop();
  94. string message = $"Unhandled exception caught: {e}";
  95. Logger.Error?.PrintMsg(LogClass.Application, message);
  96. if (Logger.Error == null) Logger.Notice.PrintMsg(LogClass.Application, message);
  97. if (isTerminating)
  98. {
  99. ProgramExit();
  100. }
  101. }
  102. private static void ProgramExit()
  103. {
  104. Ptc.Dispose();
  105. PtcProfiler.Dispose();
  106. Logger.Shutdown();
  107. }
  108. }
  109. }