2022-02-27 00:52:25 +01:00
|
|
|
using LibHac.Common.FixedArrays;
|
2020-04-08 00:41:02 +02:00
|
|
|
using LibHac.Fs;
|
2020-07-04 01:58:01 +02:00
|
|
|
using LibHac.Loader;
|
2022-01-12 12:22:19 +01:00
|
|
|
using LibHac.Tools.FsSystem;
|
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.Text;
|
|
|
|
using System.Text.RegularExpressions;
|
2020-04-08 00:41:02 +02:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.Loaders.Executables
|
|
|
|
{
|
2022-12-04 01:43:23 +01:00
|
|
|
partial class NsoExecutable : IExecutable
|
2020-04-08 00:41:02 +02:00
|
|
|
{
|
2020-07-09 06:31:15 +02:00
|
|
|
public byte[] Program { get; }
|
2022-02-22 14:32:10 +01:00
|
|
|
public Span<byte> Text => Program.AsSpan((int)TextOffset, (int)TextSize);
|
|
|
|
public Span<byte> Ro => Program.AsSpan((int)RoOffset, (int)RoSize);
|
|
|
|
public Span<byte> Data => Program.AsSpan((int)DataOffset, (int)DataSize);
|
2020-04-08 00:41:02 +02:00
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
public uint TextOffset { get; }
|
2021-09-15 01:24:49 +02:00
|
|
|
public uint RoOffset { get; }
|
2020-12-13 08:30:27 +01:00
|
|
|
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; }
|
2021-09-15 01:24:49 +02:00
|
|
|
public uint RoSize { get; }
|
2020-12-13 08:30:27 +01:00
|
|
|
public uint DataSize { get; }
|
2021-09-15 01:24:49 +02:00
|
|
|
public uint BssSize { get; }
|
2020-04-08 00:41:02 +02:00
|
|
|
|
2022-02-27 00:52:25 +01:00
|
|
|
public string Name;
|
|
|
|
public Array32<byte> BuildId;
|
2020-07-09 06:31:15 +02:00
|
|
|
|
2022-12-04 01:43:23 +01:00
|
|
|
[GeneratedRegex(@"[a-z]:[\\/][ -~]{5,}\.nss", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
|
|
|
|
private static partial Regex ModuleRegex();
|
|
|
|
[GeneratedRegex(@"sdk_version: ([0-9.]*)")]
|
|
|
|
private static partial Regex FsSdkRegex();
|
|
|
|
[GeneratedRegex(@"SDK MW[ -~]*")]
|
|
|
|
private static partial Regex SdkMwRegex();
|
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
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();
|
|
|
|
|
2022-02-22 14:32:10 +01:00
|
|
|
var span = Program.AsSpan((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()
|
|
|
|
{
|
2021-03-29 18:31:16 +02:00
|
|
|
string rawTextBuffer = Encoding.ASCII.GetString(Ro);
|
2020-11-06 19:58:57 +01:00
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
|
2021-03-29 18:31:16 +02:00
|
|
|
string modulePath = null;
|
2020-11-18 19:04:42 +01:00
|
|
|
|
2021-03-29 18:31:16 +02:00
|
|
|
if (BitConverter.ToInt32(Ro.Slice(0, 4)) == 0)
|
2020-11-06 19:58:57 +01:00
|
|
|
{
|
2021-03-29 18:31:16 +02:00
|
|
|
int length = BitConverter.ToInt32(Ro.Slice(4, 4));
|
|
|
|
if (length > 0)
|
2020-11-18 19:04:42 +01:00
|
|
|
{
|
2021-03-29 18:31:16 +02:00
|
|
|
modulePath = Encoding.UTF8.GetString(Ro.Slice(8, length));
|
2020-11-18 19:04:42 +01:00
|
|
|
}
|
2021-03-29 18:31:16 +02:00
|
|
|
}
|
2020-11-06 19:58:57 +01:00
|
|
|
|
2021-03-29 18:31:16 +02:00
|
|
|
if (string.IsNullOrEmpty(modulePath))
|
|
|
|
{
|
2022-12-04 01:43:23 +01:00
|
|
|
Match moduleMatch = ModuleRegex().Match(rawTextBuffer);
|
2021-03-29 18:31:16 +02:00
|
|
|
if (moduleMatch.Success)
|
|
|
|
{
|
|
|
|
modulePath = moduleMatch.Value;
|
|
|
|
}
|
2020-11-18 19:04:42 +01:00
|
|
|
}
|
2020-11-06 19:58:57 +01:00
|
|
|
|
2021-03-29 18:31:16 +02:00
|
|
|
stringBuilder.AppendLine($" Module: {modulePath}");
|
|
|
|
|
2022-12-04 01:43:23 +01:00
|
|
|
Match fsSdkMatch = FsSdkRegex().Match(rawTextBuffer);
|
2021-03-29 18:31:16 +02:00
|
|
|
if (fsSdkMatch.Success)
|
2020-11-06 19:58:57 +01:00
|
|
|
{
|
2021-03-29 18:31:16 +02:00
|
|
|
stringBuilder.AppendLine($" FS SDK Version: {fsSdkMatch.Value.Replace("sdk_version: ", "")}");
|
2020-11-06 19:58:57 +01:00
|
|
|
}
|
|
|
|
|
2022-12-04 01:43:23 +01:00
|
|
|
MatchCollection sdkMwMatches = SdkMwRegex().Matches(rawTextBuffer);
|
2020-11-06 19:58:57 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|