HaloMap.RawData.PRTMModel.InjectModel C# (CSharp) Method

InjectModel() public method

The inject model.
public InjectModel ( string FilePath, Meta meta ) : Meta
FilePath string The file path.
meta HaloMap.Meta.Meta The meta.
return HaloMap.Meta.Meta
        public override Meta InjectModel(string FilePath, Meta meta)
        {
            LoadFromOBJ(FilePath);

            char[] crsr = new[] { 'c', 'r', 's', 'r' };
            char[] fklb = new[] { 'f', 'k', 'l', 'b' };
            for (int x = 0; x < this.RawDataMetaChunks.Length; x++)
            {
                MemoryStream raw = new MemoryStream();
                BinaryWriter newraw = new BinaryWriter(raw);
                BinaryReader oldraw = new BinaryReader(meta.raw.rawChunks[x].MS);

                int newrawsize = 0;
                int rawchunkid = 0;

                #region Write Header

                oldraw.BaseStream.Position = 0;
                newraw.BaseStream.Position = 0;
                newraw.Write(oldraw.ReadBytes(this.RawDataMetaChunks[x].HeaderSize));
                newrawsize += this.RawDataMetaChunks[x].HeaderSize;

                #endregion

                #region Write Submesh Info

                newraw.BaseStream.Position = newrawsize;
                newraw.Write(crsr);
                newrawsize += 4;
                for (int y = 0; y < this.RawDataMetaChunks[x].SubMeshInfo.Length; y++)
                {
                    oldraw.BaseStream.Position = this.RawDataMetaChunks[x].HeaderSize +
                                                 this.RawDataMetaChunks[x].RawDataChunkInfo[0].Offset;
                    newraw.BaseStream.Position = newrawsize + (y * 72);
                    newraw.Write(oldraw.ReadBytes(72));
                    newraw.BaseStream.Position = newrawsize + (y * 72) + 4;
                    newraw.Write((short)this.RawDataMetaChunks[x].SubMeshInfo[y].ShaderNumber);
                    newraw.Write((short)this.RawDataMetaChunks[x].SubMeshInfo[y].IndiceStart);
                    newraw.Write((short)this.RawDataMetaChunks[x].SubMeshInfo[y].IndiceCount);
                }

                this.RawDataMetaChunks[x].RawDataChunkInfo[0].ChunkCount = this.RawDataMetaChunks[x].SubMeshInfo.Length;
                this.RawDataMetaChunks[x].RawDataChunkInfo[0].Size = this.RawDataMetaChunks[x].SubMeshInfo.Length * 72;
                newrawsize += this.RawDataMetaChunks[x].SubMeshInfo.Length * 72;

                // write count
                newraw.BaseStream.Position = 8;
                newraw.Write(this.RawDataMetaChunks[x].SubMeshInfo.Length);

                #endregion

                #region Write Unknown

                rawchunkid = 1;

                while (newrawsize > 0)
                {
                    if (this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].ChunkSize == 2)
                    {
                        break;
                    }

                    oldraw.BaseStream.Position = this.RawDataMetaChunks[x].HeaderSize +
                                                 this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Offset;
                    newraw.BaseStream.Position = newrawsize;
                    newraw.Write(crsr);
                    newrawsize += 4;
                    newraw.Write(oldraw.ReadBytes(this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Size));
                    this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Offset = newrawsize -
                                                                                    this.RawDataMetaChunks[x].HeaderSize;
                    newrawsize += this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Size;
                    rawchunkid++;
                }

                #endregion

                #region Write Indices

                int indicechunkid = rawchunkid;
                newraw.BaseStream.Position = newrawsize;
                newraw.Write(crsr);
                newrawsize += 4;
                for (int y = 0; y < this.RawDataMetaChunks[x].Indices.Length; y++)
                {
                    newraw.Write(this.RawDataMetaChunks[x].Indices[y]);
                }

                this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Offset = newrawsize -
                                                                                this.RawDataMetaChunks[x].HeaderSize;
                this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].ChunkCount =
                    this.RawDataMetaChunks[x].Indices.Length;
                this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Size = this.RawDataMetaChunks[x].Indices.Length *
                                                                              2;
                newrawsize += this.RawDataMetaChunks[x].Indices.Length * 2;
                rawchunkid++;

                // indice count
                newraw.BaseStream.Position = 40;
                newraw.Write((short)this.RawDataMetaChunks[x].Indices.Length);

                #endregion

                #region Write Unknown

                oldraw.BaseStream.Position = this.RawDataMetaChunks[x].HeaderSize +
                                             this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Offset;
                newraw.BaseStream.Position = newrawsize;
                newraw.Write(crsr);
                newrawsize += 4;
                newraw.Write(oldraw.ReadBytes(this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Size));
                this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Offset = newrawsize -
                                                                                this.RawDataMetaChunks[x].HeaderSize;
                newrawsize += this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Size;

                rawchunkid++;

                #endregion

                #region Write Vertices

                int verticechunkid = indicechunkid + 2;
                newraw.BaseStream.Position = newrawsize;
                newraw.Write(crsr);
                newrawsize += 4;
                newraw.Write(
                    new byte[
                        this.RawDataMetaChunks[x].RawDataChunkInfo[verticechunkid].ChunkSize *
                        this.RawDataMetaChunks[x].VerticeCount]);
                for (int y = 0; y < this.RawDataMetaChunks[x].VerticeCount; y++)
                {
                    newraw.BaseStream.Position = newrawsize +
                                                 (y *
                                                  this.RawDataMetaChunks[x].RawDataChunkInfo[verticechunkid].ChunkSize);
                    short vx =
                        (short)
                        this.RawDataMetaChunks[x].CompressVertice(
                            this.RawDataMetaChunks[x].Vertices[y].X, this.BoundingBox.MinX, this.BoundingBox.MaxX);
                    short vy =
                        (short)
                        this.RawDataMetaChunks[x].CompressVertice(
                            this.RawDataMetaChunks[x].Vertices[y].Y, this.BoundingBox.MinY, this.BoundingBox.MaxY);
                    short vz =
                        (short)
                        this.RawDataMetaChunks[x].CompressVertice(
                            this.RawDataMetaChunks[x].Vertices[y].Z, this.BoundingBox.MinZ, this.BoundingBox.MaxZ);
                    newraw.Write(vx);
                    newraw.Write(vy);
                    newraw.Write(vz);
                }

                this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Offset = newrawsize -
                                                                                this.RawDataMetaChunks[x].HeaderSize;
                newrawsize += this.RawDataMetaChunks[x].RawDataChunkInfo[verticechunkid].ChunkSize *
                              this.RawDataMetaChunks[x].VerticeCount;
                this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Size =
                    this.RawDataMetaChunks[x].RawDataChunkInfo[verticechunkid].ChunkSize *
                    this.RawDataMetaChunks[x].VerticeCount;
                this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].ChunkSize = 0;

                // this.RawDataMetaChunks[x].RawDataChunkInfo[verticechunkid].ChunkSize * this.RawDataMetaChunks[x].VerticeCount; ;
                newraw.BaseStream.Position = 100;
                newraw.Write(this.RawDataMetaChunks[x].VerticeCount);

                rawchunkid++;

                #endregion

                #region Write UVs

                int uvchunkid = verticechunkid + 1;
                newraw.BaseStream.Position = newrawsize;
                newraw.Write(crsr);
                newrawsize += 4;

                for (int y = 0; y < this.RawDataMetaChunks[x].VerticeCount; y++)
                {
                    short u =
                        (short)
                        this.RawDataMetaChunks[x].CompressVertice(
                            this.RawDataMetaChunks[x].UVs[y].X, this.BoundingBox.MinU, this.BoundingBox.MaxU);
                    short v =
                        (short)
                        this.RawDataMetaChunks[x].CompressVertice(
                            this.RawDataMetaChunks[x].UVs[y].Y, this.BoundingBox.MinV, this.BoundingBox.MaxV);
                    newraw.Write(u);
                    newraw.Write(v);
                }

                this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Offset = newrawsize -
                                                                                this.RawDataMetaChunks[x].HeaderSize;
                this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].ChunkSize = 1;
                this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Size = 4 * this.RawDataMetaChunks[x].VerticeCount;
                newrawsize += 4 * this.RawDataMetaChunks[x].VerticeCount;
                rawchunkid++;

                #endregion

                #region Write Normals

                newraw.BaseStream.Position = newrawsize;
                newraw.Write(crsr);
                newrawsize += 4;

                for (int y = 0; y < this.RawDataMetaChunks[x].VerticeCount; y++)
                {
                    int u =
                        (int)
                        this.RawDataMetaChunks[x].CompressVertice(
                            this.RawDataMetaChunks[x].UVs[y].X, this.BoundingBox.MinU, this.BoundingBox.MaxU);
                    int v =
                        (int)
                        this.RawDataMetaChunks[x].CompressVertice(
                            this.RawDataMetaChunks[x].UVs[y].Y, this.BoundingBox.MinV, this.BoundingBox.MaxV);
                    newraw.Write(u);
                    newraw.Write(v);
                    newraw.Write(v);
                }

                this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Offset = newrawsize -
                                                                                this.RawDataMetaChunks[x].HeaderSize;
                this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].ChunkSize = 2;
                this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Size = 4 * this.RawDataMetaChunks[x].VerticeCount;
                newrawsize += 12 * this.RawDataMetaChunks[x].VerticeCount;
                rawchunkid++;

                #endregion

                #region Write Other Stuff Not Yet Implemented

                while (rawchunkid < this.RawDataMetaChunks[x].RawDataChunkInfo.Length)
                {
                    oldraw.BaseStream.Position = this.RawDataMetaChunks[x].HeaderSize +
                                                 this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Offset;
                    newraw.BaseStream.Position = newrawsize;
                    newraw.Write(crsr);
                    newrawsize += 4;
                    newraw.Write(oldraw.ReadBytes(this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Size));
                    this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Offset = newrawsize -
                                                                                    this.RawDataMetaChunks[x].HeaderSize;
                    newrawsize += this.RawDataMetaChunks[x].RawDataChunkInfo[rawchunkid].Size;
                    rawchunkid++;
                }

                #endregion

                // footer
                newraw.Write(fklb);
                newrawsize += 4;

                // raw size
                int rawsize = newrawsize - this.RawDataMetaChunks[x].HeaderSize - 4;
                newraw.BaseStream.Position = 4;
                newraw.Write(rawsize);
                meta.raw.rawChunks[x].MS = raw;
                meta.raw.rawChunks[x].size = newrawsize;
            }

            MetaSplitter ms = new MetaSplitter();
            IFPIO io = IFPHashMap.GetIfp(meta.type, map.HaloVersion);
            ms.SplitWithIFP(ref io, ref meta, map);

            #region Write Bounding Box

            int c = 0;
            while (c != -1)
            {
                if (ms.Header.Chunks[0].ChunkResources[c].offset == 20)
                {
                    break;
                }

                c++;
            }

            MetaSplitter.SplitReflexive reflex = ms.Header.Chunks[0].ChunkResources[c] as MetaSplitter.SplitReflexive;

            BinaryWriter BW = new BinaryWriter(reflex.Chunks[0].MS);
            BW.BaseStream.Position = 0;
            BW.Write(this.BoundingBox.MinX);
            BW.Write(this.BoundingBox.MaxX);
            BW.Write(this.BoundingBox.MinY);
            BW.Write(this.BoundingBox.MaxY);
            BW.Write(this.BoundingBox.MinZ);
            BW.Write(this.BoundingBox.MaxZ);
            BW.Write(this.BoundingBox.MinU);
            BW.Write(this.BoundingBox.MaxU);
            BW.Write(this.BoundingBox.MinV);
            BW.Write(this.BoundingBox.MaxV);
            ms.Header.Chunks[0].ChunkResources[c] = reflex;

            #endregion

            #region metachunks

            while (c != -1)
            {
                if (ms.Header.Chunks[0].ChunkResources[c].offset == 36)
                {
                    break;
                }

                c++;
            }

            MetaSplitter.SplitReflexive reflexe = ms.Header.Chunks[0].ChunkResources[c] as MetaSplitter.SplitReflexive;

            for (int l = 0; l < reflexe.Chunks.Count; l++)
            {
                BW = new BinaryWriter(reflexe.Chunks[l].MS);
                BW.BaseStream.Position = 4;
                short facecount = (short)this.RawDataMetaChunks[l].FaceCount;
                BW.Write((short)this.RawDataMetaChunks[l].VerticeCount);
                BW.Write(facecount);
                BW.BaseStream.Position = 68;
                BW.Write(meta.raw.rawChunks[l].size - this.RawDataMetaChunks[l].HeaderSize - 4);

                for (int h = 0; h < reflexe.Chunks[l].ChunkResources.Count; h++)
                {
                    if (reflexe.Chunks[l].ChunkResources[h].offset == 72)
                    {
                        MetaSplitter.SplitReflexive reflexx =
                            reflexe.Chunks[l].ChunkResources[h] as MetaSplitter.SplitReflexive;
                        for (int k = 0; k < reflexx.Chunks.Count; k++)
                        {
                            BinaryWriter BWX = new BinaryWriter(reflexx.Chunks[k].MS);
                            BWX.BaseStream.Position = 6;
                            BWX.Write((short)this.RawDataMetaChunks[l].RawDataChunkInfo[k].ChunkSize);
                            BWX.Write(this.RawDataMetaChunks[l].RawDataChunkInfo[k].Size);
                            BWX.Write(this.RawDataMetaChunks[l].RawDataChunkInfo[k].Offset);
                        }

                        reflexe.Chunks[l].ChunkResources[h] = reflexx;
                        break;
                    }
                }
            }

            while (c != -1)
            {
                if (ms.Header.Chunks[0].ChunkResources[c].offset == 96)
                {
                    break;
                }

                c++;
            }

            reflexe = ms.Header.Chunks[0].ChunkResources[c] as MetaSplitter.SplitReflexive;

            if (this.Shaders.Shader.Length > this.Shaders.count)
            {
                int diff = this.Shaders.Shader.Length - reflexe.Chunks.Count;
                for (int x = 0; x < diff; x++)
                {
                    reflexe.Chunks.Add(reflexe.Chunks[0]);
                }
            }
            else
            {
            }

            #endregion

            Meta fucker = MetaBuilder.BuildMeta(ms, map);
            fucker.raw = meta.raw;

            return fucker;
        }