Ejemplo n.º 1
0
        private int GetLumpLength <T>(LumpType type)
            where T : struct
        {
            var info = GetLumpInfo(type);

            return(info.Length / Marshal.SizeOf <T>());
        }
Ejemplo n.º 2
0
        private int ReadLumpValues <T>(LumpType type, int srcOffset, T[] dst, int dstOffset, int count)
            where T : struct
        {
            var info   = GetLumpInfo(type);
            var tSize  = Marshal.SizeOf <T>();
            var length = info.Length / tSize;

            if (srcOffset > length)
            {
                srcOffset = length;
            }
            if (srcOffset + count > length)
            {
                count = length - srcOffset;
            }

            if (count <= 0)
            {
                return(0);
            }

            using (var stream = GetLumpStream(type))
            {
                stream.Seek(tSize * srcOffset, SeekOrigin.Begin);
                LumpReader <T> .ReadLumpFromStream(stream, count, dst, dstOffset);
            }

            return(count);
        }
Ejemplo n.º 3
0
 public HOGLump(string name, byte[] data)
 {
     this.Name = name;
     Size      = data.Length;
     Offset    = -1;
     this.Data = data;
     Type      = IdentifyLump(name, data);
 }
Ejemplo n.º 4
0
        private int GetLumpLength(LumpType lumpType, Type structType)
        {
            var info = GetLumpInfo(lumpType);

            if (info.UncompressedSize == 0)
            {
                return(info.Length / Marshal.SizeOf(structType));
            }
            return(info.UncompressedSize / Marshal.SizeOf(structType));
        }
