public static Bsp Read(string filepath) { byte[] dataBuffer = File.ReadAllBytes(filepath); GCHandle hDataBuffer = GCHandle.Alloc(dataBuffer, GCHandleType.Pinned); IntPtr ptr = hDataBuffer.AddrOfPinnedObject(); Bsp bsp = new Bsp { Header = Marshal.PtrToStructure <Header>(ptr) }; bsp.Entity = ReadEntityLump(ptr, bsp.Header.Entries[0]); bsp.Textures = ReadBasicLump <Texture>(ptr, bsp.Header.Entries[1]); bsp.Planes = ReadBasicLump <Plane>(ptr, bsp.Header.Entries[2]); bsp.Nodes = ReadBasicLump <Node>(ptr, bsp.Header.Entries[3]); bsp.Leafs = ReadBasicLump <Leaf>(ptr, bsp.Header.Entries[4]); bsp.LeafFaces = ReadBasicLump <LeafFace>(ptr, bsp.Header.Entries[5]); bsp.LeafBrushs = ReadBasicLump <LeafBrush>(ptr, bsp.Header.Entries[6]); bsp.Models = ReadBasicLump <Model>(ptr, bsp.Header.Entries[7]); bsp.Brushs = ReadBasicLump <Brush>(ptr, bsp.Header.Entries[8]); bsp.BrushSides = ReadBasicLump <BrushSide>(ptr, bsp.Header.Entries[9]); bsp.Vertices = ReadBasicLump <Vertex>(ptr, bsp.Header.Entries[10]); bsp.MeshVerts = ReadBasicLump <MeshVert>(ptr, bsp.Header.Entries[11]); bsp.Effects = ReadBasicLump <Effect>(ptr, bsp.Header.Entries[12]); bsp.Faces = ReadBasicLump <Face>(ptr, bsp.Header.Entries[13]); bsp.LightMaps = ReadBasicLump <LightMap>(ptr, bsp.Header.Entries[14]); bsp.LightVols = ReadBasicLump <LightVol>(ptr, bsp.Header.Entries[15]); bsp.Visdata = ReadVisDataLump(ptr, bsp.Header.Entries[16]); return(bsp); }
public static void Write(Bsp bsp, string location) { int headerFixedSize = 200; byte[][] lumpsBytes = new byte[][] { GetEntityLumpBytes(bsp.Entity), GetLumpBytes(bsp.Textures), GetLumpBytes(bsp.Planes), GetLumpBytes(bsp.Nodes), GetLumpBytes(bsp.Leafs), GetLumpBytes(bsp.LeafFaces), GetLumpBytes(bsp.LeafBrushs), GetLumpBytes(bsp.Models), GetLumpBytes(bsp.Brushs), GetLumpBytes(bsp.BrushSides), GetLumpBytes(bsp.Vertices), GetLumpBytes(bsp.MeshVerts), GetLumpBytes(bsp.Effects), GetLumpBytes(bsp.Faces), GetLumpBytes(bsp.LightMaps), GetLumpBytes(bsp.LightVols), GetVisdataBytes(bsp.Visdata) }; int currentOffset = headerFixedSize; for (int i = 0; i < lumpsBytes.Length; i++) { bsp.Header.Entries[i] = new LumpEntry { Length = lumpsBytes[i].Length, Offset = currentOffset }; currentOffset += lumpsBytes[i].Length; } byte[] bytesHeader = GetStructureBytes(bsp.Header, headerFixedSize); byte[] mergedLumpsBytes = Combine(lumpsBytes); byte[] finalBytes = Combine(bytesHeader, mergedLumpsBytes); File.WriteAllBytes(location, finalBytes); }
public Bsp(string filepath) { Bsp bsp = BspReader.Read(filepath); Header = bsp.Header; Entity = bsp.Entity; Textures = bsp.Textures; Planes = bsp.Planes; Nodes = bsp.Nodes; Leafs = bsp.Leafs; LeafFaces = bsp.LeafFaces; LeafBrushs = bsp.LeafBrushs; Models = bsp.Models; Brushs = bsp.Brushs; BrushSides = bsp.BrushSides; Vertices = bsp.Vertices; MeshVerts = bsp.MeshVerts; Effects = bsp.Effects; Faces = bsp.Faces; LightMaps = bsp.LightMaps; LightVols = bsp.LightVols; Visdata = bsp.Visdata; }