Switch.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Ryujinx.Audio.Backends.CompatLayer;
  2. using Ryujinx.Audio.Integration;
  3. using Ryujinx.Common.Configuration;
  4. using Ryujinx.Graphics.Gpu;
  5. using Ryujinx.HLE.FileSystem;
  6. using Ryujinx.HLE.HOS;
  7. using Ryujinx.HLE.HOS.Services.Apm;
  8. using Ryujinx.HLE.HOS.Services.Hid;
  9. using Ryujinx.HLE.Loaders.Processes;
  10. using Ryujinx.HLE.UI;
  11. using Ryujinx.Memory;
  12. using System;
  13. namespace Ryujinx.HLE
  14. {
  15. public class Switch : IDisposable
  16. {
  17. public HLEConfiguration Configuration { get; }
  18. public IHardwareDeviceDriver AudioDeviceDriver { get; }
  19. public MemoryBlock Memory { get; }
  20. public GpuContext Gpu { get; }
  21. public VirtualFileSystem FileSystem { get; }
  22. public HOS.Horizon System { get; }
  23. public ProcessLoader Processes { get; }
  24. public PerformanceStatistics Statistics { get; }
  25. public Hid Hid { get; }
  26. public TamperMachine TamperMachine { get; }
  27. public IHostUIHandler UIHandler { get; }
  28. public bool EnableDeviceVsync { get; set; }
  29. public bool IsFrameAvailable => Gpu.Window.IsFrameAvailable;
  30. public Switch(HLEConfiguration configuration)
  31. {
  32. ArgumentNullException.ThrowIfNull(configuration.GpuRenderer);
  33. ArgumentNullException.ThrowIfNull(configuration.AudioDeviceDriver);
  34. ArgumentNullException.ThrowIfNull(configuration.UserChannelPersistence);
  35. Configuration = configuration;
  36. FileSystem = Configuration.VirtualFileSystem;
  37. UIHandler = Configuration.HostUIHandler;
  38. MemoryAllocationFlags memoryAllocationFlags = configuration.MemoryManagerMode == MemoryManagerMode.SoftwarePageTable
  39. ? MemoryAllocationFlags.Reserve
  40. : MemoryAllocationFlags.Reserve | MemoryAllocationFlags.Mirrorable;
  41. #pragma warning disable IDE0055 // Disable formatting
  42. AudioDeviceDriver = new CompatLayerHardwareDeviceDriver(Configuration.AudioDeviceDriver);
  43. Memory = new MemoryBlock(Configuration.MemoryConfiguration.ToDramSize(), memoryAllocationFlags);
  44. Gpu = new GpuContext(Configuration.GpuRenderer);
  45. System = new HOS.Horizon(this);
  46. Statistics = new PerformanceStatistics();
  47. Hid = new Hid(this, System.HidStorage);
  48. Processes = new ProcessLoader(this);
  49. TamperMachine = new TamperMachine();
  50. System.InitializeServices();
  51. System.State.SetLanguage(Configuration.SystemLanguage);
  52. System.State.SetRegion(Configuration.Region);
  53. EnableDeviceVsync = Configuration.EnableVsync;
  54. System.State.DockedMode = Configuration.EnableDockedMode;
  55. System.PerformanceState.PerformanceMode = System.State.DockedMode ? PerformanceMode.Boost : PerformanceMode.Default;
  56. System.EnablePtc = Configuration.EnablePtc;
  57. System.FsIntegrityCheckLevel = Configuration.FsIntegrityCheckLevel;
  58. System.GlobalAccessLogMode = Configuration.FsGlobalAccessLogMode;
  59. #pragma warning restore IDE0055
  60. }
  61. public void ProcessFrame()
  62. {
  63. Gpu.ProcessShaderCacheQueue();
  64. Gpu.Renderer.PreFrame();
  65. Gpu.GPFifo.DispatchCalls();
  66. }
  67. public bool LoadCart(string exeFsDir, string romFsFile = null) => Processes.LoadUnpackedNca(exeFsDir, romFsFile);
  68. public bool LoadXci(string xciFile, ulong applicationId = 0) => Processes.LoadXci(xciFile, applicationId);
  69. public bool LoadNca(string ncaFile) => Processes.LoadNca(ncaFile);
  70. public bool LoadNsp(string nspFile, ulong applicationId = 0) => Processes.LoadNsp(nspFile, applicationId);
  71. public bool LoadProgram(string fileName) => Processes.LoadNxo(fileName);
  72. public void SetVolume(float volume) => AudioDeviceDriver.Volume = Math.Clamp(volume, 0f, 1f);
  73. public float GetVolume() => AudioDeviceDriver.Volume;
  74. public bool IsAudioMuted() => AudioDeviceDriver.Volume == 0;
  75. public void EnableCheats() => ModLoader.EnableCheats(Processes.ActiveApplication.ProgramId, TamperMachine);
  76. public bool WaitFifo() => Gpu.GPFifo.WaitForCommands();
  77. public bool ConsumeFrameAvailable() => Gpu.Window.ConsumeFrameAvailable();
  78. public void PresentFrame(Action swapBuffersCallback) => Gpu.Window.Present(swapBuffersCallback);
  79. public void DisposeGpu() => Gpu.Dispose();
  80. public void Dispose()
  81. {
  82. GC.SuppressFinalize(this);
  83. Dispose(true);
  84. }
  85. protected virtual void Dispose(bool disposing)
  86. {
  87. if (disposing)
  88. {
  89. System.Dispose();
  90. AudioDeviceDriver.Dispose();
  91. FileSystem.Dispose();
  92. Memory.Dispose();
  93. }
  94. }
  95. }
  96. }