Horizon.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using Ryujinx.HLE.Loaders.Executables;
  2. using Ryujinx.HLE.Logging;
  3. using Ryujinx.HLE.OsHle.Handles;
  4. using System;
  5. using System.Collections.Concurrent;
  6. using System.IO;
  7. namespace Ryujinx.HLE.OsHle
  8. {
  9. public class Horizon : IDisposable
  10. {
  11. internal const int HidSize = 0x40000;
  12. internal const int FontSize = 0x50;
  13. private Switch Ns;
  14. private KProcessScheduler Scheduler;
  15. private ConcurrentDictionary<int, Process> Processes;
  16. public SystemStateMgr SystemState { get; private set; }
  17. internal MemoryAllocator Allocator { get; private set; }
  18. internal HSharedMem HidSharedMem { get; private set; }
  19. internal HSharedMem FontSharedMem { get; private set; }
  20. internal KEvent VsyncEvent { get; private set; }
  21. public Horizon(Switch Ns)
  22. {
  23. this.Ns = Ns;
  24. Scheduler = new KProcessScheduler(Ns.Log);
  25. Processes = new ConcurrentDictionary<int, Process>();
  26. SystemState = new SystemStateMgr();
  27. Allocator = new MemoryAllocator();
  28. HidSharedMem = new HSharedMem();
  29. FontSharedMem = new HSharedMem();
  30. VsyncEvent = new KEvent();
  31. }
  32. public void LoadCart(string ExeFsDir, string RomFsFile = null)
  33. {
  34. if (RomFsFile != null)
  35. {
  36. Ns.VFs.LoadRomFs(RomFsFile);
  37. }
  38. Process MainProcess = MakeProcess();
  39. void LoadNso(string FileName)
  40. {
  41. foreach (string File in Directory.GetFiles(ExeFsDir, FileName))
  42. {
  43. if (Path.GetExtension(File) != string.Empty)
  44. {
  45. continue;
  46. }
  47. Ns.Log.PrintInfo(LogClass.Loader, $"Loading {Path.GetFileNameWithoutExtension(File)}...");
  48. using (FileStream Input = new FileStream(File, FileMode.Open))
  49. {
  50. string Name = Path.GetFileNameWithoutExtension(File);
  51. Nso Program = new Nso(Input, Name);
  52. MainProcess.LoadProgram(Program);
  53. }
  54. }
  55. }
  56. LoadNso("rtld");
  57. MainProcess.SetEmptyArgs();
  58. LoadNso("main");
  59. LoadNso("subsdk*");
  60. LoadNso("sdk");
  61. MainProcess.Run();
  62. }
  63. public void LoadProgram(string FileName)
  64. {
  65. bool IsNro = Path.GetExtension(FileName).ToLower() == ".nro";
  66. string Name = Path.GetFileNameWithoutExtension(FileName);
  67. Process MainProcess = MakeProcess();
  68. using (FileStream Input = new FileStream(FileName, FileMode.Open))
  69. {
  70. MainProcess.LoadProgram(IsNro
  71. ? (IExecutable)new Nro(Input, Name)
  72. : (IExecutable)new Nso(Input, Name));
  73. }
  74. MainProcess.SetEmptyArgs();
  75. MainProcess.Run(IsNro);
  76. }
  77. public void SignalVsync() => VsyncEvent.WaitEvent.Set();
  78. private Process MakeProcess()
  79. {
  80. Process Process;
  81. lock (Processes)
  82. {
  83. int ProcessId = 0;
  84. while (Processes.ContainsKey(ProcessId))
  85. {
  86. ProcessId++;
  87. }
  88. Process = new Process(Ns, Scheduler, ProcessId);
  89. Processes.TryAdd(ProcessId, Process);
  90. }
  91. InitializeProcess(Process);
  92. return Process;
  93. }
  94. private void InitializeProcess(Process Process)
  95. {
  96. Process.AppletState.SetFocus(true);
  97. }
  98. internal void ExitProcess(int ProcessId)
  99. {
  100. if (Processes.TryGetValue(ProcessId, out Process Process) && Process.NeedsHbAbi)
  101. {
  102. string NextNro = Homebrew.ReadHbAbiNextLoadPath(Process.Memory, Process.HbAbiDataPosition);
  103. Ns.Log.PrintInfo(LogClass.Loader, $"HbAbi NextLoadPath {NextNro}");
  104. if (NextNro == string.Empty)
  105. {
  106. NextNro = "sdmc:/hbmenu.nro";
  107. }
  108. NextNro = NextNro.Replace("sdmc:", string.Empty);
  109. NextNro = Ns.VFs.GetFullPath(Ns.VFs.GetSdCardPath(), NextNro);
  110. if (File.Exists(NextNro))
  111. {
  112. LoadProgram(NextNro);
  113. }
  114. }
  115. if (Processes.TryRemove(ProcessId, out Process))
  116. {
  117. Process.StopAllThreadsAsync();
  118. Process.Dispose();
  119. if (Processes.Count == 0)
  120. {
  121. Ns.OnFinish(EventArgs.Empty);
  122. }
  123. }
  124. }
  125. internal bool TryGetProcess(int ProcessId, out Process Process)
  126. {
  127. return Processes.TryGetValue(ProcessId, out Process);
  128. }
  129. public void Dispose()
  130. {
  131. Dispose(true);
  132. }
  133. protected virtual void Dispose(bool Disposing)
  134. {
  135. if (Disposing)
  136. {
  137. foreach (Process Process in Processes.Values)
  138. {
  139. Process.StopAllThreadsAsync();
  140. Process.Dispose();
  141. }
  142. VsyncEvent.Dispose();
  143. Scheduler.Dispose();
  144. }
  145. }
  146. }
  147. }