2020-07-09 00:31:15 -04:00
|
|
|
using LibHac.Common;
|
2020-04-07 18:41:02 -04:00
|
|
|
using LibHac.Fs;
|
2020-07-03 19:58:01 -04:00
|
|
|
using LibHac.FsSystem;
|
|
|
|
using LibHac.Loader;
|
2020-11-06 13:58:57 -05:00
|
|
|
using Ryujinx.Common.Logging;
|
2020-07-09 00:31:15 -04:00
|
|
|
using System;
|
2020-11-06 13:58:57 -05:00
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text.RegularExpressions;
|
2020-04-07 18:41:02 -04:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.Loaders.Executables
|
|
|
|
{
|
2020-07-03 19:58:01 -04:00
|
|
|
class NsoExecutable : IExecutable
|
2020-04-07 18:41:02 -04:00
|
|
|
{
|
2020-07-09 00:31:15 -04:00
|
|
|
public byte[] Program { get; }
|
|
|
|
public Span<byte> Text => Program.AsSpan().Slice(TextOffset, TextSize);
|
|
|
|
public Span<byte> Ro => Program.AsSpan().Slice(RoOffset, RoSize);
|
|
|
|
public Span<byte> Data => Program.AsSpan().Slice(DataOffset, DataSize);
|
2020-04-07 18:41:02 -04:00
|
|
|
|
2020-07-03 19:58:01 -04:00
|
|
|
public int TextOffset { get; }
|
|
|
|
public int RoOffset { get; }
|
|
|
|
public int DataOffset { get; }
|
2020-04-07 18:41:02 -04:00
|
|
|
public int BssOffset => DataOffset + Data.Length;
|
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
public int TextSize { get; }
|
|
|
|
public int RoSize { get; }
|
|
|
|
public int DataSize { get; }
|
2020-07-03 19:58:01 -04:00
|
|
|
public int BssSize { get; }
|
2020-04-07 18:41:02 -04:00
|
|
|
|
2020-11-06 13:58:57 -05:00
|
|
|
public string Name;
|
2020-07-09 00:31:15 -04:00
|
|
|
public Buffer32 BuildId;
|
|
|
|
|
|
|
|
public NsoExecutable(IStorage inStorage, string name = null)
|
2020-04-07 18:41:02 -04:00
|
|
|
{
|
2020-07-03 19:58:01 -04:00
|
|
|
NsoReader reader = new NsoReader();
|
|
|
|
|
|
|
|
reader.Initialize(inStorage.AsFile(OpenMode.Read)).ThrowIfFailure();
|
|
|
|
|
|
|
|
TextOffset = (int)reader.Header.Segments[0].MemoryOffset;
|
2020-11-06 13:58:57 -05:00
|
|
|
RoOffset = (int)reader.Header.Segments[1].MemoryOffset;
|
2020-07-03 19:58:01 -04:00
|
|
|
DataOffset = (int)reader.Header.Segments[2].MemoryOffset;
|
2020-11-06 13:58:57 -05:00
|
|
|
BssSize = (int)reader.Header.BssSize;
|
2020-07-03 19:58:01 -04:00
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
reader.GetSegmentSize(NsoReader.SegmentType.Data, out uint uncompressedSize).ThrowIfFailure();
|
2020-11-06 13:58:57 -05:00
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
Program = new byte[DataOffset + uncompressedSize];
|
|
|
|
|
2020-11-06 13:58:57 -05: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 00:31:15 -04:00
|
|
|
|
2020-11-06 13:58:57 -05:00
|
|
|
Name = name;
|
2020-07-09 00:31:15 -04:00
|
|
|
BuildId = reader.Header.ModuleId;
|
2020-11-06 13:58:57 -05:00
|
|
|
|
|
|
|
PrintRoSectionInfo();
|
2020-07-03 19:58:01 -04:00
|
|
|
}
|
|
|
|
|
2020-11-06 13:58:57 -05:00
|
|
|
private int DecompressSection(NsoReader reader, NsoReader.SegmentType segmentType, int offset)
|
2020-07-03 19:58:01 -04:00
|
|
|
{
|
|
|
|
reader.GetSegmentSize(segmentType, out uint uncompressedSize).ThrowIfFailure();
|
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
var span = Program.AsSpan().Slice(offset, (int)uncompressedSize);
|
2020-07-03 19:58:01 -04:00
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
reader.ReadSegment(segmentType, span).ThrowIfFailure();
|
2020-07-03 19:58:01 -04:00
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
return (int)uncompressedSize;
|
2020-04-07 18:41:02 -04:00
|
|
|
}
|
2020-11-06 13:58:57 -05:00
|
|
|
|
|
|
|
private void PrintRoSectionInfo()
|
|
|
|
{
|
|
|
|
byte[] roBuffer = Ro.ToArray();
|
|
|
|
string rawTextBuffer = Encoding.ASCII.GetString(roBuffer, 0, RoSize);
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
|
2020-11-18 13:04:42 -05:00
|
|
|
int zero = BitConverter.ToInt32(roBuffer, 0);
|
|
|
|
|
|
|
|
if (zero == 0)
|
2020-11-06 13:58:57 -05:00
|
|
|
{
|
2020-11-18 13:04:42 -05: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 13:58:57 -05:00
|
|
|
|
2020-11-18 13:04:42 -05:00
|
|
|
stringBuilder.AppendLine($" Module: {modulePath}");
|
|
|
|
}
|
2020-11-06 13:58:57 -05: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 13:04:42 -05:00
|
|
|
if (stringBuilder.Length > 0)
|
|
|
|
{
|
|
|
|
Logger.Info?.Print(LogClass.Loader, $"{Name}:\n{stringBuilder.ToString().TrimEnd('\r', '\n')}");
|
|
|
|
}
|
2020-11-06 13:58:57 -05:00
|
|
|
}
|
2020-04-07 18:41:02 -04:00
|
|
|
}
|
|
|
|
}
|