Horizon.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. internal ConcurrentDictionary<long, Mutex> Mutexes { get; private set; }
  13. internal ConcurrentDictionary<long, CondVar> CondVars { get; private set; }
  14. private ConcurrentDictionary<int, Process> Processes;
  15. internal HSharedMem HidSharedMem { get; private set; }
  16. internal HSharedMem FontSharedMem { get; private set; }
  17. internal KEvent VsyncEvent { get; private set; }
  18. private Switch Ns;
  19. public Horizon(Switch Ns)
  20. {
  21. this.Ns = Ns;
  22. Mutexes = new ConcurrentDictionary<long, Mutex>();
  23. CondVars = new ConcurrentDictionary<long, CondVar>();
  24. Processes = new ConcurrentDictionary<int, Process>();
  25. HidSharedMem = new HSharedMem();
  26. FontSharedMem = new HSharedMem();
  27. VsyncEvent = new KEvent();
  28. }
  29. public void LoadCart(string ExeFsDir, string RomFsFile = null)
  30. {
  31. if (RomFsFile != null)
  32. {
  33. Ns.VFs.LoadRomFs(RomFsFile);
  34. }
  35. Process MainProcess = MakeProcess();
  36. void LoadNso(string FileName)
  37. {
  38. foreach (string File in Directory.GetFiles(ExeFsDir, FileName))
  39. {
  40. if (Path.GetExtension(File) != string.Empty)
  41. {
  42. continue;
  43. }
  44. Logging.Info($"Loading {Path.GetFileNameWithoutExtension(File)}...");
  45. using (FileStream Input = new FileStream(File, FileMode.Open))
  46. {
  47. Nso Program = new Nso(Input);
  48. MainProcess.LoadProgram(Program);
  49. }
  50. }
  51. }
  52. LoadNso("rtld");
  53. MainProcess.SetEmptyArgs();
  54. LoadNso("main");
  55. LoadNso("subsdk*");
  56. LoadNso("sdk");
  57. MainProcess.Run();
  58. }
  59. public void LoadProgram(string FileName)
  60. {
  61. bool IsNro = Path.GetExtension(FileName).ToLower() == ".nro";
  62. Process MainProcess = MakeProcess();
  63. using (FileStream Input = new FileStream(FileName, FileMode.Open))
  64. {
  65. MainProcess.LoadProgram(IsNro
  66. ? (IExecutable)new Nro(Input)
  67. : (IExecutable)new Nso(Input));
  68. }
  69. MainProcess.SetEmptyArgs();
  70. MainProcess.Run(IsNro);
  71. }
  72. public void SignalVsync() => VsyncEvent.Handle.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, 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($"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. //TODO: Those dictionaries shouldn't even exist,
  108. //the Mutex and CondVar helper classes should be static.
  109. Mutexes.Clear();
  110. CondVars.Clear();
  111. LoadProgram(NextNro);
  112. }
  113. }
  114. if (Processes.TryRemove(ProcessId, out Process))
  115. {
  116. Process.StopAllThreadsAsync();
  117. Process.Dispose();
  118. if (Processes.Count == 0)
  119. {
  120. Ns.OnFinish(EventArgs.Empty);
  121. }
  122. }
  123. }
  124. internal bool TryGetProcess(int ProcessId, out Process Process)
  125. {
  126. return Processes.TryGetValue(ProcessId, out Process);
  127. }
  128. public void Dispose()
  129. {
  130. Dispose(true);
  131. }
  132. protected virtual void Dispose(bool Disposing)
  133. {
  134. if (Disposing)
  135. {
  136. foreach (Process Process in Processes.Values)
  137. {
  138. Process.StopAllThreadsAsync();
  139. Process.Dispose();
  140. }
  141. VsyncEvent.Dispose();
  142. }
  143. }
  144. }
  145. }