1+ using System . Collections . Generic ;
2+ using System . IO ;
3+ using Warcraft . NET . Attribute ;
4+ using Warcraft . NET . Extensions ;
5+ using Warcraft . NET . Files . Interfaces ;
6+ using Warcraft . NET . Files . Structures ;
7+
8+ namespace Warcraft . NET . Files . M2 . Chunks . TWW
9+ {
10+ [ AutoDocChunk ( AutoDocChunkVersionHelper . VersionAfterTWW , AutoDocChunkVersionHelper . VersionBeforeMD ) ]
11+ public class PCOL : IIFFChunk , IBinarySerializable
12+ {
13+ /// <summary>
14+ /// Holds the binary chunk signature.
15+ /// </summary>
16+ public const string Signature = "PCOL" ;
17+
18+ /// <summary>
19+ /// Gets or sets the vertex positions
20+ /// </summary>
21+ public List < C3Vector > VertexPositions { get ; set ; }
22+
23+ /// <summary>
24+ /// Gets or sets the face normals
25+ /// </summary>
26+ public List < C3Vector > FaceNormals { get ; set ; }
27+
28+ /// <summary>
29+ /// Gets or sets the indices
30+ /// </summary>
31+ public List < ushort > Indices { get ; set ; }
32+
33+ /// <summary>
34+ /// Gets or sets the flags (unknown meaning)
35+ /// </summary>
36+ public List < ushort > Flags { get ; set ; }
37+
38+ /// <summary>
39+ /// Initializes a new instance of <see cref="PCOL"/>
40+ /// </summary>
41+ public PCOL ( ) { }
42+
43+ /// <summary>
44+ /// Initializes a new instance of <see cref="PCOL"/>
45+ /// </summary>
46+ /// <param name="inData">ExtendedData.</param>
47+ public PCOL ( byte [ ] inData ) => LoadBinaryData ( inData ) ;
48+
49+ /// <inheritdoc />
50+ public string GetSignature ( ) { return Signature ; }
51+
52+ /// <inheritdoc />
53+ public uint GetSize ( ) { return ( uint ) Serialize ( ) . Length ; }
54+
55+ /// <inheritdoc />
56+ public void LoadBinaryData ( byte [ ] inData )
57+ {
58+ using ( var ms = new MemoryStream ( inData ) )
59+ using ( var br = new BinaryReader ( ms ) )
60+ {
61+ var vertexPositionCount = br . ReadUInt32 ( ) ;
62+ var vertexPositionOffset = br . ReadUInt32 ( ) ;
63+ var faceNormalCount = br . ReadUInt32 ( ) ;
64+ var faceNormalOffset = br . ReadUInt32 ( ) ;
65+ var indexCount = br . ReadUInt32 ( ) ;
66+ var indexOffset = br . ReadUInt32 ( ) ;
67+ var flagCount = br . ReadUInt32 ( ) ;
68+ var flagOffset = br . ReadUInt32 ( ) ;
69+
70+ VertexPositions = br . ReadStructList < C3Vector > ( vertexPositionCount , vertexPositionOffset ) ;
71+ FaceNormals = br . ReadStructList < C3Vector > ( faceNormalCount , faceNormalOffset ) ;
72+ Indices = br . ReadStructList < ushort > ( indexCount , indexOffset ) ;
73+ Flags = br . ReadStructList < ushort > ( flagCount , flagOffset ) ;
74+ }
75+ }
76+
77+ /// <inheritdoc />
78+ public byte [ ] Serialize ( long offset = 0 )
79+ {
80+ using var ms = new MemoryStream ( ) ;
81+ using ( var bw = new BinaryWriter ( ms ) )
82+ {
83+ bw . Write ( ( uint ) VertexPositions . Count ) ;
84+ bw . Write ( ( uint ) ( 32 ) ) ; // vertex positions offset
85+
86+ bw . Write ( ( uint ) FaceNormals . Count ) ;
87+ bw . Write ( ( uint ) ( 32 + VertexPositions . Count * sizeof ( float ) * 3 ) ) ; // face normals offset
88+
89+ bw . Write ( ( uint ) Indices . Count ) ;
90+ bw . Write ( ( uint ) ( 32 + VertexPositions . Count * sizeof ( float ) * 3 + FaceNormals . Count * sizeof ( float ) * 3 ) ) ; // indices offset
91+
92+ bw . Write ( ( uint ) Flags . Count ) ;
93+ bw . Write ( ( uint ) ( 32 + VertexPositions . Count * sizeof ( float ) * 3 + FaceNormals . Count * sizeof ( float ) * 3 + Indices . Count * sizeof ( ushort ) ) ) ; // flags offset
94+
95+ foreach ( var vertex in VertexPositions )
96+ bw . WriteStruct ( vertex ) ;
97+
98+ foreach ( var normal in FaceNormals )
99+ bw . WriteStruct ( normal ) ;
100+
101+ foreach ( var index in Indices )
102+ bw . Write ( index ) ;
103+
104+ foreach ( var flag in Flags )
105+ bw . Write ( flag ) ;
106+
107+ return ms . ToArray ( ) ;
108+ }
109+ }
110+ }
111+ }
0 commit comments