public string ReadEntities(BinaryReader reader, BspHeader header)
        {
            var entry = header.GetLump(Lumps.ENTITIES);

            reader.BaseStream.Seek(entry.Offset, SeekOrigin.Begin);
            return(new string(reader.ReadChars(entry.Length)));
        }
        public List <Face> ReadFaces(BinaryReader reader, BspHeader header)
        {
            List <Face> faces    = new List <Face>();
            var         entry    = header.GetLump(Lumps.FACES);
            int         numFaces = entry.Length / 20;

            reader.BaseStream.Seek(entry.Offset, SeekOrigin.Begin);
            for (int i = 0; i < numFaces; i++)
            {
                var face = new Face();
                face.plane             = reader.ReadUInt16();
                face.planeSide         = reader.ReadUInt16();
                face.firstEdge         = reader.ReadUInt32();
                face.edgeCount         = reader.ReadUInt16();
                face.texInfo           = reader.ReadUInt16();
                face.lightmapStyles[0] = reader.ReadChar();
                face.lightmapStyles[1] = reader.ReadChar();
                face.lightmapStyles[2] = reader.ReadChar();
                face.lightmapStyles[3] = reader.ReadChar();
                face.lightmapOffset    = reader.ReadUInt32();
                faces.Add(face);
            }

            return(faces);
        }
Exemplo n.º 3
0
        private static void WriteBspHeader(Stream stream, BspHeader header)
        {
            stream.Write(header.Version);

            foreach (var lump in header.Lumps)
            {
                stream.Write(lump.Offset);
                stream.Write(lump.Length);
            }
        }
        public List <Edge> ReadEdges(BinaryReader reader, BspHeader header)
        {
            List <Edge> edges    = new List <Edge>();
            var         entry    = header.GetLump(Lumps.EDGES);
            int         numEdges = entry.Length / 4;

            reader.BaseStream.Seek(entry.Offset, SeekOrigin.Begin);
            for (int i = 0; i < numEdges; i++)
            {
                edges.Add(new Edge(reader.ReadUInt16(), reader.ReadUInt16()));
            }

            return(edges);
        }
        public List <int> ReadFaceEdges(BinaryReader reader, BspHeader header)
        {
            List <int> faceEdges = new List <int>();
            var        entry     = header.GetLump(Lumps.FACE_EDGE_TABLE);
            int        numEdges  = entry.Length / 4;

            reader.BaseStream.Seek(entry.Offset, SeekOrigin.Begin);
            for (int i = 0; i < numEdges; i++)
            {
                faceEdges.Add(reader.ReadInt32());
            }

            return(faceEdges);
        }
Exemplo n.º 6
0
        private static BspHeader ReadBspHeader(Stream stream)
        {
            var header = new BspHeader();

            header.Version = stream.ReadInt();
            if (header.Version != 30)
            {
                throw new NotSupportedException("Only BSP v30 is supported.");
            }

            header.Lumps = Enumerable.Range(0, 15)
                           .Select(i => new Lump {
                Offset = stream.ReadInt(), Length = stream.ReadInt()
            })
                           .ToArray();

            return(header);
        }
        public BspFile LoadBsp(string path)
        {
            BinaryReader reader = new BinaryReader(File.Open(path, FileMode.Open));

            BspHeader header = new BspHeader(reader);

            BspFile parsed = ScriptableObject.CreateInstance <BspFile>();

            parsed.entities  = ReadEntities(reader, header);
            parsed.verts     = ReadVerts(reader, header);
            parsed.edges     = ReadEdges(reader, header);
            parsed.faces     = ReadFaces(reader, header);
            parsed.faceEdges = ReadFaceEdges(reader, header);
            parsed.texInfo   = ReadTexInfo(reader, header);

            reader.BaseStream.Dispose();

            return(parsed);
        }
        public List <Vector3> ReadVerts(BinaryReader reader, BspHeader header)
        {
            List <Vector3> verts    = new List <Vector3>();
            var            entry    = header.GetLump(Lumps.VERTICES);
            int            numVerts = entry.Length / 12;

            reader.BaseStream.Seek(entry.Offset, SeekOrigin.Begin);
            for (int i = 0; i < numVerts; i++)
            {
                float x = reader.ReadSingle();
                float y = reader.ReadSingle();
                float z = reader.ReadSingle();
                // convert to unity axis
                Vector3 vec = new Vector3(-x, z, -y);
                // scale for unity
                vec.Scale(Vector3.one * QUAKE_TO_UNITY_CONVERSION_SCALE);
                verts.Add(vec);
            }

            return(verts);
        }
        public List <TexInfo> ReadTexInfo(BinaryReader reader, BspHeader header)
        {
            List <TexInfo> infos = new List <TexInfo>();

            var entry    = header.GetLump(Lumps.TEXTURES);
            int numInfos = entry.Length / 76;

            Debug.Log(numInfos + " Texture Infos");
            reader.BaseStream.Seek(entry.Offset, SeekOrigin.Begin);
            for (int i = 0; i < numInfos; i++)
            {
                var info = new TexInfo();

                float ux = reader.ReadSingle();
                float uy = reader.ReadSingle();
                float uz = reader.ReadSingle();
                info.uAxis   = new Vector3(-ux, uz, -uy);
                info.uOffset = reader.ReadSingle();                // * QUAKE_TO_UNITY_CONVERSION_SCALE; // * scale??
                float vx = reader.ReadSingle();
                float vy = reader.ReadSingle();
                float vz = reader.ReadSingle();
                info.vAxis   = new Vector3(-vx, vz, -vy);
                info.vOffset = reader.ReadSingle();                // * QUAKE_TO_UNITY_CONVERSION_SCALE; // * scale??

                info.flags = reader.ReadUInt32();
                info.value = reader.ReadUInt32();
                info.name  = new string(reader.ReadChars(32));
                int pos = info.name.IndexOf('\0');
                if (pos >= 0)
                {
                    info.name = info.name.Substring(0, pos);
                }
                info.nextTexInfo = reader.ReadUInt32();

                infos.Add(info);
            }

            return(infos);
        }
