private int GetLumpLength <T>(LumpType type) where T : struct { var info = GetLumpInfo(type); return(info.Length / Marshal.SizeOf <T>()); }
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); }
public HOGLump(string name, byte[] data) { this.Name = name; Size = data.Length; Offset = -1; this.Data = data; Type = IdentifyLump(name, data); }
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)); }
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]); }
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); }
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); }
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; }
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); } } }
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); }
public StructArrayLump(ValveBspFile bspFile, LumpType type) : base(bspFile, type) { }
public ArrayLump(ValveBspFile bspFile, LumpType type) { BspFile = bspFile; LumpType = type; Length = BspFile.GetLumpLength(type, StructType); }
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); }
/// <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()); }
public ArrayLump(ValveBspFile bspFile, LumpType type) { _bspFile = bspFile; LumpType = type; _length = _bspFile.GetLumpLength <T>(LumpType); }
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"); } }
public BspLumpAttribute(LumpType type) { Type = type; }
public PakFileLump(ValveBspFile bspFile, LumpType type) { _bspFile = bspFile; _loaded = false; LumpType = type; }
private int GetLumpLength(LumpType lumpType, Type structType) { var info = GetLumpInfo(lumpType); return(info.Length / Marshal.SizeOf(structType)); }
public VersionedArrayLump(ValveBspFile bspFile, LumpType type) : base(bspFile, type) { }
public SetLumpType(LumpType lump_t, int bytesPerBlock ) { this.lump_t = lump_t; this.bytes = bytesPerBlock; }
public VisibilityLump(ValveBspFile bspFile, LumpType type) { _bspFile = bspFile; LumpType = type; }
public Entry(string name, LumpType lumpType) { Name = name; Type = lumpType; }
public BspLump GetLump(LumpType type) { return(Lumps.First(x => x.Type == type)); }
public EntityLump(ValveBspFile bspFile, LumpType type) { _bspFile = bspFile; LumpType = type; }
public Stream GetLumpStream(LumpType type) { var info = GetLumpInfo(type); return(GetSubStream(info.Offset, info.Length)); }
public SetLumpType(LumpType lump_t, int bytesPerBlock) { this.lump_t = lump_t; this.bytes = bytesPerBlock; }
public GameLump(ValveBspFile bspFile, LumpType type) { _bspFile = bspFile; LumpType = type; }