Ejemplo n.º 5
0
        private LumpInfo GetLumpInfo(LumpType type)
        {
            var lumpIndex = (int)type;

            if (lumpIndex < 0 || lumpIndex >= _header.Lumps.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            return(_header.Lumps[lumpIndex]);
        }
Ejemplo n.º 6
0
        private Array ReadLump(MemoryStream ms, Type elementType, LumpType lumpType)
        {
            DirEntry lump  = bspHeader.GetLump(lumpType);
            int      count = lump.iLength / Marshal.SizeOf(elementType);
            Array    arr   = Array.CreateInstance(elementType, count);

            ms.Position = lump.iOffset;

            for (int i = 0; i < count; i++)
            {
                arr.SetValue(ReadStruct(ms, elementType), i);
            }

            Type arrType = elementType.MakeArrayType();

            return(arr);
        }
Ejemplo n.º 7
0
        public T[] GetLump <T>() where T : new()
        {
            if (!headerReady)
            {
                return(default(T[]));
            }

            LumpType lt        = typeof(T).GetCustomAttribute <SetLumpType>().lump_t;
            int      chunkSize = typeof(T).GetCustomAttribute <SetLumpType>().bytes;

            lump_t lumpdefinition = header.lumps[(int)lt];

            if (lumpdefinition.chunk.Length == 0)
            {
                return(default(T[]));
            }


            byte[] chunk      = lumpdefinition.chunk;
            int    chunkCount = chunk.Length / chunkSize;

            T[] chunks = new T[chunkCount];

            using (BinaryReader br = new BinaryReader(new MemoryStream()))
            {
                br.BaseStream.Write(chunk, 0, chunk.Length);
                br.BaseStream.Position = 0;

                for (int I = 0; I < chunkCount; I++)
                {
                    byte[] curBuffer = br.ReadBytes(chunkSize);

                    IntPtr intPtr = Marshal.AllocHGlobal(curBuffer.Length);
                    Marshal.Copy(curBuffer, 0, intPtr, curBuffer.Length);

                    T LumpStruct = (T)Marshal.PtrToStructure(intPtr, typeof(T));
                    chunks[I] = LumpStruct;

                    Marshal.FreeHGlobal(intPtr);
                }
                br.Close();
            }

            return(chunks);
        }
Ejemplo n.º 8
0
        protected Lump(BinaryReader reader, LumpType index)
        {
            // Read header
            Index  = index;
            Offset = reader.ReadInt32();
            var length = reader.ReadInt32();

            Version = reader.ReadInt32();
            IDENT   = reader.ReadInt32();

            // Read data
            var prevPosition = reader.BaseStream.Position;

            reader.BaseStream.Seek(Offset, SeekOrigin.Begin);
            var data = new byte[length];

            reader.Read(data, 0, length);
            reader.BaseStream.Seek(prevPosition, SeekOrigin.Begin);

            DataBytes = data;
        }
Ejemplo n.º 9
0
        public Stream GetLumpStream(LumpType type)
        {
            var info = GetLumpInfo(type);

            if (info.UncompressedSize == 0)
            {
                return(GetSubStream(info.Offset, info.Length));
            }

            using (var stream = GetSubStream(info.Offset, LzmaHeader.Size))
            {
                var lzmaHeader = LzmaHeader.Read(stream);

                using (var compressedStream = GetSubStream(info.Offset + LzmaHeader.Size, lzmaHeader.LzmaSize))
                {
                    var     uncompressedStream = new MemoryStream(info.UncompressedSize);
                    Decoder decoder            = new Decoder();
                    decoder.SetDecoderProperties(lzmaHeader.Properties);
                    decoder.Code(compressedStream, uncompressedStream, lzmaHeader.LzmaSize, lzmaHeader.ActualSize, null);
                    uncompressedStream.Seek(0, SeekOrigin.Begin);
                    return(uncompressedStream);
                }
            }
        }
Ejemplo n.º 10
0
        private int ReadLumpValues <T>(LumpType type, int srcOffset, T[] dst, int dstOffset, int count)
            where T : struct
        {
            var info = GetLumpInfo(type);

            // 0 = no compression
            if (info.UncompressedSize == 0)
            {
                var tSize  = Marshal.SizeOf <T>();
                var length = info.Length / tSize;

                if (srcOffset > length)
                {
                    srcOffset = length;
                }
                if (srcOffset + count > length)
                {
                    count = length - srcOffset;
                }

                if (count <= 0)
                {
                    return(0);
                }

                using (var stream = GetLumpStream(type))
                {
                    stream.Seek(tSize * srcOffset, SeekOrigin.Begin);
                    LumpReader <T> .ReadLumpFromStream(stream, count, dst, dstOffset);
                }
            }
            else
            {
                // LZMA compressed lump

                if (type == LumpType.GAME_LUMP)
                {
                    // game lumps are compressed individually
                    // https://developer.valvesoftware.com/wiki/Source_BSP_File_Format#Lump_compression
                    throw new NotImplementedException();
                }

                using (var stream = GetSubStream(info.Offset, LzmaHeader.Size))
                {
                    var lzmaHeader = LzmaHeader.Read(stream);

                    using (var compressedStream = GetSubStream(info.Offset + LzmaHeader.Size, lzmaHeader.LzmaSize))
                    {
                        using (var uncompressedStream = new MemoryStream(info.UncompressedSize))
                        {
                            Decoder decoder = new Decoder();
                            decoder.SetDecoderProperties(lzmaHeader.Properties);
                            decoder.Code(compressedStream, uncompressedStream, lzmaHeader.LzmaSize, lzmaHeader.ActualSize, null);

                            var tSize  = Marshal.SizeOf <T>();
                            var length = info.UncompressedSize / tSize;

                            if (srcOffset > length)
                            {
                                srcOffset = length;
                            }
                            if (srcOffset + count > length)
                            {
                                count = length - srcOffset;
                            }

                            if (count <= 0)
                            {
                                return(0);
                            }

                            uncompressedStream.Seek(tSize * srcOffset, SeekOrigin.Begin);
                            LumpReader <T> .ReadLumpFromStream(uncompressedStream, count, dst, dstOffset);

                            if (type == LumpType.PLANES)
                            {
                                return(count);
                            }
                        }
                    }
                }
            }

            return(count);
        }
Ejemplo n.º 11
0
 public StructArrayLump(ValveBspFile bspFile, LumpType type)
     : base(bspFile, type)
 {
 }
Ejemplo n.º 12
0
 public ArrayLump(ValveBspFile bspFile, LumpType type)
 {
     BspFile  = bspFile;
     LumpType = type;
     Length   = BspFile.GetLumpLength(type, StructType);
 }
Ejemplo n.º 13
0
        public static Lump Read(BinaryReader reader, LumpType type)
        {
            var lump = new Lump();

            lump.FileOffset = reader.ReadInt32();
            lump.FileLength = reader.ReadInt32();
            lump.Version    = reader.ReadInt32();
            lump.FourCC     = reader.ReadInt32();
            lump.LumpType   = type;

            var position = reader.BaseStream.Position;

            reader.BaseStream.Position = lump.FileOffset;
            switch (lump.LumpType)
            {
            case LumpType.Planes:
                lump.Data = new ArrayLumpData <LumpData.Plane>(reader, lump.FileLength);
                break;

            case LumpType.Vertices:
                lump.Data = new ArrayLumpData <Vector3>(reader, lump.FileLength);
                break;

            case LumpType.Edges:
                lump.Data = new ArrayLumpData <Edge>(reader, lump.FileLength);
                break;

            case LumpType.SurfaceEdges:
                lump.Data = new ArrayLumpData <int>(reader, lump.FileLength);
                break;

            case LumpType.Faces:
                lump.Data = new ArrayLumpData <Face>(reader, lump.FileLength);
                break;

            case LumpType.OriginalFaces:
                lump.Data = new ArrayLumpData <Face>(reader, lump.FileLength);
                break;

            case LumpType.Brushes:
                lump.Data = new ArrayLumpData <Brush>(reader, lump.FileLength);
                break;

            case LumpType.BrushSides:
                lump.Data = new ArrayLumpData <BrushSide>(reader, lump.FileLength);
                break;

            case LumpType.Nodes:
                lump.Data = new ArrayLumpData <Node>(reader, lump.FileLength);
                break;

            case LumpType.Leafs:
                lump.Data = new ArrayLumpData <Leaf>(reader, lump.FileLength);
                break;

            case LumpType.LeafFaces:
                lump.Data = new ArrayLumpData <ushort>(reader, lump.FileLength);
                break;

            case LumpType.LeafBrushes:
                lump.Data = new ArrayLumpData <ushort>(reader, lump.FileLength);
                break;

            case LumpType.TextureInfo:
                lump.Data = new ArrayLumpData <TextureInfo>(reader, lump.FileLength);
                break;

            case LumpType.TextureData:
                lump.Data = new ArrayLumpData <TextureData>(reader, lump.FileLength);
                break;

            case LumpType.TextureStringData:
                lump.Data = new TextureDataString(reader, lump.FileLength);
                break;

            case LumpType.TextureDataStringTable:
                lump.Data = new ArrayLumpData <int>(reader, lump.FileLength);
                break;

            case LumpType.DisplacementVertices:
                lump.Data = new ArrayLumpData <DisplacementVertex>(reader, lump.FileLength);
                break;

            case LumpType.DisplacementTriangles:
                lump.Data = new ArrayLumpData <DisplacementTriangle>(reader, lump.FileLength);
                break;

            case LumpType.DisplacementInfo:
                lump.Data = new ArrayLumpData <DisplacementInfo>(reader, lump.FileLength);
                break;

            case LumpType.VertexNormals:
                lump.Data = new ArrayLumpData <Vector3>(reader, lump.FileLength);
                break;

            case LumpType.VertexNormalIndices:
                lump.Data = new ArrayLumpData <uint>(reader, lump.FileLength);
                break;

            case LumpType.Models:
                lump.Data = new ArrayLumpData <Model>(reader, lump.FileLength);
                break;

            case LumpType.PakFile:
                lump.Data = new ArrayLumpData <byte>(reader, lump.FileLength);
                break;

            case LumpType.GameLump:
                lump.Data = new GameLump(reader);
                break;
            }
            reader.BaseStream.Position = position;
            return(lump);
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Finds all lumps that match the specified type.
 /// </summary>
 /// <param name="type">The lump type to search for.</param>
 /// <returns>All lumps in the HOG file that match the type requested.</returns>
 public List <HOGLump> GetLumpsByType(LumpType type)
 {
     return(lumps.Where(lump => lump.Type == type).ToList());
 }
Ejemplo n.º 15
0
 public ArrayLump(ValveBspFile bspFile, LumpType type)
 {
     _bspFile = bspFile;
     LumpType = type;
     _length  = _bspFile.GetLumpLength <T>(LumpType);
 }
Ejemplo n.º 16
0
 public DirEntry GetLump(LumpType index)
 {
     if (LumpType.Entities == index)
     {
         return(entities);
     }
     else if (LumpType.Textures == index)
     {
         return(textures);
     }
     else if (LumpType.Planes == index)
     {
         return(planes);
     }
     else if (LumpType.Nodes == index)
     {
         return(nodes);
     }
     else if (LumpType.Leafs == index)
     {
         return(leafs);
     }
     else if (LumpType.LeafFaces == index)
     {
         return(leafFaces);
     }
     else if (LumpType.LeafBrushes == index)
     {
         return(leafBrushes);
     }
     else if (LumpType.Brushes == index)
     {
         return(brushes);
     }
     else if (LumpType.BrushSides == index)
     {
         return(brushSides);
     }
     else if (LumpType.Vertices == index)
     {
         return(vertices);
     }
     else if (LumpType.MeshVertices == index)
     {
         return(meshVertices);
     }
     else if (LumpType.Faces == index)
     {
         return(faces);
     }
     else if (LumpType.LightMaps == index)
     {
         return(lightmaps);
     }
     else if (LumpType.VisData == index)
     {
         return(visData);
     }
     else
     {
         throw new ArgumentException("Wrong lump index specified", "index");
     }
 }
Ejemplo n.º 17
0
 public BspLumpAttribute(LumpType type)
 {
     Type = type;
 }
Ejemplo n.º 18
0
 public PakFileLump(ValveBspFile bspFile, LumpType type)
 {
     _bspFile = bspFile;
     _loaded  = false;
     LumpType = type;
 }
Ejemplo n.º 19
0
        private int GetLumpLength(LumpType lumpType, Type structType)
        {
            var info = GetLumpInfo(lumpType);

            return(info.Length / Marshal.SizeOf(structType));
        }
Ejemplo n.º 20
0
 public VersionedArrayLump(ValveBspFile bspFile, LumpType type)
     : base(bspFile, type)
 {
 }
Ejemplo n.º 21
0
 public SetLumpType(LumpType lump_t, int bytesPerBlock )
 {
     this.lump_t = lump_t;
     this.bytes = bytesPerBlock;
 }
Ejemplo n.º 22
0
 public VisibilityLump(ValveBspFile bspFile, LumpType type)
 {
     _bspFile = bspFile;
     LumpType = type;
 }
Ejemplo n.º 23
0
 public Entry(string name, LumpType lumpType)
 {
     Name = name;
     Type = lumpType;
 }
Ejemplo n.º 24
0
 public BspLump GetLump(LumpType type)
 {
     return(Lumps.First(x => x.Type == type));
 }
Ejemplo n.º 25
0
 public EntityLump(ValveBspFile bspFile, LumpType type)
 {
     _bspFile = bspFile;
     LumpType = type;
 }
Ejemplo n.º 26
0
        public Stream GetLumpStream(LumpType type)
        {
            var info = GetLumpInfo(type);

            return(GetSubStream(info.Offset, info.Length));
        }
Ejemplo n.º 27
0
 public SetLumpType(LumpType lump_t, int bytesPerBlock)
 {
     this.lump_t = lump_t;
     this.bytes  = bytesPerBlock;
 }
Ejemplo n.º 28
0
 public GameLump(ValveBspFile bspFile, LumpType type)
 {
     _bspFile = bspFile;
     LumpType = type;
 }