Esempio n. 1
0
        private void WritePmd(BinaryWriter w)
        {
            w.Write(PmdVersion);

            w.Write(Looks[0].Materials.Count);
            w.Write(Looks.Count);
            w.Write(Parts.Sum(x => x.Pieces.Count)); // TODO: Why is the pmd piece count different from the pmg piece count?
            w.Write(Variants.Count);
            w.Write(Parts.Count);
            w.Write(Parts.Count); // attribs count

            byte[] lookNames = WriteToByteArray((_w) =>
            {
                foreach (var look in Looks)
                {
                    _w.Write(look.Name);
                }
            });

            byte[] variantNames = WriteToByteArray((_w) =>
            {
                foreach (var variant in Variants)
                {
                    _w.Write(variant.Name);
                }
            });

            // placeholder code
            byte[] partAttribs = WriteToByteArray((_w) =>
            {
                for (int i = 0; i < Parts.Count; i++)
                {
                    _w.Write(i);
                    _w.Write(i + 1);
                }
            });

            byte[] attribsHeader = WriteToByteArray((_w) =>
            {
                var offset = 0;
                foreach (var attrib in Variants[0].Attributes)
                {
                    _w.Write(attrib.Tag);
                    _w.Write(attrib.Type);
                    _w.Write(offset);
                    offset += 4;
                }
            });

            byte[] attribsValues = WriteToByteArray((_w) =>
            {
                foreach (var variant in Variants)
                {
                    foreach (var attrib in variant.Attributes)
                    {
                        _w.Write(attrib.Value);
                    }
                }
            });

            // materials offsets
            var materialOffsetsLength = Looks[0].Materials.Count * sizeof(int);

            var materialStrs = Looks.Select(x => x.Materials).SelectMany(x => x).ToList();
            var materials = StringUtils.ListToCStringByteList(materialStrs);

            w.Write(attribsValues.Length / Variants.Count);
            w.Write(materials.Sum(x => x.Length));

            var offsetPartLength = 7 * sizeof(int);

            int lookNamesOffset = (int)w.BaseStream.Position + offsetPartLength;
            w.Write(lookNamesOffset);

            int variantNamesOffset = lookNamesOffset + lookNames.Length;
            w.Write(variantNamesOffset);

            int partAttribsOffset = variantNamesOffset + variantNames.Length;
            w.Write(partAttribsOffset);

            int attribsHeaderOffset = partAttribsOffset + partAttribs.Length;
            int attribsValuesOffset = attribsHeaderOffset + attribsHeader.Length;
            w.Write(attribsValuesOffset);
            w.Write(attribsHeaderOffset);

            int materialOffsetsOffset = attribsValuesOffset + attribsValues.Length;
            w.Write(materialOffsetsOffset);

            int materialDataOffset = materialOffsetsOffset + materialOffsetsLength;
            w.Write(materialDataOffset);

            w.Write(lookNames);
            w.Write(variantNames);
            w.Write(partAttribs);
            w.Write(attribsHeader);
            w.Write(attribsValues);

            var materialOffset = (int)w.BaseStream.Position + (materials.Count * sizeof(int));
            for (int i = 0; i < materials.Count; i++)
            {
                w.Write(materialOffset);
                materialOffset += materials[i].Length;
            }

            foreach (var str in materials)
            {
                w.Write(str);
            }
        }