2020-07-09 06:31:15 +02:00
|
|
|
using LibHac.Common;
|
2020-04-08 00:41:02 +02:00
|
|
|
using LibHac.Fs;
|
2020-07-04 01:58:01 +02:00
|
|
|
using LibHac.FsSystem;
|
|
|
|
using LibHac.Loader;
|
2020-11-06 19:58:57 +01:00
|
|
|
using Ryujinx.Common.Logging;
|
2020-07-09 06:31:15 +02:00
|
|
|
using System;
|
2020-11-06 19:58:57 +01:00
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text.RegularExpressions;
|
2020-04-08 00:41:02 +02:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.Loaders.Executables
|
|
|
|
{
|
2020-07-04 01:58:01 +02:00
|
|
|
class NsoExecutable : IExecutable
|
2020-04-08 00:41:02 +02:00
|
|
|
{
|
2020-07-09 06:31:15 +02:00
|
|
|
public byte[] Program { get; }
|
2020-12-13 08:30:27 +01:00
|
|
|
public Span<byte> Text => Program.AsSpan().Slice((int)TextOffset, (int)TextSize);
|
|
|
|
public Span<byte> Ro => Program.AsSpan().Slice((int)RoOffset, (int)RoSize);
|
|
|
|
public Span<byte> Data => Program.AsSpan().Slice((int)DataOffset, (int)DataSize);
|
2020-04-08 00:41:02 +02:00
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
public uint TextOffset { get; }
|
|
|
|
public uint RoOffset { get; }
|
|
|
|
public uint DataOffset { get; }
|
|
|
|
public uint BssOffset => DataOffset + (uint)Data.Length;
|
2020-04-08 00:41:02 +02:00
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
public uint TextSize { get; }
|
|
|
|
public uint RoSize { get; }
|
|
|
|
public uint DataSize { get; }
|
|
|
|
public uint BssSize { get; }
|
2020-04-08 00:41:02 +02:00
|
|
|
|
2020-11-06 19:58:57 +01:00
|
|
|
public string Name;
|
2020-07-09 06:31:15 +02:00
|
|
|
public Buffer32 BuildId;
|
|
|
|
|
|
|
|
public NsoExecutable(IStorage inStorage, string name = null)
|
2020-04-08 00:41:02 +02:00
|
|
|
{
|
2020-07-04 01:58:01 +02:00
|
|
|
NsoReader reader = new NsoReader();
|
|
|
|
|
|
|
|
reader.Initialize(inStorage.AsFile(OpenMode.Read)).ThrowIfFailure();
|
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
TextOffset = reader.Header.Segments[0].MemoryOffset;
|
|
|
|
RoOffset = reader.Header.Segments[1].MemoryOffset;
|
|
|
|
DataOffset = reader.Header.Segments[2].MemoryOffset;
|
|
|
|
BssSize = reader.Header.BssSize;
|
2020-07-04 01:58:01 +02:00
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
reader.GetSegmentSize(NsoReader.SegmentType.Data, out uint uncompressedSize).ThrowIfFailure();
|
2020-11-06 19:58:57 +01:00
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
Program = new byte[DataOffset + uncompressedSize];
|
|
|
|
|
2020-11-06 19:58:57 +01:00
|
|
|
TextSize = DecompressSection(reader, NsoReader.SegmentType.Text, TextOffset);
|
|
|
|
RoSize = DecompressSection(reader, NsoReader.SegmentType.Ro, RoOffset);
|
|
|
|
DataSize = DecompressSection(reader, NsoReader.SegmentType.Data, DataOffset);
|
2020-07-09 06:31:15 +02:00
|
|
|
|
2020-11-06 19:58:57 +01:00
|
|
|
Name = name;
|
2020-07-09 06:31:15 +02:00
|
|
|
BuildId = reader.Header.ModuleId;
|
2020-11-06 19:58:57 +01:00
|
|
|
|
|
|
|
PrintRoSectionInfo();
|
2020-07-04 01:58:01 +02:00
|
|
|
}
|
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
private uint DecompressSection(NsoReader reader, NsoReader.SegmentType segmentType, uint offset)
|
2020-07-04 01:58:01 +02:00
|
|
|
{
|
|
|
|
reader.GetSegmentSize(segmentType, out uint uncompressedSize).ThrowIfFailure();
|
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
var span = Program.AsSpan().Slice((int)offset, (int)uncompressedSize);
|
2020-07-04 01:58:01 +02:00
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
reader.ReadSegment(segmentType, span).ThrowIfFailure();
|
2020-07-04 01:58:01 +02:00
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
return uncompressedSize;
|
2020-04-08 00:41:02 +02:00
|
|
|
}
|
2020-11-06 19:58:57 +01:00
|
|
|
|
|
|
|
private void PrintRoSectionInfo()
|
|
|
|
{
|
|
|
|
byte[] roBuffer = Ro.ToArray();
|
2020-12-13 08:30:27 +01:00
|
|
|
string rawTextBuffer = Encoding.ASCII.GetString(roBuffer, 0, (int)RoSize);
|
2020-11-06 19:58:57 +01:00
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
|
2020-11-18 19:04:42 +01:00
|
|
|
int zero = BitConverter.ToInt32(roBuffer, 0);
|
|
|
|
|
|
|
|
if (zero == 0)
|
2020-11-06 19:58:57 +01:00
|
|
|
{
|
2020-11-18 19:04:42 +01:00
|
|
|
int length = BitConverter.ToInt32(roBuffer, 4);
|
|
|
|
string modulePath = Encoding.UTF8.GetString(roBuffer, 8, length);
|
|
|
|
|
|
|
|
MatchCollection moduleMatches = Regex.Matches(rawTextBuffer, @"[a-z]:[\\/][ -~]{5,}\.nss", RegexOptions.IgnoreCase);
|
|
|
|
if (moduleMatches.Count > 0)
|
|
|
|
{
|
|
|
|
modulePath = moduleMatches.First().Value;
|
|
|
|
}
|
2020-11-06 19:58:57 +01:00
|
|
|
|
2020-11-18 19:04:42 +01:00
|
|
|
stringBuilder.AppendLine($" Module: {modulePath}");
|
|
|
|
}
|
2020-11-06 19:58:57 +01:00
|
|
|
|
|
|
|
MatchCollection fsSdkMatches = Regex.Matches(rawTextBuffer, @"sdk_version: ([0-9.]*)");
|
|
|
|
if (fsSdkMatches.Count != 0)
|
|
|
|
{
|
|
|
|
stringBuilder.AppendLine($" FS SDK Version: {fsSdkMatches.First().Value.Replace("sdk_version: ", "")}");
|
|
|
|
}
|
|
|
|
|
|
|
|
MatchCollection sdkMwMatches = Regex.Matches(rawTextBuffer, @"SDK MW[ -~]*");
|
|
|
|
if (sdkMwMatches.Count != 0)
|
|
|
|
{
|
|
|
|
string libHeader = " SDK Libraries: ";
|
|
|
|
string libContent = string.Join($"\n{new string(' ', libHeader.Length)}", sdkMwMatches);
|
|
|
|
|
|
|
|
stringBuilder.AppendLine($"{libHeader}{libContent}");
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:04:42 +01:00
|
|
|
if (stringBuilder.Length > 0)
|
|
|
|
{
|
|
|
|
Logger.Info?.Print(LogClass.Loader, $"{Name}:\n{stringBuilder.ToString().TrimEnd('\r', '\n')}");
|
|
|
|
}
|
2020-11-06 19:58:57 +01:00
|
|
|
}
|
2020-04-08 00:41:02 +02:00
|
|
|
}
|
|
|
|
}
|