LibHac/libhac/Cnmt.cs

135 lines
4.2 KiB
C#
Raw Normal View History

2018-06-23 04:02:19 +02:00
using System.IO;
namespace libhac
{
public class Cnmt
{
public ulong TitleId { get; set; }
public TitleVersion TitleVersion { get; set; }
2018-06-23 04:02:19 +02:00
public TitleType Type { get; set; }
public byte FieldD { get; set; }
public int TableOffset { get; set; }
public int ContentEntryCount { get; set; }
public int MetaEntryCount { get; set; }
public CnmtContentEntry[] ContentEntries { get; set; }
public CnmtMetaEntry[] MetaEntries { get; set; }
public ulong ApplicationTitleId { get; set; }
public ulong PatchTitleId { get; set; }
public TitleVersion MinimumSystemVersion { get; }
public TitleVersion MinimumApplicationVersion { get; }
2018-06-23 04:02:19 +02:00
2018-08-02 23:40:08 +02:00
public Cnmt() { }
2018-06-23 04:02:19 +02:00
public Cnmt(Stream file)
{
using (var reader = new BinaryReader(file))
{
TitleId = reader.ReadUInt64();
var version = reader.ReadUInt32();
2018-06-23 04:02:19 +02:00
Type = (TitleType)reader.ReadByte();
TitleVersion = new TitleVersion(version, Type < TitleType.Application);
2018-06-23 04:02:19 +02:00
FieldD = reader.ReadByte();
TableOffset = reader.ReadUInt16();
ContentEntryCount = reader.ReadUInt16();
MetaEntryCount = reader.ReadUInt16();
file.Position += 12;
2018-08-02 23:40:08 +02:00
switch (Type)
{
case TitleType.Application:
2018-07-02 20:16:38 +02:00
ApplicationTitleId = TitleId;
PatchTitleId = reader.ReadUInt64();
MinimumSystemVersion = new TitleVersion(reader.ReadUInt32(), true);
break;
case TitleType.Patch:
ApplicationTitleId = reader.ReadUInt64();
MinimumSystemVersion = new TitleVersion(reader.ReadUInt32(), true);
break;
case TitleType.AddOnContent:
ApplicationTitleId = reader.ReadUInt64();
MinimumApplicationVersion = new TitleVersion(reader.ReadUInt32());
break;
}
file.Position = 0x20 + TableOffset;
2018-06-23 04:02:19 +02:00
ContentEntries = new CnmtContentEntry[ContentEntryCount];
MetaEntries = new CnmtMetaEntry[MetaEntryCount];
2018-06-23 04:02:19 +02:00
for (int i = 0; i < ContentEntryCount; i++)
{
ContentEntries[i] = new CnmtContentEntry(reader);
}
for (int i = 0; i < MetaEntryCount; i++)
{
MetaEntries[i] = new CnmtMetaEntry(reader);
}
2018-06-23 04:02:19 +02:00
}
}
}
public class CnmtContentEntry
{
public byte[] Hash { get; set; }
public byte[] NcaId { get; set; }
public long Size { get; set; }
public CnmtContentType Type { get; set; }
2018-08-02 23:40:08 +02:00
public CnmtContentEntry() { }
2018-06-23 04:02:19 +02:00
public CnmtContentEntry(BinaryReader reader)
{
Hash = reader.ReadBytes(0x20);
NcaId = reader.ReadBytes(0x10);
Size = reader.ReadUInt32();
Size |= ((long)reader.ReadUInt16() << 32);
Type = (CnmtContentType)reader.ReadByte();
reader.BaseStream.Position += 1;
}
}
public class CnmtMetaEntry
{
public ulong TitleId { get; }
public TitleVersion Version { get; }
public CnmtContentType Type { get; }
2018-08-02 23:40:08 +02:00
public CnmtMetaEntry() { }
public CnmtMetaEntry(BinaryReader reader)
{
TitleId = reader.ReadUInt64();
Version = new TitleVersion(reader.ReadUInt32(), true);
Type = (CnmtContentType)reader.ReadByte();
reader.BaseStream.Position += 3;
}
}
2018-06-23 04:02:19 +02:00
public enum CnmtContentType
{
Meta,
Program,
Data,
Control,
OfflineManualHtml,
LegalHtml,
UpdatePatch
}
public enum TitleType
{
SystemProgram = 1,
SystemData,
2018-06-23 04:02:19 +02:00
SystemUpdate,
BootImagePackage,
BootImagePackageSafe,
2018-06-25 21:01:24 +02:00
Application = 0x80,
Patch,
2018-06-23 04:02:19 +02:00
AddOnContent,
DeltaTitle
}
}