2018-02-20 15:09:23 -05:00
|
|
|
using Ryujinx.Core.Loaders.Executables;
|
|
|
|
using Ryujinx.Core.OsHle.Handles;
|
|
|
|
using System;
|
2018-02-04 18:08:20 -05:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.IO;
|
|
|
|
|
2018-02-20 15:09:23 -05:00
|
|
|
namespace Ryujinx.Core.OsHle
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
public class Horizon : IDisposable
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
|
|
|
internal const int HidSize = 0x40000;
|
|
|
|
internal const int FontSize = 0x50;
|
|
|
|
|
2018-04-18 22:52:23 -04:00
|
|
|
private KProcessScheduler Scheduler;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
private ConcurrentDictionary<int, Process> Processes;
|
|
|
|
|
2018-03-19 14:58:46 -04:00
|
|
|
internal HSharedMem HidSharedMem { get; private set; }
|
|
|
|
internal HSharedMem FontSharedMem { get; private set; }
|
|
|
|
|
|
|
|
internal KEvent VsyncEvent { get; private set; }
|
2018-02-17 16:36:08 -05:00
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
private Switch Ns;
|
|
|
|
|
|
|
|
public Horizon(Switch Ns)
|
|
|
|
{
|
|
|
|
this.Ns = Ns;
|
|
|
|
|
2018-04-18 22:52:23 -04:00
|
|
|
Scheduler = new KProcessScheduler();
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
Processes = new ConcurrentDictionary<int, Process>();
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
HidSharedMem = new HSharedMem();
|
|
|
|
FontSharedMem = new HSharedMem();
|
2018-03-19 14:58:46 -04:00
|
|
|
|
|
|
|
VsyncEvent = new KEvent();
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void LoadCart(string ExeFsDir, string RomFsFile = null)
|
|
|
|
{
|
|
|
|
if (RomFsFile != null)
|
|
|
|
{
|
|
|
|
Ns.VFs.LoadRomFs(RomFsFile);
|
|
|
|
}
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
Process MainProcess = MakeProcess();
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
void LoadNso(string FileName)
|
|
|
|
{
|
|
|
|
foreach (string File in Directory.GetFiles(ExeFsDir, FileName))
|
|
|
|
{
|
|
|
|
if (Path.GetExtension(File) != string.Empty)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-13 21:02:24 -04:00
|
|
|
Logging.Info(LogClass.Loader, $"Loading {Path.GetFileNameWithoutExtension(File)}...");
|
2018-02-17 16:06:11 -05:00
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
using (FileStream Input = new FileStream(File, FileMode.Open))
|
|
|
|
{
|
2018-04-22 00:21:49 -04:00
|
|
|
string Name = Path.GetFileNameWithoutExtension(File);
|
|
|
|
|
|
|
|
Nso Program = new Nso(Input, Name);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
MainProcess.LoadProgram(Program);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadNso("rtld");
|
|
|
|
|
|
|
|
MainProcess.SetEmptyArgs();
|
|
|
|
|
|
|
|
LoadNso("main");
|
|
|
|
LoadNso("subsdk*");
|
|
|
|
LoadNso("sdk");
|
|
|
|
|
|
|
|
MainProcess.Run();
|
|
|
|
}
|
|
|
|
|
2018-02-17 16:36:08 -05:00
|
|
|
public void LoadProgram(string FileName)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-23 19:59:38 -05:00
|
|
|
bool IsNro = Path.GetExtension(FileName).ToLower() == ".nro";
|
|
|
|
|
2018-04-22 00:21:49 -04:00
|
|
|
string Name = Path.GetFileNameWithoutExtension(FileName);
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
Process MainProcess = MakeProcess();
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
using (FileStream Input = new FileStream(FileName, FileMode.Open))
|
|
|
|
{
|
2018-02-23 19:59:38 -05:00
|
|
|
MainProcess.LoadProgram(IsNro
|
2018-04-22 00:21:49 -04:00
|
|
|
? (IExecutable)new Nro(Input, Name)
|
|
|
|
: (IExecutable)new Nso(Input, Name));
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
MainProcess.SetEmptyArgs();
|
2018-02-23 19:59:38 -05:00
|
|
|
MainProcess.Run(IsNro);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-04-18 22:52:23 -04:00
|
|
|
public void SignalVsync() => VsyncEvent.WaitEvent.Set();
|
2018-03-19 14:58:46 -04:00
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
private Process MakeProcess()
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
Process Process;
|
|
|
|
|
|
|
|
lock (Processes)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
int ProcessId = 0;
|
|
|
|
|
|
|
|
while (Processes.ContainsKey(ProcessId))
|
|
|
|
{
|
|
|
|
ProcessId++;
|
|
|
|
}
|
|
|
|
|
2018-04-18 22:52:23 -04:00
|
|
|
Process = new Process(Ns, Scheduler, ProcessId);
|
2018-03-12 00:04:52 -04:00
|
|
|
|
|
|
|
Processes.TryAdd(ProcessId, Process);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
2018-03-12 00:04:52 -04:00
|
|
|
|
2018-03-19 14:58:46 -04:00
|
|
|
InitializeProcess(Process);
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
return Process;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-03-19 14:58:46 -04:00
|
|
|
private void InitializeProcess(Process Process)
|
|
|
|
{
|
|
|
|
Process.AppletState.SetFocus(true);
|
|
|
|
}
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
internal void ExitProcess(int ProcessId)
|
2018-02-17 16:36:08 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
if (Processes.TryGetValue(ProcessId, out Process Process) && Process.NeedsHbAbi)
|
2018-02-17 16:36:08 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
string NextNro = Homebrew.ReadHbAbiNextLoadPath(Process.Memory, Process.HbAbiDataPosition);
|
|
|
|
|
2018-04-13 21:02:24 -04:00
|
|
|
Logging.Info(LogClass.Loader, $"HbAbi NextLoadPath {NextNro}");
|
2018-03-12 00:04:52 -04:00
|
|
|
|
|
|
|
if (NextNro == string.Empty)
|
|
|
|
{
|
|
|
|
NextNro = "sdmc:/hbmenu.nro";
|
|
|
|
}
|
|
|
|
|
|
|
|
NextNro = NextNro.Replace("sdmc:", string.Empty);
|
|
|
|
|
|
|
|
NextNro = Ns.VFs.GetFullPath(Ns.VFs.GetSdCardPath(), NextNro);
|
|
|
|
|
|
|
|
if (File.Exists(NextNro))
|
|
|
|
{
|
|
|
|
LoadProgram(NextNro);
|
|
|
|
}
|
2018-02-15 07:16:16 -05:00
|
|
|
}
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
if (Processes.TryRemove(ProcessId, out Process))
|
2018-02-17 16:36:08 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
Process.StopAllThreadsAsync();
|
|
|
|
Process.Dispose();
|
2018-02-17 16:36:08 -05:00
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
if (Processes.Count == 0)
|
|
|
|
{
|
|
|
|
Ns.OnFinish(EventArgs.Empty);
|
|
|
|
}
|
|
|
|
}
|
2018-02-15 07:16:16 -05:00
|
|
|
}
|
2018-02-17 16:36:08 -05:00
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
internal bool TryGetProcess(int ProcessId, out Process Process)
|
|
|
|
{
|
2018-02-20 05:54:00 -05:00
|
|
|
return Processes.TryGetValue(ProcessId, out Process);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
public void Dispose()
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
Dispose(true);
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
protected virtual void Dispose(bool Disposing)
|
|
|
|
{
|
|
|
|
if (Disposing)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
foreach (Process Process in Processes.Values)
|
|
|
|
{
|
|
|
|
Process.StopAllThreadsAsync();
|
|
|
|
Process.Dispose();
|
|
|
|
}
|
2018-03-19 14:58:46 -04:00
|
|
|
|
|
|
|
VsyncEvent.Dispose();
|
2018-04-18 23:19:22 -04:00
|
|
|
|
|
|
|
Scheduler.Dispose();
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|