Switch.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.Ui;
  10. using Ryujinx.Memory;
  11. using System;
  12. namespace Ryujinx.HLE
  13. {
  14. public class Switch : IDisposable
  15. {
  16. public HLEConfiguration Configuration { get; }
  17. public IHardwareDeviceDriver AudioDeviceDriver { get; }
  18. public MemoryBlock Memory { get; }
  19. public GpuContext Gpu { get; }
  20. public VirtualFileSystem FileSystem { get; }
  21. public Horizon System { get; }
  22. public ApplicationLoader Application { get; }
  23. public PerformanceStatistics Statistics { get; }
  24. public Hid Hid { get; }
  25. public TamperMachine TamperMachine { get; }
  26. public IHostUiHandler UiHandler { get; }
  27. public bool EnableDeviceVsync { get; set; } = true;
  28. public Switch(HLEConfiguration configuration)
  29. {
  30. if (configuration.GpuRenderer == null)
  31. {
  32. throw new ArgumentNullException(nameof(configuration.GpuRenderer));
  33. }
  34. if (configuration.AudioDeviceDriver == null)
  35. {
  36. throw new ArgumentNullException(nameof(configuration.AudioDeviceDriver));
  37. }
  38. if (configuration.UserChannelPersistence == null)
  39. {
  40. throw new ArgumentNullException(nameof(configuration.UserChannelPersistence));
  41. }
  42. Configuration = configuration;
  43. FileSystem = Configuration.VirtualFileSystem;
  44. UiHandler = Configuration.HostUiHandler;
  45. MemoryAllocationFlags memoryAllocationFlags = configuration.MemoryManagerMode == MemoryManagerMode.SoftwarePageTable
  46. ? MemoryAllocationFlags.Reserve
  47. : MemoryAllocationFlags.Reserve | MemoryAllocationFlags.Mirrorable;
  48. AudioDeviceDriver = new CompatLayerHardwareDeviceDriver(Configuration.AudioDeviceDriver);
  49. Memory = new MemoryBlock(Configuration.MemoryConfiguration.ToDramSize(), memoryAllocationFlags);
  50. Gpu = new GpuContext(Configuration.GpuRenderer);
  51. System = new Horizon(this);
  52. Statistics = new PerformanceStatistics();
  53. Hid = new Hid(this, System.HidStorage);
  54. Application = new ApplicationLoader(this);
  55. TamperMachine = new TamperMachine();
  56. System.State.SetLanguage(Configuration.SystemLanguage);
  57. System.State.SetRegion(Configuration.Region);
  58. EnableDeviceVsync = Configuration.EnableVsync;
  59. System.State.DockedMode = Configuration.EnableDockedMode;
  60. System.PerformanceState.PerformanceMode = System.State.DockedMode ? PerformanceMode.Boost : PerformanceMode.Default;
  61. System.EnablePtc = Configuration.EnablePtc;
  62. System.FsIntegrityCheckLevel = Configuration.FsIntegrityCheckLevel;
  63. System.GlobalAccessLogMode = Configuration.FsGlobalAccessLogMode;
  64. }
  65. public void LoadCart(string exeFsDir, string romFsFile = null)
  66. {
  67. Application.LoadCart(exeFsDir, romFsFile);
  68. }
  69. public void LoadXci(string xciFile)
  70. {
  71. Application.LoadXci(xciFile);
  72. }
  73. public void LoadNca(string ncaFile)
  74. {
  75. Application.LoadNca(ncaFile);
  76. }
  77. public void LoadNsp(string nspFile)
  78. {
  79. Application.LoadNsp(nspFile);
  80. }
  81. public void LoadProgram(string fileName)
  82. {
  83. Application.LoadProgram(fileName);
  84. }
  85. public bool WaitFifo()
  86. {
  87. return Gpu.GPFifo.WaitForCommands();
  88. }
  89. public void ProcessFrame()
  90. {
  91. Gpu.ProcessShaderCacheQueue();
  92. Gpu.Renderer.PreFrame();
  93. Gpu.GPFifo.DispatchCalls();
  94. }
  95. public bool ConsumeFrameAvailable()
  96. {
  97. return Gpu.Window.ConsumeFrameAvailable();
  98. }
  99. public void PresentFrame(Action swapBuffersCallback)
  100. {
  101. Gpu.Window.Present(swapBuffersCallback);
  102. }
  103. public void SetVolume(float volume)
  104. {
  105. System.SetVolume(volume);
  106. }
  107. public float GetVolume()
  108. {
  109. return System.GetVolume();
  110. }
  111. public void EnableCheats()
  112. {
  113. FileSystem.ModLoader.EnableCheats(Application.TitleId, TamperMachine);
  114. }
  115. public bool IsAudioMuted()
  116. {
  117. return System.GetVolume() == 0;
  118. }
  119. public void DisposeGpu()
  120. {
  121. Gpu.Dispose();
  122. }
  123. public void Dispose()
  124. {
  125. Dispose(true);
  126. }
  127. protected virtual void Dispose(bool disposing)
  128. {
  129. if (disposing)
  130. {
  131. System.Dispose();
  132. AudioDeviceDriver.Dispose();
  133. FileSystem.Dispose();
  134. Memory.Dispose();
  135. }
  136. }
  137. }
  138. }