HeadlessRyujinx.Init.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. using DiscordRPC;
  2. using LibHac.Tools.FsSystem;
  3. using Ryujinx.Audio.Backends.SDL2;
  4. using Ryujinx.Ava;
  5. using Ryujinx.Ava.Utilities.Configuration;
  6. using Ryujinx.Common.Configuration;
  7. using Ryujinx.Common.Configuration.Hid;
  8. using Ryujinx.Common.Configuration.Hid.Controller;
  9. using Ryujinx.Common.Configuration.Hid.Controller.Motion;
  10. using Ryujinx.Common.Configuration.Hid.Keyboard;
  11. using Ryujinx.Common.Logging;
  12. using Ryujinx.Common.Utilities;
  13. using Ryujinx.Graphics.GAL;
  14. using Ryujinx.Graphics.GAL.Multithreading;
  15. using Ryujinx.Graphics.Metal;
  16. using Ryujinx.Graphics.OpenGL;
  17. using Ryujinx.Graphics.Vulkan;
  18. using Ryujinx.HLE;
  19. using Ryujinx.Input;
  20. using Silk.NET.Vulkan;
  21. using System;
  22. using System.IO;
  23. using System.Text.Json;
  24. using System.Threading.Tasks;
  25. using ConfigGamepadInputId = Ryujinx.Common.Configuration.Hid.Controller.GamepadInputId;
  26. using ConfigStickInputId = Ryujinx.Common.Configuration.Hid.Controller.StickInputId;
  27. using Key = Ryujinx.Common.Configuration.Hid.Key;
  28. namespace Ryujinx.Headless
  29. {
  30. public partial class HeadlessRyujinx
  31. {
  32. public static void Initialize()
  33. {
  34. // Ensure Discord presence timestamp begins at the absolute start of when Ryujinx is launched
  35. DiscordIntegrationModule.StartedAt = Timestamps.Now;
  36. // Delete backup files after updating.
  37. Task.Run(Updater.CleanupUpdate);
  38. // Hook unhandled exception and process exit events.
  39. AppDomain.CurrentDomain.UnhandledException += (sender, e)
  40. => Program.ProcessUnhandledException(sender, e.ExceptionObject as Exception, e.IsTerminating);
  41. AppDomain.CurrentDomain.ProcessExit += (_, _) => Program.Exit();
  42. // Initialize the configuration.
  43. ConfigurationState.Initialize();
  44. // Initialize Discord integration.
  45. DiscordIntegrationModule.Initialize();
  46. // Logging system information.
  47. Program.PrintSystemInfo();
  48. }
  49. private static InputConfig HandlePlayerConfiguration(string inputProfileName, string inputId, PlayerIndex index)
  50. {
  51. if (inputId == null)
  52. {
  53. if (index == PlayerIndex.Player1)
  54. {
  55. Logger.Info?.Print(LogClass.Application, $"{index} not configured, defaulting to default keyboard.");
  56. // Default to keyboard
  57. inputId = "0";
  58. }
  59. else
  60. {
  61. Logger.Info?.Print(LogClass.Application, $"{index} not configured");
  62. return null;
  63. }
  64. }
  65. IGamepad gamepad = _inputManager.KeyboardDriver.GetGamepad(inputId);
  66. bool isKeyboard = true;
  67. if (gamepad == null)
  68. {
  69. gamepad = _inputManager.GamepadDriver.GetGamepad(inputId);
  70. isKeyboard = false;
  71. if (gamepad == null)
  72. {
  73. Logger.Error?.Print(LogClass.Application, $"{index} gamepad not found (\"{inputId}\")");
  74. return null;
  75. }
  76. }
  77. string gamepadName = gamepad.Name;
  78. gamepad.Dispose();
  79. InputConfig config;
  80. if (inputProfileName == null || inputProfileName.Equals("default"))
  81. {
  82. if (isKeyboard)
  83. {
  84. config = new StandardKeyboardInputConfig
  85. {
  86. Version = InputConfig.CurrentVersion,
  87. Backend = InputBackendType.WindowKeyboard,
  88. Id = null,
  89. ControllerType = ControllerType.JoyconPair,
  90. LeftJoycon = new LeftJoyconCommonConfig<Key>
  91. {
  92. DpadUp = Key.Up,
  93. DpadDown = Key.Down,
  94. DpadLeft = Key.Left,
  95. DpadRight = Key.Right,
  96. ButtonMinus = Key.Minus,
  97. ButtonL = Key.E,
  98. ButtonZl = Key.Q,
  99. ButtonSl = Key.Unbound,
  100. ButtonSr = Key.Unbound,
  101. },
  102. LeftJoyconStick = new JoyconConfigKeyboardStick<Key>
  103. {
  104. StickUp = Key.W,
  105. StickDown = Key.S,
  106. StickLeft = Key.A,
  107. StickRight = Key.D,
  108. StickButton = Key.F,
  109. },
  110. RightJoycon = new RightJoyconCommonConfig<Key>
  111. {
  112. ButtonA = Key.Z,
  113. ButtonB = Key.X,
  114. ButtonX = Key.C,
  115. ButtonY = Key.V,
  116. ButtonPlus = Key.Plus,
  117. ButtonR = Key.U,
  118. ButtonZr = Key.O,
  119. ButtonSl = Key.Unbound,
  120. ButtonSr = Key.Unbound,
  121. },
  122. RightJoyconStick = new JoyconConfigKeyboardStick<Key>
  123. {
  124. StickUp = Key.I,
  125. StickDown = Key.K,
  126. StickLeft = Key.J,
  127. StickRight = Key.L,
  128. StickButton = Key.H,
  129. },
  130. };
  131. }
  132. else
  133. {
  134. bool isNintendoStyle = gamepadName.Contains("Nintendo");
  135. config = new StandardControllerInputConfig
  136. {
  137. Version = InputConfig.CurrentVersion,
  138. Backend = InputBackendType.GamepadSDL2,
  139. Id = null,
  140. ControllerType = ControllerType.JoyconPair,
  141. DeadzoneLeft = 0.1f,
  142. DeadzoneRight = 0.1f,
  143. RangeLeft = 1.0f,
  144. RangeRight = 1.0f,
  145. TriggerThreshold = 0.5f,
  146. LeftJoycon = new LeftJoyconCommonConfig<ConfigGamepadInputId>
  147. {
  148. DpadUp = ConfigGamepadInputId.DpadUp,
  149. DpadDown = ConfigGamepadInputId.DpadDown,
  150. DpadLeft = ConfigGamepadInputId.DpadLeft,
  151. DpadRight = ConfigGamepadInputId.DpadRight,
  152. ButtonMinus = ConfigGamepadInputId.Minus,
  153. ButtonL = ConfigGamepadInputId.LeftShoulder,
  154. ButtonZl = ConfigGamepadInputId.LeftTrigger,
  155. ButtonSl = ConfigGamepadInputId.Unbound,
  156. ButtonSr = ConfigGamepadInputId.Unbound,
  157. },
  158. LeftJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  159. {
  160. Joystick = ConfigStickInputId.Left,
  161. StickButton = ConfigGamepadInputId.LeftStick,
  162. InvertStickX = false,
  163. InvertStickY = false,
  164. Rotate90CW = false,
  165. },
  166. RightJoycon = new RightJoyconCommonConfig<ConfigGamepadInputId>
  167. {
  168. ButtonA = isNintendoStyle ? ConfigGamepadInputId.A : ConfigGamepadInputId.B,
  169. ButtonB = isNintendoStyle ? ConfigGamepadInputId.B : ConfigGamepadInputId.A,
  170. ButtonX = isNintendoStyle ? ConfigGamepadInputId.X : ConfigGamepadInputId.Y,
  171. ButtonY = isNintendoStyle ? ConfigGamepadInputId.Y : ConfigGamepadInputId.X,
  172. ButtonPlus = ConfigGamepadInputId.Plus,
  173. ButtonR = ConfigGamepadInputId.RightShoulder,
  174. ButtonZr = ConfigGamepadInputId.RightTrigger,
  175. ButtonSl = ConfigGamepadInputId.Unbound,
  176. ButtonSr = ConfigGamepadInputId.Unbound,
  177. },
  178. RightJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  179. {
  180. Joystick = ConfigStickInputId.Right,
  181. StickButton = ConfigGamepadInputId.RightStick,
  182. InvertStickX = false,
  183. InvertStickY = false,
  184. Rotate90CW = false,
  185. },
  186. Motion = new StandardMotionConfigController
  187. {
  188. MotionBackend = MotionInputBackendType.GamepadDriver,
  189. EnableMotion = true,
  190. Sensitivity = 100,
  191. GyroDeadzone = 1,
  192. },
  193. Rumble = new RumbleConfigController
  194. {
  195. StrongRumble = 1f,
  196. WeakRumble = 1f,
  197. EnableRumble = false,
  198. },
  199. };
  200. }
  201. }
  202. else
  203. {
  204. string profileBasePath;
  205. if (isKeyboard)
  206. {
  207. profileBasePath = Path.Combine(AppDataManager.ProfilesDirPath, "keyboard");
  208. }
  209. else
  210. {
  211. profileBasePath = Path.Combine(AppDataManager.ProfilesDirPath, "controller");
  212. }
  213. string path = Path.Combine(profileBasePath, inputProfileName + ".json");
  214. if (!File.Exists(path))
  215. {
  216. Logger.Error?.Print(LogClass.Application, $"Input profile \"{inputProfileName}\" not found for \"{inputId}\"");
  217. return null;
  218. }
  219. try
  220. {
  221. config = JsonHelper.DeserializeFromFile(path, _serializerContext.InputConfig);
  222. }
  223. catch (JsonException)
  224. {
  225. Logger.Error?.Print(LogClass.Application, $"Input profile \"{inputProfileName}\" parsing failed for \"{inputId}\"");
  226. return null;
  227. }
  228. }
  229. config.Id = inputId;
  230. config.PlayerIndex = index;
  231. string inputTypeName = isKeyboard ? "Keyboard" : "Gamepad";
  232. Logger.Info?.Print(LogClass.Application, $"{config.PlayerIndex} configured with {inputTypeName} \"{config.Id}\"");
  233. // If both stick ranges are 0 (usually indicative of an outdated profile load) then both sticks will be set to 1.0.
  234. if (config is StandardControllerInputConfig controllerConfig)
  235. {
  236. if (controllerConfig.RangeLeft <= 0.0f && controllerConfig.RangeRight <= 0.0f)
  237. {
  238. controllerConfig.RangeLeft = 1.0f;
  239. controllerConfig.RangeRight = 1.0f;
  240. Logger.Info?.Print(LogClass.Application, $"{config.PlayerIndex} stick range reset. Save the profile now to update your configuration");
  241. }
  242. }
  243. return config;
  244. }
  245. private static IRenderer CreateRenderer(Options options, WindowBase window)
  246. {
  247. if (options.GraphicsBackend == GraphicsBackend.Vulkan && window is VulkanWindow vulkanWindow)
  248. {
  249. string preferredGpuId = string.Empty;
  250. Vk api = Vk.GetApi();
  251. if (!string.IsNullOrEmpty(options.PreferredGPUVendor))
  252. {
  253. string preferredGpuVendor = options.PreferredGPUVendor.ToLowerInvariant();
  254. var devices = VulkanRenderer.GetPhysicalDevices(api);
  255. foreach (var device in devices)
  256. {
  257. if (device.Vendor.ToLowerInvariant() == preferredGpuVendor)
  258. {
  259. preferredGpuId = device.Id;
  260. break;
  261. }
  262. }
  263. }
  264. return new VulkanRenderer(
  265. api,
  266. (instance, vk) => new SurfaceKHR((ulong)(vulkanWindow.CreateWindowSurface(instance.Handle))),
  267. vulkanWindow.GetRequiredInstanceExtensions,
  268. preferredGpuId);
  269. }
  270. if (options.GraphicsBackend == GraphicsBackend.Metal && window is MetalWindow metalWindow && OperatingSystem.IsMacOS())
  271. {
  272. return new MetalRenderer(metalWindow.GetLayer);
  273. }
  274. return new OpenGLRenderer();
  275. }
  276. private static Switch InitializeEmulationContext(WindowBase window, IRenderer renderer, Options options)
  277. {
  278. BackendThreading threadingMode = options.BackendThreading;
  279. bool threadedGAL = threadingMode == BackendThreading.On || (threadingMode == BackendThreading.Auto && renderer.PreferThreading);
  280. if (threadedGAL)
  281. {
  282. renderer = new ThreadedRenderer(renderer);
  283. }
  284. HLEConfiguration configuration = new(_virtualFileSystem,
  285. _libHacHorizonManager,
  286. _contentManager,
  287. _accountManager,
  288. _userChannelPersistence,
  289. renderer,
  290. new SDL2HardwareDeviceDriver(),
  291. options.DramSize,
  292. window,
  293. options.SystemLanguage,
  294. options.SystemRegion,
  295. options.VSyncMode,
  296. !options.DisableDockedMode,
  297. !options.DisablePTC,
  298. options.EnableInternetAccess,
  299. !options.DisableFsIntegrityChecks ? IntegrityCheckLevel.ErrorOnInvalid : IntegrityCheckLevel.None,
  300. options.FsGlobalAccessLogMode,
  301. options.SystemTimeOffset,
  302. options.SystemTimeZone,
  303. options.MemoryManagerMode,
  304. options.IgnoreMissingServices,
  305. options.AspectRatio,
  306. options.AudioVolume,
  307. options.UseHypervisor ?? true,
  308. options.MultiplayerLanInterfaceId,
  309. Common.Configuration.Multiplayer.MultiplayerMode.Disabled,
  310. false,
  311. string.Empty,
  312. string.Empty,
  313. options.CustomVSyncInterval);
  314. return new Switch(configuration);
  315. }
  316. }
  317. }