Switch.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #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 bool LoadCart(string exeFsDir, string romFsFile = null)
  62. {
  63. return Processes.LoadUnpackedNca(exeFsDir, romFsFile);
  64. }
  65. public bool LoadXci(string xciFile)
  66. {
  67. return Processes.LoadXci(xciFile);
  68. }
  69. public bool LoadNca(string ncaFile)
  70. {
  71. return Processes.LoadNca(ncaFile);
  72. }
  73. public bool LoadNsp(string nspFile)
  74. {
  75. return Processes.LoadNsp(nspFile);
  76. }
  77. public bool LoadProgram(string fileName)
  78. {
  79. return Processes.LoadNxo(fileName);
  80. }
  81. public bool WaitFifo()
  82. {
  83. return Gpu.GPFifo.WaitForCommands();
  84. }
  85. public void ProcessFrame()
  86. {
  87. Gpu.ProcessShaderCacheQueue();
  88. Gpu.Renderer.PreFrame();
  89. Gpu.GPFifo.DispatchCalls();
  90. }
  91. public bool ConsumeFrameAvailable()
  92. {
  93. return Gpu.Window.ConsumeFrameAvailable();
  94. }
  95. public void PresentFrame(Action swapBuffersCallback)
  96. {
  97. Gpu.Window.Present(swapBuffersCallback);
  98. }
  99. public void SetVolume(float volume)
  100. {
  101. AudioDeviceDriver.Volume = Math.Clamp(volume, 0f, 1f);
  102. }
  103. public float GetVolume()
  104. {
  105. return AudioDeviceDriver.Volume;
  106. }
  107. public void EnableCheats()
  108. {
  109. ModLoader.EnableCheats(Processes.ActiveApplication.ProgramId, TamperMachine);
  110. }
  111. public bool IsAudioMuted()
  112. {
  113. return AudioDeviceDriver.Volume == 0;
  114. }
  115. public void DisposeGpu()
  116. {
  117. Gpu.Dispose();
  118. }
  119. public void Dispose()
  120. {
  121. GC.SuppressFinalize(this);
  122. Dispose(true);
  123. }
  124. protected virtual void Dispose(bool disposing)
  125. {
  126. if (disposing)
  127. {
  128. System.Dispose();
  129. AudioDeviceDriver.Dispose();
  130. FileSystem.Dispose();
  131. Memory.Dispose();
  132. }
  133. }
  134. }
  135. }