Program.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using Avalonia;
  2. using Avalonia.Threading;
  3. using Ryujinx.Ava.UI.Windows;
  4. using Ryujinx.Common;
  5. using Ryujinx.Common.Configuration;
  6. using Ryujinx.Common.GraphicsDriver;
  7. using Ryujinx.Common.Logging;
  8. using Ryujinx.Common.SystemInterop;
  9. using Ryujinx.Common.SystemInfo;
  10. using Ryujinx.Modules;
  11. using Ryujinx.SDL2.Common;
  12. using Ryujinx.Ui.Common;
  13. using Ryujinx.Ui.Common.Configuration;
  14. using Ryujinx.Ui.Common.Helper;
  15. using System;
  16. using System.Diagnostics;
  17. using System.IO;
  18. using System.Runtime.InteropServices;
  19. using System.Runtime.Versioning;
  20. using System.Threading.Tasks;
  21. namespace Ryujinx.Ava
  22. {
  23. internal partial class Program
  24. {
  25. public static double WindowScaleFactor { get; set; }
  26. public static double DesktopScaleFactor { get; set; } = 1.0;
  27. public static string Version { get; private set; }
  28. public static string ConfigurationPath { get; private set; }
  29. public static bool PreviewerDetached { get; private set; }
  30. [LibraryImport("user32.dll", SetLastError = true)]
  31. public static partial int MessageBoxA(IntPtr hWnd, [MarshalAs(UnmanagedType.LPStr)] string text, [MarshalAs(UnmanagedType.LPStr)] string caption, uint type);
  32. private const uint MB_ICONWARNING = 0x30;
  33. [SupportedOSPlatform("linux")]
  34. static void RegisterMimeTypes()
  35. {
  36. if (ReleaseInformation.IsFlatHubBuild())
  37. {
  38. return;
  39. }
  40. string mimeDbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".local", "share", "mime");
  41. if (!File.Exists(Path.Combine(mimeDbPath, "packages", "Ryujinx.xml")))
  42. {
  43. string mimeTypesFile = Path.Combine(ReleaseInformation.GetBaseApplicationDirectory(), "mime", "Ryujinx.xml");
  44. using Process mimeProcess = new();
  45. mimeProcess.StartInfo.FileName = "xdg-mime";
  46. mimeProcess.StartInfo.Arguments = $"install --novendor --mode user {mimeTypesFile}";
  47. mimeProcess.Start();
  48. mimeProcess.WaitForExit();
  49. if (mimeProcess.ExitCode != 0)
  50. {
  51. Logger.Error?.PrintMsg(LogClass.Application, $"Unable to install mime types. Make sure xdg-utils is installed. Process exited with code: {mimeProcess.ExitCode}");
  52. return;
  53. }
  54. using Process updateMimeProcess = new();
  55. updateMimeProcess.StartInfo.FileName = "update-mime-database";
  56. updateMimeProcess.StartInfo.Arguments = mimeDbPath;
  57. updateMimeProcess.Start();
  58. updateMimeProcess.WaitForExit();
  59. if (updateMimeProcess.ExitCode != 0)
  60. {
  61. Logger.Error?.PrintMsg(LogClass.Application, $"Could not update local mime database. Process exited with code: {updateMimeProcess.ExitCode}");
  62. }
  63. }
  64. }
  65. public static void Main(string[] args)
  66. {
  67. Version = ReleaseInformation.GetVersion();
  68. if (OperatingSystem.IsWindows() && !OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17134))
  69. {
  70. _ = MessageBoxA(IntPtr.Zero, "You are running an outdated version of Windows.\n\nStarting on June 1st 2022, Ryujinx will only support Windows 10 1803 and newer.\n", $"Ryujinx {Version}", MB_ICONWARNING);
  71. }
  72. PreviewerDetached = true;
  73. Initialize(args);
  74. BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
  75. }
  76. public static AppBuilder BuildAvaloniaApp()
  77. {
  78. return AppBuilder.Configure<App>()
  79. .UsePlatformDetect()
  80. .With(new X11PlatformOptions
  81. {
  82. EnableMultiTouch = true,
  83. EnableIme = true,
  84. UseEGL = false,
  85. UseGpu = true
  86. })
  87. .With(new Win32PlatformOptions
  88. {
  89. EnableMultitouch = true,
  90. UseWgl = false,
  91. AllowEglInitialization = false,
  92. CompositionBackdropCornerRadius = 8.0f,
  93. })
  94. .UseSkia()
  95. .LogToTrace();
  96. }
  97. private static void Initialize(string[] args)
  98. {
  99. // Parse arguments
  100. CommandLineState.ParseArguments(args);
  101. // Delete backup files after updating.
  102. Task.Run(Updater.CleanupUpdate);
  103. Console.Title = $"Ryujinx Console {Version}";
  104. // Hook unhandled exception and process exit events.
  105. AppDomain.CurrentDomain.UnhandledException += (sender, e) => ProcessUnhandledException(e.ExceptionObject as Exception, e.IsTerminating);
  106. AppDomain.CurrentDomain.ProcessExit += (sender, e) => Exit();
  107. // Setup base data directory.
  108. AppDataManager.Initialize(CommandLineState.BaseDirPathArg);
  109. // Initialize the configuration.
  110. ConfigurationState.Initialize();
  111. // Initialize the logger system.
  112. LoggerModule.Initialize();
  113. // Register mime types on linux.
  114. if (OperatingSystem.IsLinux())
  115. {
  116. RegisterMimeTypes();
  117. }
  118. // Initialize Discord integration.
  119. DiscordIntegrationModule.Initialize();
  120. // Initialize SDL2 driver
  121. SDL2Driver.MainThreadDispatcher = action => Dispatcher.UIThread.InvokeAsync(action, DispatcherPriority.Input);
  122. ReloadConfig();
  123. ForceDpiAware.Windows();
  124. WindowScaleFactor = ForceDpiAware.GetWindowScaleFactor();
  125. // Logging system information.
  126. PrintSystemInfo();
  127. // Enable OGL multithreading on the driver, when available.
  128. DriverUtilities.ToggleOGLThreading(ConfigurationState.Instance.Graphics.BackendThreading == BackendThreading.Off);
  129. // Check if keys exists.
  130. if (!File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys")))
  131. {
  132. if (!(AppDataManager.Mode == AppDataManager.LaunchMode.UserProfile && File.Exists(Path.Combine(AppDataManager.KeysDirPathUser, "prod.keys"))))
  133. {
  134. MainWindow.ShowKeyErrorOnLoad = true;
  135. }
  136. }
  137. if (CommandLineState.LaunchPathArg != null)
  138. {
  139. MainWindow.DeferLoadApplication(CommandLineState.LaunchPathArg, CommandLineState.StartFullscreenArg);
  140. }
  141. }
  142. public static void ReloadConfig()
  143. {
  144. string localConfigurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
  145. string appDataConfigurationPath = Path.Combine(AppDataManager.BaseDirPath, "Config.json");
  146. // Now load the configuration as the other subsystems are now registered
  147. if (File.Exists(localConfigurationPath))
  148. {
  149. ConfigurationPath = localConfigurationPath;
  150. }
  151. else if (File.Exists(appDataConfigurationPath))
  152. {
  153. ConfigurationPath = appDataConfigurationPath;
  154. }
  155. if (ConfigurationPath == null)
  156. {
  157. // No configuration, we load the default values and save it to disk
  158. ConfigurationPath = appDataConfigurationPath;
  159. ConfigurationState.Instance.LoadDefault();
  160. ConfigurationState.Instance.ToFileFormat().SaveConfig(ConfigurationPath);
  161. }
  162. else
  163. {
  164. if (ConfigurationFileFormat.TryLoad(ConfigurationPath, out ConfigurationFileFormat configurationFileFormat))
  165. {
  166. ConfigurationState.Instance.Load(configurationFileFormat, ConfigurationPath);
  167. }
  168. else
  169. {
  170. ConfigurationState.Instance.LoadDefault();
  171. Logger.Warning?.PrintMsg(LogClass.Application, $"Failed to load config! Loading the default config instead.\nFailed config location {ConfigurationPath}");
  172. }
  173. }
  174. // Check if graphics backend was overridden
  175. if (CommandLineState.OverrideGraphicsBackend != null)
  176. {
  177. if (CommandLineState.OverrideGraphicsBackend.ToLower() == "opengl")
  178. {
  179. ConfigurationState.Instance.Graphics.GraphicsBackend.Value = GraphicsBackend.OpenGl;
  180. }
  181. else if (CommandLineState.OverrideGraphicsBackend.ToLower() == "vulkan")
  182. {
  183. ConfigurationState.Instance.Graphics.GraphicsBackend.Value = GraphicsBackend.Vulkan;
  184. }
  185. }
  186. }
  187. private static void PrintSystemInfo()
  188. {
  189. Logger.Notice.Print(LogClass.Application, $"Ryujinx Version: {Version}");
  190. SystemInfo.Gather().Print();
  191. Logger.Notice.Print(LogClass.Application, $"Logs Enabled: {(Logger.GetEnabledLevels().Count == 0 ? "<None>" : string.Join(", ", Logger.GetEnabledLevels()))}");
  192. if (AppDataManager.Mode == AppDataManager.LaunchMode.Custom)
  193. {
  194. Logger.Notice.Print(LogClass.Application, $"Launch Mode: Custom Path {AppDataManager.BaseDirPath}");
  195. }
  196. else
  197. {
  198. Logger.Notice.Print(LogClass.Application, $"Launch Mode: {AppDataManager.Mode}");
  199. }
  200. }
  201. private static void ProcessUnhandledException(Exception ex, bool isTerminating)
  202. {
  203. string message = $"Unhandled exception caught: {ex}";
  204. Logger.Error?.PrintMsg(LogClass.Application, message);
  205. if (Logger.Error == null)
  206. {
  207. Logger.Notice.PrintMsg(LogClass.Application, message);
  208. }
  209. if (isTerminating)
  210. {
  211. Exit();
  212. }
  213. }
  214. public static void Exit()
  215. {
  216. DiscordIntegrationModule.Exit();
  217. Logger.Shutdown();
  218. }
  219. }
  220. }