Horizon.cs 5.1 KB

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