Example #1
0
        public void ReadFromFile(BinaryReader reader)
        {
            //KynogonRuntimeMesh
            long   mesh_pos  = reader.BaseStream.Position;
            string magicName = new string(reader.ReadChars(18));

            if (magicName != "KynogonRuntimeMesh")
            {
                throw new FormatException("Did not find KynogonRuntimeMesh");
            }

            short mesh_unk0    = reader.ReadInt16();
            int   magicVersion = reader.ReadInt32();

            if (magicVersion != 2)
            {
                throw new FormatException("Version did not equal 2");
            }

            Unk0 = reader.ReadInt32();
            Unk1 = reader.ReadInt32();
            Unk2 = reader.ReadSingle();
            Unk3 = reader.ReadSingle();

            float boundMinX = reader.ReadSingle();
            float boundMaxX = reader.ReadSingle();
            float boundMaxY = -reader.ReadSingle();
            float boundMinY = -reader.ReadSingle();

            BoundMin = new Vector2(boundMinX, boundMinY);
            BoundMax = new Vector2(boundMaxX, boundMaxY);

            //BoundingBox = BoundingBoxExtenders.ReadFromFile(reader);
            CellSizeX = reader.ReadInt32();
            CellSizeY = reader.ReadInt32();
            Radius    = reader.ReadSingle();
            Unk4      = reader.ReadInt32();
            Height    = reader.ReadInt32();
            Offset    = reader.ReadInt32(); //this is a potential offset;
            Grid      = new int[(CellSizeX * CellSizeY)];
            Cells     = new Cell[Grid.Length];

            for (int i = 0; i < Grid.Length; i++)
            {
                Grid[i] = reader.ReadInt32();
            }

            int end   = reader.ReadInt32();
            int Count = 0;

            for (int i = 0; i < Cells.Length; i++)
            {
                //if (i + 1 >= Cells.Length)
                //    break;

                Cell cell    = new Cell();
                int  numSet0 = reader.ReadInt32();
                cell.Sets = new UnkSet0[numSet0];
                Cells[i]  = cell;

                if (numSet0 == 0)
                {
                    continue;
                }

                //NOTE: EVERY OFFSET IN UNKSET0 BEGINS FROM MESH_POS HIGHER IN THE CODE FILE.
                cell.Offset = reader.ReadInt32(); /// start offset (excluding sets)

                for (int x = 0; x < numSet0; x++)
                {
                    UnkSet0 set = new UnkSet0();
                    set.X        = reader.ReadSingle();
                    set.Y        = reader.ReadSingle();
                    set.Offset   = reader.ReadInt32(); // End offset
                    cell.Sets[x] = set;
                }

                //NOTE: EVERY BLOCK OF DATA SEEMS TO START WITH 96, THIS IS HOWEVER UNCONFIRMED.
                for (int z = 0; z < numSet0; z++)
                {
                    long set_offset = reader.BaseStream.Position - mesh_pos;

                    UnkSet0 set = cell.Sets[z];
                    //NOTE: MOST OF THE OFFSETS BEGIN HERE, JUST AFTER THE SETS HAVE BEEN DEFINED ABOVE.
                    set.cellUnk0 = reader.ReadInt32();

                    if (set.cellUnk0 != Unk1)
                    {
                        throw new FormatException();
                    }

                    set.cellUnk1      = reader.ReadInt32();
                    set.cellUnk2      = reader.ReadInt32();
                    set.cellUnk3      = reader.ReadInt32();
                    set.cellUnk4      = reader.ReadSingle();
                    set.cellUnk5      = reader.ReadSingle();
                    set.cellUnk6      = reader.ReadSingle();
                    set.cellUnk7      = reader.ReadSingle();
                    set.cellUnk8      = reader.ReadInt32();
                    set.cellUnk9      = reader.ReadInt32(); //-1?
                    set.NumUnk10Boxes = reader.ReadInt32(); //1;
                    set.cellUnk11     = reader.ReadInt32(); // offset is start - setpos
                    set.NumUnk12Boxes = reader.ReadInt32(); //0
                    set.cellUnk13     = reader.ReadInt32(); // offset is start - setpos
                    set.NumUn14Boxes  = reader.ReadInt32(); //0
                    set.cellUnk15     = reader.ReadInt32(); // offset is start - setpos
                    set.NumEdges      = reader.ReadInt32(); //8;
                    set.cellUnk17     = reader.ReadInt32(); // offset is start - setpos
                    set.NumUnk18Boxes = reader.ReadInt32(); //0;
                    set.cellUnk19     = reader.ReadInt32(); // offset is start - setpos

                    Count += set.NumUnk12Boxes;

                    // First set of boxes
                    set.unk10Boxes = new Unk10DataSet[set.NumUnk10Boxes];
                    for (int x = 0; x < set.NumUnk10Boxes; x++)
                    {
                        Unk10DataSet unk10Set = new Unk10DataSet();
                        unk10Set.B1        = BoundingBoxExtenders.ReadFromFile(reader);
                        unk10Set.UnkOffset = reader.ReadInt32(); // offset is next set in next cell?

                        //  Linked cell?
                        unk10Set.Unk20    = reader.ReadInt32(); // really unsure on what this is
                        set.unk10Boxes[x] = unk10Set;
                    }

                    // 2nd set of boxes
                    set.unk12Boxes = new Unk12DataSet[set.NumUnk12Boxes];
                    for (int x = 0; x < set.NumUnk12Boxes; x++)
                    {
                        Unk12DataSet unk12Set = new Unk12DataSet();
                        unk12Set.B1    = BoundingBoxExtenders.ReadFromFile(reader);
                        unk12Set.Unk01 = reader.ReadInt32();
                        unk12Set.Unk02 = reader.ReadInt32();
                        unk12Set.Unk03 = reader.ReadSingle();
                        unk12Set.Unk04 = reader.ReadSingle();
                        unk12Set.Unk05 = reader.ReadSingle();

                        set.unk12Boxes[x] = unk12Set;
                    }

                    // 3rd set of boxes, but these are offsets
                    set.unk14Boxes = new Unk14DataSet[set.NumUn14Boxes];
                    for (int x = 0; x < set.NumUn14Boxes; x++)
                    {
                        var dataSet = new Unk14DataSet();
                        dataSet.Offset = reader.ReadInt32();

                        set.unk14Boxes[x] = dataSet;
                    }

                    // 4th set of boxes, based on the offsets above.
                    if (set.NumUn14Boxes > 0)
                    {
                        set.unk14End = reader.ReadInt32();
                        var currentOffset = set.unk14Boxes[0].Offset;
                        for (int x = 0; x < set.NumUn14Boxes; x++)
                        {
                            var dataSet = set.unk14Boxes[x];
                            var offset  = (x + 1 < set.NumUn14Boxes ? set.unk14Boxes[x + 1].Offset : set.unk14End);
                            var size    = offset - currentOffset;
                            currentOffset = offset;
                            var count = size / 12;
                            dataSet.Points = new Vector3[count];

                            for (int f = 0; f < dataSet.Points.Length; f++)
                            {
                                dataSet.Points[f] = Vector3Utils.ReadFromFile(reader);
                            }

                            set.unk14Boxes[x] = dataSet;
                        }
                    }

                    // 5th set of boxes
                    set.unk16Offsets = new int[set.NumEdges];
                    for (int x = 0; x < set.NumEdges; x++)
                    {
                        set.unk16Offsets[x] = reader.ReadInt32(); // bounding box position - set_pos
                    }

                    //ALWAYS A 4-BYTE INTEGER WHICH DENOTES THE END OF THE BATCH
                    if (set.NumEdges > 0)
                    {
                        set.EdgesDataEndOffset = reader.ReadInt32(); // very end of set_pos
                    }

                    // 6th set of boxes
                    set.EdgeBoxes = new BoundingBox[set.NumEdges];
                    for (int x = 0; x < set.NumEdges; x++)
                    {
                        set.EdgeBoxes[x] = BoundingBoxExtenders.ReadFromFile(reader);
                    }

                    // 7th set of boxes
                    set.unk18Set = new Unk18DataSet[set.NumUnk18Boxes];
                    if (set.NumUnk18Boxes > 0)
                    {
                        set.unk18End = reader.ReadInt32();
                        for (int x = 0; x < set.NumUnk18Boxes; x++)
                        {
                            Unk18DataSet dataSet = new Unk18DataSet();
                            dataSet.Unk0   = reader.ReadSingle();
                            dataSet.Unk1   = reader.ReadSingle();
                            dataSet.Unk2   = reader.ReadSingle();
                            dataSet.Offset = reader.ReadInt32();

                            set.unk18Set[x] = dataSet;
                        }

                        byte[] unknown_data = reader.ReadBytes(12); //usually padding

                        var currentOffset = set.unk18End;
                        for (int x = 0; x < set.NumUnk18Boxes; x++)
                        {
                            var dataSet = set.unk18Set[x];
                            var size    = dataSet.Offset - currentOffset;
                            var count   = size / 12;
                            dataSet.Points = new Vector3[count];

                            for (int f = 0; f < dataSet.Points.Length; f++)
                            {
                                dataSet.Points[f] = Vector3Utils.ReadFromFile(reader);
                            }

                            currentOffset   = dataSet.Offset;
                            set.unk18Set[x] = dataSet;
                        }
                    }

                    cell.Sets[z] = set;
                }
            }

            /*uint UnkFooter0 = reader.ReadUInt32();
             * if(UnkFooter0 == 0)
             * {
             *  throw new NotImplementedException();
             * }
             *
             * uint OffsetToFooter = reader.ReadUInt32(); // should be same as reader.position - kynogon mesh start
             * uint FooterPadding = reader.ReadUInt32(); // Usually zero for me.
             * string FooterName = StringHelpers.ReadString(reader); // A null terminated string.
             * uint FooterUnk0 = reader.ReadUInt32(); // Size of string. Goes _after_ the string has been stored.
             * uint FooterMagic = reader.ReadUInt32(); // should be 0x1213F001
             * Debug.Assert(FooterMagic == 0x1213F001, "Didn't reach end of file.");
             */

            //File.WriteAllLines("model.obj", data.ToArray());
        }
