Skip to content

Commit

Permalink
Merge pull request #9 from Galaco/dev
Browse files Browse the repository at this point in the history
dev->master
  • Loading branch information
Galaco authored Oct 15, 2018
2 parents 5064ee7 + 99534ad commit b9ba153
Show file tree
Hide file tree
Showing 107 changed files with 2,352 additions and 1,387 deletions.
25 changes: 0 additions & 25 deletions BSP_FORMAT.md

This file was deleted.

20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
Go library for manipulating Source Engine .bsp map files.

### Features:
* Read support for (probably) all documented bsp formats
* Write support for (probably) all documented bsp formats.
* Read support for (most) non-xbox360 bsps.
* Freely modify and resize any Lump data.
* Limited write support

##### Not all lumps are current supported, but can be freely read and modified, as they are treated as `[]byte`

The following lumps currently have a full implementation for v20 bsp's:
The following lumps currently have a full implementation for v20 bsp's (tested against CS:S & CS:GO):

```
0: Entdata
Expand All @@ -33,13 +33,16 @@ The following lumps currently have a full implementation for v20 bsp's:
19: Brushsides
20: Areas
21: AreaPortals
26: DispInfo
27: OriginalFaces
28: PhysDisp
30: VertNormals
31: VertNormalIndices
33: DispVerts
34: DispLightmapSamplePosition
35: Game lump (partial: sprp only)
36: LeafWaterData
37: Primitives
38: PrimVerts
39: PrimIndices
40: Pakfile
Expand All @@ -66,7 +69,7 @@ The following lumps currently have a full implementation for v20 bsp's:
# Usage

Minimal example of obtaining entdata from a BSP. The following will print the entdata
blocks of a specified .bsp to terminal.
lump (entdata is a single json-like string) of a specified .bsp to console.

```go
package main
Expand All @@ -90,14 +93,15 @@ func main() {
}
f.Close()

lump := file.GetLump(bsp.LUMP_ENTITIES)
log.Println(lump.GetContents().GetData().(string))
lump := file.GetLump(bsp.LUMP_ENTITIES).(*lump.Entities)
log.Println(lump.GetData())
}
```

## Real World examples
Replace game_text newline placeholder characters (avoids Hammer crash) as a compile step: [https://github.com/Galaco/CS-GO-game_text-newline-inserter/tree/golang](https://github.com/Galaco/CS-GO-game_text-newline-inserter/tree/golang)
* Replace game_text newline placeholder characters (avoids Hammer crash) as a compile step: [https://github.com/Galaco/CS-GO-game_text-newline-inserter/tree/golang](https://github.com/Galaco/CS-GO-game_text-newline-inserter/tree/golang)
* Proof of concept BSP viewer: [https://github.com/Galaco/Gource-Engine](https://github.com/Galaco/Gource-Engine)


# Contributing
If you want to contribute, feel free to fork and raise a Pull Request for new additions.
All contributions welcome. Known unsupported games/maps are especially useful.
25 changes: 16 additions & 9 deletions bsp.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package bsp

import "github.com/galaco/bsp/lumps"

// Root .bsp filetype container.
// Consists of a 1036byte header and 64 lump blocks.
type Bsp struct {
header Header
lumps [64]Lump
lumps [64]Lump
}

// Bsp header. Contains format and lump layout data.
// Do not trust lump information between import and export
type Header struct {
Id int32
Version int32
Lumps [64]HeaderLump
Id int32
Version int32
Lumps [64]HeaderLump
Revision int32
}

// Layout information for a given lump, stored in the Header.
type HeaderLump struct {
Offset int32
Length int32
Offset int32
Length int32
Version int32
Id [4]byte
Id [4]byte
}

// Get the header for a bsp.
Expand All @@ -30,11 +32,16 @@ func (bsp *Bsp) GetHeader() Header {
}

// Get the lump for a given index.
func (bsp *Bsp) GetLump(index int) *Lump {
func (bsp *Bsp) GetLump(index int) lumps.ILump {
return bsp.GetLumpRaw(index).GetContents()
}

// Get the lump for a given index.
func (bsp *Bsp) GetLumpRaw(index int) *Lump {
return &bsp.lumps[index]
}

// Set the lump data for a given index.
func (bsp *Bsp) SetLump(index int, lump Lump) {
bsp.lumps[index] = lump
}
}
15 changes: 8 additions & 7 deletions bsp_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package bsp

import (
"testing"
"log"
"bytes"
"log"
"testing"
)

// Test that resultant lump data matches expected.
func TestLumpExports(t *testing.T) {
file,err := ReadFromFile("maps/v20/de_dust2.bsp")
file, err := ReadFromFile("maps/v20/de_dust2.bsp")
if err != nil {
t.Error(err)
}
Expand All @@ -17,16 +17,17 @@ func TestLumpExports(t *testing.T) {
lumpIndex := 0
for lumpIndex < 64 {
lump := file.GetLump(lumpIndex)
lumpBytes := lump.GetContents().ToBytes()
rawLump := file.GetLumpRaw(lumpIndex)
lumpBytes := lump.ToBytes()
if len(lumpBytes) != int(file.GetHeader().Lumps[lumpIndex].Length) {
t.Errorf("Lump: %d length mismatch. Got: %dbytes, expected: %dbytes", lumpIndex, len(lumpBytes), file.header.Lumps[lumpIndex].Length)
} else {
log.Printf("Index: %d, Expected: %d, Actual: %d\n", lumpIndex, len(lump.GetRawContents()), len(lumpBytes))
if !bytes.Equal(lumpBytes, lump.GetRawContents()) {
log.Printf("Index: %d, Expected: %d, Actual: %d\n", lumpIndex, len(rawLump.GetRawContents()), len(lumpBytes))
if !bytes.Equal(lumpBytes, rawLump.GetRawContents()) {
t.Errorf("Lump: %d data mismatch", lumpIndex)
}
}

lumpIndex += 1
}
}
}
Loading

0 comments on commit b9ba153

Please sign in to comment.