Skip to content

Commit

Permalink
[pro]: support parsing version 11 trg
Browse files Browse the repository at this point in the history
  • Loading branch information
Js41637 committed Aug 23, 2024
1 parent ffedc81 commit e23572c
Showing 1 changed file with 61 additions and 17 deletions.
78 changes: 61 additions & 17 deletions TACTLib/Core/Product/Tank/ResourceGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public struct TRGHeader6 { // version 5 and 6
public int m_graphBlockSize; // 48
public uint m_footerMagic; // 52

public TRGHeader Upgrade() => new TRGHeader
{
public TRGHeader Upgrade() => new TRGHeader {
m_0 = m_0,
m_buildVersion = m_buildVersion,
m_8 = m_8,
Expand All @@ -48,7 +47,7 @@ public struct TRGHeader6 { // version 5 and 6
}

[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct TRGHeader { // version 7
public struct TRGHeader7 { // version 7, 8, 9, & 10
public uint m_0; // 0
public uint m_buildVersion; // 4
public uint m_8; // 8
Expand All @@ -66,21 +65,59 @@ public struct TRGHeader { // version 7
public int m_graphBlockSize; // 56
public uint m_footerMagic; // 60

public TRGHeader Upgrade() => new TRGHeader {
m_0 = m_0,
m_buildVersion = m_buildVersion,
m_8 = m_8,
m_12 = m_12,
m_16 = m_16,
m_20 = m_20,
m_packageCount = m_packageCount,
m_packageBlockSize = m_packageBlockSize,
m_skinCount = m_skinCount,
m_skinBlockSize = m_skinBlockSize,
m_typeBundleIndexCount = m_typeBundleIndexCount,
m_typeBundleIndexBlockSize = m_typeBundleIndexBlockSize,
m_48 = m_48,
m_52 = m_52,
m_graphBlockSize = m_graphBlockSize,
m_footerMagic = m_footerMagic
};
}

[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct TRGHeader { // version 11
public uint m_0; // 0
public uint m_buildVersion; // 4
public uint m_8; // 8
public uint m_12; // 12
public uint m_16; // 16
public uint m_20; // 20
public int m_packageCount; // 24
public int m_packageBlockSize; // 28
public int m_skinCount; // 32
public int m_skinBlockSize; // 36
public int m_typeBundleIndexCount; // 40
public int m_typeBundleIndexBlockSize; // 44
public uint m_48; // 48
public uint m_52; // 52
public uint m_56; // 56
public uint m_60; // 60
public int m_graphBlockSize; // 64
public uint m_footerMagic; // 68

public const uint UNENCRYPTED_MAGIC = 0x747267;
public const uint ENCRYPTED_MAGIC = 0x677274;

public uint GetNonEncryptedMagic()
{
public uint GetNonEncryptedMagic() {
return (uint)(UNENCRYPTED_MAGIC | (GetVersion() << 24));
}

public byte GetVersion()
{
public byte GetVersion() {
return IsEncrypted() ? (byte)(m_footerMagic & 0x000000FF) : (byte)((m_footerMagic & 0xFF000000) >> 24);
}

public bool IsEncrypted()
{
public bool IsEncrypted() {
return (m_footerMagic >> 8) == ENCRYPTED_MAGIC;
}
}
Expand All @@ -94,8 +131,7 @@ public struct Package5 { // before 9
public uint m_24;
public byte m_28;

public Package Upgrade() => new Package()
{
public Package Upgrade() => new Package() {
m_assetGUID = m_assetGUID,
m_resourceKeyID = m_resourceKeyID,
m_16 = m_16,
Expand All @@ -104,7 +140,7 @@ public struct Package5 { // before 9
m_28 = m_28
};
}

[StructLayout(LayoutKind.Sequential, Size = 30, Pack = 1)]
public struct Package { // 9+
public ulong m_assetGUID;
Expand Down Expand Up @@ -179,11 +215,14 @@ public struct SkinAsset8B {
public byte[] m_graphBlock;
public byte[]? m_typeBundleIndexBlock;

public static bool IsPre152(TRGHeader header)
{
public static bool IsPre152(TRGHeader header) {
return header.m_buildVersion < ProductHandler_Tank.VERSION_152_PTR || header.m_buildVersion == 72604; // 72604 = 1.51 on proc2
}

public static bool IsPre212(TRGHeader header) {
return header.m_buildVersion < 128702; // 128702 = 2.12 on pro
}

public ResourceGraph(ClientHandler client, Stream stream, string name) {
using (BinaryReader reader = new BinaryReader(stream)) {
m_header = reader.Read<TRGHeader>();
Expand All @@ -192,11 +231,16 @@ public ResourceGraph(ClientHandler client, Stream stream, string name) {
m_header = reader.Read<TRGHeader6>().Upgrade();
}

if (IsPre212(m_header)) {
stream.Position = 0;
m_header = reader.Read<TRGHeader7>().Upgrade();
}

var version = m_header.GetVersion();
if (version != 5 && version != 6 && version != 7 && version != 8 && version != 9 && version != 10) {
throw new UnsupportedBuildVersionException($"unable to parse TRG. invalid version {version}, expected 5, 6, 7, 8, 9 or 10");
if (version is < 5 or > 11) {
throw new UnsupportedBuildVersionException($"unable to parse TRG. invalid version {version}, expected 5, 6, 7, 8, 9, 10, or 11");
}

// version 10: added extra entries to skin assets.. for trg runtime overrides (instead of on the skin asset)

var isEnc = m_header.IsEncrypted();
Expand Down

0 comments on commit e23572c

Please sign in to comment.