/ src / Ryujinx.HLE / Switch.cs
Switch.cs
  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  
 14  namespace Ryujinx.HLE
 15  {
 16      public class Switch : IDisposable
 17      {
 18          public HLEConfiguration Configuration { get; }
 19          public IHardwareDeviceDriver AudioDeviceDriver { get; }
 20          public MemoryBlock Memory { get; }
 21          public GpuContext Gpu { get; }
 22          public VirtualFileSystem FileSystem { get; }
 23          public HOS.Horizon System { get; }
 24          public ProcessLoader Processes { get; }
 25          public PerformanceStatistics Statistics { get; }
 26          public Hid Hid { get; }
 27          public TamperMachine TamperMachine { get; }
 28          public IHostUIHandler UIHandler { get; }
 29  
 30          public bool EnableDeviceVsync { get; set; } = true;
 31  
 32          public bool IsFrameAvailable => Gpu.Window.IsFrameAvailable;
 33  
 34          public Switch(HLEConfiguration configuration)
 35          {
 36              ArgumentNullException.ThrowIfNull(configuration.GpuRenderer);
 37              ArgumentNullException.ThrowIfNull(configuration.AudioDeviceDriver);
 38              ArgumentNullException.ThrowIfNull(configuration.UserChannelPersistence);
 39  
 40              Configuration = configuration;
 41              FileSystem = Configuration.VirtualFileSystem;
 42              UIHandler = Configuration.HostUIHandler;
 43  
 44              MemoryAllocationFlags memoryAllocationFlags = configuration.MemoryManagerMode == MemoryManagerMode.SoftwarePageTable
 45                  ? MemoryAllocationFlags.Reserve
 46                  : MemoryAllocationFlags.Reserve | MemoryAllocationFlags.Mirrorable;
 47  
 48  #pragma warning disable IDE0055 // Disable formatting
 49              AudioDeviceDriver = new CompatLayerHardwareDeviceDriver(Configuration.AudioDeviceDriver);
 50              Memory            = new MemoryBlock(Configuration.MemoryConfiguration.ToDramSize(), memoryAllocationFlags);
 51              Gpu               = new GpuContext(Configuration.GpuRenderer);
 52              System            = new HOS.Horizon(this);
 53              Statistics        = new PerformanceStatistics();
 54              Hid               = new Hid(this, System.HidStorage);
 55              Processes         = new ProcessLoader(this);
 56              TamperMachine     = new TamperMachine();
 57  
 58              System.InitializeServices();
 59              System.State.SetLanguage(Configuration.SystemLanguage);
 60              System.State.SetRegion(Configuration.Region);
 61  
 62              EnableDeviceVsync                       = Configuration.EnableVsync;
 63              System.State.DockedMode                 = Configuration.EnableDockedMode;
 64              System.PerformanceState.PerformanceMode = System.State.DockedMode ? PerformanceMode.Boost : PerformanceMode.Default;
 65              System.EnablePtc                        = Configuration.EnablePtc;
 66              System.FsIntegrityCheckLevel            = Configuration.FsIntegrityCheckLevel;
 67              System.GlobalAccessLogMode              = Configuration.FsGlobalAccessLogMode;
 68  #pragma warning restore IDE0055
 69          }
 70  
 71          public bool LoadCart(string exeFsDir, string romFsFile = null)
 72          {
 73              return Processes.LoadUnpackedNca(exeFsDir, romFsFile);
 74          }
 75  
 76          public bool LoadXci(string xciFile, ulong applicationId = 0)
 77          {
 78              return Processes.LoadXci(xciFile, applicationId);
 79          }
 80  
 81          public bool LoadNca(string ncaFile)
 82          {
 83              return Processes.LoadNca(ncaFile);
 84          }
 85  
 86          public bool LoadNsp(string nspFile, ulong applicationId = 0)
 87          {
 88              return Processes.LoadNsp(nspFile, applicationId);
 89          }
 90  
 91          public bool LoadProgram(string fileName)
 92          {
 93              return Processes.LoadNxo(fileName);
 94          }
 95  
 96          public bool WaitFifo()
 97          {
 98              return Gpu.GPFifo.WaitForCommands();
 99          }
100  
101          public void ProcessFrame()
102          {
103              Gpu.ProcessShaderCacheQueue();
104              Gpu.Renderer.PreFrame();
105              Gpu.GPFifo.DispatchCalls();
106          }
107  
108          public bool ConsumeFrameAvailable()
109          {
110              return Gpu.Window.ConsumeFrameAvailable();
111          }
112  
113          public void PresentFrame(Action swapBuffersCallback)
114          {
115              Gpu.Window.Present(swapBuffersCallback);
116          }
117  
118          public void SetVolume(float volume)
119          {
120              AudioDeviceDriver.Volume = Math.Clamp(volume, 0f, 1f);
121          }
122  
123          public float GetVolume()
124          {
125              return AudioDeviceDriver.Volume;
126          }
127  
128          public void EnableCheats()
129          {
130              ModLoader.EnableCheats(Processes.ActiveApplication.ProgramId, TamperMachine);
131          }
132  
133          public bool IsAudioMuted()
134          {
135              return AudioDeviceDriver.Volume == 0;
136          }
137  
138          public void DisposeGpu()
139          {
140              Gpu.Dispose();
141          }
142  
143          public void Dispose()
144          {
145              GC.SuppressFinalize(this);
146              Dispose(true);
147          }
148  
149          protected virtual void Dispose(bool disposing)
150          {
151              if (disposing)
152              {
153                  System.Dispose();
154                  AudioDeviceDriver.Dispose();
155                  FileSystem.Dispose();
156                  Memory.Dispose();
157              }
158          }
159      }
160  }