Exemplo n.º 10
0
        public ArrayList Read(string filename)
        {
            //open the file for reading
            FileStream fs = null;

            try
            {
                fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
                BinaryReader reader = new BinaryReader(fs);
                //read the contens from the file
                //init the header
                BspHeader header = new BspHeader();
                header.Read(reader);
                //allocate space needed for the data structures

                int entityOffset = header.Entries[0].Offset;
                int entityLength = header.Entries[0].Length;

                fs.Seek(entityOffset, SeekOrigin.Begin);

                ArrayList entities = new ArrayList();

                StringBuilder entityText = new StringBuilder();

                int count = 72;                 // entities start at this number?

                bool inEnt = false;

                Hashtable ignored     = GetIgnoredEntityClasses();
                Hashtable conditional = GetConditionalIgnoredEntityClasses();
                Hashtable targetNames = new Hashtable();

                ArrayList spawners = new ArrayList();

                for (int x = 0; x < entityLength; x++)
                {
                    char read = reader.ReadChar();

                    if (inEnt || read == '{')
                    {
                        entityText.Append(read);
                        inEnt = true;
                    }

                    if (read == '}')
                    {
                        BspEntity ent = new BspEntity(0, entityText.ToString());

                        bool ignore = false;
                        if (ignored.ContainsKey(ent.ClassName))
                        {
                            ignore = true;
                        }
                        else if (conditional.ContainsKey(ent.ClassName))
                        {
                            string propNeeded = (string)conditional[ent.ClassName];

                            if (ent.GetProperty(propNeeded) == "UNDEFINED")
                            {
                                ignore = true;
                            }
                        }

                        if (ent.ClassName == "misc_gamemodel" &&
                            ent.TargetName == "UNDEFINED" &&
                            ent.ScriptName == "UNDEFINED" &&
                            ent.SpawnFlags == "UNDEFINED")
                        {
                            ignore = true;
                        }

                        if (!ignore)
                        {
                            if (ent.ClassName == "worldspawn")
                            {
                                ent.Number = 1023;
                                entities.Add(ent);
                            }
                            else
                            {
                                if (ent.ClassName == "misc_mg42")
                                {
                                    spawners.Add(ent);                                     // mg42's here are just placements, the engine creates em
                                }
                                else if (ent.ClassName == "misc_flak" || ent.ClassName == "misc_grabber_trap")
                                {
                                    BspEntity spawned = new BspEntity(0, "This is a spawned entity not defined in the bsp");
                                    spawners.Add(spawned);

                                    ent.Number = count;
                                    entities.Add(ent);
                                }
                                else if (ent.ClassName == "func_plat")
                                {
                                    if (ent.TargetName == "UNDEFINED")
                                    {
                                        BspEntity spawned = new BspEntity(0, "This is a spawned entity not defined in the bsp");
                                        spawners.Add(spawned);
                                    }
                                    ent.Number = count;
                                    entities.Add(ent);
                                }
                                else if (ent.ClassName == "func_door_rotating")
                                {
                                    /*
                                     * else if ((ent->spawnflags & 8) && (strcmp( ent->classname, "func_door_rotating" )))
                                     *      ent->think = Think_SpawnNewDoorTrigger;
                                     */
                                    bool handledDoor = false;
                                    if (ent.SpawnFlags != "UNDEFINED")
                                    {
                                        try
                                        {
                                            int flags = Convert.ToInt32(ent.SpawnFlags);

                                            if ((flags & 8) == 8)
                                            {
                                                BspEntity doorTrigger = new BspEntity(0, "This is a spawned entity not defined in the bsp");

                                                spawners.Add(doorTrigger);

                                                ent.Number = count;
                                                entities.Add(ent);

                                                handledDoor = true;
                                            }
                                        }
                                        catch {}
                                    }

                                    if (!handledDoor)
                                    {
                                        ent.Number = count;
                                        entities.Add(ent);
                                    }
                                }
                                else if (ent.ClassName == "trigger_objective_info")
                                {
                                    BspEntity checkIndicator = new BspEntity(0, ent.Text);
                                    spawners.Add(checkIndicator);

                                    ent.Number = count;
                                    entities.Add(ent);
                                }
                                else
                                {
                                    ent.Number = count;
                                    entities.Add(ent);
                                }
                                count++;
                            }
                        }

                        string targetKey = ent.TargetName;

                        if (targetKey != "UNDEFINED" && !targetNames.ContainsKey(targetKey))
                        {
                            targetNames.Add(targetKey, ent);
                        }

                        entityText = new StringBuilder();
                        inEnt      = false;
                    }
                }

                foreach (BspEntity spawner in spawners)
                {
                    bool addSpawner = false;
                    if (spawner.ClassName == "misc_mg42")
                    {
                        count++;                         // skip the base
                        spawner.Number = count;
                        addSpawner     = true;
                    }
                    else if (spawner.ClassName == "trigger_objective_info")
                    {
                        // check to see if there should be exlosive or construct indicators
                        // spawned for this item
                        if (spawner.Target != "UNDEFINED")
                        {
                            BspEntity target = targetNames[spawner.Target] as BspEntity;

                            if (target != null)
                            {
                                if (target.ClassName == "func_constructible")
                                {
                                    spawner.Number = count;
                                    spawner.Text   = "{ \"classname\" \"constructible_indicator\" }";
                                    addSpawner     = false;                                 // added in the fourth frame
                                }
                                else if (target.ClassName == "func_explosive" && target.SpawnFlags != "UNDEFINED")
                                {
                                    // if there is an axis or ally spawnflags bit, add it
                                    try
                                    {
                                        int spawnflags = Convert.ToInt32(spawner.SpawnFlags);

                                        if (((spawnflags & 1) == 1) || ((spawnflags & 2) == 2))
                                        {
                                            spawner.Number = count;
                                            spawner.Text   = "{ \"classname\" \"explosive_indicator\" }";
                                            addSpawner     = false;                                         // added in the fourth frame
                                        }
                                    }
                                    catch {}
                                }
                            }
                        }
                    }
                    else
                    {
                        spawner.Number = count;
                        addSpawner     = true;
                    }

                    if (addSpawner)
                    {
                        entities.Add(spawner);
                        count++;
                    }
                }
                return(entities);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
Exemplo n.º 11
0
        public ArrayList Read(string filename)
        {
            //open the file for reading
            FileStream fs = null;

            try
            {
                fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
                BinaryReader reader = new BinaryReader(fs);
                //read the contens from the file
                //init the header
                BspHeader header = new BspHeader();
                header.Read(reader);
                //allocate space needed for the data structures

                int entityOffset = header.Entries[0].Offset;
                int entityLength = header.Entries[0].Length;

                fs.Seek(entityOffset, SeekOrigin.Begin);

                ArrayList entities = new ArrayList();

                StringBuilder entityText = new StringBuilder();

                int count = 72; // entities start at this number?

                bool inEnt = false;

                Hashtable ignored = GetIgnoredEntityClasses();
                Hashtable conditional = GetConditionalIgnoredEntityClasses();
                Hashtable targetNames = new Hashtable();

                ArrayList spawners = new ArrayList();

                for (int x = 0; x < entityLength; x++)
                {
                    char read = reader.ReadChar();

                    if (inEnt || read == '{')
                    {
                        entityText.Append(read);
                        inEnt = true;
                    }

                    if (read == '}')
                    {
                        BspEntity ent = new BspEntity(0, entityText.ToString());

                        bool ignore = false;
                        if (ignored.ContainsKey(ent.ClassName))
                        {
                            ignore = true;
                        }
                        else if (conditional.ContainsKey(ent.ClassName))
                        {
                            string propNeeded = (string)conditional[ent.ClassName];

                            if (ent.GetProperty(propNeeded) == "UNDEFINED")
                            {
                                ignore = true;
                            }
                        }

                        if (ent.ClassName == "misc_gamemodel" &&
                            ent.TargetName == "UNDEFINED" &&
                            ent.ScriptName == "UNDEFINED" &&
                            ent.SpawnFlags == "UNDEFINED")
                        {
                            ignore = true;
                        }

                        if (!ignore)
                        {
                            if (ent.ClassName == "worldspawn")
                            {
                                ent.Number = 1023;
                                entities.Add(ent);
                            }
                            else
                            {
                                if (ent.ClassName == "misc_mg42")
                                {
                                    spawners.Add(ent); // mg42's here are just placements, the engine creates em
                                }
                                else if (ent.ClassName == "misc_flak" || ent.ClassName == "misc_grabber_trap")
                                {
                                    BspEntity spawned = new BspEntity(0, "This is a spawned entity not defined in the bsp");
                                    spawners.Add(spawned);

                                    ent.Number = count;
                                    entities.Add(ent);
                                }
                                else if (ent.ClassName == "func_plat")
                                {
                                    if (ent.TargetName == "UNDEFINED")
                                    {
                                        BspEntity spawned = new BspEntity(0, "This is a spawned entity not defined in the bsp");
                                        spawners.Add(spawned);
                                    }
                                    ent.Number = count;
                                    entities.Add(ent);
                                }
                                else if (ent.ClassName == "func_door_rotating")
                                {
                                    /*
                                    else if ((ent->spawnflags & 8) && (strcmp( ent->classname, "func_door_rotating" )))
                                        ent->think = Think_SpawnNewDoorTrigger;
                                    */
                                    bool handledDoor = false;
                                    if (ent.SpawnFlags != "UNDEFINED")
                                    {
                                        try
                                        {
                                            int flags = Convert.ToInt32(ent.SpawnFlags);

                                            if ((flags & 8) == 8)
                                            {
                                                BspEntity doorTrigger = new BspEntity(0, "This is a spawned entity not defined in the bsp");

                                                spawners.Add(doorTrigger);

                                                ent.Number = count;
                                                entities.Add(ent);

                                                handledDoor = true;
                                            }
                                        }
                                        catch {}
                                    }

                                    if (!handledDoor)
                                    {
                                        ent.Number = count;
                                        entities.Add(ent);
                                    }
                                }
                                else if (ent.ClassName == "trigger_objective_info")
                                {
                                    BspEntity checkIndicator = new BspEntity(0, ent.Text);
                                    spawners.Add(checkIndicator);

                                    ent.Number = count;
                                    entities.Add(ent);
                                }
                                else
                                {
                                    ent.Number = count;
                                    entities.Add(ent);
                                }
                                count++;
                            }
                        }

                        string targetKey = ent.TargetName;

                        if (targetKey != "UNDEFINED" && !targetNames.ContainsKey(targetKey))
                        {
                            targetNames.Add(targetKey, ent);
                        }

                        entityText = new StringBuilder();
                        inEnt = false;
                    }
                }

                foreach (BspEntity spawner in spawners)
                {
                    bool addSpawner = false;
                    if (spawner.ClassName == "misc_mg42")
                    {
                        count++; // skip the base
                        spawner.Number = count;
                        addSpawner = true;
                    }
                    else if (spawner.ClassName == "trigger_objective_info")
                    {
                        // check to see if there should be exlosive or construct indicators
                        // spawned for this item
                        if (spawner.Target != "UNDEFINED")
                        {
                            BspEntity target = targetNames[spawner.Target] as BspEntity;

                            if (target != null)
                            {
                                if (target.ClassName == "func_constructible")
                                {
                                    spawner.Number = count;
                                    spawner.Text = "{ \"classname\" \"constructible_indicator\" }";
                                    addSpawner = false; // added in the fourth frame
                                }
                                else if (target.ClassName == "func_explosive" && target.SpawnFlags != "UNDEFINED")
                                {
                                    // if there is an axis or ally spawnflags bit, add it
                                    try
                                    {
                                        int spawnflags = Convert.ToInt32(spawner.SpawnFlags);

                                        if (((spawnflags & 1) == 1) || ((spawnflags & 2) == 2))
                                        {
                                            spawner.Number = count;
                                            spawner.Text = "{ \"classname\" \"explosive_indicator\" }";
                                            addSpawner = false; // added in the fourth frame
                                        }
                                    }
                                    catch {}
                                }
                            }
                        }
                    }
                    else
                    {
                        spawner.Number = count;
                        addSpawner = true;
                    }

                    if (addSpawner)
                    {
                        entities.Add(spawner);
                        count++;
                    }
                }
                return entities;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }