NsoExecutable.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using LibHac.Common;
  2. using LibHac.Fs;
  3. using LibHac.FsSystem;
  4. using LibHac.Loader;
  5. using Ryujinx.Common.Logging;
  6. using System;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. namespace Ryujinx.HLE.Loaders.Executables
  11. {
  12. class NsoExecutable : IExecutable
  13. {
  14. public byte[] Program { get; }
  15. public Span<byte> Text => Program.AsSpan().Slice((int)TextOffset, (int)TextSize);
  16. public Span<byte> Ro => Program.AsSpan().Slice((int)RoOffset, (int)RoSize);
  17. public Span<byte> Data => Program.AsSpan().Slice((int)DataOffset, (int)DataSize);
  18. public uint TextOffset { get; }
  19. public uint RoOffset { get; }
  20. public uint DataOffset { get; }
  21. public uint BssOffset => DataOffset + (uint)Data.Length;
  22. public uint TextSize { get; }
  23. public uint RoSize { get; }
  24. public uint DataSize { get; }
  25. public uint BssSize { get; }
  26. public string Name;
  27. public Buffer32 BuildId;
  28. public NsoExecutable(IStorage inStorage, string name = null)
  29. {
  30. NsoReader reader = new NsoReader();
  31. reader.Initialize(inStorage.AsFile(OpenMode.Read)).ThrowIfFailure();
  32. TextOffset = reader.Header.Segments[0].MemoryOffset;
  33. RoOffset = reader.Header.Segments[1].MemoryOffset;
  34. DataOffset = reader.Header.Segments[2].MemoryOffset;
  35. BssSize = reader.Header.BssSize;
  36. reader.GetSegmentSize(NsoReader.SegmentType.Data, out uint uncompressedSize).ThrowIfFailure();
  37. Program = new byte[DataOffset + uncompressedSize];
  38. TextSize = DecompressSection(reader, NsoReader.SegmentType.Text, TextOffset);
  39. RoSize = DecompressSection(reader, NsoReader.SegmentType.Ro, RoOffset);
  40. DataSize = DecompressSection(reader, NsoReader.SegmentType.Data, DataOffset);
  41. Name = name;
  42. BuildId = reader.Header.ModuleId;
  43. PrintRoSectionInfo();
  44. }
  45. private uint DecompressSection(NsoReader reader, NsoReader.SegmentType segmentType, uint offset)
  46. {
  47. reader.GetSegmentSize(segmentType, out uint uncompressedSize).ThrowIfFailure();
  48. var span = Program.AsSpan().Slice((int)offset, (int)uncompressedSize);
  49. reader.ReadSegment(segmentType, span).ThrowIfFailure();
  50. return uncompressedSize;
  51. }
  52. private void PrintRoSectionInfo()
  53. {
  54. byte[] roBuffer = Ro.ToArray();
  55. string rawTextBuffer = Encoding.ASCII.GetString(roBuffer, 0, (int)RoSize);
  56. StringBuilder stringBuilder = new StringBuilder();
  57. int zero = BitConverter.ToInt32(roBuffer, 0);
  58. if (zero == 0)
  59. {
  60. int length = BitConverter.ToInt32(roBuffer, 4);
  61. string modulePath = Encoding.UTF8.GetString(roBuffer, 8, length);
  62. MatchCollection moduleMatches = Regex.Matches(rawTextBuffer, @"[a-z]:[\\/][ -~]{5,}\.nss", RegexOptions.IgnoreCase);
  63. if (moduleMatches.Count > 0)
  64. {
  65. modulePath = moduleMatches.First().Value;
  66. }
  67. stringBuilder.AppendLine($" Module: {modulePath}");
  68. }
  69. MatchCollection fsSdkMatches = Regex.Matches(rawTextBuffer, @"sdk_version: ([0-9.]*)");
  70. if (fsSdkMatches.Count != 0)
  71. {
  72. stringBuilder.AppendLine($" FS SDK Version: {fsSdkMatches.First().Value.Replace("sdk_version: ", "")}");
  73. }
  74. MatchCollection sdkMwMatches = Regex.Matches(rawTextBuffer, @"SDK MW[ -~]*");
  75. if (sdkMwMatches.Count != 0)
  76. {
  77. string libHeader = " SDK Libraries: ";
  78. string libContent = string.Join($"\n{new string(' ', libHeader.Length)}", sdkMwMatches);
  79. stringBuilder.AppendLine($"{libHeader}{libContent}");
  80. }
  81. if (stringBuilder.Length > 0)
  82. {
  83. Logger.Info?.Print(LogClass.Loader, $"{Name}:\n{stringBuilder.ToString().TrimEnd('\r', '\n')}");
  84. }
  85. }
  86. }
  87. }