Program.cs 7.6 KB

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