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