Program.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 hasSystemProdKeys = File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys"));
  112. bool hasCommonProdKeys = AppDataManager.Mode == AppDataManager.LaunchMode.UserProfile && File.Exists(Path.Combine(AppDataManager.KeysDirPathUser, "prod.keys"));
  113. if (!hasSystemProdKeys && !hasCommonProdKeys)
  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).ContinueWith(task =>
  129. {
  130. Logger.Error?.Print(LogClass.Application, $"Updater Error: {task.Exception}");
  131. }, TaskContinuationOptions.OnlyOnFaulted);
  132. }
  133. Application.Run();
  134. }
  135. private static void PrintSystemInfo()
  136. {
  137. Logger.Notice.Print(LogClass.Application, $"Ryujinx Version: {Version}");
  138. SystemInfo.Gather().Print();
  139. var enabledLogs = Logger.GetEnabledLevels();
  140. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {(enabledLogs.Count == 0 ? "<None>" : string.Join(", ", enabledLogs))}");
  141. if (AppDataManager.Mode == AppDataManager.LaunchMode.Custom)
  142. {
  143. Logger.Notice.Print(LogClass.Application, $"Launch Mode: Custom Path {AppDataManager.BaseDirPath}");
  144. }
  145. else
  146. {
  147. Logger.Notice.Print(LogClass.Application, $"Launch Mode: {AppDataManager.Mode}");
  148. }
  149. }
  150. private static void ProcessUnhandledException(Exception ex, bool isTerminating)
  151. {
  152. Ptc.Close();
  153. PtcProfiler.Stop();
  154. string message = $"Unhandled exception caught: {ex}";
  155. Logger.Error?.PrintMsg(LogClass.Application, message);
  156. if (Logger.Error == null)
  157. {
  158. Logger.Notice.PrintMsg(LogClass.Application, message);
  159. }
  160. if (isTerminating)
  161. {
  162. Exit();
  163. }
  164. }
  165. public static void Exit()
  166. {
  167. DiscordIntegrationModule.Exit();
  168. Ptc.Dispose();
  169. PtcProfiler.Dispose();
  170. Logger.Shutdown();
  171. }
  172. }
  173. }