From e23572ce6005081b7bcf2162fb65d9071d00d950 Mon Sep 17 00:00:00 2001 From: Js41637 Date: Fri, 23 Aug 2024 17:12:07 +1000 Subject: [PATCH] [pro]: support parsing version 11 trg --- TACTLib/Core/Product/Tank/ResourceGraph.cs | 78 +++++++++++++++++----- 1 file changed, 61 insertions(+), 17 deletions(-) diff --git a/TACTLib/Core/Product/Tank/ResourceGraph.cs b/TACTLib/Core/Product/Tank/ResourceGraph.cs index 83d0c3d..3553c85 100644 --- a/TACTLib/Core/Product/Tank/ResourceGraph.cs +++ b/TACTLib/Core/Product/Tank/ResourceGraph.cs @@ -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, @@ -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 @@ -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; } } @@ -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, @@ -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; @@ -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(); @@ -192,11 +231,16 @@ public ResourceGraph(ClientHandler client, Stream stream, string name) { m_header = reader.Read().Upgrade(); } + if (IsPre212(m_header)) { + stream.Position = 0; + m_header = reader.Read().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();