Program.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. using ARMeilleure.Translation;
  2. using CommandLine;
  3. using LibHac.Tools.FsSystem;
  4. using Ryujinx.Audio.Backends.SDL2;
  5. using Ryujinx.Common;
  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.SystemInterop;
  13. using Ryujinx.Common.Utilities;
  14. using Ryujinx.Cpu;
  15. using Ryujinx.Graphics.GAL;
  16. using Ryujinx.Graphics.GAL.Multithreading;
  17. using Ryujinx.Graphics.Gpu;
  18. using Ryujinx.Graphics.Gpu.Shader;
  19. using Ryujinx.Graphics.OpenGL;
  20. using Ryujinx.Graphics.Vulkan;
  21. using Ryujinx.Headless.SDL2.OpenGL;
  22. using Ryujinx.Headless.SDL2.Vulkan;
  23. using Ryujinx.HLE;
  24. using Ryujinx.HLE.FileSystem;
  25. using Ryujinx.HLE.HOS;
  26. using Ryujinx.HLE.HOS.Services.Account.Acc;
  27. using Ryujinx.Input;
  28. using Ryujinx.Input.HLE;
  29. using Ryujinx.Input.SDL2;
  30. using Silk.NET.Vulkan;
  31. using System;
  32. using System.Collections.Generic;
  33. using System.IO;
  34. using System.Text.Json;
  35. using System.Threading;
  36. using ConfigGamepadInputId = Ryujinx.Common.Configuration.Hid.Controller.GamepadInputId;
  37. using ConfigStickInputId = Ryujinx.Common.Configuration.Hid.Controller.StickInputId;
  38. using Key = Ryujinx.Common.Configuration.Hid.Key;
  39. namespace Ryujinx.Headless.SDL2
  40. {
  41. class Program
  42. {
  43. public static string Version { get; private set; }
  44. private static VirtualFileSystem _virtualFileSystem;
  45. private static ContentManager _contentManager;
  46. private static AccountManager _accountManager;
  47. private static LibHacHorizonManager _libHacHorizonManager;
  48. private static UserChannelPersistence _userChannelPersistence;
  49. private static InputManager _inputManager;
  50. private static Switch _emulationContext;
  51. private static WindowBase _window;
  52. private static WindowsMultimediaTimerResolution _windowsMultimediaTimerResolution;
  53. private static List<InputConfig> _inputConfiguration;
  54. private static bool _enableKeyboard;
  55. private static bool _enableMouse;
  56. private static readonly InputConfigJsonSerializerContext SerializerContext = new(JsonHelper.GetDefaultSerializerOptions());
  57. static void Main(string[] args)
  58. {
  59. Version = ReleaseInformation.GetVersion();
  60. Console.Title = $"Ryujinx Console {Version} (Headless SDL2)";
  61. if (OperatingSystem.IsMacOS() || OperatingSystem.IsLinux())
  62. {
  63. AutoResetEvent invoked = new AutoResetEvent(false);
  64. // MacOS must perform SDL polls from the main thread.
  65. Ryujinx.SDL2.Common.SDL2Driver.MainThreadDispatcher = (Action action) =>
  66. {
  67. invoked.Reset();
  68. WindowBase.QueueMainThreadAction(() =>
  69. {
  70. action();
  71. invoked.Set();
  72. });
  73. invoked.WaitOne();
  74. };
  75. }
  76. Parser.Default.ParseArguments<Options>(args)
  77. .WithParsed(Load)
  78. .WithNotParsed(errors => errors.Output());
  79. }
  80. private static InputConfig HandlePlayerConfiguration(string inputProfileName, string inputId, PlayerIndex index)
  81. {
  82. if (inputId == null)
  83. {
  84. if (index == PlayerIndex.Player1)
  85. {
  86. Logger.Info?.Print(LogClass.Application, $"{index} not configured, defaulting to default keyboard.");
  87. // Default to keyboard
  88. inputId = "0";
  89. }
  90. else
  91. {
  92. Logger.Info?.Print(LogClass.Application, $"{index} not configured");
  93. return null;
  94. }
  95. }
  96. IGamepad gamepad;
  97. bool isKeyboard = true;
  98. gamepad = _inputManager.KeyboardDriver.GetGamepad(inputId);
  99. if (gamepad == null)
  100. {
  101. gamepad = _inputManager.GamepadDriver.GetGamepad(inputId);
  102. isKeyboard = false;
  103. if (gamepad == null)
  104. {
  105. Logger.Error?.Print(LogClass.Application, $"{index} gamepad not found (\"{inputId}\")");
  106. return null;
  107. }
  108. }
  109. string gamepadName = gamepad.Name;
  110. gamepad.Dispose();
  111. InputConfig config;
  112. if (inputProfileName == null || inputProfileName.Equals("default"))
  113. {
  114. if (isKeyboard)
  115. {
  116. config = new StandardKeyboardInputConfig
  117. {
  118. Version = InputConfig.CurrentVersion,
  119. Backend = InputBackendType.WindowKeyboard,
  120. Id = null,
  121. ControllerType = ControllerType.JoyconPair,
  122. LeftJoycon = new LeftJoyconCommonConfig<Key>
  123. {
  124. DpadUp = Key.Up,
  125. DpadDown = Key.Down,
  126. DpadLeft = Key.Left,
  127. DpadRight = Key.Right,
  128. ButtonMinus = Key.Minus,
  129. ButtonL = Key.E,
  130. ButtonZl = Key.Q,
  131. ButtonSl = Key.Unbound,
  132. ButtonSr = Key.Unbound
  133. },
  134. LeftJoyconStick = new JoyconConfigKeyboardStick<Key>
  135. {
  136. StickUp = Key.W,
  137. StickDown = Key.S,
  138. StickLeft = Key.A,
  139. StickRight = Key.D,
  140. StickButton = Key.F,
  141. },
  142. RightJoycon = new RightJoyconCommonConfig<Key>
  143. {
  144. ButtonA = Key.Z,
  145. ButtonB = Key.X,
  146. ButtonX = Key.C,
  147. ButtonY = Key.V,
  148. ButtonPlus = Key.Plus,
  149. ButtonR = Key.U,
  150. ButtonZr = Key.O,
  151. ButtonSl = Key.Unbound,
  152. ButtonSr = Key.Unbound
  153. },
  154. RightJoyconStick = new JoyconConfigKeyboardStick<Key>
  155. {
  156. StickUp = Key.I,
  157. StickDown = Key.K,
  158. StickLeft = Key.J,
  159. StickRight = Key.L,
  160. StickButton = Key.H,
  161. }
  162. };
  163. }
  164. else
  165. {
  166. bool isNintendoStyle = gamepadName.Contains("Nintendo");
  167. config = new StandardControllerInputConfig
  168. {
  169. Version = InputConfig.CurrentVersion,
  170. Backend = InputBackendType.GamepadSDL2,
  171. Id = null,
  172. ControllerType = ControllerType.JoyconPair,
  173. DeadzoneLeft = 0.1f,
  174. DeadzoneRight = 0.1f,
  175. RangeLeft = 1.0f,
  176. RangeRight = 1.0f,
  177. TriggerThreshold = 0.5f,
  178. LeftJoycon = new LeftJoyconCommonConfig<ConfigGamepadInputId>
  179. {
  180. DpadUp = ConfigGamepadInputId.DpadUp,
  181. DpadDown = ConfigGamepadInputId.DpadDown,
  182. DpadLeft = ConfigGamepadInputId.DpadLeft,
  183. DpadRight = ConfigGamepadInputId.DpadRight,
  184. ButtonMinus = ConfigGamepadInputId.Minus,
  185. ButtonL = ConfigGamepadInputId.LeftShoulder,
  186. ButtonZl = ConfigGamepadInputId.LeftTrigger,
  187. ButtonSl = ConfigGamepadInputId.Unbound,
  188. ButtonSr = ConfigGamepadInputId.Unbound,
  189. },
  190. LeftJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  191. {
  192. Joystick = ConfigStickInputId.Left,
  193. StickButton = ConfigGamepadInputId.LeftStick,
  194. InvertStickX = false,
  195. InvertStickY = false,
  196. Rotate90CW = false,
  197. },
  198. RightJoycon = new RightJoyconCommonConfig<ConfigGamepadInputId>
  199. {
  200. ButtonA = isNintendoStyle ? ConfigGamepadInputId.A : ConfigGamepadInputId.B,
  201. ButtonB = isNintendoStyle ? ConfigGamepadInputId.B : ConfigGamepadInputId.A,
  202. ButtonX = isNintendoStyle ? ConfigGamepadInputId.X : ConfigGamepadInputId.Y,
  203. ButtonY = isNintendoStyle ? ConfigGamepadInputId.Y : ConfigGamepadInputId.X,
  204. ButtonPlus = ConfigGamepadInputId.Plus,
  205. ButtonR = ConfigGamepadInputId.RightShoulder,
  206. ButtonZr = ConfigGamepadInputId.RightTrigger,
  207. ButtonSl = ConfigGamepadInputId.Unbound,
  208. ButtonSr = ConfigGamepadInputId.Unbound,
  209. },
  210. RightJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  211. {
  212. Joystick = ConfigStickInputId.Right,
  213. StickButton = ConfigGamepadInputId.RightStick,
  214. InvertStickX = false,
  215. InvertStickY = false,
  216. Rotate90CW = false,
  217. },
  218. Motion = new StandardMotionConfigController
  219. {
  220. MotionBackend = MotionInputBackendType.GamepadDriver,
  221. EnableMotion = true,
  222. Sensitivity = 100,
  223. GyroDeadzone = 1,
  224. },
  225. Rumble = new RumbleConfigController
  226. {
  227. StrongRumble = 1f,
  228. WeakRumble = 1f,
  229. EnableRumble = false
  230. }
  231. };
  232. }
  233. }
  234. else
  235. {
  236. string profileBasePath;
  237. if (isKeyboard)
  238. {
  239. profileBasePath = Path.Combine(AppDataManager.ProfilesDirPath, "keyboard");
  240. }
  241. else
  242. {
  243. profileBasePath = Path.Combine(AppDataManager.ProfilesDirPath, "controller");
  244. }
  245. string path = Path.Combine(profileBasePath, inputProfileName + ".json");
  246. if (!File.Exists(path))
  247. {
  248. Logger.Error?.Print(LogClass.Application, $"Input profile \"{inputProfileName}\" not found for \"{inputId}\"");
  249. return null;
  250. }
  251. try
  252. {
  253. config = JsonHelper.DeserializeFromFile(path, SerializerContext.InputConfig);
  254. }
  255. catch (JsonException)
  256. {
  257. Logger.Error?.Print(LogClass.Application, $"Input profile \"{inputProfileName}\" parsing failed for \"{inputId}\"");
  258. return null;
  259. }
  260. }
  261. config.Id = inputId;
  262. config.PlayerIndex = index;
  263. string inputTypeName = isKeyboard ? "Keyboard" : "Gamepad";
  264. Logger.Info?.Print(LogClass.Application, $"{config.PlayerIndex} configured with {inputTypeName} \"{config.Id}\"");
  265. // If both stick ranges are 0 (usually indicative of an outdated profile load) then both sticks will be set to 1.0.
  266. if (config is StandardControllerInputConfig controllerConfig)
  267. {
  268. if (controllerConfig.RangeLeft <= 0.0f && controllerConfig.RangeRight <= 0.0f)
  269. {
  270. controllerConfig.RangeLeft = 1.0f;
  271. controllerConfig.RangeRight = 1.0f;
  272. Logger.Info?.Print(LogClass.Application, $"{config.PlayerIndex} stick range reset. Save the profile now to update your configuration");
  273. }
  274. }
  275. return config;
  276. }
  277. static void Load(Options option)
  278. {
  279. AppDataManager.Initialize(option.BaseDataDir);
  280. _virtualFileSystem = VirtualFileSystem.CreateInstance();
  281. _libHacHorizonManager = new LibHacHorizonManager();
  282. _libHacHorizonManager.InitializeFsServer(_virtualFileSystem);
  283. _libHacHorizonManager.InitializeArpServer();
  284. _libHacHorizonManager.InitializeBcatServer();
  285. _libHacHorizonManager.InitializeSystemClients();
  286. _contentManager = new ContentManager(_virtualFileSystem);
  287. _accountManager = new AccountManager(_libHacHorizonManager.RyujinxClient, option.UserProfile);
  288. _userChannelPersistence = new UserChannelPersistence();
  289. _inputManager = new InputManager(new SDL2KeyboardDriver(), new SDL2GamepadDriver());
  290. GraphicsConfig.EnableShaderCache = true;
  291. IGamepad gamepad;
  292. if (option.ListInputIds)
  293. {
  294. Logger.Info?.Print(LogClass.Application, "Input Ids:");
  295. foreach (string id in _inputManager.KeyboardDriver.GamepadsIds)
  296. {
  297. gamepad = _inputManager.KeyboardDriver.GetGamepad(id);
  298. Logger.Info?.Print(LogClass.Application, $"- {id} (\"{gamepad.Name}\")");
  299. gamepad.Dispose();
  300. }
  301. foreach (string id in _inputManager.GamepadDriver.GamepadsIds)
  302. {
  303. gamepad = _inputManager.GamepadDriver.GetGamepad(id);
  304. Logger.Info?.Print(LogClass.Application, $"- {id} (\"{gamepad.Name}\")");
  305. gamepad.Dispose();
  306. }
  307. return;
  308. }
  309. if (option.InputPath == null)
  310. {
  311. Logger.Error?.Print(LogClass.Application, "Please provide a file to load");
  312. return;
  313. }
  314. _inputConfiguration = new List<InputConfig>();
  315. _enableKeyboard = option.EnableKeyboard;
  316. _enableMouse = option.EnableMouse;
  317. void LoadPlayerConfiguration(string inputProfileName, string inputId, PlayerIndex index)
  318. {
  319. InputConfig inputConfig = HandlePlayerConfiguration(inputProfileName, inputId, index);
  320. if (inputConfig != null)
  321. {
  322. _inputConfiguration.Add(inputConfig);
  323. }
  324. }
  325. LoadPlayerConfiguration(option.InputProfile1Name, option.InputId1, PlayerIndex.Player1);
  326. LoadPlayerConfiguration(option.InputProfile2Name, option.InputId2, PlayerIndex.Player2);
  327. LoadPlayerConfiguration(option.InputProfile3Name, option.InputId3, PlayerIndex.Player3);
  328. LoadPlayerConfiguration(option.InputProfile4Name, option.InputId4, PlayerIndex.Player4);
  329. LoadPlayerConfiguration(option.InputProfile5Name, option.InputId5, PlayerIndex.Player5);
  330. LoadPlayerConfiguration(option.InputProfile6Name, option.InputId6, PlayerIndex.Player6);
  331. LoadPlayerConfiguration(option.InputProfile7Name, option.InputId7, PlayerIndex.Player7);
  332. LoadPlayerConfiguration(option.InputProfile8Name, option.InputId8, PlayerIndex.Player8);
  333. LoadPlayerConfiguration(option.InputProfileHandheldName, option.InputIdHandheld, PlayerIndex.Handheld);
  334. if (_inputConfiguration.Count == 0)
  335. {
  336. return;
  337. }
  338. // Setup logging level
  339. Logger.SetEnable(LogLevel.Debug, option.LoggingEnableDebug);
  340. Logger.SetEnable(LogLevel.Stub, !option.LoggingDisableStub);
  341. Logger.SetEnable(LogLevel.Info, !option.LoggingDisableInfo);
  342. Logger.SetEnable(LogLevel.Warning, !option.LoggingDisableWarning);
  343. Logger.SetEnable(LogLevel.Error, option.LoggingEnableError);
  344. Logger.SetEnable(LogLevel.Trace, option.LoggingEnableTrace);
  345. Logger.SetEnable(LogLevel.Guest, !option.LoggingDisableGuest);
  346. Logger.SetEnable(LogLevel.AccessLog, option.LoggingEnableFsAccessLog);
  347. if (!option.DisableFileLog)
  348. {
  349. Logger.AddTarget(new AsyncLogTargetWrapper(
  350. new FileLogTarget(ReleaseInformation.GetBaseApplicationDirectory(), "file"),
  351. 1000,
  352. AsyncLogTargetOverflowAction.Block
  353. ));
  354. }
  355. // Setup graphics configuration
  356. GraphicsConfig.EnableShaderCache = !option.DisableShaderCache;
  357. GraphicsConfig.EnableTextureRecompression = option.EnableTextureRecompression;
  358. GraphicsConfig.ResScale = option.ResScale;
  359. GraphicsConfig.MaxAnisotropy = option.MaxAnisotropy;
  360. GraphicsConfig.ShadersDumpPath = option.GraphicsShadersDumpPath;
  361. GraphicsConfig.EnableMacroHLE = !option.DisableMacroHLE;
  362. while (true)
  363. {
  364. LoadApplication(option);
  365. if (_userChannelPersistence.PreviousIndex == -1 || !_userChannelPersistence.ShouldRestart)
  366. {
  367. break;
  368. }
  369. _userChannelPersistence.ShouldRestart = false;
  370. }
  371. _inputManager.Dispose();
  372. }
  373. private static void SetupProgressHandler()
  374. {
  375. if (_emulationContext.Processes.ActiveApplication.DiskCacheLoadState != null)
  376. {
  377. _emulationContext.Processes.ActiveApplication.DiskCacheLoadState.StateChanged -= ProgressHandler;
  378. _emulationContext.Processes.ActiveApplication.DiskCacheLoadState.StateChanged += ProgressHandler;
  379. }
  380. _emulationContext.Gpu.ShaderCacheStateChanged -= ProgressHandler;
  381. _emulationContext.Gpu.ShaderCacheStateChanged += ProgressHandler;
  382. }
  383. private static void ProgressHandler<T>(T state, int current, int total) where T : Enum
  384. {
  385. string label;
  386. switch (state)
  387. {
  388. case LoadState ptcState:
  389. label = $"PTC : {current}/{total}";
  390. break;
  391. case ShaderCacheState shaderCacheState:
  392. label = $"Shaders : {current}/{total}";
  393. break;
  394. default:
  395. throw new ArgumentException($"Unknown Progress Handler type {typeof(T)}");
  396. }
  397. Logger.Info?.Print(LogClass.Application, label);
  398. }
  399. private static WindowBase CreateWindow(Options options)
  400. {
  401. return options.GraphicsBackend == GraphicsBackend.Vulkan
  402. ? new VulkanWindow(_inputManager, options.LoggingGraphicsDebugLevel, options.AspectRatio, options.EnableMouse, options.HideCursor)
  403. : new OpenGLWindow(_inputManager, options.LoggingGraphicsDebugLevel, options.AspectRatio, options.EnableMouse, options.HideCursor);
  404. }
  405. private static IRenderer CreateRenderer(Options options, WindowBase window)
  406. {
  407. if (options.GraphicsBackend == GraphicsBackend.Vulkan && window is VulkanWindow vulkanWindow)
  408. {
  409. string preferredGpuId = string.Empty;
  410. if (!string.IsNullOrEmpty(options.PreferredGpuVendor))
  411. {
  412. string preferredGpuVendor = options.PreferredGpuVendor.ToLowerInvariant();
  413. var devices = VulkanRenderer.GetPhysicalDevices();
  414. foreach (var device in devices)
  415. {
  416. if (device.Vendor.ToLowerInvariant() == preferredGpuVendor)
  417. {
  418. preferredGpuId = device.Id;
  419. break;
  420. }
  421. }
  422. }
  423. return new VulkanRenderer(
  424. (instance, vk) => new SurfaceKHR((ulong)(vulkanWindow.CreateWindowSurface(instance.Handle))),
  425. vulkanWindow.GetRequiredInstanceExtensions,
  426. preferredGpuId);
  427. }
  428. else
  429. {
  430. return new OpenGLRenderer();
  431. }
  432. }
  433. private static Switch InitializeEmulationContext(WindowBase window, IRenderer renderer, Options options)
  434. {
  435. BackendThreading threadingMode = options.BackendThreading;
  436. bool threadedGAL = threadingMode == BackendThreading.On || (threadingMode == BackendThreading.Auto && renderer.PreferThreading);
  437. if (threadedGAL)
  438. {
  439. renderer = new ThreadedRenderer(renderer);
  440. }
  441. HLEConfiguration configuration = new HLEConfiguration(_virtualFileSystem,
  442. _libHacHorizonManager,
  443. _contentManager,
  444. _accountManager,
  445. _userChannelPersistence,
  446. renderer,
  447. new SDL2HardwareDeviceDriver(),
  448. options.ExpandRam ? MemoryConfiguration.MemoryConfiguration6GiB : MemoryConfiguration.MemoryConfiguration4GiB,
  449. window,
  450. options.SystemLanguage,
  451. options.SystemRegion,
  452. !options.DisableVsync,
  453. !options.DisableDockedMode,
  454. !options.DisablePtc,
  455. options.EnableInternetAccess,
  456. !options.DisableFsIntegrityChecks ? IntegrityCheckLevel.ErrorOnInvalid : IntegrityCheckLevel.None,
  457. options.FsGlobalAccessLogMode,
  458. options.SystemTimeOffset,
  459. options.SystemTimeZone,
  460. options.MemoryManagerMode,
  461. options.IgnoreMissingServices,
  462. options.AspectRatio,
  463. options.AudioVolume,
  464. options.UseHypervisor);
  465. return new Switch(configuration);
  466. }
  467. private static void ExecutionEntrypoint()
  468. {
  469. if (OperatingSystem.IsWindows())
  470. {
  471. _windowsMultimediaTimerResolution = new WindowsMultimediaTimerResolution(1);
  472. }
  473. DisplaySleep.Prevent();
  474. _window.Initialize(_emulationContext, _inputConfiguration, _enableKeyboard, _enableMouse);
  475. _window.Execute();
  476. _emulationContext.Dispose();
  477. _window.Dispose();
  478. if (OperatingSystem.IsWindows())
  479. {
  480. _windowsMultimediaTimerResolution?.Dispose();
  481. _windowsMultimediaTimerResolution = null;
  482. }
  483. }
  484. private static bool LoadApplication(Options options)
  485. {
  486. string path = options.InputPath;
  487. Logger.RestartTime();
  488. WindowBase window = CreateWindow(options);
  489. IRenderer renderer = CreateRenderer(options, window);
  490. _window = window;
  491. _emulationContext = InitializeEmulationContext(window, renderer, options);
  492. SystemVersion firmwareVersion = _contentManager.GetCurrentFirmwareVersion();
  493. Logger.Notice.Print(LogClass.Application, $"Using Firmware Version: {firmwareVersion?.VersionString}");
  494. if (Directory.Exists(path))
  495. {
  496. string[] romFsFiles = Directory.GetFiles(path, "*.istorage");
  497. if (romFsFiles.Length == 0)
  498. {
  499. romFsFiles = Directory.GetFiles(path, "*.romfs");
  500. }
  501. if (romFsFiles.Length > 0)
  502. {
  503. Logger.Info?.Print(LogClass.Application, "Loading as cart with RomFS.");
  504. if (!_emulationContext.LoadCart(path, romFsFiles[0]))
  505. {
  506. _emulationContext.Dispose();
  507. return false;
  508. }
  509. }
  510. else
  511. {
  512. Logger.Info?.Print(LogClass.Application, "Loading as cart WITHOUT RomFS.");
  513. if (!_emulationContext.LoadCart(path))
  514. {
  515. _emulationContext.Dispose();
  516. return false;
  517. }
  518. }
  519. }
  520. else if (File.Exists(path))
  521. {
  522. switch (Path.GetExtension(path).ToLowerInvariant())
  523. {
  524. case ".xci":
  525. Logger.Info?.Print(LogClass.Application, "Loading as XCI.");
  526. if (!_emulationContext.LoadXci(path))
  527. {
  528. _emulationContext.Dispose();
  529. return false;
  530. }
  531. break;
  532. case ".nca":
  533. Logger.Info?.Print(LogClass.Application, "Loading as NCA.");
  534. if (!_emulationContext.LoadNca(path))
  535. {
  536. _emulationContext.Dispose();
  537. return false;
  538. }
  539. break;
  540. case ".nsp":
  541. case ".pfs0":
  542. Logger.Info?.Print(LogClass.Application, "Loading as NSP.");
  543. if (!_emulationContext.LoadNsp(path))
  544. {
  545. _emulationContext.Dispose();
  546. return false;
  547. }
  548. break;
  549. default:
  550. Logger.Info?.Print(LogClass.Application, "Loading as Homebrew.");
  551. try
  552. {
  553. if (!_emulationContext.LoadProgram(path))
  554. {
  555. _emulationContext.Dispose();
  556. return false;
  557. }
  558. }
  559. catch (ArgumentOutOfRangeException)
  560. {
  561. Logger.Error?.Print(LogClass.Application, "The specified file is not supported by Ryujinx.");
  562. _emulationContext.Dispose();
  563. return false;
  564. }
  565. break;
  566. }
  567. }
  568. else
  569. {
  570. Logger.Warning?.Print(LogClass.Application, $"Couldn't load '{options.InputPath}'. Please specify a valid XCI/NCA/NSP/PFS0/NRO file.");
  571. _emulationContext.Dispose();
  572. return false;
  573. }
  574. SetupProgressHandler();
  575. Translator.IsReadyForTranslation.Reset();
  576. ExecutionEntrypoint();
  577. return true;
  578. }
  579. }
  580. }