Advertisement
Brick

ReadPKGBinary

Jan 28th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. public static int GfxFVFOffset(int fvfFlags, int a2)
  2. {
  3.     int result = 0;
  4.  
  5.     if (a2 != 0x2)
  6.     {
  7.         result += 12;
  8.  
  9.         if (a2 != 0x10)
  10.         {
  11.             if ((fvfFlags & 0x10) != 0 /*D3DFVF_NORMAL*/ )
  12.             {
  13.                 result = 24;
  14.             }
  15.             if ((fvfFlags & 0x20) != 0 /*D3DFVF_PSIZE*/ )
  16.             {
  17.                 result += 4;
  18.             }
  19.             if (a2 != 0x40)
  20.             {
  21.                 if ((fvfFlags & 0x40) != 0 /*D3DFVF_DIFFUSE*/ )
  22.                 {
  23.                     result += 4;
  24.                 }
  25.                 if ((fvfFlags & 0x80) != 0 /*D3DFVF_SPECULAR*/ )
  26.                 {
  27.                     result += 4;
  28.                 }
  29.                 if (a2 != 0x100)
  30.                 {
  31.                     result += 8;
  32.                 }
  33.             }
  34.         }
  35.     }
  36.  
  37.     return result / 4;
  38. }
  39.  
  40. static int[] GfxBaseFVFSizes = { 0, 0, 12, 0, 16, 0, 16, 0, 20, 0, 24, 0, 28, 0, 32, 0 };
  41.  
  42. public static int GfxFVFSize(int fvfFlags)
  43. {
  44.     int result = GfxBaseFVFSizes[fvfFlags & 0xF];
  45.  
  46.     if ((fvfFlags & 0x10) != 0 /*D3DFVF_NORMAL*/ )
  47.     {
  48.         result += 12;
  49.     }
  50.     if ((fvfFlags & 0x20) != 0 /*D3DFVF_PSIZE*/ )
  51.     {
  52.         result += 4;
  53.     }
  54.     if ((fvfFlags & 0x40) != 0 /*D3DFVF_DIFFUSE*/ )
  55.     {
  56.         result += 4;
  57.     }
  58.     if ((fvfFlags & 0x80) != 0 /*D3DFVF_SPECULAR*/ )
  59.     {
  60.         result += 4;
  61.     }
  62.     if ((fvfFlags & 0x100) != 0)                 // 0x100
  63.     {
  64.         result += 8;
  65.     }
  66.     if ((fvfFlags & 0x200) != 0)                 // 0x200
  67.     {
  68.         result += 8;
  69.     }
  70.  
  71.     return result / 4;
  72. }
  73.  
  74. public class GfxPacket
  75. {
  76.     public int Type;
  77.     public float[] Vertices;
  78.     public int[] Indices;
  79. }
  80.  
  81. public class ModStatic
  82. {
  83.     public int Fvf;
  84.     public List<GfxPacket> Packets;
  85.     public List<int> ShaderIndices;
  86. }
  87.  
  88. public static ModStatic ReadModStatic(BinaryReader reader)
  89. {
  90.     var result = new ModStatic();
  91.  
  92.     var packetCount = reader.ReadInt32();
  93.  
  94.     if ((packetCount & 0x80) != 0)
  95.     {
  96.         packetCount &= 0x7F;
  97.  
  98.         result.Fvf = reader.ReadInt32();
  99.         result.ShaderIndices = reader.ReadBytes(packetCount).Select(x => (int) x).ToList();
  100.     }
  101.     else
  102.     {
  103.         var expectedVertices = reader.ReadInt32();
  104.         var expectedIndices  = reader.ReadInt32();
  105.         var expectedPackets  = reader.ReadInt32();
  106.         var fvf = reader.ReadInt32();
  107.  
  108.         result.Fvf = fvf;
  109.         result.Packets = new List<GfxPacket>();
  110.         result.ShaderIndices = new List<int>();
  111.  
  112.         var fvfOffset = GfxFVFOffset(fvf, 0x40);
  113.         var fvfSize = GfxFVFSize(fvf);
  114.  
  115.         var totalVerts = 0;
  116.         var totalIndices = 0;
  117.  
  118.         for (int i = 0; i < packetCount; ++i)
  119.         {
  120.             var stripCount = reader.ReadInt32();
  121.  
  122.             result.ShaderIndices.Add(reader.ReadInt32());
  123.  
  124.             for (int j = 0; j < stripCount; ++j)
  125.             {
  126.                 var type = reader.ReadInt32();
  127.                 var vertCount = reader.ReadInt32();
  128.  
  129.                 var verts = new float[fvfSize * vertCount];
  130.  
  131.                 for (int k = 0; k < vertCount; ++k)
  132.                 {
  133.                     for (int l = 0; l < fvfSize; ++l)
  134.                     {
  135.                         verts[(k * fvfSize) + l] = reader.ReadSingle();
  136.                     }
  137.  
  138.                     if ((fvf & 0x40) != 0)
  139.                     {
  140.                         verts[(k * fvfSize) + fvfOffset] = SwapEndian(verts[(k * fvfSize) + fvfOffset]);
  141.                     }
  142.                 }
  143.  
  144.                 var indicesCount = reader.ReadInt32();
  145.                 var indices = new int[indicesCount];
  146.  
  147.                 for (int k = 0; k < indices.Length; ++k)
  148.                 {
  149.                     indices[k] = reader.ReadUInt16();
  150.                 }
  151.  
  152.                 result.Packets.Add(new GfxPacket
  153.                 {
  154.                     Type = type,
  155.                     Vertices = verts,
  156.                     Indices = indices
  157.                 });
  158.  
  159.                 totalVerts += vertCount;
  160.                 totalIndices += indicesCount;
  161.             }
  162.         }
  163.  
  164.         Console.WriteLine("Verts:   {0,3} (expected {1,3})", totalVerts,   expectedVertices);
  165.         Console.WriteLine("Indices: {0,3} (expected {1,3})", totalIndices, expectedIndices);
  166.         Console.WriteLine("Packets: {0,3} (expected {1,3})", packetCount,  expectedPackets);
  167.     }
  168.  
  169.     return result;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement