/ src / Ryujinx.HLE / Loaders / Executables / NsoExecutable.cs
NsoExecutable.cs
  1  using LibHac.Common.FixedArrays;
  2  using LibHac.Fs;
  3  using LibHac.Loader;
  4  using LibHac.Tools.FsSystem;
  5  using Ryujinx.Common.Logging;
  6  using System;
  7  using System.Text;
  8  using System.Text.RegularExpressions;
  9  
 10  namespace Ryujinx.HLE.Loaders.Executables
 11  {
 12      partial class NsoExecutable : IExecutable
 13      {
 14          public byte[] Program { get; }
 15          public Span<byte> Text => Program.AsSpan((int)TextOffset, (int)TextSize);
 16          public Span<byte> Ro => Program.AsSpan((int)RoOffset, (int)RoSize);
 17          public Span<byte> Data => Program.AsSpan((int)DataOffset, (int)DataSize);
 18  
 19          public uint TextOffset { get; }
 20          public uint RoOffset { get; }
 21          public uint DataOffset { get; }
 22          public uint BssOffset => DataOffset + (uint)Data.Length;
 23  
 24          public uint TextSize { get; }
 25          public uint RoSize { get; }
 26          public uint DataSize { get; }
 27          public uint BssSize { get; }
 28  
 29          public string Name;
 30          public Array32<byte> BuildId;
 31  
 32          [GeneratedRegex(@"[a-z]:[\\/][ -~]{5,}\.nss", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
 33          private static partial Regex ModuleRegex();
 34          [GeneratedRegex(@"sdk_version: ([0-9.]*)")]
 35          private static partial Regex FsSdkRegex();
 36          [GeneratedRegex(@"SDK MW[ -~]*")]
 37          private static partial Regex SdkMwRegex();
 38  
 39          public NsoExecutable(IStorage inStorage, string name = null)
 40          {
 41              NsoReader reader = new();
 42  
 43              reader.Initialize(inStorage.AsFile(OpenMode.Read)).ThrowIfFailure();
 44  
 45              TextOffset = reader.Header.Segments[0].MemoryOffset;
 46              RoOffset = reader.Header.Segments[1].MemoryOffset;
 47              DataOffset = reader.Header.Segments[2].MemoryOffset;
 48              BssSize = reader.Header.BssSize;
 49  
 50              reader.GetSegmentSize(NsoReader.SegmentType.Data, out uint uncompressedSize).ThrowIfFailure();
 51  
 52              Program = new byte[DataOffset + uncompressedSize];
 53  
 54              TextSize = DecompressSection(reader, NsoReader.SegmentType.Text, TextOffset);
 55              RoSize = DecompressSection(reader, NsoReader.SegmentType.Ro, RoOffset);
 56              DataSize = DecompressSection(reader, NsoReader.SegmentType.Data, DataOffset);
 57  
 58              Name = name;
 59              BuildId = reader.Header.ModuleId;
 60  
 61              PrintRoSectionInfo();
 62          }
 63  
 64          private uint DecompressSection(NsoReader reader, NsoReader.SegmentType segmentType, uint offset)
 65          {
 66              reader.GetSegmentSize(segmentType, out uint uncompressedSize).ThrowIfFailure();
 67  
 68              var span = Program.AsSpan((int)offset, (int)uncompressedSize);
 69  
 70              reader.ReadSegment(segmentType, span).ThrowIfFailure();
 71  
 72              return uncompressedSize;
 73          }
 74  
 75          private void PrintRoSectionInfo()
 76          {
 77              string rawTextBuffer = Encoding.ASCII.GetString(Ro);
 78              StringBuilder stringBuilder = new();
 79  
 80              string modulePath = null;
 81  
 82              if (BitConverter.ToInt32(Ro[..4]) == 0)
 83              {
 84                  int length = BitConverter.ToInt32(Ro.Slice(4, 4));
 85                  if (length > 0)
 86                  {
 87                      modulePath = Encoding.UTF8.GetString(Ro.Slice(8, length));
 88                  }
 89              }
 90  
 91              if (string.IsNullOrEmpty(modulePath))
 92              {
 93                  Match moduleMatch = ModuleRegex().Match(rawTextBuffer);
 94                  if (moduleMatch.Success)
 95                  {
 96                      modulePath = moduleMatch.Value;
 97                  }
 98              }
 99  
100              stringBuilder.AppendLine($"    Module: {modulePath}");
101  
102              Match fsSdkMatch = FsSdkRegex().Match(rawTextBuffer);
103              if (fsSdkMatch.Success)
104              {
105                  stringBuilder.AppendLine($"    FS SDK Version: {fsSdkMatch.Value.Replace("sdk_version: ", "")}");
106              }
107  
108              MatchCollection sdkMwMatches = SdkMwRegex().Matches(rawTextBuffer);
109              if (sdkMwMatches.Count != 0)
110              {
111                  string libHeader = "    SDK Libraries: ";
112                  string libContent = string.Join($"\n{new string(' ', libHeader.Length)}", sdkMwMatches);
113  
114                  stringBuilder.AppendLine($"{libHeader}{libContent}");
115              }
116  
117              if (stringBuilder.Length > 0)
118              {
119                  Logger.Info?.Print(LogClass.Loader, $"{Name}:\n{stringBuilder.ToString().TrimEnd('\r', '\n')}");
120              }
121          }
122      }
123  }