Example #1
0
        private CollisionFile(CollisionFileInfo info, Stream stream)
        {
            Name    = info.Name;
            ModelId = info.ModelId;

            var version = info.Version;

            var reader = new BinaryReader(stream);

            stream.Seek(info.Offset + 28, SeekOrigin.Begin);

            Bounds = new Bounds(reader, version);

            int  spheres, boxes, verts, faces, faceGroups;
            long spheresOffset, boxesOffset, vertsOffset,
                 facesOffset, faceGroupsOffset;

            switch (version)
            {
            case Version.COLL: {
                spheres       = reader.ReadInt32();
                spheresOffset = stream.Position;
                stream.Seek(spheres * Sphere.Size, SeekOrigin.Current);

                reader.ReadInt32();

                boxes       = reader.ReadInt32();
                boxesOffset = stream.Position;
                stream.Seek(boxes * Box.Size, SeekOrigin.Current);

                verts       = reader.ReadInt32();
                vertsOffset = stream.Position;
                stream.Seek(verts * Vertex.SizeV1, SeekOrigin.Current);

                faces       = reader.ReadInt32();
                facesOffset = stream.Position;

                faceGroups       = 0;
                faceGroupsOffset = 0;

                break;
            }

            default: {
                spheres = reader.ReadUInt16();
                boxes   = reader.ReadUInt16();
                faces   = reader.ReadUInt16();
                reader.ReadInt16();

                Flags = (Flags)reader.ReadInt32();

                spheresOffset = reader.ReadUInt32() + info.Offset;
                boxesOffset   = reader.ReadUInt32() + info.Offset;
                reader.ReadUInt32();
                vertsOffset = reader.ReadUInt32() + info.Offset;
                facesOffset = reader.ReadUInt32() + info.Offset;

                if (faces > 0 && (Flags & Flags.HasFaceGroups) == Flags.HasFaceGroups)
                {
                    stream.Seek(facesOffset - 4, SeekOrigin.Begin);
                    faceGroups       = reader.ReadInt32();
                    faceGroupsOffset = facesOffset - 4 - FaceGroup.Size * faceGroups;
                }
                else
                {
                    faceGroups       = 0;
                    faceGroupsOffset = 0;
                }

                verts = -1;

                break;
            }
            }

            Spheres    = new Sphere[spheres];
            Boxes      = new Box[boxes];
            Faces      = new Face[faces];
            FaceGroups = new FaceGroup[faceGroups];

            if (spheres > 0)
            {
                stream.Seek(spheresOffset, SeekOrigin.Begin);
                for (var i = 0; i < spheres; ++i)
                {
                    Spheres[i] = new Sphere(reader, version);
                }
            }

            if (boxes > 0)
            {
                stream.Seek(boxesOffset, SeekOrigin.Begin);
                for (var i = 0; i < boxes; ++i)
                {
                    Boxes[i] = new Box(reader, version);
                }
            }

            if (faces > 0)
            {
                stream.Seek(facesOffset, SeekOrigin.Begin);
                for (var i = 0; i < faces; ++i)
                {
                    Faces[i] = new Face(reader, version);
                }

                if (verts == -1)
                {
                    verts = Faces.Max(x => x.GetIndices().Max()) + 1;
                }

                Vertices = new Vertex[verts];

                stream.Seek(vertsOffset, SeekOrigin.Begin);
                for (var i = 0; i < verts; ++i)
                {
                    Vertices[i] = new Vertex(reader, version);
                }

                if (faceGroups > 0)
                {
                    stream.Seek(faceGroupsOffset, SeekOrigin.Begin);
                    for (var i = 0; i < faceGroups; ++i)
                    {
                        FaceGroups[i] = new FaceGroup(reader);
                    }
                }
            }
            else
            {
                Vertices = new Vertex[0];
            }
        }
        private CollisionFile(CollisionFileInfo info, Stream stream)
        {
            Name = info.Name;
            ModelId = info.ModelId;

            var version = info.Version;

            var reader = new BinaryReader(stream);

            stream.Seek(info.Offset + 28, SeekOrigin.Begin);

            Bounds = new Bounds(reader, version);

            int spheres, boxes, verts, faces, faceGroups;
            long spheresOffset, boxesOffset, vertsOffset,
                facesOffset, faceGroupsOffset;

            switch (version) {
                case Version.COLL: {
                    spheres = reader.ReadInt32();
                    spheresOffset = stream.Position;
                    stream.Seek(spheres * Sphere.Size, SeekOrigin.Current);

                    reader.ReadInt32();

                    boxes = reader.ReadInt32();
                    boxesOffset = stream.Position;
                    stream.Seek(boxes * Box.Size, SeekOrigin.Current);

                    verts = reader.ReadInt32();
                    vertsOffset = stream.Position;
                    stream.Seek(verts * Vertex.SizeV1, SeekOrigin.Current);

                    faces = reader.ReadInt32();
                    facesOffset = stream.Position;

                    faceGroups = 0;
                    faceGroupsOffset = 0;

                    break;
                }
                default: {
                    spheres = reader.ReadUInt16();
                    boxes = reader.ReadUInt16();
                    faces = reader.ReadUInt16();
                    reader.ReadInt16();

                    Flags = (Flags) reader.ReadInt32();

                    spheresOffset = reader.ReadUInt32() + info.Offset;
                    boxesOffset = reader.ReadUInt32() + info.Offset;
                    reader.ReadUInt32();
                    vertsOffset = reader.ReadUInt32() + info.Offset;
                    facesOffset = reader.ReadUInt32() + info.Offset;

                    if (faces > 0 && (Flags & Flags.HasFaceGroups) == Flags.HasFaceGroups) {
                        stream.Seek(facesOffset - 4, SeekOrigin.Begin);
                        faceGroups = reader.ReadInt32();
                        faceGroupsOffset = facesOffset - 4 - FaceGroup.Size * faceGroups;
                    } else {
                        faceGroups = 0;
                        faceGroupsOffset = 0;
                    }

                    verts = -1;

                    break;
                }
            }

            Spheres = new Sphere[spheres];
            Boxes = new Box[boxes];
            Faces = new Face[faces];
            FaceGroups = new FaceGroup[faceGroups];

            if (spheres > 0) {
                stream.Seek(spheresOffset, SeekOrigin.Begin);
                for (var i = 0; i < spheres; ++i) {
                    Spheres[i] = new Sphere(reader, version);
                }
            }

            if (boxes > 0) {
                stream.Seek(boxesOffset, SeekOrigin.Begin);
                for (var i = 0; i < boxes; ++i) {
                    Boxes[i] = new Box(reader, version);
                }
            }

            if (faces > 0) {
                stream.Seek(facesOffset, SeekOrigin.Begin);
                for (var i = 0; i < faces; ++i) {
                    Faces[i] = new Face(reader, version);
                }

                if (verts == -1) {
                    verts = Faces.Max(x => x.GetIndices().Max()) + 1;
                }

                Vertices = new Vertex[verts];

                stream.Seek(vertsOffset, SeekOrigin.Begin);
                for (var i = 0; i < verts; ++i) {
                    Vertices[i] = new Vertex(reader, version);
                }

                if (faceGroups > 0) {
                    stream.Seek(faceGroupsOffset, SeekOrigin.Begin);
                    for (var i = 0; i < faceGroups; ++i) {
                        FaceGroups[i] = new FaceGroup(reader);
                    }
                }
            } else {
                Vertices = new Vertex[0];
            }
        }