Program.cs 6.8 KB

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