Ryujinx/Ryujinx.HLE/Loaders/Executables/NxStaticObject.cs
Alex Barney 85dbb9559a Adjust naming conventions and general refactoring in HLE Project (#490)
* Rename enum fields

* Naming conventions

* Remove unneeded ".this"

* Remove unneeded semicolons

* Remove unused Usings

* Don't use var

* Remove unneeded enum underlying types

* Explicitly label class visibility

* Remove unneeded @ prefixes

* Remove unneeded commas

* Remove unneeded if expressions

* Method doesn't use unsafe code

* Remove unneeded casts

* Initialized objects don't need an empty constructor

* Remove settings from DotSettings

* Revert "Explicitly label class visibility"

This reverts commit ad5eb5787c.

* Small changes

* Revert external enum renaming

* Changes from feedback

* Remove unneeded property setters
2018-12-04 18:23:37 -02:00

109 lines
No EOL
3.4 KiB
C#

using Ryujinx.HLE.Loaders.Compression;
using System;
using System.IO;
namespace Ryujinx.HLE.Loaders.Executables
{
class NxStaticObject : IExecutable
{
public byte[] Text { get; }
public byte[] Ro { get; }
public byte[] Data { get; }
public int TextOffset { get; }
public int RoOffset { get; }
public int DataOffset { get; }
public int BssSize { get; }
public int BssOffset => DataOffset + Data.Length;
[Flags]
private enum NsoFlags
{
IsTextCompressed = 1 << 0,
IsRoCompressed = 1 << 1,
IsDataCompressed = 1 << 2,
HasTextHash = 1 << 3,
HasRoHash = 1 << 4,
HasDataHash = 1 << 5
}
public NxStaticObject(Stream input)
{
BinaryReader reader = new BinaryReader(input);
input.Seek(0, SeekOrigin.Begin);
int nsoMagic = reader.ReadInt32();
int version = reader.ReadInt32();
int reserved = reader.ReadInt32();
int flagsMsk = reader.ReadInt32();
int textOffset = reader.ReadInt32();
int textMemOffset = reader.ReadInt32();
int textDecSize = reader.ReadInt32();
int modNameOffset = reader.ReadInt32();
int roOffset = reader.ReadInt32();
int roMemOffset = reader.ReadInt32();
int roDecSize = reader.ReadInt32();
int modNameSize = reader.ReadInt32();
int dataOffset = reader.ReadInt32();
int dataMemOffset = reader.ReadInt32();
int dataDecSize = reader.ReadInt32();
int bssSize = reader.ReadInt32();
byte[] buildId = reader.ReadBytes(0x20);
int textSize = reader.ReadInt32();
int roSize = reader.ReadInt32();
int dataSize = reader.ReadInt32();
input.Seek(0x24, SeekOrigin.Current);
int dynStrOffset = reader.ReadInt32();
int dynStrSize = reader.ReadInt32();
int dynSymOffset = reader.ReadInt32();
int dynSymSize = reader.ReadInt32();
byte[] textHash = reader.ReadBytes(0x20);
byte[] roHash = reader.ReadBytes(0x20);
byte[] dataHash = reader.ReadBytes(0x20);
NsoFlags flags = (NsoFlags)flagsMsk;
TextOffset = textMemOffset;
RoOffset = roMemOffset;
DataOffset = dataMemOffset;
BssSize = bssSize;
//Text segment
input.Seek(textOffset, SeekOrigin.Begin);
Text = reader.ReadBytes(textSize);
if (flags.HasFlag(NsoFlags.IsTextCompressed))
{
Text = Lz4.Decompress(Text, textDecSize);
}
//Read-only data segment
input.Seek(roOffset, SeekOrigin.Begin);
Ro = reader.ReadBytes(roSize);
if (flags.HasFlag(NsoFlags.IsRoCompressed))
{
Ro = Lz4.Decompress(Ro, roDecSize);
}
//Data segment
input.Seek(dataOffset, SeekOrigin.Begin);
Data = reader.ReadBytes(dataSize);
if (flags.HasFlag(NsoFlags.IsDataCompressed))
{
Data = Lz4.Decompress(Data, dataDecSize);
}
}
}
}