Switch.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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; } = true;
  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. AudioDeviceDriver = new CompatLayerHardwareDeviceDriver(Configuration.AudioDeviceDriver);
  42. Memory = new MemoryBlock(Configuration.MemoryConfiguration.ToDramSize(), memoryAllocationFlags);
  43. Gpu = new GpuContext(Configuration.GpuRenderer);
  44. System = new HOS.Horizon(this);
  45. Statistics = new PerformanceStatistics();
  46. Hid = new Hid(this, System.HidStorage);
  47. Processes = new ProcessLoader(this);
  48. TamperMachine = new TamperMachine();
  49. System.State.SetLanguage(Configuration.SystemLanguage);
  50. System.State.SetRegion(Configuration.Region);
  51. EnableDeviceVsync = Configuration.EnableVsync;
  52. System.State.DockedMode = Configuration.EnableDockedMode;
  53. System.PerformanceState.PerformanceMode = System.State.DockedMode ? PerformanceMode.Boost : PerformanceMode.Default;
  54. System.EnablePtc = Configuration.EnablePtc;
  55. System.FsIntegrityCheckLevel = Configuration.FsIntegrityCheckLevel;
  56. System.GlobalAccessLogMode = Configuration.FsGlobalAccessLogMode;
  57. }
  58. public bool LoadCart(string exeFsDir, string romFsFile = null)
  59. {
  60. return Processes.LoadUnpackedNca(exeFsDir, romFsFile);
  61. }
  62. public bool LoadXci(string xciFile)
  63. {
  64. return Processes.LoadXci(xciFile);
  65. }
  66. public bool LoadNca(string ncaFile)
  67. {
  68. return Processes.LoadNca(ncaFile);
  69. }
  70. public bool LoadNsp(string nspFile)
  71. {
  72. return Processes.LoadNsp(nspFile);
  73. }
  74. public bool LoadProgram(string fileName)
  75. {
  76. return Processes.LoadNxo(fileName);
  77. }
  78. public bool WaitFifo()
  79. {
  80. return Gpu.GPFifo.WaitForCommands();
  81. }
  82. public void ProcessFrame()
  83. {
  84. Gpu.ProcessShaderCacheQueue();
  85. Gpu.Renderer.PreFrame();
  86. Gpu.GPFifo.DispatchCalls();
  87. }
  88. public bool ConsumeFrameAvailable()
  89. {
  90. return Gpu.Window.ConsumeFrameAvailable();
  91. }
  92. public void PresentFrame(Action swapBuffersCallback)
  93. {
  94. Gpu.Window.Present(swapBuffersCallback);
  95. }
  96. public void SetVolume(float volume)
  97. {
  98. System.SetVolume(Math.Clamp(volume, 0, 1));
  99. }
  100. public float GetVolume()
  101. {
  102. return System.GetVolume();
  103. }
  104. public void EnableCheats()
  105. {
  106. FileSystem.ModLoader.EnableCheats(Processes.ActiveApplication.ProgramId, TamperMachine);
  107. }
  108. public bool IsAudioMuted()
  109. {
  110. return System.GetVolume() == 0;
  111. }
  112. public void DisposeGpu()
  113. {
  114. Gpu.Dispose();
  115. }
  116. public void Dispose()
  117. {
  118. Dispose(true);
  119. }
  120. protected virtual void Dispose(bool disposing)
  121. {
  122. if (disposing)
  123. {
  124. System.Dispose();
  125. AudioDeviceDriver.Dispose();
  126. FileSystem.Dispose();
  127. Memory.Dispose();
  128. }
  129. }
  130. }
  131. }