Example #2
0
        public void ReadFromFile(BinaryReader reader)
        {
            StreamWriter writer = File.CreateText("NAV_AI_OBJ_DATA.txt");

            unk0     = reader.ReadInt32();
            unk2     = reader.ReadInt32();
            unk3     = reader.ReadInt32();
            unk4     = reader.ReadInt32();
            vertSize = reader.ReadInt32();
            triSize  = reader.ReadInt32();
            //writer.WriteLine(string.Format("{0}, )

            //List<string> data = new List<string>();
            vertices = new VertexStruct[vertSize];
            for (int i = 0; i < vertSize; i++)
            {
                VertexStruct vertex = new VertexStruct();
                vertex.unk7     = reader.ReadUInt32() & 0x7FFFFFFF;
                vertex.position = Vector3Extenders.ReadFromFile(reader);
                //float pos = vertex.position.Y;
                //vertex.position.Y = vertex.position.Z;
                //vertex.position.Z = pos;
                vertex.unk0 = reader.ReadSingle();
                vertex.unk1 = reader.ReadSingle();
                vertex.unk2 = reader.ReadInt32();
                vertex.unk3 = reader.ReadInt16();
                vertex.unk4 = reader.ReadInt16();
                vertex.unk5 = reader.ReadInt32();
                vertex.unk6 = reader.ReadInt32();
                //data.Add(string.Format("v {0} {1} {2}", vertex.position.X, vertex.position.Z, vertex.position.Y));
                vertices[i] = vertex;
            }
            //data.Add("");
            //data.Add("g mesh");
            indices = new uint[triSize * 3];
            int index = 0;

            for (int i = 0; i < triSize; i++)
            {
                indices[index]     = reader.ReadUInt32() & 0x7FFFFFFF;
                indices[index + 1] = reader.ReadUInt32() & 0x7FFFFFFF;
                indices[index + 2] = reader.ReadUInt32() & 0x7FFFFFFF;
                //data.Add(string.Format("f {0} {1} {2}", indices[index] + 1, indices[index + 1] + 1, indices[index + 2] + 1));
                index += 3;
            }

            //KynogonRuntimeMesh
            long   mesh_pos  = reader.BaseStream.Position;
            string magicName = new string(reader.ReadChars(18));

            if (magicName != "KynogonRuntimeMesh")
            {
                throw new FormatException("Did not find KynogonRuntimeMesh");
            }

            short mesh_unk0    = reader.ReadInt16();
            int   magicVersion = reader.ReadInt32();

            if (magicVersion != 2)
            {
                throw new FormatException("Version did not equal 2");
            }

            int         mesh_unk1 = reader.ReadInt32();
            int         mesh_unk2 = reader.ReadInt32();
            BoundingBox bbox      = BoundingBoxExtenders.ReadFromFile(reader);
            int         cellSizeX = reader.ReadInt32();
            int         cellSizeY = reader.ReadInt32();
            float       radius    = reader.ReadSingle();
            int         map_unk3  = reader.ReadInt32();
            int         height    = reader.ReadInt32();
            int         offset    = reader.ReadInt32(); //this is a potential offset;

            int[]            grid     = new int[(cellSizeX * cellSizeY) + 1];
            List <UnkSet0[]> gridSets = new List <UnkSet0[]>();

            for (int i = 0; i < grid.Length; i++)
            {
                grid[i] = reader.ReadInt32();
            }

            for (int i = 0; i < grid.Length; i++)
            {
                if (i + 1 >= grid.Length)
                {
                    break;
                }

                if (i == 189)
                {
                    Console.WriteLine("st");
                }

                int       numSet0 = reader.ReadInt32();
                UnkSet0[] set0s   = new UnkSet0[numSet0];
                gridSets.Add(set0s);

                writer.WriteLine("-----------------------");
                writer.WriteLine(string.Format("{0} {1}", i, numSet0));
                writer.WriteLine("");

                if (numSet0 == 0)
                {
                    continue;
                }

                //NOTE: EVERY OFFSET IN UNKSET0 BEGINS FROM MESH_POS HIGHER IN THE CODE FILE.
                int offset0 = reader.ReadInt32();

                for (int x = 0; x < numSet0; x++)
                {
                    UnkSet0 set = new UnkSet0();
                    set.X      = reader.ReadSingle();
                    set.Y      = reader.ReadSingle();
                    set.Offset = reader.ReadInt32();
                    set0s[x]   = set;
                }

                //NOTE: EVERY BLOCK OF DATA SEEMS TO START WITH 96, THIS IS HOWEVER UNCONFIRMED.
                for (int z = 0; z < numSet0; z++)
                {
                    UnkSet0 set = set0s[z];
                    //NOTE: MOST OF THE OFFSETS BEGIN HERE, JUST AFTER THE SETS HAVE BEEN DEFINED ABOVE.
                    set.cellUnk0 = reader.ReadInt32();

                    if (set.cellUnk0 != mesh_unk2)
                    {
                        throw new FormatException();
                    }
                    writer.WriteLine("");
                    set.cellUnk1  = reader.ReadInt32();
                    set.cellUnk2  = reader.ReadInt32();
                    set.cellUnk3  = reader.ReadInt32();
                    set.cellUnk4  = reader.ReadSingle();
                    set.cellUnk5  = reader.ReadSingle();
                    set.cellUnk6  = reader.ReadSingle();
                    set.cellUnk7  = reader.ReadSingle();
                    set.cellUnk8  = reader.ReadInt32();
                    set.cellUnk9  = reader.ReadInt32(); //-1?
                    set.cellUnk10 = reader.ReadInt32(); //1;
                    set.cellUnk11 = reader.ReadInt32();
                    set.cellUnk12 = reader.ReadInt32(); //0
                    set.cellUnk13 = reader.ReadInt32(); //-1;
                    set.cellUnk14 = reader.ReadInt32(); //0
                    set.cellUnk15 = reader.ReadInt32(); //-1;
                    set.cellUnk16 = reader.ReadInt32(); //8;
                    set.cellUnk17 = reader.ReadInt32(); //112;
                    set.cellUnk18 = reader.ReadInt32(); //0;
                    set.cellUnk19 = reader.ReadInt32(); //-1;
                    writer.WriteLine(string.Format("Unk1: {0}", set.cellUnk1));
                    writer.WriteLine(string.Format("Unk2: {0}", set.cellUnk2));
                    writer.WriteLine(string.Format("Unk3: {0}", set.cellUnk3));
                    writer.WriteLine(string.Format("Unk4: {0}", set.cellUnk4));
                    writer.WriteLine(string.Format("Unk5: {0}", set.cellUnk5));
                    writer.WriteLine(string.Format("Unk6: {0}", set.cellUnk6));
                    writer.WriteLine(string.Format("Unk7: {0}", set.cellUnk7));
                    writer.WriteLine(string.Format("Unk8: {0}", set.cellUnk8));
                    writer.WriteLine(string.Format("Unk9: {0}", set.cellUnk9));
                    writer.WriteLine(string.Format("Unk10: {0}", set.cellUnk10));
                    writer.WriteLine(string.Format("Unk11: {0}", set.cellUnk11));
                    writer.WriteLine(string.Format("Unk12: {0}", set.cellUnk12));
                    writer.WriteLine(string.Format("Unk13: {0}", set.cellUnk13));
                    writer.WriteLine(string.Format("Unk14: {0}", set.cellUnk14));
                    writer.WriteLine(string.Format("Unk15: {0}", set.cellUnk15));
                    writer.WriteLine(string.Format("Unk16: {0}", set.cellUnk16));
                    writer.WriteLine(string.Format("Unk17: {0}", set.cellUnk17));
                    writer.WriteLine(string.Format("Unk18: {0}", set.cellUnk18));
                    writer.WriteLine(string.Format("Unk19: {0}", set.cellUnk19));
                    writer.WriteLine("");
                    //THIS BIT IS UNKNOWN, UPTO CELLUNK20
                    set.unk10Boxes = new Unk10DataSet[set.cellUnk10];
                    writer.WriteLine("Unk10 Boxes");
                    for (int x = 0; x < set.cellUnk10; x++)
                    {
                        Unk10DataSet unk10Set = new Unk10DataSet();
                        unk10Set.B1        = BoundingBoxExtenders.ReadFromFile(reader);
                        unk10Set.UnkOffset = reader.ReadInt32();
                        unk10Set.Unk20     = reader.ReadInt32();
                        set.unk10Boxes[x]  = unk10Set;
                        writer.WriteLine(string.Format("Minimum: {0} ", unk10Set.B1.Minimum.ToString()));
                        writer.WriteLine(string.Format("Maximum: {0} ", unk10Set.B1.Maximum.ToString()));
                        writer.WriteLine(string.Format("UnkOffset: {0} ", unk10Set.UnkOffset));
                        writer.WriteLine(string.Format("Unk20: {0} ", unk10Set.Unk20));
                        writer.WriteLine("");
                    }
                    //END OF CONFUSING BIT.


                    //THIS BIT IS UNKNOWN, BUT IS CELLUNK12
                    set.unk12Boxes = new Unk12DataSet[set.cellUnk12];
                    writer.WriteLine("Unk12 Boxes");
                    for (int x = 0; x < set.cellUnk12; x++)
                    {
                        Unk12DataSet unk12Set = new Unk12DataSet();
                        unk12Set.B1       = BoundingBoxExtenders.ReadFromFile(reader);
                        unk12Set.Unk01    = reader.ReadInt32();
                        unk12Set.Unk02    = reader.ReadInt32();
                        unk12Set.Unk03    = reader.ReadSingle();
                        unk12Set.Unk04    = reader.ReadSingle();
                        unk12Set.Unk05    = reader.ReadSingle();
                        set.unk12Boxes[x] = unk12Set;
                        writer.WriteLine(string.Format("Minimum: {0} ", unk12Set.B1.Minimum.ToString()));
                        writer.WriteLine(string.Format("Maximum: {0} ", unk12Set.B1.Maximum.ToString()));
                        writer.WriteLine(string.Format("Unk01: {0} ", unk12Set.Unk01));
                        writer.WriteLine(string.Format("Unk02: {0} ", unk12Set.Unk02));
                        writer.WriteLine(string.Format("Unk03: {0} ", unk12Set.Unk03));
                        writer.WriteLine(string.Format("Unk04: {0} ", unk12Set.Unk04));
                        writer.WriteLine(string.Format("Unk05: {0} ", unk12Set.Unk05));
                        writer.WriteLine("");
                    }

                    //END OF CONFUSING BIT.

                    //THIS LOOPS THROUGH OFFSETS TO BBOX'S
                    writer.WriteLine("Unk14 Offsets");
                    set.unk14Offsets = new int[set.cellUnk14];
                    for (int x = 0; x < set.cellUnk14; x++)
                    {
                        set.unk14Offsets[x] = reader.ReadInt32();
                        writer.WriteLine(string.Format("{0} ", set.unk14Offsets[x]));
                    }

                    //ALWAYS A 4-BYTE INTEGER WHICH DENOTES THE END OF THE BATCH
                    if (set.cellUnk14 > 0)
                    {
                        set.unk14End = reader.ReadInt32();
                    }

                    if (set.cellUnk14 > 0)
                    {
                        set.unk14Data = reader.ReadBytes(set.unk14End - set.unk14Offsets[0]);
                    }
                    writer.WriteLine("");

                    //set.unk14Boxes = new BoundingBox[set.cellUnk14];
                    //writer.WriteLine("Unk14 Boxes");
                    //for (int x = 0; x < set.cellUnk14; x++)
                    //{
                    //    set.unk14Boxes[x] = BoundingBoxExtenders.ReadFromFile(reader);
                    //    writer.WriteLine(string.Format("{0} ", set.unk14Boxes[x].ToString()));
                    //}
                    //writer.WriteLine("");



                    //CONTINUE ONTO THE NEXT BATCH
                    set.unk16Offsets = new int[set.cellUnk16];
                    writer.WriteLine("Unk16 Offsets");
                    for (int x = 0; x < set.cellUnk16; x++)
                    {
                        set.unk16Offsets[x] = reader.ReadInt32();
                        writer.WriteLine(string.Format("{0} ", set.unk16Offsets[x]));
                    }
                    writer.WriteLine("");
                    //ALWAYS A 4-BYTE INTEGER WHICH DENOTES THE END OF THE BATCH
                    if (set.cellUnk16 > 0)
                    {
                        set.unk16End = reader.ReadInt32();
                    }

                    set.unk16Boxes = new BoundingBox[set.cellUnk16];
                    writer.WriteLine("Unk16 Boxes");
                    for (int x = 0; x < set.cellUnk16; x++)
                    {
                        set.unk16Boxes[x] = BoundingBoxExtenders.ReadFromFile(reader);
                        writer.WriteLine(string.Format("{0} ", set.unk16Boxes[x]));
                    }
                    writer.WriteLine("");
                    if (set.cellUnk18 > 1)
                    {
                        throw new FormatException();
                    }

                    set.unk18Set = new Unk18DataSet[set.cellUnk18];
                    writer.WriteLine("Unk18 Boxes");
                    for (int x = 0; x < set.cellUnk18; x++)
                    {
                        //THIS COULD BE AN OFFSET LIST WITH SOMEKIND OF FLOAT/ROTATION DATA
                        Unk18DataSet dataSet = new Unk18DataSet();
                        dataSet.Unk0 = reader.ReadInt32();
                        dataSet.Unk1 = reader.ReadSingle();
                        dataSet.Unk2 = reader.ReadSingle();
                        dataSet.Unk3 = reader.ReadSingle();

                        //THIS COULD BE THE FINAL AREA WITH THE 12 BYTES SIMPLY PADDING OUT
                        dataSet.Unk4 = reader.ReadInt32();
                        dataSet.Unk5 = reader.ReadBytes(12);

                        //BOUNDING BOX FOR THIS KIND OF DATA.
                        dataSet.B1      = BoundingBoxExtenders.ReadFromFile(reader);
                        dataSet.B2      = BoundingBoxExtenders.ReadFromFile(reader);
                        dataSet.B3      = BoundingBoxExtenders.ReadFromFile(reader);
                        set.unk18Set[x] = dataSet;


                        writer.WriteLine(string.Format("Unk01: {0} ", dataSet.Unk1));
                        writer.WriteLine(string.Format("Unk02: {0} ", dataSet.Unk2));
                        writer.WriteLine(string.Format("Unk03: {0} ", dataSet.Unk3));
                        writer.WriteLine(string.Format("Unk04: {0} ", dataSet.Unk4));
                        writer.WriteLine(string.Format("Unk05: {0} ", dataSet.Unk5));
                        writer.WriteLine(string.Format("Minimum: {0} ", dataSet.B1.Minimum.ToString()));
                        writer.WriteLine(string.Format("Maximum: {0} ", dataSet.B1.Maximum.ToString()));
                        writer.WriteLine(string.Format("Minimum: {0} ", dataSet.B2.Minimum.ToString()));
                        writer.WriteLine(string.Format("Maximum: {0} ", dataSet.B2.Maximum.ToString()));
                        writer.WriteLine(string.Format("Minimum: {0} ", dataSet.B3.Minimum.ToString()));
                        writer.WriteLine(string.Format("Maximum: {0} ", dataSet.B3.Maximum.ToString()));
                    }
                    writer.WriteLine("");
                    set0s[z] = set;
                }
                Console.WriteLine("Completed: " + i);
                //byte[] data = reader.ReadBytes(size);
                //File.WriteAllBytes("grid_" + i + ".bin", data);
            }


            //File.WriteAllLines("model.obj", data.ToArray());
        }
        public void ReadFromFile(BinaryReader reader, StreamWriter writer)
        {
            //KynogonRuntimeMesh
            long   mesh_pos  = reader.BaseStream.Position;
            string magicName = new string(reader.ReadChars(18));

            if (magicName != "KynogonRuntimeMesh")
            {
                throw new FormatException("Did not find KynogonRuntimeMesh");
            }

            short mesh_unk0    = reader.ReadInt16();
            int   magicVersion = reader.ReadInt32();

            if (magicVersion != 2)
            {
                throw new FormatException("Version did not equal 2");
            }

            writer.WriteLine("");
            Unk0 = reader.ReadInt32();
            Unk1 = reader.ReadInt32();
            Unk2 = reader.ReadSingle();
            Unk3 = reader.ReadSingle();
            writer.WriteLine(Unk0);
            writer.WriteLine(Unk1);
            writer.WriteLine(Unk2);
            writer.WriteLine(Unk3);

            float boundMinX = reader.ReadSingle();
            float boundMaxX = reader.ReadSingle();
            float boundMaxY = -reader.ReadSingle();
            float boundMinY = -reader.ReadSingle();

            BoundMin = new Vector2(boundMinX, boundMinY);
            BoundMax = new Vector2(boundMaxX, boundMaxY);



            writer.WriteLine(BoundMin);
            writer.WriteLine(BoundMax);
            //BoundingBox = BoundingBoxExtenders.ReadFromFile(reader);
            CellSizeX = reader.ReadInt32();
            CellSizeY = reader.ReadInt32();
            writer.WriteLine(CellSizeX);
            writer.WriteLine(CellSizeY);
            Radius = reader.ReadSingle();
            Unk4   = reader.ReadInt32();
            Height = reader.ReadInt32();
            Offset = reader.ReadInt32(); //this is a potential offset;
            writer.WriteLine(Radius);
            writer.WriteLine(Unk4);
            writer.WriteLine(Height);
            writer.WriteLine(Offset);
            Grid  = new int[(CellSizeX * CellSizeY)];
            Cells = new Cell[Grid.Length];
            writer.WriteLine("");

            for (int i = 0; i < Grid.Length; i++)
            {
                Grid[i] = reader.ReadInt32();
            }

            int end = reader.ReadInt32();

            for (int i = 0; i < Cells.Length; i++)
            {
                //if (i + 1 >= Cells.Length)
                //    break;

                Cell cell    = new Cell();
                int  numSet0 = reader.ReadInt32();
                cell.Sets = new UnkSet0[numSet0];
                Cells[i]  = cell;

                writer.WriteLine("-----------------------");
                writer.WriteLine(string.Format("{0} {1}", i, numSet0));
                writer.WriteLine("");

                if (numSet0 == 0)
                {
                    continue;
                }

                //NOTE: EVERY OFFSET IN UNKSET0 BEGINS FROM MESH_POS HIGHER IN THE CODE FILE.
                cell.Offset = reader.ReadInt32();

                for (int x = 0; x < numSet0; x++)
                {
                    UnkSet0 set = new UnkSet0();
                    set.X        = reader.ReadSingle();
                    set.Y        = reader.ReadSingle();
                    set.Offset   = reader.ReadInt32();
                    cell.Sets[x] = set;
                }

                //NOTE: EVERY BLOCK OF DATA SEEMS TO START WITH 96, THIS IS HOWEVER UNCONFIRMED.
                for (int z = 0; z < numSet0; z++)
                {
                    UnkSet0 set = cell.Sets[z];
                    //NOTE: MOST OF THE OFFSETS BEGIN HERE, JUST AFTER THE SETS HAVE BEEN DEFINED ABOVE.
                    set.cellUnk0 = reader.ReadInt32();

                    if (set.cellUnk0 != Unk1)
                    {
                        throw new FormatException();
                    }
                    writer.WriteLine("");
                    set.cellUnk1  = reader.ReadInt32();
                    set.cellUnk2  = reader.ReadInt32();
                    set.cellUnk3  = reader.ReadInt32();
                    set.cellUnk4  = reader.ReadSingle();
                    set.cellUnk5  = reader.ReadSingle();
                    set.cellUnk6  = reader.ReadSingle();
                    set.cellUnk7  = reader.ReadSingle();
                    set.cellUnk8  = reader.ReadInt32();
                    set.cellUnk9  = reader.ReadInt32(); //-1?
                    set.cellUnk10 = reader.ReadInt32(); //1;
                    set.cellUnk11 = reader.ReadInt32();
                    set.cellUnk12 = reader.ReadInt32(); //0
                    set.cellUnk13 = reader.ReadInt32(); //-1;
                    set.cellUnk14 = reader.ReadInt32(); //0
                    set.cellUnk15 = reader.ReadInt32(); //-1;
                    set.NumEdges  = reader.ReadInt32(); //8;
                    set.cellUnk17 = reader.ReadInt32(); //112;
                    set.cellUnk18 = reader.ReadInt32(); //0;
                    set.cellUnk19 = reader.ReadInt32(); //-1;
                    writer.WriteLine(string.Format("Unk1: {0}", set.cellUnk1));
                    writer.WriteLine(string.Format("Unk2: {0}", set.cellUnk2));
                    writer.WriteLine(string.Format("Unk3: {0}", set.cellUnk3));
                    writer.WriteLine(string.Format("Unk4: {0}", set.cellUnk4));
                    writer.WriteLine(string.Format("Unk5: {0}", set.cellUnk5));
                    writer.WriteLine(string.Format("Unk6: {0}", set.cellUnk6));
                    writer.WriteLine(string.Format("Unk7: {0}", set.cellUnk7));
                    writer.WriteLine(string.Format("Unk8: {0}", set.cellUnk8));
                    writer.WriteLine(string.Format("Unk9: {0}", set.cellUnk9));
                    writer.WriteLine(string.Format("Unk10: {0}", set.cellUnk10));
                    writer.WriteLine(string.Format("Unk11: {0}", set.cellUnk11));
                    writer.WriteLine(string.Format("Unk12: {0}", set.cellUnk12));
                    writer.WriteLine(string.Format("Unk13: {0}", set.cellUnk13));
                    writer.WriteLine(string.Format("Unk14: {0}", set.cellUnk14));
                    writer.WriteLine(string.Format("Unk15: {0}", set.cellUnk15));
                    writer.WriteLine(string.Format("Unk16: {0}", set.NumEdges));
                    writer.WriteLine(string.Format("Unk17: {0}", set.cellUnk17));
                    writer.WriteLine(string.Format("Unk18: {0}", set.cellUnk18));
                    writer.WriteLine(string.Format("Unk19: {0}", set.cellUnk19));
                    writer.WriteLine("");
                    //THIS BIT IS UNKNOWN, UPTO CELLUNK20
                    set.unk10Boxes = new Unk10DataSet[set.cellUnk10];
                    writer.WriteLine("Unk10 Boxes");
                    for (int x = 0; x < set.cellUnk10; x++)
                    {
                        Unk10DataSet unk10Set = new Unk10DataSet();
                        unk10Set.B1        = BoundingBoxExtenders.ReadFromFile(reader);
                        unk10Set.UnkOffset = reader.ReadInt32();
                        unk10Set.Unk20     = reader.ReadInt32();
                        set.unk10Boxes[x]  = unk10Set;
                        writer.WriteLine(string.Format("Minimum: {0} ", unk10Set.B1.Minimum.ToString()));
                        writer.WriteLine(string.Format("Maximum: {0} ", unk10Set.B1.Maximum.ToString()));
                        writer.WriteLine(string.Format("UnkOffset: {0} ", unk10Set.UnkOffset));
                        writer.WriteLine(string.Format("Unk20: {0} ", unk10Set.Unk20));
                        writer.WriteLine("");
                    }
                    //END OF CONFUSING BIT.


                    //THIS BIT IS UNKNOWN, BUT IS CELLUNK12
                    set.unk12Boxes = new Unk12DataSet[set.cellUnk12];
                    writer.WriteLine("Unk12 Boxes");
                    for (int x = 0; x < set.cellUnk12; x++)
                    {
                        Unk12DataSet unk12Set = new Unk12DataSet();
                        unk12Set.B1       = BoundingBoxExtenders.ReadFromFile(reader);
                        unk12Set.Unk01    = reader.ReadInt32();
                        unk12Set.Unk02    = reader.ReadInt32();
                        unk12Set.Unk03    = reader.ReadSingle();
                        unk12Set.Unk04    = reader.ReadSingle();
                        unk12Set.Unk05    = reader.ReadSingle();
                        set.unk12Boxes[x] = unk12Set;
                        writer.WriteLine(string.Format("Minimum: {0} ", unk12Set.B1.Minimum.ToString()));
                        writer.WriteLine(string.Format("Maximum: {0} ", unk12Set.B1.Maximum.ToString()));
                        writer.WriteLine(string.Format("Unk01: {0} ", unk12Set.Unk01));
                        writer.WriteLine(string.Format("Unk02: {0} ", unk12Set.Unk02));
                        writer.WriteLine(string.Format("Unk03: {0} ", unk12Set.Unk03));
                        writer.WriteLine(string.Format("Unk04: {0} ", unk12Set.Unk04));
                        writer.WriteLine(string.Format("Unk05: {0} ", unk12Set.Unk05));
                        writer.WriteLine("");
                    }

                    //END OF CONFUSING BIT.

                    //THIS LOOPS THROUGH OFFSETS TO BBOX'S
                    writer.WriteLine("Unk14 Offsets");
                    set.unk14Boxes = new Unk14DataSet[set.cellUnk14];
                    for (int x = 0; x < set.cellUnk14; x++)
                    {
                        var dataSet = new Unk14DataSet();
                        dataSet.Offset    = reader.ReadInt32();
                        set.unk14Boxes[x] = dataSet;
                        writer.WriteLine(string.Format("{0} ", dataSet.Offset));
                    }

                    //ALWAYS A 4-BYTE INTEGER WHICH DENOTES THE END OF THE BATCH
                    if (set.cellUnk14 > 0)
                    {
                        set.unk14End = reader.ReadInt32();
                        var currentOffset = set.unk14Boxes[0].Offset;
                        for (int x = 0; x < set.cellUnk14; x++)
                        {
                            var dataSet = set.unk14Boxes[x];
                            var offset  = (x + 1 < set.cellUnk14 ? set.unk14Boxes[x + 1].Offset : set.unk14End);
                            var size    = offset - currentOffset;
                            currentOffset = offset;
                            var count = size / 12;
                            dataSet.Points = new Vector3[count];
                            writer.WriteLine(string.Format("Unk14: {0} {1}", x, count));
                            for (int f = 0; f < dataSet.Points.Length; f++)
                            {
                                dataSet.Points[f] = Vector3Extenders.ReadFromFile(reader);
                                writer.WriteLine("{0}: {1}", f, dataSet.Points[f]);
                            }
                            set.unk14Boxes[x] = dataSet;
                        }
                    }
                    writer.WriteLine("");

                    //CONTINUE ONTO THE NEXT BATCH
                    set.unk16Offsets = new int[set.NumEdges];
                    writer.WriteLine("Unk16 Offsets");
                    for (int x = 0; x < set.NumEdges; x++)
                    {
                        set.unk16Offsets[x] = reader.ReadInt32();
                        writer.WriteLine(string.Format("{0} ", set.unk16Offsets[x]));
                    }
                    writer.WriteLine("");
                    //ALWAYS A 4-BYTE INTEGER WHICH DENOTES THE END OF THE BATCH
                    if (set.NumEdges > 0)
                    {
                        set.EdgesDataEndOffset = reader.ReadInt32();
                    }

                    set.EdgeBoxes = new BoundingBox[set.NumEdges];
                    writer.WriteLine("Unk16 Boxes");
                    for (int x = 0; x < set.NumEdges; x++)
                    {
                        set.EdgeBoxes[x] = BoundingBoxExtenders.ReadFromFile(reader);
                        writer.WriteLine(string.Format("{0} ", set.EdgeBoxes[x]));
                    }

                    set.unk18Set = new Unk18DataSet[set.cellUnk18];
                    writer.WriteLine("");
                    if (set.cellUnk18 > 0)
                    {
                        set.unk18End = reader.ReadInt32();
                        writer.WriteLine("Unk18 Boxes:");
                        for (int x = 0; x < set.cellUnk18; x++)
                        {
                            //THIS COULD BE AN OFFSET LIST WITH SOMEKIND OF FLOAT/ROTATION DATA
                            Unk18DataSet dataSet = new Unk18DataSet();
                            dataSet.Unk0   = reader.ReadSingle();
                            dataSet.Unk1   = reader.ReadSingle();
                            dataSet.Unk2   = reader.ReadSingle();
                            dataSet.Offset = reader.ReadInt32();
                            writer.WriteLine(string.Format("{0} {1} {2} {3}", dataSet.Unk0, dataSet.Unk1, dataSet.Unk2, dataSet.Offset));
                            set.unk18Set[x] = dataSet;
                        }

                        byte[] unknown_data = reader.ReadBytes(12); //usually padding

                        var currentOffset = set.unk18End;
                        for (int x = 0; x < set.cellUnk18; x++)
                        {
                            var dataSet = set.unk18Set[x];
                            var size    = dataSet.Offset - currentOffset;
                            var count   = size / 12;
                            dataSet.Points = new Vector3[count];

                            for (int f = 0; f < dataSet.Points.Length; f++)
                            {
                                dataSet.Points[f] = Vector3Extenders.ReadFromFile(reader);
                                writer.WriteLine("{0}: {1}", f, dataSet.Points[f]);
                            }
                            currentOffset   = dataSet.Offset;
                            set.unk18Set[x] = dataSet;
                        }
                    }
                    writer.WriteLine("");
                    cell.Sets[z] = set;
                }
                Console.WriteLine("Completed: " + i);
                //byte[] data = reader.ReadBytes(size);
                //File.WriteAllBytes("grid_" + i + ".bin", data);
            }
            writer.Close();

            //File.WriteAllLines("model.obj", data.ToArray());
        }