diff --git a/bsp.go b/bsp.go index c7c42e9..5313886 100644 --- a/bsp.go +++ b/bsp.go @@ -31,17 +31,17 @@ func (bsp *Bsp) GetHeader() Header { return bsp.header } -// GetLump gets the lump for a given index. +// GetLump gets the lump for a given id. func (bsp *Bsp) GetLump(index LumpId) lumps.ILump { return bsp.GetLumpRaw(index).GetContents() } -// GetLumpRaw gets the lump for a given index. +// GetLumpRaw gets the lump for a given id. func (bsp *Bsp) GetLumpRaw(index LumpId) *Lump { return &bsp.lumps[int(index)] } -// SetLump sets the lump data for a given index. +// SetLump sets the lump data for a given id. func (bsp *Bsp) SetLump(index LumpId, lump Lump) { bsp.lumps[int(index)] = lump } diff --git a/flags/constants.go b/flags/constants.go deleted file mode 100644 index 0cfcbd5..0000000 --- a/flags/constants.go +++ /dev/null @@ -1 +0,0 @@ -package flags diff --git a/internal/versions/v19.go b/internal/versions/v19.go index c1c1bee..b8cdfc9 100644 --- a/internal/versions/v19.go +++ b/internal/versions/v19.go @@ -4,7 +4,7 @@ import ( "github.com/galaco/bsp/lumps" ) -// Returns an empty bsp v19 lump for the specified lump id +// Getv19Lump returns the corresponding v19 lump for provided index func Getv19Lump(index int) (lumps.ILump, error) { return Getv20Lump(index) } diff --git a/internal/versions/v20.go b/internal/versions/v20.go index 0446a58..7b8352c 100644 --- a/internal/versions/v20.go +++ b/internal/versions/v20.go @@ -5,6 +5,7 @@ import ( "github.com/galaco/bsp/lumps" ) +// Getv20Lump returns the corresponding v20 lump for provided index func Getv20Lump(index int) (lumps.ILump, error) { var ret lumps.ILump var err error diff --git a/internal/versions/v21.go b/internal/versions/v21.go index 2dfd7f4..178737d 100644 --- a/internal/versions/v21.go +++ b/internal/versions/v21.go @@ -4,7 +4,7 @@ import ( "github.com/galaco/bsp/lumps" ) -// Returns an empty bsp v19 lump for the specified lump id +// Getv21Lump returns the corresponding v21 lump for provided index func Getv21Lump(index int) (lumps.ILump, error) { return Getv20Lump(index) } diff --git a/internal/versions/versions.go b/internal/versions/versions.go index 7c8f21a..de4b9d7 100644 --- a/internal/versions/versions.go +++ b/internal/versions/versions.go @@ -2,7 +2,7 @@ package versions import "github.com/galaco/bsp/lumps" -// Returns an empty bsp lump for the specified bsp version and lump id +// GetLumpForVersion returns an empty bsp lump for the specified bsp version and lump id // If a version is not 19,20,21 then a generic lump that holds // raw bytes only ([]byte) is returned. func GetLumpForVersion(bspVersion int, lumpId int) (lumps.ILump, error) { diff --git a/lump.go b/lump.go index 78d92ea..283a8d5 100644 --- a/lump.go +++ b/lump.go @@ -6,59 +6,61 @@ import ( "github.com/galaco/bsp/lumps" ) -// Container for a lump. Also includes metadata about the lump. +// Lump is a container for a lump. Also includes metadata about the lump. // N.B. Some information mirrors the header's lump descriptor, but header information should not be trusted after // import completion. type Lump struct { raw []byte data lumps.ILump length int32 - index LumpId + id LumpId loaded bool } -// Get lump identifier -// Id is the lump type index (not the index for the order the lumps are stored) +// SetId Get lump identifier +// Id is the lump type id (not the id for the order the lumps are stored) func (l *Lump) SetId(index LumpId) { - l.index = index + l.id = index } -// Get the contents of a lump. +// GetContents Get the contents of a lump. // NOTE: Will need to be cast to the relevant lumps func (l *Lump) GetContents() lumps.ILump { if !l.loaded { - l.data.Unmarshall(l.raw, int32(len(l.raw))) + if l.data.Unmarshall(l.raw) != nil { + return nil + } l.loaded = true } return l.data } -// Set content type of a lump. +// SetContents Set content type of a lump. func (l *Lump) SetContents(data lumps.ILump) { l.data = data l.loaded = false } -// Get the raw []byte contents of a lump. +// GetRawContents Get the raw []byte contents of a lump. // N.B. This is the raw imported value. To get the raw value of a modified lump, use GetContents().Marshall() func (l *Lump) GetRawContents() []byte { return l.raw } -// Set raw []byte contents of a lump. +// SetRawContents Set raw []byte contents of a lump. func (l *Lump) SetRawContents(raw []byte) { l.raw = raw } -// Get length of a lump in bytes. +// GetLength Get length of a lump in bytes. func (l *Lump) GetLength() int32 { return l.length } -// Return an instance of a Lump for a given offset. +// getReferenceLumpByIndex Return an instance of a Lump for a given offset. func getReferenceLumpByIndex(index int, version int32) (lumps.ILump, error) { if index < 0 || index > 63 { - return nil, fmt.Errorf("invalid lump index: %d provided", index) + return nil, fmt.Errorf("invalid lump id: %d provided", index) } return versions.GetLumpForVersion(int(version), index) diff --git a/lump_test.go b/lump_test.go index 225d81a..c316d56 100644 --- a/lump_test.go +++ b/lump_test.go @@ -44,7 +44,7 @@ func TestLump_SetId(t *testing.T) { sut := Lump{} sut.SetId(LumpPakfile) - if sut.index != LumpPakfile { + if sut.id != LumpPakfile { t.Error("incorrect lump id") } } diff --git a/lumps/area.go b/lumps/area.go index 3aabaaa..1a9a40b 100644 --- a/lumps/area.go +++ b/lumps/area.go @@ -4,29 +4,31 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/area" - "log" "unsafe" ) // Area is Lump 20: Areas type Area struct { - LumpGeneric + Generic data []primitives.Area } // Unmarshall Imports this lump from raw byte data -func (lump *Area) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *Area) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { - return + return nil } - lump.data = make([]primitives.Area, length/int32(unsafe.Sizeof(primitives.Area{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + lump.data = make([]primitives.Area, length/int(unsafe.Sizeof(primitives.Area{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return nil } // GetData gets internal format structure data diff --git a/lumps/areaportal.go b/lumps/areaportal.go index 7433f84..dcf0533 100644 --- a/lumps/areaportal.go +++ b/lumps/areaportal.go @@ -4,27 +4,29 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/areaportal" - "log" "unsafe" ) // AreaPortal is Lump 21: Areaportals type AreaPortal struct { - LumpGeneric + Generic data []primitives.AreaPortal } // Unmarshall Imports this lump from raw byte data -func (lump *AreaPortal) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *AreaPortal) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { - return + return nil } - lump.data = make([]primitives.AreaPortal, length/int32(unsafe.Sizeof(primitives.AreaPortal{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + lump.data = make([]primitives.AreaPortal, length/int(unsafe.Sizeof(primitives.AreaPortal{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } + + return nil } // GetData gets internal format structure data diff --git a/lumps/brush.go b/lumps/brush.go index 2cd521f..11fc707 100644 --- a/lumps/brush.go +++ b/lumps/brush.go @@ -4,24 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/brush" - "log" "unsafe" ) // Brush is Lump 18: Brush type Brush struct { - LumpGeneric + Generic data []primitives.Brush } // Unmarshall Imports this lump from raw byte data -func (lump *Brush) Unmarshall(raw []byte, length int32) { - lump.data = make([]primitives.Brush, length/int32(unsafe.Sizeof(primitives.Brush{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) +func (lump *Brush) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.data = make([]primitives.Brush, length/int(unsafe.Sizeof(primitives.Brush{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return nil } // GetData gets internal format structure data diff --git a/lumps/brushside.go b/lumps/brushside.go index 511495a..4286381 100644 --- a/lumps/brushside.go +++ b/lumps/brushside.go @@ -4,24 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/brushside" - "log" "unsafe" ) // BrushSide is Lump 19: BrushSide type BrushSide struct { - LumpGeneric + Generic data []primitives.BrushSide // MAX_MAP_BRUSHSIDES = 65536 } // Unmarshall Imports this lump from raw byte data -func (lump *BrushSide) Unmarshall(raw []byte, length int32) { - lump.data = make([]primitives.BrushSide, length/int32(unsafe.Sizeof(primitives.BrushSide{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) +func (lump *BrushSide) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.data = make([]primitives.BrushSide, length/int(unsafe.Sizeof(primitives.BrushSide{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return nil } // GetData gets internal format structure data diff --git a/lumps/clipportalverts.go b/lumps/clipportalverts.go index 50be21f..13fd16f 100644 --- a/lumps/clipportalverts.go +++ b/lumps/clipportalverts.go @@ -4,24 +4,25 @@ import ( "bytes" "encoding/binary" "github.com/go-gl/mathgl/mgl32" - "log" "unsafe" ) // ClipPortalVerts is Lump 41: ClipPortalVerts type ClipPortalVerts struct { - LumpGeneric + Generic data []mgl32.Vec3 } // Unmarshall Imports this lump from raw byte data -func (lump *ClipPortalVerts) Unmarshall(raw []byte, length int32) { - lump.data = make([]mgl32.Vec3, length/int32(unsafe.Sizeof(mgl32.Vec3{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) +func (lump *ClipPortalVerts) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.data = make([]mgl32.Vec3, length/int(unsafe.Sizeof(mgl32.Vec3{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + return nil } // GetData gets internal format structure data diff --git a/lumps/clipportalverts_test.go b/lumps/clipportalverts_test.go new file mode 100644 index 0000000..212fce0 --- /dev/null +++ b/lumps/clipportalverts_test.go @@ -0,0 +1,83 @@ +package lumps + +import ( + "bytes" + "encoding/binary" + "github.com/go-gl/mathgl/mgl32" + "testing" +) + +func TestClipPortalVerts_GetData(t *testing.T) { + sut := ClipPortalVerts{} + data := []mgl32.Vec3{ + {0, 1, 2}, + {5, 43, 156}, + } + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + err = sut.Unmarshall(buf.Bytes()) + if err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestClipPortalVerts_Marshall(t *testing.T) { + sut := ClipPortalVerts{} + data := []mgl32.Vec3{ + {0, 1, 2}, + {5, 43, 156}, + } + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + err = sut.Unmarshall(buf.Bytes()) + if err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if buf.Bytes()[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestClipPortalVerts_Unmarshall(t *testing.T) { + sut := ClipPortalVerts{} + data := []mgl32.Vec3{ + {0, 1, 2}, + {5, 43, 156}, + } + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + err = sut.Unmarshall(buf.Bytes()) + if err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/cubemap.go b/lumps/cubemap.go index 93d01d9..90519e7 100644 --- a/lumps/cubemap.go +++ b/lumps/cubemap.go @@ -4,27 +4,29 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/cubemap" - "log" "unsafe" ) // Cubemap is Lump 42: Cubemaps type Cubemap struct { - LumpGeneric + Generic data []primitives.CubemapSample } // Unmarshall Imports this lump from raw byte data -func (lump *Cubemap) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *Cubemap) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.CubemapSample, length/int32(unsafe.Sizeof(primitives.CubemapSample{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + lump.data = make([]primitives.CubemapSample, length/int(unsafe.Sizeof(primitives.CubemapSample{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } + + return nil } // GetData gets internal format structure data diff --git a/lumps/dispinfo.go b/lumps/dispinfo.go index 9134357..30f88c2 100644 --- a/lumps/dispinfo.go +++ b/lumps/dispinfo.go @@ -4,24 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/dispinfo" - "log" "unsafe" ) // DispInfo is Lump 26: DispInfo type DispInfo struct { - LumpGeneric + Generic data []primitives.DispInfo } // Unmarshall Imports this lump from raw byte data -func (lump *DispInfo) Unmarshall(raw []byte, length int32) { - lump.data = make([]primitives.DispInfo, length/int32(unsafe.Sizeof(primitives.DispInfo{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) +func (lump *DispInfo) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.data = make([]primitives.DispInfo, length/int(unsafe.Sizeof(primitives.DispInfo{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return nil } // GetData gets internal format structure data diff --git a/lumps/displightmapsampleposition.go b/lumps/displightmapsampleposition.go index 245bd8b..f0d969e 100644 --- a/lumps/displightmapsampleposition.go +++ b/lumps/displightmapsampleposition.go @@ -3,14 +3,16 @@ package lumps // DispLightmapSamplePosition is Lump 34: DispLightmapSamplePosition // NOTE: This does NOT have a mapped format yet, and is readable as []byte only type DispLightmapSamplePosition struct { - LumpGeneric + Generic data []byte } // Unmarshall Imports this lump from raw byte data -func (lump *DispLightmapSamplePosition) Unmarshall(raw []byte, length int32) { +func (lump *DispLightmapSamplePosition) Unmarshall(raw []byte) (err error) { lump.data = raw - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(len(raw)) + + return nil } // GetData gets internal format structure data diff --git a/lumps/displightmapsampleposition_test.go b/lumps/displightmapsampleposition_test.go new file mode 100644 index 0000000..95173d0 --- /dev/null +++ b/lumps/displightmapsampleposition_test.go @@ -0,0 +1,51 @@ +package lumps + +import "testing" + +func TestDispLightmapSamplePosition_GetData(t *testing.T) { + sut := DispLightmapSamplePosition{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestDispLightmapSamplePosition_Marshall(t *testing.T) { + sut := DispLightmapSamplePosition{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if data[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestDispLightmapSamplePosition_Unmarshall(t *testing.T) { + sut := DispLightmapSamplePosition{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/disptris.go b/lumps/disptris.go index b4c958f..0b2542c 100644 --- a/lumps/disptris.go +++ b/lumps/disptris.go @@ -4,28 +4,27 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/disptris" - "log" "unsafe" ) // DispTris is Lump 48: DispTris type DispTris struct { - LumpGeneric + Generic data []primitives.DispTri } // Unmarshall Imports this lump from raw byte data -func (lump *DispTris) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *DispTris) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.DispTri, length/int32(unsafe.Sizeof(primitives.DispTri{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.DispTri, length/int(unsafe.Sizeof(primitives.DispTri{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + + return err } // GetData gets internal format structure data diff --git a/lumps/disptris_test.go b/lumps/disptris_test.go new file mode 100644 index 0000000..d4cf988 --- /dev/null +++ b/lumps/disptris_test.go @@ -0,0 +1,70 @@ +package lumps + +import ( + "bytes" + "encoding/binary" + "testing" +) + +func TestDispTris_GetData(t *testing.T) { + sut := DispTris{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b.Tags { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestDispTris_Marshall(t *testing.T) { + sut := DispTris{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b.Tags { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if buf.Bytes()[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestDispTris_Unmarshall(t *testing.T) { + sut := DispTris{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b.Tags { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/dispvert.go b/lumps/dispvert.go index 32067aa..201d0d5 100644 --- a/lumps/dispvert.go +++ b/lumps/dispvert.go @@ -4,28 +4,27 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/dispvert" - "log" "unsafe" ) // DispVert is Lump 33: DispVert type DispVert struct { - LumpGeneric + Generic data []primitives.DispVert } // Unmarshall Imports this lump from raw byte data -func (lump *DispVert) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *DispVert) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.DispVert, length/int32(unsafe.Sizeof(primitives.DispVert{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.DispVert, length/int(unsafe.Sizeof(primitives.DispVert{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + + return err } // GetData gets internal format structure data diff --git a/lumps/edge.go b/lumps/edge.go index 4b365bf..d3743c8 100644 --- a/lumps/edge.go +++ b/lumps/edge.go @@ -3,23 +3,25 @@ package lumps import ( "bytes" "encoding/binary" - "log" ) // Edge is Lump 12: Edge type Edge struct { - LumpGeneric + Generic data [][2]uint16 // MAX_MAP_EDGES = 256000 } // Unmarshall Imports this lump from raw byte data -func (lump *Edge) Unmarshall(raw []byte, length int32) { +func (lump *Edge) Unmarshall(raw []byte) (err error) { + length := len(raw) lump.data = make([][2]uint16, length/4) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/edge_test.go b/lumps/edge_test.go new file mode 100644 index 0000000..10c5648 --- /dev/null +++ b/lumps/edge_test.go @@ -0,0 +1 @@ +package lumps diff --git a/lumps/entdata.go b/lumps/entdata.go index 0e53a16..a2a0b41 100644 --- a/lumps/entdata.go +++ b/lumps/entdata.go @@ -2,14 +2,17 @@ package lumps // EntData is Lump 0: Entdata type EntData struct { - LumpGeneric + Generic data string } // Unmarshall Imports this lump from raw byte data -func (lump *EntData) Unmarshall(raw []byte, length int32) { +func (lump *EntData) Unmarshall(raw []byte) (err error) { lump.data = string(raw) - lump.LumpInfo.SetLength(length) + length := len(raw) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/entdata_test.go b/lumps/entdata_test.go new file mode 100644 index 0000000..e29e4dc --- /dev/null +++ b/lumps/entdata_test.go @@ -0,0 +1,43 @@ +package lumps + +import "testing" + +func TestEntData_GetData(t *testing.T) { + sut := EntData{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + if string(sut.GetData()) != string(data) { + t.Error("mismatched between expected and actual unmarshalled bytes") + } +} + +func TestEntData_Marshall(t *testing.T) { + sut := EntData{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + if string(sut.GetData()) != string(data) { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + if string(res) != string(data) { + t.Error("mismatched between expected and actual marshalled bytes") + } +} + +func TestEntData_Unmarshall(t *testing.T) { + sut := EntData{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + if string(sut.data) != string(data) { + t.Error("mismatched between expected and actual unmarshalled bytes") + } +} diff --git a/lumps/face.go b/lumps/face.go index 5444579..0aa068c 100644 --- a/lumps/face.go +++ b/lumps/face.go @@ -4,24 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/face" - "log" "unsafe" ) // Face is Lump 7: Face type Face struct { - LumpGeneric + Generic data []primitives.Face } // Unmarshall Imports this lump from raw byte data -func (lump *Face) Unmarshall(raw []byte, length int32) { - lump.data = make([]primitives.Face, length/int32(unsafe.Sizeof(primitives.Face{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) +func (lump *Face) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.data = make([]primitives.Face, length/int(unsafe.Sizeof(primitives.Face{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/facehdr.go b/lumps/facehdr.go index 188112f..a3e997d 100644 --- a/lumps/facehdr.go +++ b/lumps/facehdr.go @@ -4,24 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/face" - "log" "unsafe" ) // FaceHDR is Lump 58: FaceHDR type FaceHDR struct { - LumpGeneric + Generic data []primitives.Face } // Unmarshall Imports this lump from raw byte data -func (lump *FaceHDR) Unmarshall(raw []byte, length int32) { - lump.data = make([]primitives.Face, length/int32(unsafe.Sizeof(primitives.Face{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) +func (lump *FaceHDR) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.data = make([]primitives.Face, length/int(unsafe.Sizeof(primitives.Face{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/faceid.go b/lumps/faceid.go index bce82f7..3a540f3 100644 --- a/lumps/faceid.go +++ b/lumps/faceid.go @@ -4,27 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/faceid" - "log" "unsafe" ) // FaceId is Lump 11: FaceIds type FaceId struct { - LumpGeneric + Generic data []primitives.FaceId } // Unmarshall Imports this lump from raw byte data -func (lump *FaceId) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *FaceId) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.FaceId, length/int32(unsafe.Sizeof(primitives.FaceId{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.FaceId, length/int(unsafe.Sizeof(primitives.FaceId{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + + return err } // GetData gets internal format structure data diff --git a/lumps/faceid_test.go b/lumps/faceid_test.go new file mode 100644 index 0000000..0665a33 --- /dev/null +++ b/lumps/faceid_test.go @@ -0,0 +1,70 @@ +package lumps + +import ( + "bytes" + "encoding/binary" + "testing" +) + +func TestFaceId_GetData(t *testing.T) { + sut := FaceId{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b.HammerFaceId { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestFaceId_Marshall(t *testing.T) { + sut := FaceId{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b.HammerFaceId { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if buf.Bytes()[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestFaceId_Unmarshall(t *testing.T) { + sut := FaceId{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b.HammerFaceId { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/facemacrotextureinfo.go b/lumps/facemacrotextureinfo.go index cb8eea7..4c27667 100644 --- a/lumps/facemacrotextureinfo.go +++ b/lumps/facemacrotextureinfo.go @@ -4,28 +4,27 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/facemacrotextureinfo" - "log" "unsafe" ) // FaceMacroTextureInfo is Lump 47: FaceMacroTextureInfo type FaceMacroTextureInfo struct { - LumpGeneric + Generic data []primitives.FaceMacroTextureInfo } // Unmarshall Imports this lump from raw byte data -func (lump *FaceMacroTextureInfo) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *FaceMacroTextureInfo) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.FaceMacroTextureInfo, length/int32(unsafe.Sizeof(primitives.FaceMacroTextureInfo{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.FaceMacroTextureInfo, length/int(unsafe.Sizeof(primitives.FaceMacroTextureInfo{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + + return err } // GetData gets internal format structure data diff --git a/lumps/game.go b/lumps/game.go index f53fc2b..77361de 100644 --- a/lumps/game.go +++ b/lumps/game.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/game" - "log" "strings" "unsafe" ) @@ -13,7 +12,7 @@ import ( // @TODO NOTE: This really needs per-game implementations to be completely useful, // otherwise we only get staticprop data from it type Game struct { - LumpGeneric + Generic Header primitives.Header GameLumps []primitives.GenericGameLump LumpOffset int32 @@ -21,8 +20,9 @@ type Game struct { } // Unmarshall Imports this lump from raw byte data -func (lump *Game) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *Game) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if len(raw) == 0 { return @@ -35,9 +35,9 @@ func (lump *Game) Unmarshall(raw []byte, length int32) { // Read header lump.Header.GameLumps = make([]primitives.LumpDef, lumpCount) headerSize := 4 + (int32(unsafe.Sizeof(primitives.LumpDef{})) * int32(lumpCount)) - err := binary.Read(bytes.NewBuffer(raw[4:headerSize]), binary.LittleEndian, &lump.Header.GameLumps) + err = binary.Read(bytes.NewBuffer(raw[4:headerSize]), binary.LittleEndian, &lump.Header.GameLumps) if err != nil { - log.Fatal(err) + return err } // Correct file offsets @@ -54,6 +54,8 @@ func (lump *Game) Unmarshall(raw []byte, length int32) { lump.GameLumps[i].Length = lumpHeader.FileLength lump.GameLumps[i].Data = raw[lumpHeader.FileOffset : lumpHeader.FileOffset+lumpHeader.FileLength] } + + return err } // GetData gets internal format structure data diff --git a/lumps/interface.go b/lumps/interface.go index 4a16cea..7c0efdd 100644 --- a/lumps/interface.go +++ b/lumps/interface.go @@ -4,7 +4,7 @@ package lumps // Organise Lump data in a cleaner and more accessible manner type ILump interface { // Unmarshall Imports a []byte to a defined lump structure(s). - Unmarshall([]byte, int32) + Unmarshall([]byte) error // Marshall Exports lump structure back to []byte. Marshall() ([]byte, error) diff --git a/lumps/leaf.go b/lumps/leaf.go index ba5ed6e..9447b00 100644 --- a/lumps/leaf.go +++ b/lumps/leaf.go @@ -15,27 +15,30 @@ const ( // Leaf is Lump 10: Leaf type Leaf struct { - LumpGeneric + Generic data []primitives.Leaf } // Unmarshall Imports this lump from raw byte data -func (lump *Leaf) Unmarshall(raw []byte, length int32) { - lump.data = make([]primitives.Leaf, length/int32(unsafe.Sizeof(primitives.Leaf{}))) +func (lump *Leaf) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.data = make([]primitives.Leaf, length/int(unsafe.Sizeof(primitives.Leaf{}))) structSize := int(unsafe.Sizeof(primitives.Leaf{})) numLeafs := len(lump.data) i := 0 for i < numLeafs { - err := binary.Read(bytes.NewBuffer(raw[(structSize*i):(structSize*i)+structSize]), binary.LittleEndian, &lump.data[i]) + err = binary.Read(bytes.NewBuffer(raw[(structSize*i):(structSize*i)+structSize]), binary.LittleEndian, &lump.data[i]) if err != nil { - log.Fatal(err) + return err } i++ if i > MaxMapLeafs { log.Fatalf("Leaf count overflows maximum allowed size of %d\n", MaxMapLeafs) } } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/leaf_test.go b/lumps/leaf_test.go index b5b34da..fd7acb8 100644 --- a/lumps/leaf_test.go +++ b/lumps/leaf_test.go @@ -7,18 +7,21 @@ import ( "unsafe" ) -const C_STRUCT_SIZE = 32 +const leafStructSizeRaw = 32 // Assert leaf data when read from bytes is valid -func TestLeafDataUnmarshall(t *testing.T) { +func TestLeafUnmarshall(t *testing.T) { l := unsafe.Sizeof(primitives.Leaf{}) - if l != C_STRUCT_SIZE { - t.Errorf("Leaf struct is of incorrect size, expected: %d, actual: %d", C_STRUCT_SIZE, l) + if l != leafStructSizeRaw { + t.Errorf("Leaf struct is of incorrect size, expected: %d, actual: %d", leafStructSizeRaw, l) } lump := Leaf{} - lump.Unmarshall(GetTestDataBytes(), int32(len(GetTestDataBytes()))) + err := lump.Unmarshall(GetTestDataBytes()) + if err != nil { + t.Error(err) + } expected := GetTestLeafData() actual := lump.GetData()[0] diff --git a/lumps/leafambientindex.go b/lumps/leafambientindex.go index ede11b6..1b96598 100644 --- a/lumps/leafambientindex.go +++ b/lumps/leafambientindex.go @@ -4,27 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/leafambientindex" - "log" "unsafe" ) // LeafAmbientIndex is Lump 52: Leaf Ambient Index type LeafAmbientIndex struct { - LumpGeneric + Generic data []primitives.LeafAmbientIndex } // Unmarshall Imports this lump from raw byte data -func (lump *LeafAmbientIndex) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *LeafAmbientIndex) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.LeafAmbientIndex, length/int32(unsafe.Sizeof(primitives.LeafAmbientIndex{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.LeafAmbientIndex, length/int(unsafe.Sizeof(primitives.LeafAmbientIndex{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + + return err } // GetData gets internal format structure data diff --git a/lumps/leafambientindexhdr.go b/lumps/leafambientindexhdr.go index ee5eb94..1a7cde1 100644 --- a/lumps/leafambientindexhdr.go +++ b/lumps/leafambientindexhdr.go @@ -4,27 +4,29 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/leafambientindex" - "log" "unsafe" ) // LeafAmbientIndexHDR is Lump 51: Leaf Ambient Index HDR type LeafAmbientIndexHDR struct { - LumpGeneric + Generic data []primitives.LeafAmbientIndex } // Unmarshall Imports this lump from raw byte data -func (lump *LeafAmbientIndexHDR) Unmarshall(raw []byte, length int32) { +func (lump *LeafAmbientIndexHDR) Unmarshall(raw []byte) (err error) { + length := len(raw) if length == 0 { return } - lump.data = make([]primitives.LeafAmbientIndex, length/int32(unsafe.Sizeof(primitives.LeafAmbientIndex{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + lump.data = make([]primitives.LeafAmbientIndex, length/int(unsafe.Sizeof(primitives.LeafAmbientIndex{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/leafambientlighting.go b/lumps/leafambientlighting.go index 9762e7c..35d64f0 100644 --- a/lumps/leafambientlighting.go +++ b/lumps/leafambientlighting.go @@ -4,28 +4,30 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/leafambientlighting" - "log" "unsafe" ) // LeafAmbientLighting is Lump 56: LeafAmbientLighting type LeafAmbientLighting struct { - LumpGeneric + Generic data []primitives.LeafAmbientLighting } // Unmarshall Imports this lump from raw byte data -func (lump *LeafAmbientLighting) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *LeafAmbientLighting) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.LeafAmbientLighting, length/int32(unsafe.Sizeof(primitives.LeafAmbientLighting{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + lump.data = make([]primitives.LeafAmbientLighting, length/int(unsafe.Sizeof(primitives.LeafAmbientLighting{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/leafambientlightinghdr.go b/lumps/leafambientlightinghdr.go index 1e9fc03..87c8261 100644 --- a/lumps/leafambientlightinghdr.go +++ b/lumps/leafambientlightinghdr.go @@ -4,27 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/leafambientlighting" - "log" "unsafe" ) // LeafAmbientLightingHDR is Lump 55: LeafAmbientLightingHDR type LeafAmbientLightingHDR struct { - LumpGeneric + Generic data []primitives.LeafAmbientLighting } // Unmarshall Imports this lump from raw byte data -func (lump *LeafAmbientLightingHDR) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *LeafAmbientLightingHDR) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.LeafAmbientLighting, length/int32(unsafe.Sizeof(primitives.LeafAmbientLighting{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.LeafAmbientLighting, length/int(unsafe.Sizeof(primitives.LeafAmbientLighting{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + + return err } // GetData gets internal format structure data diff --git a/lumps/leafbrush.go b/lumps/leafbrush.go index b641a82..2aad87c 100644 --- a/lumps/leafbrush.go +++ b/lumps/leafbrush.go @@ -3,23 +3,25 @@ package lumps import ( "bytes" "encoding/binary" - "log" ) // LeafBrush is Lump 17: LeafBrush type LeafBrush struct { - LumpGeneric + Generic data []uint16 // MAX_MAP_LEAFBRUSHES = 65536 } // Unmarshall Imports this lump from raw byte data -func (lump *LeafBrush) Unmarshall(raw []byte, length int32) { +func (lump *LeafBrush) Unmarshall(raw []byte) (err error) { + length := len(raw) lump.data = make([]uint16, length/2) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/leafbrush_test.go b/lumps/leafbrush_test.go new file mode 100644 index 0000000..9cf8e06 --- /dev/null +++ b/lumps/leafbrush_test.go @@ -0,0 +1,70 @@ +package lumps + +import ( + "bytes" + "encoding/binary" + "testing" +) + +func TestLeafBrush_GetData(t *testing.T) { + sut := LeafBrush{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestLeafBrush_Marshall(t *testing.T) { + sut := LeafBrush{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if buf.Bytes()[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestLeafBrush_Unmarshall(t *testing.T) { + sut := LeafBrush{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/leafface.go b/lumps/leafface.go index d943fbc..c4ae8de 100644 --- a/lumps/leafface.go +++ b/lumps/leafface.go @@ -3,23 +3,25 @@ package lumps import ( "bytes" "encoding/binary" - "log" ) // LeafFace is Lump 16: LeafFace type LeafFace struct { - LumpGeneric + Generic data []uint16 // MAX_MAP_LEAFFACES = 65536 } // Unmarshall Imports this lump from raw byte data -func (lump *LeafFace) Unmarshall(raw []byte, length int32) { +func (lump *LeafFace) Unmarshall(raw []byte) (err error) { + length := len(raw) lump.data = make([]uint16, length/2) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/leafface_test.go b/lumps/leafface_test.go new file mode 100644 index 0000000..9d6655a --- /dev/null +++ b/lumps/leafface_test.go @@ -0,0 +1,70 @@ +package lumps + +import ( + "bytes" + "encoding/binary" + "testing" +) + +func TestLeafFace_GetData(t *testing.T) { + sut := LeafFace{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestLeafFace_Marshall(t *testing.T) { + sut := LeafFace{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if buf.Bytes()[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestLeafFace_Unmarshall(t *testing.T) { + sut := LeafFace{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/leafmindisttowater.go b/lumps/leafmindisttowater.go index 2f69e9a..e2b0d22 100644 --- a/lumps/leafmindisttowater.go +++ b/lumps/leafmindisttowater.go @@ -3,23 +3,25 @@ package lumps import ( "bytes" "encoding/binary" - "log" ) // LeafMinDistToWater is Lump 46: LeafMinDistToWater type LeafMinDistToWater struct { - LumpGeneric + Generic data []uint16 } // Unmarshall Imports this lump from raw byte data -func (lump *LeafMinDistToWater) Unmarshall(raw []byte, length int32) { - lump.data = make([]uint16, length/int32(2)) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) +func (lump *LeafMinDistToWater) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.data = make([]uint16, length/2) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/leafmindisttowater_test.go b/lumps/leafmindisttowater_test.go new file mode 100644 index 0000000..6d27e44 --- /dev/null +++ b/lumps/leafmindisttowater_test.go @@ -0,0 +1,70 @@ +package lumps + +import ( + "bytes" + "encoding/binary" + "testing" +) + +func TestLeafMinDistToWater_GetData(t *testing.T) { + sut := LeafMinDistToWater{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestLeafMinDistToWater_Marshall(t *testing.T) { + sut := LeafMinDistToWater{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if buf.Bytes()[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestLeafMinDistToWater_Unmarshall(t *testing.T) { + sut := LeafMinDistToWater{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/leafwaterdata.go b/lumps/leafwaterdata.go index 92b0e5e..d6ce4d5 100644 --- a/lumps/leafwaterdata.go +++ b/lumps/leafwaterdata.go @@ -4,28 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/leafwaterdata" - "log" "unsafe" ) // LeafWaterData is Lump 36: leafwaterdata type LeafWaterData struct { - LumpGeneric + Generic data []primitives.LeafWaterData } // Unmarshall Imports this lump from raw byte data -func (lump *LeafWaterData) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *LeafWaterData) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.LeafWaterData, length/int32(unsafe.Sizeof(primitives.LeafWaterData{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.LeafWaterData, length/int(unsafe.Sizeof(primitives.LeafWaterData{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + return err } // GetData gets internal format structure data diff --git a/lumps/lighting.go b/lumps/lighting.go index 48e3721..e4c18b0 100644 --- a/lumps/lighting.go +++ b/lumps/lighting.go @@ -4,27 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/common" - "log" "unsafe" ) // Lighting is Lump 8: Lighting type Lighting struct { - LumpGeneric + Generic data []primitives.ColorRGBExponent32 } // Unmarshall Imports this lump from raw byte data -func (lump *Lighting) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *Lighting) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.ColorRGBExponent32, length/int32(unsafe.Sizeof(primitives.ColorRGBExponent32{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.ColorRGBExponent32, length/int(unsafe.Sizeof(primitives.ColorRGBExponent32{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + + return err } // GetData gets internal format structure data diff --git a/lumps/lump.go b/lumps/lump.go index 547065a..67f58e0 100644 --- a/lumps/lump.go +++ b/lumps/lump.go @@ -1,41 +1,48 @@ package lumps -// LumpGeneric is a raw byte lump +// Generic is a raw byte lump // It should be used in cases where the data has no or unknown // structures. -type LumpGeneric struct { - LumpInfo +type Generic struct { + Metadata data []byte } // Unmarshall Imports this lump from raw byte data -func (lump *LumpGeneric) Unmarshall(data []byte, length int32) { - lump.length = length +func (lump *Generic) Unmarshall(data []byte) (err error) { + lump.length = len(data) lump.data = data + + return err } // Marshall Dumps this lump back to raw byte data -func (lump *LumpGeneric) Marshall() ([]byte, error) { +func (lump *Generic) Marshall() ([]byte, error) { return lump.data, nil } -// LumpInfo is a Helper info for a lump -type LumpInfo struct { - length int32 +// Metadata is a Helper info for a lump +type Metadata struct { + length int version int32 } -// GetLength Returns lump import length in bytes. -func (info LumpInfo) GetLength() int32 { +// Length Returns lump import length in bytes. +func (info *Metadata) Length() int { return info.length } // SetLength sets lump import length in bytes -func (info LumpInfo) SetLength(length int32) { +func (info *Metadata) SetLength(length int) { info.length = length } +// Version Returns lump import version in bytes. +func (info *Metadata) Version() int32 { + return info.version +} + // SetVersion sets bsp version of lump -func (info LumpInfo) SetVersion(version int32) { +func (info *Metadata) SetVersion(version int32) { info.version = version } diff --git a/lumps/lump_test.go b/lumps/lump_test.go new file mode 100644 index 0000000..894c7c4 --- /dev/null +++ b/lumps/lump_test.go @@ -0,0 +1,74 @@ +package lumps + +import ( + "log" + "testing" +) + +func TestGeneric_Marshall(t *testing.T) { + sut := Generic{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if data[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestGeneric_Unmarshall(t *testing.T) { + sut := Generic{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestMetadata_Length(t *testing.T) { + sut := Metadata{} + sut.SetLength(32) + if sut.Length() != 32 { + t.Errorf("wrong length returned fron Length") + } +} + +func TestMetadata_SetLength(t *testing.T) { + sut := Metadata{} + sut.SetLength(32) + if sut.Length() != 32 { + log.Println(sut.length) + t.Errorf("wrong length returned fron Length") + } +} + +func TestMetadata_Version(t *testing.T) { + sut := Metadata{} + sut.SetVersion(11) + if sut.Version() != 11 { + t.Errorf("wrong version set") + } +} + +func TestMetadata_SetVersion(t *testing.T) { + sut := Metadata{} + sut.SetVersion(11) + if sut.version != 11 { + t.Errorf("wrong version set") + } +} diff --git a/lumps/mapflags.go b/lumps/mapflags.go index 41af314..8c906dd 100644 --- a/lumps/mapflags.go +++ b/lumps/mapflags.go @@ -4,26 +4,28 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/mapflags" - "log" ) // MapFlags is Lump 59: MapFlags type MapFlags struct { - LumpGeneric + Generic data primitives.MapFlags } // Unmarshall Imports this lump from raw byte data -func (lump *MapFlags) Unmarshall(raw []byte, length int32) { +func (lump *MapFlags) Unmarshall(raw []byte) (err error) { + length := len(raw) if length == 0 { return } - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/mapflags_test.go b/lumps/mapflags_test.go new file mode 100644 index 0000000..c0eaeaf --- /dev/null +++ b/lumps/mapflags_test.go @@ -0,0 +1,75 @@ +package lumps + +import ( + "bytes" + "encoding/binary" + "github.com/galaco/bsp/primitives/mapflags" + "testing" +) + +func TestMapFlags_GetData(t *testing.T) { + sut := MapFlags{} + data := mapflags.MapFlags{ + LevelFlags: 523423, + } + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + if sut.GetData().LevelFlags != data.LevelFlags { + t.Error("mismatched between expected and actual unmarshalled bytes") + } +} + +func TestMapFlags_Marshall(t *testing.T) { + sut := MapFlags{} + data := mapflags.MapFlags{ + LevelFlags: 523423, + } + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + if sut.data.LevelFlags != data.LevelFlags { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + res, err := sut.Marshall() + if err != nil { + t.Error(err) + } + for idx, b := range res { + if buf.Bytes()[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } + +} + +func TestMapFlags_Unmarshall(t *testing.T) { + sut := MapFlags{} + data := mapflags.MapFlags{ + LevelFlags: 523423, + } + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + if sut.data.LevelFlags != data.LevelFlags { + t.Error("mismatched between expected and actual unmarshalled bytes") + } +} diff --git a/lumps/model.go b/lumps/model.go index b5cd4ba..99edabb 100644 --- a/lumps/model.go +++ b/lumps/model.go @@ -4,24 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/model" - "log" "unsafe" ) // Model is Lump 14: Model type Model struct { - LumpGeneric + Generic data []primitives.Model } // Unmarshall Imports this lump from raw byte data -func (lump *Model) Unmarshall(raw []byte, length int32) { - lump.data = make([]primitives.Model, length/int32(unsafe.Sizeof(primitives.Model{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) +func (lump *Model) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.data = make([]primitives.Model, length/int(unsafe.Sizeof(primitives.Model{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/node.go b/lumps/node.go index 0275611..4c770ba 100644 --- a/lumps/node.go +++ b/lumps/node.go @@ -4,24 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/node" - "log" "unsafe" ) // Node is Lump 5: Node type Node struct { - LumpGeneric + Generic data []primitives.Node // MAP_MAX_NODES = 65536 } // Unmarshall Imports this lump from raw byte data -func (lump *Node) Unmarshall(raw []byte, length int32) { - lump.data = make([]primitives.Node, length/int32(unsafe.Sizeof(primitives.Node{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) +func (lump *Node) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.data = make([]primitives.Node, length/int(unsafe.Sizeof(primitives.Node{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/occlusion.go b/lumps/occlusion.go index 4849474..0780d02 100644 --- a/lumps/occlusion.go +++ b/lumps/occlusion.go @@ -4,13 +4,12 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/occlusion" - "log" "unsafe" ) // Occlusion is Lump 9: Occlusion type Occlusion struct { - LumpGeneric + Generic Count int32 Data []primitives.OcclusionData // len(slice) = Count PolyDataCount int32 @@ -20,50 +19,53 @@ type Occlusion struct { } // Unmarshall Imports this lump from raw byte data -func (lump *Occlusion) Unmarshall(raw []byte, length int32) { +func (lump *Occlusion) Unmarshall(raw []byte) (err error) { + length := len(raw) if length == 0 { return } offset := 0 // data - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.Count) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.Count) if err != nil { - log.Fatal(err) + return err } offset += 4 lump.Data = make([]primitives.OcclusionData, lump.Count) err = binary.Read(bytes.NewBuffer(raw[offset:]), binary.LittleEndian, &lump.Data) if err != nil { - log.Fatal(err) + return err } offset += int(unsafe.Sizeof(primitives.OcclusionData{})) * int(lump.Count) // polydata err = binary.Read(bytes.NewBuffer(raw[offset:]), binary.LittleEndian, &lump.PolyDataCount) if err != nil { - log.Fatal(err) + return err } offset += 4 lump.PolyData = make([]primitives.OcclusionPolyData, lump.PolyDataCount) err = binary.Read(bytes.NewBuffer(raw[offset:]), binary.LittleEndian, &lump.PolyData) if err != nil { - log.Fatal(err) + return err } offset += int(unsafe.Sizeof(primitives.OcclusionPolyData{})) * int(lump.PolyDataCount) // vertexdata err = binary.Read(bytes.NewBuffer(raw[offset:]), binary.LittleEndian, &lump.VertexIndexCount) if err != nil { - log.Fatal(err) + return err } offset += 4 lump.VertexIndices = make([]int32, lump.VertexIndexCount) err = binary.Read(bytes.NewBuffer(raw[offset:]), binary.LittleEndian, &lump.VertexIndices) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/overlay.go b/lumps/overlay.go index f5fd30e..0600286 100644 --- a/lumps/overlay.go +++ b/lumps/overlay.go @@ -4,28 +4,27 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/overlay" - "log" "unsafe" ) // Overlay is Lump 45: Overlay // Consists of an array of Overlay structs type Overlay struct { - LumpGeneric + Generic data []primitives.Overlay } // Unmarshall Imports this lump from raw byte data -func (lump *Overlay) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *Overlay) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.Overlay, length/int32(unsafe.Sizeof(primitives.Overlay{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.Overlay, length/int(unsafe.Sizeof(primitives.Overlay{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + + return err } // GetData gets internal format structure data diff --git a/lumps/overlayfade.go b/lumps/overlayfade.go index 8150436..2d3fbdf 100644 --- a/lumps/overlayfade.go +++ b/lumps/overlayfade.go @@ -4,27 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/overlayfade" - "log" "unsafe" ) // OverlayFade is Lump 60: Overlayfades type OverlayFade struct { - LumpGeneric + Generic data []primitives.OverlayFade } // Unmarshall Imports this lump from raw byte data -func (lump *OverlayFade) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *OverlayFade) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.OverlayFade, length/int32(unsafe.Sizeof(primitives.OverlayFade{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.OverlayFade, length/int(unsafe.Sizeof(primitives.OverlayFade{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + + return err } // GetData gets internal format structure data diff --git a/lumps/pakfile.go b/lumps/pakfile.go index 95b0daf..84d43aa 100644 --- a/lumps/pakfile.go +++ b/lumps/pakfile.go @@ -10,20 +10,23 @@ import ( // Pakfile is Lump 40: Pakfile type Pakfile struct { - LumpGeneric + Generic zipReader *zip.Reader } // Unmarshall Imports this lump from raw byte data -func (lump *Pakfile) Unmarshall(raw []byte, length int32) { +func (lump *Pakfile) Unmarshall(raw []byte) (err error) { + length := len(raw) lump.data = raw - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) b := bytes.NewReader(raw) zipReader, err := zip.NewReader(b, int64(length)) if err == nil { lump.zipReader = zipReader } + + return err } // GetData GetData gets internal format structure data diff --git a/lumps/physcollide.go b/lumps/physcollide.go index 3701b95..0962525 100644 --- a/lumps/physcollide.go +++ b/lumps/physcollide.go @@ -4,28 +4,27 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/physcollide" - "log" "unsafe" ) // PhysCollide is Lump 20: PhysCollide type PhysCollide struct { - LumpGeneric + Generic data []primitives.PhysCollideEntry } // Unmarshall Imports this lump from raw byte data -func (lump *PhysCollide) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *PhysCollide) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.PhysCollideEntry, length/int32(unsafe.Sizeof(primitives.PhysCollideEntry{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.PhysCollideEntry, length/int(unsafe.Sizeof(primitives.PhysCollideEntry{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + + return err } // GetData gets internal format structure data diff --git a/lumps/physdisp.go b/lumps/physdisp.go index 911ccc3..d45998e 100644 --- a/lumps/physdisp.go +++ b/lumps/physdisp.go @@ -2,14 +2,17 @@ package lumps // PhysDisp is Lump 28: PhysDisp type PhysDisp struct { - LumpGeneric + Generic data []byte } // Unmarshall Imports this lump from raw byte data -func (lump *PhysDisp) Unmarshall(raw []byte, length int32) { +func (lump *PhysDisp) Unmarshall(raw []byte) (err error) { + length := len(raw) lump.data = raw - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/physdisp_test.go b/lumps/physdisp_test.go new file mode 100644 index 0000000..637d7b1 --- /dev/null +++ b/lumps/physdisp_test.go @@ -0,0 +1,51 @@ +package lumps + +import "testing" + +func TestPhysDisp_GetData(t *testing.T) { + sut := PhysDisp{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestPhysDisp_Marshall(t *testing.T) { + sut := PhysDisp{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if data[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestPhysDisp_Unmarshall(t *testing.T) { + sut := PhysDisp{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/planes.go b/lumps/planes.go index fdbce95..801b640 100644 --- a/lumps/planes.go +++ b/lumps/planes.go @@ -4,24 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/plane" - "log" "unsafe" ) // Planes is Lump 1: Planes type Planes struct { - LumpGeneric + Generic data []primitives.Plane // MAP_MAX_PLANES = 65536 } // Unmarshall Imports this lump from raw byte data -func (lump *Planes) Unmarshall(raw []byte, length int32) { - lump.data = make([]primitives.Plane, length/int32(unsafe.Sizeof(primitives.Plane{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) +func (lump *Planes) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.data = make([]primitives.Plane, length/int(unsafe.Sizeof(primitives.Plane{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/primindice.go b/lumps/primindice.go index 24e0320..044ce48 100644 --- a/lumps/primindice.go +++ b/lumps/primindice.go @@ -3,27 +3,26 @@ package lumps import ( "bytes" "encoding/binary" - "log" ) // PrimIndice is Lump 39: PrimIndice type PrimIndice struct { - LumpGeneric + Generic data []uint16 } // Unmarshall Imports this lump from raw byte data -func (lump *PrimIndice) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *PrimIndice) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]uint16, length/int32(2)) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]uint16, length/2) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + + return err } // GetData gets internal format structure data diff --git a/lumps/primindice_test.go b/lumps/primindice_test.go new file mode 100644 index 0000000..6bd5ca7 --- /dev/null +++ b/lumps/primindice_test.go @@ -0,0 +1,70 @@ +package lumps + +import ( + "bytes" + "encoding/binary" + "testing" +) + +func TestPrimIndice_GetData(t *testing.T) { + sut := PrimIndice{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestPrimIndice_Marshall(t *testing.T) { + sut := PrimIndice{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if buf.Bytes()[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestPrimIndice_Unmarshall(t *testing.T) { + sut := PrimIndice{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/primitive.go b/lumps/primitive.go index 2e26fab..392e0d7 100644 --- a/lumps/primitive.go +++ b/lumps/primitive.go @@ -4,28 +4,27 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/primitive" - "log" "unsafe" ) // Primitive is Lump 36: Primitive type Primitive struct { - LumpGeneric + Generic data []primitives.Primitive } // Unmarshall Imports this lump from raw byte data -func (lump *Primitive) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *Primitive) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.Primitive, length/int32(unsafe.Sizeof(primitives.Primitive{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.Primitive, length/int(unsafe.Sizeof(primitives.Primitive{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + + return err } // GetData gets internal format structure data diff --git a/lumps/primvert.go b/lumps/primvert.go index a7febb0..4298500 100644 --- a/lumps/primvert.go +++ b/lumps/primvert.go @@ -4,28 +4,27 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/primvert" - "log" "unsafe" ) // PrimVert is Lump 37: PrimVert type PrimVert struct { - LumpGeneric + Generic data []primitives.PrimVert } // Unmarshall Imports this lump from raw byte data -func (lump *PrimVert) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *PrimVert) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.PrimVert, length/int32(unsafe.Sizeof(primitives.PrimVert{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.PrimVert, length/int(unsafe.Sizeof(primitives.PrimVert{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + + return err } // GetData gets internal format structure data diff --git a/lumps/primvert_test.go b/lumps/primvert_test.go new file mode 100644 index 0000000..6a3742d --- /dev/null +++ b/lumps/primvert_test.go @@ -0,0 +1,80 @@ +package lumps + +import ( + "bytes" + "encoding/binary" + "github.com/go-gl/mathgl/mgl32" + "testing" +) + +func TestPrimVert_GetData(t *testing.T) { + sut := PrimVert{} + data := []mgl32.Vec3{ + {0, 1, 2}, + {5, 43, 156}, + } + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b.Pos { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestPrimVert_Marshall(t *testing.T) { + sut := PrimVert{} + data := []mgl32.Vec3{ + {0, 1, 2}, + {5, 43, 156}, + } + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b.Pos { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if buf.Bytes()[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestPrimVert_Unmarshall(t *testing.T) { + sut := PrimVert{} + data := []mgl32.Vec3{ + {0, 1, 2}, + {5, 43, 156}, + } + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b.Pos { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/surfedge.go b/lumps/surfedge.go index c4318a8..e084f0d 100644 --- a/lumps/surfedge.go +++ b/lumps/surfedge.go @@ -3,23 +3,25 @@ package lumps import ( "bytes" "encoding/binary" - "log" ) // Surfedge is Lump 13: Surfedge type Surfedge struct { - LumpGeneric + Generic data []int32 // MAX_MAP_SURFEDGES = 512000 } // Unmarshall Imports this lump from raw byte data -func (lump *Surfedge) Unmarshall(raw []byte, length int32) { +func (lump *Surfedge) Unmarshall(raw []byte) (err error) { + length := len(raw) lump.data = make([]int32, length/4) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/surfedge_test.go b/lumps/surfedge_test.go new file mode 100644 index 0000000..bb953d1 --- /dev/null +++ b/lumps/surfedge_test.go @@ -0,0 +1,70 @@ +package lumps + +import ( + "bytes" + "encoding/binary" + "testing" +) + +func TestSurfedge_GetData(t *testing.T) { + sut := Surfedge{} + data := []int32{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestSurfedge_Marshall(t *testing.T) { + sut := Surfedge{} + data := []int32{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if buf.Bytes()[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestSurfedge_Unmarshall(t *testing.T) { + sut := Surfedge{} + data := []int32{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/texdata.go b/lumps/texdata.go index fcada24..a80ab33 100644 --- a/lumps/texdata.go +++ b/lumps/texdata.go @@ -4,24 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/texdata" - "log" "unsafe" ) // TexData is Lump 2: TexData type TexData struct { - LumpGeneric + Generic data []primitives.TexData } // Unmarshall Imports this lump from raw byte data -func (lump *TexData) Unmarshall(raw []byte, length int32) { - lump.data = make([]primitives.TexData, length/int32(unsafe.Sizeof(primitives.TexData{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) +func (lump *TexData) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.data = make([]primitives.TexData, length/int(unsafe.Sizeof(primitives.TexData{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/texdatastringdata.go b/lumps/texdatastringdata.go index a08e724..00021bb 100644 --- a/lumps/texdatastringdata.go +++ b/lumps/texdatastringdata.go @@ -2,14 +2,17 @@ package lumps // TexDataStringData is Lump 43: TexDataStringData type TexDataStringData struct { - LumpGeneric + Generic data string // MAX_MAP_TEXDATA_STRING_DATA = 256000, TEXTURE_NAME_LENGTH = 128 } // Unmarshall Imports this lump from raw byte data -func (lump *TexDataStringData) Unmarshall(raw []byte, length int32) { +func (lump *TexDataStringData) Unmarshall(raw []byte) (err error) { + length := len(raw) lump.data = string(raw) - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/texdatastringdata_test.go b/lumps/texdatastringdata_test.go new file mode 100644 index 0000000..7830ed6 --- /dev/null +++ b/lumps/texdatastringdata_test.go @@ -0,0 +1,43 @@ +package lumps + +import "testing" + +func TestTexDataStringData_GetData(t *testing.T) { + sut := TexDataStringData{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + if string(sut.GetData()) != string(data) { + t.Error("mismatched between expected and actual unmarshalled bytes") + } +} + +func TestTexDataStringData_Marshall(t *testing.T) { + sut := TexDataStringData{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + if string(sut.GetData()) != string(data) { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + if string(res) != string(data) { + t.Error("mismatched between expected and actual marshalled bytes") + } +} + +func TestTexDataStringData_Unmarshall(t *testing.T) { + sut := TexDataStringData{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + if string(sut.data) != string(data) { + t.Error("mismatched between expected and actual unmarshalled bytes") + } +} diff --git a/lumps/texdatastringtable.go b/lumps/texdatastringtable.go index 9b86cca..687db6c 100644 --- a/lumps/texdatastringtable.go +++ b/lumps/texdatastringtable.go @@ -3,23 +3,25 @@ package lumps import ( "bytes" "encoding/binary" - "log" ) // TexDataStringTable is Lump 44: TexDataStringTable type TexDataStringTable struct { - LumpGeneric + Generic data []int32 // MAX_MAP_TEXINFO = 2048 } // Unmarshall Imports this lump from raw byte data -func (lump *TexDataStringTable) Unmarshall(raw []byte, length int32) { +func (lump *TexDataStringTable) Unmarshall(raw []byte) (err error) { + length := len(raw) lump.data = make([]int32, length/4) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return nil } // GetData gets internal format structure data diff --git a/lumps/texdatastringtable_test.go b/lumps/texdatastringtable_test.go new file mode 100644 index 0000000..65b8e54 --- /dev/null +++ b/lumps/texdatastringtable_test.go @@ -0,0 +1,70 @@ +package lumps + +import ( + "bytes" + "encoding/binary" + "testing" +) + +func TestTexDataStringTable_GetData(t *testing.T) { + sut := TexDataStringTable{} + data := []int32{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestTexDataStringTable_Marshall(t *testing.T) { + sut := TexDataStringTable{} + data := []int32{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if buf.Bytes()[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestTexDataStringTable_Unmarshall(t *testing.T) { + sut := TexDataStringTable{} + data := []int32{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/texinfo.go b/lumps/texinfo.go index 19cf73c..a8a9283 100644 --- a/lumps/texinfo.go +++ b/lumps/texinfo.go @@ -4,24 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/texinfo" - "log" "unsafe" ) // TexInfo is Lump 6: TexInfo type TexInfo struct { - LumpGeneric + Generic data []primitives.TexInfo } // Unmarshall Imports this lump from raw byte data -func (lump *TexInfo) Unmarshall(raw []byte, length int32) { - lump.data = make([]primitives.TexInfo, length/int32(unsafe.Sizeof(primitives.TexInfo{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) +func (lump *TexInfo) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.data = make([]primitives.TexInfo, length/int(unsafe.Sizeof(primitives.TexInfo{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/unimplemented.go b/lumps/unimplemented.go index 1e00969..4269a5a 100644 --- a/lumps/unimplemented.go +++ b/lumps/unimplemented.go @@ -2,14 +2,17 @@ package lumps // Unimplemented is Lump n: Unimplemented lump type type Unimplemented struct { - LumpGeneric + Generic data []byte } // Unmarshall Imports this lump from raw byte data -func (lump *Unimplemented) Unmarshall(raw []byte, length int32) { +func (lump *Unimplemented) Unmarshall(raw []byte) (err error) { + length := len(raw) lump.data = raw - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return err } // GetData gets internal format structure data diff --git a/lumps/unimplemented_test.go b/lumps/unimplemented_test.go new file mode 100644 index 0000000..cf50f12 --- /dev/null +++ b/lumps/unimplemented_test.go @@ -0,0 +1,51 @@ +package lumps + +import "testing" + +func TestUnimplemented_GetData(t *testing.T) { + sut := Unimplemented{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestUnimplemented_Marshall(t *testing.T) { + sut := Unimplemented{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if data[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestUnimplemented_Unmarshall(t *testing.T) { + sut := Unimplemented{} + data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + if err := sut.Unmarshall(data); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/vertex.go b/lumps/vertex.go index 26840d7..220969a 100644 --- a/lumps/vertex.go +++ b/lumps/vertex.go @@ -4,27 +4,25 @@ import ( "bytes" "encoding/binary" "github.com/go-gl/mathgl/mgl32" - "log" "unsafe" ) // Vertex is Lump 3: Vertex type Vertex struct { - LumpGeneric + Generic data []mgl32.Vec3 } // Unmarshall Imports this lump from raw byte data -func (lump *Vertex) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *Vertex) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]mgl32.Vec3, length/int32(unsafe.Sizeof(mgl32.Vec3{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]mgl32.Vec3, length/int(unsafe.Sizeof(mgl32.Vec3{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + return err } // GetData gets internal format structure data diff --git a/lumps/vertex_test.go b/lumps/vertex_test.go new file mode 100644 index 0000000..7d1db0c --- /dev/null +++ b/lumps/vertex_test.go @@ -0,0 +1,80 @@ +package lumps + +import ( + "bytes" + "encoding/binary" + "github.com/go-gl/mathgl/mgl32" + "testing" +) + +func TestVertex_GetData(t *testing.T) { + sut := Vertex{} + data := []mgl32.Vec3{ + {0, 1, 2}, + {5, 43, 156}, + } + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestVertex_Marshall(t *testing.T) { + sut := Vertex{} + data := []mgl32.Vec3{ + {0, 1, 2}, + {5, 43, 156}, + } + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if buf.Bytes()[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestVertex_Unmarshall(t *testing.T) { + sut := Vertex{} + data := []mgl32.Vec3{ + {0, 1, 2}, + {5, 43, 156}, + } + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/vertnormal.go b/lumps/vertnormal.go index 8450ec1..2977da7 100644 --- a/lumps/vertnormal.go +++ b/lumps/vertnormal.go @@ -4,28 +4,26 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/vertnormal" - "log" "unsafe" ) // VertNormal is Lump 30: VertNormal type VertNormal struct { - LumpGeneric + Generic data []primitives.VertNormal } // Unmarshall Imports this lump from raw byte data -func (lump *VertNormal) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *VertNormal) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.VertNormal, length/int32(unsafe.Sizeof(primitives.VertNormal{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.VertNormal, length/int(unsafe.Sizeof(primitives.VertNormal{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + return err } // GetData gets internal format structure data diff --git a/lumps/vertnormal_test.go b/lumps/vertnormal_test.go new file mode 100644 index 0000000..b145118 --- /dev/null +++ b/lumps/vertnormal_test.go @@ -0,0 +1,80 @@ +package lumps + +import ( + "bytes" + "encoding/binary" + "github.com/go-gl/mathgl/mgl32" + "testing" +) + +func TestVertNormal_GetData(t *testing.T) { + sut := VertNormal{} + data := []mgl32.Vec3{ + {0, 1, 2}, + {5, 43, 156}, + } + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b.Pos { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestVertNormal_Marshall(t *testing.T) { + sut := VertNormal{} + data := []mgl32.Vec3{ + {0, 1, 2}, + {5, 43, 156}, + } + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b.Pos { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if buf.Bytes()[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestVertNormal_Unmarshall(t *testing.T) { + sut := VertNormal{} + data := []mgl32.Vec3{ + {0, 1, 2}, + {5, 43, 156}, + } + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b.Pos { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/vertnormalindice.go b/lumps/vertnormalindice.go index ca4815f..97af173 100644 --- a/lumps/vertnormalindice.go +++ b/lumps/vertnormalindice.go @@ -3,27 +3,25 @@ package lumps import ( "bytes" "encoding/binary" - "log" ) // VertNormalIndice is Lump 31: VertNormalIndice type VertNormalIndice struct { - LumpGeneric + Generic data []uint16 } // Unmarshall Imports this lump from raw byte data -func (lump *VertNormalIndice) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *VertNormalIndice) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]uint16, length/int32(2)) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]uint16, length/2) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + return err } // GetData gets internal format structure data diff --git a/lumps/vertnormalindice_test.go b/lumps/vertnormalindice_test.go new file mode 100644 index 0000000..10e5ff5 --- /dev/null +++ b/lumps/vertnormalindice_test.go @@ -0,0 +1,70 @@ +package lumps + +import ( + "bytes" + "encoding/binary" + "testing" +) + +func TestVertNormalIndice_GetData(t *testing.T) { + sut := VertNormalIndice{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.GetData() { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} + +func TestVertNormalIndice_Marshall(t *testing.T) { + sut := VertNormalIndice{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } + res, err := sut.Marshall() + if err != nil { + t.Errorf("unexpected error during marshalling") + } + for idx, b := range res { + if buf.Bytes()[idx] != b { + t.Error("mismatched between expected and actual marshalled bytes") + } + } +} + +func TestVertNormalIndice_Unmarshall(t *testing.T) { + sut := VertNormalIndice{} + data := []uint16{0, 1, 2, 5, 43, 156, 146, 3, 3, 6} + var buf bytes.Buffer + err := binary.Write(&buf, binary.LittleEndian, data) + if err != nil { + t.Error(err) + } + if err := sut.Unmarshall(buf.Bytes()); err != nil { + t.Error(err) + } + for idx, b := range sut.data { + if data[idx] != b { + t.Error("mismatched between expected and actual unmarshalled bytes") + } + } +} diff --git a/lumps/visibility.go b/lumps/visibility.go index 290d8c4..a4c9085 100644 --- a/lumps/visibility.go +++ b/lumps/visibility.go @@ -4,33 +4,35 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/visibility" - "log" ) // Visibility is Lump 4: Visibility type Visibility struct { - LumpGeneric + Generic data primitives.Vis } // Unmarshall Imports this lump from raw byte data -func (lump *Visibility) Unmarshall(raw []byte, length int32) { - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data.NumClusters) +func (lump *Visibility) Unmarshall(raw []byte) (err error) { + length := len(raw) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data.NumClusters) if err != nil { - log.Fatal(err) + return err } lump.data.ByteOffset = make([][2]int32, lump.data.NumClusters) err = binary.Read(bytes.NewBuffer(raw[4:]), binary.LittleEndian, &lump.data.ByteOffset) if err != nil { - log.Fatal(err) + return err } lump.data.BitVectors = make([]byte, length) err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data.BitVectors) if err != nil { - log.Fatal(err) + return err } - lump.LumpInfo.SetLength(length) + lump.Metadata.SetLength(length) + + return nil } // GetData gets internal format structure data diff --git a/lumps/worldlight.go b/lumps/worldlight.go index fb4f139..46cf9d7 100644 --- a/lumps/worldlight.go +++ b/lumps/worldlight.go @@ -4,27 +4,25 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/worldlight" - "log" "unsafe" ) // WorldLight is Lump 15: Worldlight type WorldLight struct { - LumpGeneric + Generic data []primitives.WorldLight } // Unmarshall Imports this lump from raw byte data -func (lump *WorldLight) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *WorldLight) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.WorldLight, length/int32(unsafe.Sizeof(primitives.WorldLight{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.WorldLight, length/int(unsafe.Sizeof(primitives.WorldLight{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + return err } // GetData gets internal format structure data diff --git a/lumps/worldlighthdr.go b/lumps/worldlighthdr.go index c168636..8fcc99c 100644 --- a/lumps/worldlighthdr.go +++ b/lumps/worldlighthdr.go @@ -4,27 +4,25 @@ import ( "bytes" "encoding/binary" primitives "github.com/galaco/bsp/primitives/worldlight" - "log" "unsafe" ) // WorldLightHDR is Lump 15: Worldlight type WorldLightHDR struct { - LumpGeneric + Generic data []primitives.WorldLight } // Unmarshall Imports this lump from raw byte data -func (lump *WorldLightHDR) Unmarshall(raw []byte, length int32) { - lump.LumpInfo.SetLength(length) +func (lump *WorldLightHDR) Unmarshall(raw []byte) (err error) { + length := len(raw) + lump.Metadata.SetLength(length) if length == 0 { return } - lump.data = make([]primitives.WorldLight, length/int32(unsafe.Sizeof(primitives.WorldLight{}))) - err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) - if err != nil { - log.Fatal(err) - } + lump.data = make([]primitives.WorldLight, length/int(unsafe.Sizeof(primitives.WorldLight{}))) + err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data) + return err } // GetData gets internal format structure data diff --git a/primitives/game/staticpropv10.go b/primitives/game/staticpropv10.go index 0723d0a..99035e9 100644 --- a/primitives/game/staticpropv10.go +++ b/primitives/game/staticpropv10.go @@ -4,7 +4,7 @@ import ( "github.com/go-gl/mathgl/mgl32" ) -// StaticProp v10 type +// StaticPropV10 v10 type // v10 is the model prolific format, most of Valves games use v10 type StaticPropV10 struct { Origin mgl32.Vec3 @@ -28,103 +28,117 @@ type StaticPropV10 struct { //DisableXBox360 bool } -// Origin of object in world +// GetOrigin Origin of object in world func (l *StaticPropV10) GetOrigin() mgl32.Vec3 { return l.Origin } -// Rotation of object in world +// GetAngles Rotation of object in world func (l *StaticPropV10) GetAngles() mgl32.Vec3 { return l.Angles } +// GetUniformScale Uniform scaling of prop (added in this version) // Not defined in v10 func (l *StaticPropV10) GetUniformScale() float32 { return 1 } +// GetPropType prop type func (l *StaticPropV10) GetPropType() uint16 { return l.PropType } -// Index into StaticPropLeafLump +// GetFirstLeaf Index into StaticPropLeafLump func (l *StaticPropV10) GetFirstLeaf() uint16 { return l.FirstLeaf } -// Number of leafs this prop is in +// GetLeafCount Number of leafs this prop is in func (l *StaticPropV10) GetLeafCount() uint16 { return l.LeafCount } +// GetSolid is solid func (l *StaticPropV10) GetSolid() uint8 { return l.Solid } +// GetFlags staticprop flags func (l *StaticPropV10) GetFlags() uint8 { return l.Flags } -// Skin index of this prop +// GetSkin skin index (default 0) func (l *StaticPropV10) GetSkin() int32 { return l.Skin } -// Distance from prop that it starts to fade +// GetFadeMinDist Distance from prop that it starts to fade func (l *StaticPropV10) GetFadeMinDist() float32 { return l.FadeMinDist } -// Distance from prop that it is fully invisible/not rendered +// GetFadeMaxDist Distance from prop that it is fully invisible/not rendered func (l *StaticPropV10) GetFadeMaxDist() float32 { return l.FadeMaxDist } -// World position to sample light from. +// GetLightingOrigin world position to sample light from. // This may differ from prop origin func (l *StaticPropV10) GetLightingOrigin() mgl32.Vec3 { return l.LightingOrigin } +// GetForcedFadeScale func (l *StaticPropV10) GetForcedFadeScale() float32 { return l.ForcedFadeScale } +// GetMinDXLevel // Not defined in v10 func (l *StaticPropV10) GetMinDXLevel() uint16 { return 0 } +// GetMaxDXLevel // Not defined in v10 func (l *StaticPropV10) GetMaxDXLevel() uint16 { return 0 } +// GetMinCPULevel minimum cpu to render func (l *StaticPropV10) GetMinCPULevel() uint8 { return l.MinCPULevel } +// GetMaxCPULevel maximum cpu to render func (l *StaticPropV10) GetMaxCPULevel() uint8 { return l.MaxCPULevel } +// GetMinGPULevel minimum GPU to render func (l *StaticPropV10) GetMinGPULevel() uint8 { return l.MinGPULevel } +// GetMaxGPULevel Maximum GPU to render func (l *StaticPropV10) GetMaxGPULevel() uint8 { return l.MaxGPULevel } +// GetDiffuseModulation func (l *StaticPropV10) GetDiffuseModulation() float32 { return l.DiffuseModulation } +// GetUnknown // Not defined in v10 func (l *StaticPropV10) GetUnknown() float32 { return 0 } +// GetDisableXBox360 should disable on XBox 360 func (l *StaticPropV10) GetDisableXBox360() bool { return true } diff --git a/primitives/game/staticpropv11.go b/primitives/game/staticpropv11.go index 5d2bcd9..4877b96 100644 --- a/primitives/game/staticpropv11.go +++ b/primitives/game/staticpropv11.go @@ -4,7 +4,7 @@ import ( "github.com/go-gl/mathgl/mgl32" ) -// StaticProp v11 type +// StaticPropV11 v11 type // v11 introduced uniform staticprop scaling in csgo // there is trailing [7]byte with unknown purpose right now type StaticPropV11 struct { @@ -30,101 +30,115 @@ type StaticPropV11 struct { _ [7]byte } -// Origin of object in world +// GetOrigin Origin of object in world func (l *StaticPropV11) GetOrigin() mgl32.Vec3 { return l.Origin } -// Rotation of object in world +// GetAngles Rotation of object in world func (l *StaticPropV11) GetAngles() mgl32.Vec3 { return l.Angles } -// Uniform scaling of prop (added in this version) +// GetUniformScale Uniform scaling of prop (added in this version) func (l *StaticPropV11) GetUniformScale() float32 { return l.UniformScale } +// GetPropType prop type func (l *StaticPropV11) GetPropType() uint16 { return l.PropType } -// Index into StaticPropLeafLump +// GetFirstLeaf Index into StaticPropLeafLump func (l *StaticPropV11) GetFirstLeaf() uint16 { return l.FirstLeaf } -// Number of leafs this prop is in +// GetLeafCount Number of leafs this prop is in func (l *StaticPropV11) GetLeafCount() uint16 { return l.LeafCount } +// GetSolid is solid func (l *StaticPropV11) GetSolid() uint8 { return l.Solid } +// GetFlags staticprop flags func (l *StaticPropV11) GetFlags() uint8 { return l.Flags } +// GetSkin skin index (default 0) func (l *StaticPropV11) GetSkin() int32 { return l.Skin } -// Distance from prop that it starts to fade +// GetFadeMinDist Distance from prop that it starts to fade func (l *StaticPropV11) GetFadeMinDist() float32 { return l.FadeMinDist } -// Distance from prop that it is fully invisible/not rendered +// GetFadeMaxDist Distance from prop that it is fully invisible/not rendered func (l *StaticPropV11) GetFadeMaxDist() float32 { return l.FadeMaxDist } -// World position to sample light from. +// GetLightingOrigin World position to sample light from. // This may differ from prop origin func (l *StaticPropV11) GetLightingOrigin() mgl32.Vec3 { return l.LightingOrigin } +// GetForcedFadeScale func (l *StaticPropV11) GetForcedFadeScale() float32 { return l.ForcedFadeScale } +// GetMinDXLevel // Not defined in v11 func (l *StaticPropV11) GetMinDXLevel() uint16 { return 0 } +// GetMaxDXLevel // Not defined in v11 func (l *StaticPropV11) GetMaxDXLevel() uint16 { return 0 } +// GetMinCPULevel minimum cpu to render func (l *StaticPropV11) GetMinCPULevel() uint8 { return l.MinCPULevel } +// GetMaxCPULevel maximum cpu to render func (l *StaticPropV11) GetMaxCPULevel() uint8 { return l.MaxCPULevel } +// GetMinGPULevel minimum GPU to render func (l *StaticPropV11) GetMinGPULevel() uint8 { return l.MinGPULevel } +// GetMaxGPULevel Maximum GPU to render func (l *StaticPropV11) GetMaxGPULevel() uint8 { return l.MaxGPULevel } +// GetDiffuseModulation func (l *StaticPropV11) GetDiffuseModulation() float32 { return l.DiffuseModulation } +// GetUnknown func (l *StaticPropV11) GetUnknown() float32 { return 0 } +// GetDisableXBox360 should disable on XBox 360 func (l *StaticPropV11) GetDisableXBox360() bool { return l.DisableXBox360 } diff --git a/primitives/game/staticpropv4.go b/primitives/game/staticpropv4.go index d6ac2a9..8b52849 100644 --- a/primitives/game/staticpropv4.go +++ b/primitives/game/staticpropv4.go @@ -24,7 +24,7 @@ func (l *StaticPropV4) GetOrigin() mgl32.Vec3 { return l.Origin } -// GetAngles Rotation of object in world +// GetAngles rotation of object in world func (l *StaticPropV4) GetAngles() mgl32.Vec3 { return l.Angles } @@ -35,7 +35,7 @@ func (l *StaticPropV4) GetUniformScale() float32 { return 1 } -// GetPropType +// GetPropType prop type func (l *StaticPropV4) GetPropType() uint16 { return l.PropType } @@ -50,15 +50,17 @@ func (l *StaticPropV4) GetLeafCount() uint16 { return l.LeafCount } +// GetSolid is solid func (l *StaticPropV4) GetSolid() uint8 { return l.Solid } +// GetFlags prop flags func (l *StaticPropV4) GetFlags() uint8 { return l.Flags } -// GetSkin Skin index of this prop +// GetSkin skin index of this prop (default 0) func (l *StaticPropV4) GetSkin() int32 { return l.Skin } @@ -79,6 +81,7 @@ func (l *StaticPropV4) GetLightingOrigin() mgl32.Vec3 { return l.LightingOrigin } +// GetForcedFadeScale // Not defined in v4 func (l *StaticPropV4) GetForcedFadeScale() float32 { return 0 @@ -108,21 +111,25 @@ func (l *StaticPropV4) GetMaxCPULevel() uint8 { return 0 } +// GetMinGPULevel minimum GPU to render // Not defined in v4 func (l *StaticPropV4) GetMinGPULevel() uint8 { return 0 } +// GetMaxGPULevel Maximum GPU to render // Not defined in v4 func (l *StaticPropV4) GetMaxGPULevel() uint8 { return 0 } +// GetDiffuseModulation // Not defined in v4 func (l *StaticPropV4) GetDiffuseModulation() float32 { return 0 } +// GetUnknown // Not defined in v4 func (l *StaticPropV4) GetUnknown() float32 { return 0 diff --git a/primitives/game/staticpropv5.go b/primitives/game/staticpropv5.go index cca8c8b..a550efa 100644 --- a/primitives/game/staticpropv5.go +++ b/primitives/game/staticpropv5.go @@ -4,7 +4,7 @@ import ( "github.com/go-gl/mathgl/mgl32" ) -// StaticProp v5 type +// StaticPropV5 v5 type type StaticPropV5 struct { Origin mgl32.Vec3 Angles mgl32.Vec3 @@ -20,112 +20,122 @@ type StaticPropV5 struct { ForcedFadeScale float32 } -// Origin of object in world +// GetOrigin origin of object in world func (l *StaticPropV5) GetOrigin() mgl32.Vec3 { return l.Origin } -// Rotation of object in world +// GetAngles rotation of object in world func (l *StaticPropV5) GetAngles() mgl32.Vec3 { return l.Angles } +// GetUniformScale is always 1 (i.e. 1x scale multiplier) // Not defined in v5 func (l *StaticPropV5) GetUniformScale() float32 { return 1 } +// GetPropType prop type func (l *StaticPropV5) GetPropType() uint16 { return l.PropType } -// Index into StaticPropLeafLump +// GetFirstLeaf index into StaticPropLeafLump func (l *StaticPropV5) GetFirstLeaf() uint16 { return l.FirstLeaf } -// Number of leafs this prop is in +// GetLeafCount number of leafs this prop is in func (l *StaticPropV5) GetLeafCount() uint16 { return l.LeafCount } +// GetSolid is solid func (l *StaticPropV5) GetSolid() uint8 { return l.Solid } +// GetFlags prop flags func (l *StaticPropV5) GetFlags() uint8 { return l.Flags } -// Skin index of this prop +// GetSkin skin index of this prop (default 0) func (l *StaticPropV5) GetSkin() int32 { return l.Skin } -// Distance from prop that it starts to fade +// GetFadeMinDist distance from prop that it starts to fade func (l *StaticPropV5) GetFadeMinDist() float32 { return l.FadeMinDist } -// Distance from prop that it is fully invisible/not rendered +// GetFadeMaxDist distance from prop that it is fully invisible/not rendered func (l *StaticPropV5) GetFadeMaxDist() float32 { return l.FadeMaxDist } -// World position to sample light from. +// GetLightingOrigin world position to sample light from. // This may differ from prop origin func (l *StaticPropV5) GetLightingOrigin() mgl32.Vec3 { return l.LightingOrigin } +// GetForcedFadeScale func (l *StaticPropV5) GetForcedFadeScale() float32 { return l.ForcedFadeScale } -// Minimum directx level to render this prop +// GetMinDXLevel Minimum directx level to render this prop // Not defined in v5 func (l *StaticPropV5) GetMinDXLevel() uint16 { return 0 } -// Maximum directx level to render this prop +// GetMaxDXLevel Maximum directx level to render this prop // Not defined in v5 func (l *StaticPropV5) GetMaxDXLevel() uint16 { return 0 } -// Minimum CPU type to render this prop +// GetMinCPULevel Minimum CPU type to render this prop // Not defined in v5 func (l *StaticPropV5) GetMinCPULevel() uint8 { return 0 } -// Maximum CPU type to render this prop +// GetMaxCPULevel Maximum CPU type to render this prop // Not defined in v5 func (l *StaticPropV5) GetMaxCPULevel() uint8 { return 0 } +// GetMinGPULevel minimum GPU to render // Not defined in v5 func (l *StaticPropV5) GetMinGPULevel() uint8 { return 0 } +// GetMaxGPULevel Maximum GPU to render // Not defined in v5 func (l *StaticPropV5) GetMaxGPULevel() uint8 { return 0 } +// GetDiffuseModulation // Not defined in v5 func (l *StaticPropV5) GetDiffuseModulation() float32 { return 0 } +// GetUnknown // Not defined in v5 func (l *StaticPropV5) GetUnknown() float32 { return 0 } +// GetDisableXBox360 should disable on XBox 360 // Not defined in v5 func (l *StaticPropV5) GetDisableXBox360() bool { return false diff --git a/primitives/game/staticpropv6.go b/primitives/game/staticpropv6.go index 89f0a17..4a5f322 100644 --- a/primitives/game/staticpropv6.go +++ b/primitives/game/staticpropv6.go @@ -4,7 +4,7 @@ import ( "github.com/go-gl/mathgl/mgl32" ) -// StaticProp v6 type +// StaticPropV6 v6 type type StaticPropV6 struct { Origin mgl32.Vec3 Angles mgl32.Vec3 @@ -22,110 +22,120 @@ type StaticPropV6 struct { MaxDXLevel uint16 } -// Origin of object in world +// GetOrigin origin of object in world func (l *StaticPropV6) GetOrigin() mgl32.Vec3 { return l.Origin } -// Rotation of object in world +// GetAngles rotation of object in world func (l *StaticPropV6) GetAngles() mgl32.Vec3 { return l.Angles } +// GetUniformScale is always 1 (i.e. 1x scale multiplier) // Not defined in v6 func (l *StaticPropV6) GetUniformScale() float32 { return 1 } +// GetPropType prop type func (l *StaticPropV6) GetPropType() uint16 { return l.PropType } -// Index into StaticPropLeafLump +// GetFirstLeaf index into StaticPropLeafLump func (l *StaticPropV6) GetFirstLeaf() uint16 { return l.FirstLeaf } -// Number of leafs this prop is in +// GetLeafCount number of leafs this prop is in func (l *StaticPropV6) GetLeafCount() uint16 { return l.LeafCount } +// GetSolid is solid func (l *StaticPropV6) GetSolid() uint8 { return l.Solid } +// GetFlags prop flags func (l *StaticPropV6) GetFlags() uint8 { return l.Flags } -// Skin index of this prop +// GetSkin skin index of this prop (default 0) func (l *StaticPropV6) GetSkin() int32 { return l.Skin } -// Distance from prop that it starts to fade +// GetFadeMinDist distance from prop that it starts to fade func (l *StaticPropV6) GetFadeMinDist() float32 { return l.FadeMinDist } -// Distance from prop that it is fully invisible/not rendered +// GetFadeMaxDist distance from prop that it is fully invisible/not rendered func (l *StaticPropV6) GetFadeMaxDist() float32 { return l.FadeMaxDist } -// World position to sample light from. +// GetLightingOrigin world position to sample light from. // This may differ from prop origin func (l *StaticPropV6) GetLightingOrigin() mgl32.Vec3 { return l.LightingOrigin } +// GetForcedFadeScale func (l *StaticPropV6) GetForcedFadeScale() float32 { return l.ForcedFadeScale } -// Minimum directx level to render this prop +// GetMinDXLevel Minimum directx level to render this prop func (l *StaticPropV6) GetMinDXLevel() uint16 { return l.MinDXLevel } -// Maximum directx level to render this prop +// GetMaxDXLevel Maximum directx level to render this prop func (l *StaticPropV6) GetMaxDXLevel() uint16 { return l.MaxDXLevel } -// Minimum CPU type to render this prop +// GetMinCPULevel Minimum CPU type to render this prop // Not defined in v6 func (l *StaticPropV6) GetMinCPULevel() uint8 { return 0 } -// Maximum CPU type to render this prop +// GetMaxCPULevel maximum cpu to render // Not defined in v6 func (l *StaticPropV6) GetMaxCPULevel() uint8 { return 0 } +// GetMinGPULevel minimum GPU to render // Not defined in v6 func (l *StaticPropV6) GetMinGPULevel() uint8 { return 0 } +// GetMaxGPULevel Maximum GPU to render // Not defined in v6 func (l *StaticPropV6) GetMaxGPULevel() uint8 { return 0 } +// GetDiffuseModulation // Not defined in v6 func (l *StaticPropV6) GetDiffuseModulation() float32 { return 0 } +// GetUnknown // Not defined in v6 func (l *StaticPropV6) GetUnknown() float32 { return 0 } +// GetDisableXBox360 should disable on XBox 360 // Not defined in v6 func (l *StaticPropV6) GetDisableXBox360() bool { return false diff --git a/primitives/game/staticpropv7.go b/primitives/game/staticpropv7.go index 707c6c7..f451569 100644 --- a/primitives/game/staticpropv7.go +++ b/primitives/game/staticpropv7.go @@ -4,7 +4,7 @@ import ( "github.com/go-gl/mathgl/mgl32" ) -// StaticProp v7 type +// StaticPropV7 v7 type type StaticPropV7 struct { Origin mgl32.Vec3 Angles mgl32.Vec3 @@ -23,105 +23,119 @@ type StaticPropV7 struct { DiffuseModulation float32 } -// Origin of object in world +// GetOrigin origin of object in world func (l *StaticPropV7) GetOrigin() mgl32.Vec3 { return l.Origin } -// Rotation of object in world +// GetAngles rotation of object in world func (l *StaticPropV7) GetAngles() mgl32.Vec3 { return l.Angles } +// GetUniformScale is always 1 (i.e. 1x scale multiplier) // Not defined in v7 func (l *StaticPropV7) GetUniformScale() float32 { return 1 } +// GetPropType prop type func (l *StaticPropV7) GetPropType() uint16 { return l.PropType } -// Index into StaticPropLeafLump +// GetFirstLeaf index into StaticPropLeafLump func (l *StaticPropV7) GetFirstLeaf() uint16 { return l.FirstLeaf } -// Number of leafs this prop is in +// GetLeafCount number of leafs this prop is in func (l *StaticPropV7) GetLeafCount() uint16 { return l.LeafCount } +// GetSolid is solid func (l *StaticPropV7) GetSolid() uint8 { return l.Solid } +// GetFlags prop flags func (l *StaticPropV7) GetFlags() uint8 { return l.Flags } -// Skin index of this prop +// GetSkin skin index of this prop (default 0) func (l *StaticPropV7) GetSkin() int32 { return l.Skin } -// Distance from prop that it starts to fade +// GetFadeMinDist distance from prop that it starts to fade func (l *StaticPropV7) GetFadeMinDist() float32 { return l.FadeMinDist } -// Distance from prop that it is fully invisible/not rendered +// GetFadeMaxDist distance from prop that it is fully invisible/not rendered func (l *StaticPropV7) GetFadeMaxDist() float32 { return l.FadeMaxDist } -// World position to sample light from. +// GetLightingOrigin world position to sample light from. // This may differ from prop origin func (l *StaticPropV7) GetLightingOrigin() mgl32.Vec3 { return l.LightingOrigin } +// GetForcedFadeScale func (l *StaticPropV7) GetForcedFadeScale() float32 { return l.ForcedFadeScale } +// GetMinDXLevel Minimum directx level to render this prop func (l *StaticPropV7) GetMinDXLevel() uint16 { return l.MinDXLevel } +// GetMaxDXLevel Maximum directx level to render this prop func (l *StaticPropV7) GetMaxDXLevel() uint16 { return l.MaxDXLevel } +// GetMinCPULevel Minimum CPU type to render this prop // Not defined in v7 func (l *StaticPropV7) GetMinCPULevel() uint8 { return 0 } +// GetMaxCPULevel Maximum CPU type to render this prop // Not defined in v7 func (l *StaticPropV7) GetMaxCPULevel() uint8 { return 0 } +// GetMinGPULevel minimum GPU to render // Not defined in v7 func (l *StaticPropV7) GetMinGPULevel() uint8 { return 0 } +// GetMaxGPULevel Maximum GPU to render // Not defined in v7 func (l *StaticPropV7) GetMaxGPULevel() uint8 { return 0 } +// GetDiffuseModulation func (l *StaticPropV7) GetDiffuseModulation() float32 { return l.DiffuseModulation } +// GetUnknown // Not defined in v7 func (l *StaticPropV7) GetUnknown() float32 { return 0 } +// GetDisableXBox360 should disable on XBox 360 // Not defined in v7 func (l *StaticPropV7) GetDisableXBox360() bool { return false diff --git a/primitives/game/staticpropv8.go b/primitives/game/staticpropv8.go index c77f891..e645214 100644 --- a/primitives/game/staticpropv8.go +++ b/primitives/game/staticpropv8.go @@ -4,7 +4,7 @@ import ( "github.com/go-gl/mathgl/mgl32" ) -// StaticProp v8 type +// StaticPropV8 v8 type type StaticPropV8 struct { Origin mgl32.Vec3 Angles mgl32.Vec3 @@ -25,103 +25,117 @@ type StaticPropV8 struct { DiffuseModulation float32 } -// Origin of object in world +// GetOrigin origin of object in world func (l *StaticPropV8) GetOrigin() mgl32.Vec3 { return l.Origin } -// Rotation of object in world +// GetAngles rotation of object in world func (l *StaticPropV8) GetAngles() mgl32.Vec3 { return l.Angles } +// GetUniformScale is always 1 (i.e. 1x scale multiplier) // Not defined in v8 func (l *StaticPropV8) GetUniformScale() float32 { return 1 } +// GetPropType prop type func (l *StaticPropV8) GetPropType() uint16 { return l.PropType } -// Index into StaticPropLeafLump +// GetFirstLeaf index into StaticPropLeafLump func (l *StaticPropV8) GetFirstLeaf() uint16 { return l.FirstLeaf } -// Number of leafs this prop is in +// GetLeafCount number of leafs this prop is in func (l *StaticPropV8) GetLeafCount() uint16 { return l.LeafCount } +// GetSolid is solid func (l *StaticPropV8) GetSolid() uint8 { return l.Solid } +// GetFlags prop flags func (l *StaticPropV8) GetFlags() uint8 { return l.Flags } -// Skin index of this prop +// GetSkin skin index of this prop (default 0) func (l *StaticPropV8) GetSkin() int32 { return l.Skin } -// Distance from prop that it starts to fade +// GetFadeMinDist distance from prop that it starts to fade func (l *StaticPropV8) GetFadeMinDist() float32 { return l.FadeMinDist } -// Distance from prop that it is fully invisible/not rendered +// GetFadeMaxDist distance from prop that it is fully invisible/not rendered func (l *StaticPropV8) GetFadeMaxDist() float32 { return l.FadeMaxDist } -// World position to sample light from. +// GetLightingOrigin world position to sample light from. // This may differ from prop origin func (l *StaticPropV8) GetLightingOrigin() mgl32.Vec3 { return l.LightingOrigin } +// GetForcedFadeScale func (l *StaticPropV8) GetForcedFadeScale() float32 { return l.ForcedFadeScale } +// GetMinDXLevel Minimum directx level to render this prop // Not defined in v8 func (l *StaticPropV8) GetMinDXLevel() uint16 { return 0 } +// GetMaxDXLevel Maximum directx level to render this prop // Not defined in v8 func (l *StaticPropV8) GetMaxDXLevel() uint16 { return 0 } +// GetMinCPULevel minimum cpu to render func (l *StaticPropV8) GetMinCPULevel() uint8 { return l.MinCPULevel } +// GetMaxCPULevel maximum cpu to render func (l *StaticPropV8) GetMaxCPULevel() uint8 { return l.MaxCPULevel } +// GetMinGPULevel minimum GPU to render func (l *StaticPropV8) GetMinGPULevel() uint8 { return l.MinGPULevel } +// GetMaxGPULevel Maximum GPU to render func (l *StaticPropV8) GetMaxGPULevel() uint8 { return l.MaxGPULevel } +// GetDiffuseModulation func (l *StaticPropV8) GetDiffuseModulation() float32 { return l.DiffuseModulation } +// GetUnknown // Not defined in v8 func (l *StaticPropV8) GetUnknown() float32 { return 0 } +// GetDisableXBox360 should disable on XBox 360 // Not defined in v8 func (l *StaticPropV8) GetDisableXBox360() bool { return false diff --git a/primitives/game/staticpropv9.go b/primitives/game/staticpropv9.go index ce8b33b..1593ed0 100644 --- a/primitives/game/staticpropv9.go +++ b/primitives/game/staticpropv9.go @@ -4,7 +4,7 @@ import ( "github.com/go-gl/mathgl/mgl32" ) -// StaticProp v9 type +// StaticPropV9 v9 type type StaticPropV9 struct { Origin mgl32.Vec3 Angles mgl32.Vec3 @@ -26,103 +26,117 @@ type StaticPropV9 struct { DisableXBox360 bool } -// Origin of object in world +// GetOrigin origin of object in world func (l *StaticPropV9) GetOrigin() mgl32.Vec3 { return l.Origin } -// Rotation of object in world +// GetAngles rotation of object in world func (l *StaticPropV9) GetAngles() mgl32.Vec3 { return l.Angles } +// GetUniformScale is always 1 (i.e. 1x scale multiplier) // Not defined in v9 func (l *StaticPropV9) GetUniformScale() float32 { return 1 } +// GetPropType prop type func (l *StaticPropV9) GetPropType() uint16 { return l.PropType } -// Index into StaticPropLeafLump +// GetFirstLeaf index into StaticPropLeafLump func (l *StaticPropV9) GetFirstLeaf() uint16 { return l.FirstLeaf } -// Number of leafs this prop is in +// GetLeafCount number of leafs this prop is in func (l *StaticPropV9) GetLeafCount() uint16 { return l.LeafCount } +// GetSolid is solid func (l *StaticPropV9) GetSolid() uint8 { return l.Solid } +// GetFlags prop flags func (l *StaticPropV9) GetFlags() uint8 { return l.Flags } -// Skin index of this prop +// GetSkin skin index of this prop (default 0) func (l *StaticPropV9) GetSkin() int32 { return l.Skin } -// Distance from prop that it starts to fade +// GetFadeMinDist distance from prop that it starts to fade func (l *StaticPropV9) GetFadeMinDist() float32 { return l.FadeMinDist } -// Distance from prop that it is fully invisible/not rendered +// GetFadeMaxDist distance from prop that it is fully invisible/not rendered func (l *StaticPropV9) GetFadeMaxDist() float32 { return l.FadeMaxDist } -// World position to sample light from. +// GetLightingOrigin world position to sample light from. // This may differ from prop origin func (l *StaticPropV9) GetLightingOrigin() mgl32.Vec3 { return l.LightingOrigin } +// GetForcedFadeScale func (l *StaticPropV9) GetForcedFadeScale() float32 { return l.ForcedFadeScale } +// GetMinDXLevel Minimum directx level to render this prop // Not defined in v9 func (l *StaticPropV9) GetMinDXLevel() uint16 { return 0 } +// GetMaxDXLevel Maximum directx level to render this prop // Not defined in v9 func (l *StaticPropV9) GetMaxDXLevel() uint16 { return 0 } +// GetMinCPULevel minimum cpu to render func (l *StaticPropV9) GetMinCPULevel() uint8 { return l.MinCPULevel } +// GetMaxCPULevel maximum cpu to render func (l *StaticPropV9) GetMaxCPULevel() uint8 { return l.MaxCPULevel } +// GetMinGPULevel minimum GPU to render func (l *StaticPropV9) GetMinGPULevel() uint8 { return l.MinGPULevel } +// GetMaxGPULevel Maximum GPU to render func (l *StaticPropV9) GetMaxGPULevel() uint8 { return l.MaxGPULevel } +// GetDiffuseModulation func (l *StaticPropV9) GetDiffuseModulation() float32 { return l.DiffuseModulation } +// GetUnknown // Not defined in v9 func (l *StaticPropV9) GetUnknown() float32 { return 0 } +// GetDisableXBox360 should disable on XBox 360 func (l *StaticPropV9) GetDisableXBox360() bool { return l.DisableXBox360 } diff --git a/primitives/leaf/leaf.go b/primitives/leaf/leaf.go index 9553771..feb6e95 100644 --- a/primitives/leaf/leaf.go +++ b/primitives/leaf/leaf.go @@ -3,7 +3,8 @@ package leaf const bitmaskLower9 = 0x1FF // 511 (2^9 - 1) const bitmaskLower7 = 0x7F // 127 (2^7 - 1) -// Leaf +// Leaf represents a single convex volume that contains 0 or more +// faces (or entities) within its bounds type Leaf struct { // Contents Contents int32 @@ -28,7 +29,7 @@ type Leaf struct { _ [2]byte } -// Flags returns flags (second 7 bits) +// Area returns area (first 9 bits) func (b *Leaf) Area() int16 { return int16((b.BitField) & bitmaskLower7) } @@ -39,7 +40,7 @@ func (b *Leaf) SetArea(area int16) { b.BitField = int16((v & bitmaskLower9) | (area)) } -// Area returns area (first 9 bits) +// Flags returns flags (second 7 bits) func (b *Leaf) Flags() int16 { return int16((b.BitField >> 9) & bitmaskLower9) } diff --git a/primitives/node/node.go b/primitives/node/node.go index 312368e..a518d1c 100644 --- a/primitives/node/node.go +++ b/primitives/node/node.go @@ -1,7 +1,7 @@ package node // Node is a bsp node that has 2 children, because it was splittable along a plane. -// It represents a convex volume with child volumes +// It represents a non-convex volume with child volumes type Node struct { // PlaneNum is index of splitting place PlaneNum int32 diff --git a/primitives/visibility/visibility.go b/primitives/visibility/visibility.go index cd110e3..1ade15f 100644 --- a/primitives/visibility/visibility.go +++ b/primitives/visibility/visibility.go @@ -1,6 +1,6 @@ package visibility -// MaxMapVisibility +// MaxMapVisibility is the maximum size of visibility data in bytes const MaxMapVisibility = 0x1000000 // VisPVS index @@ -9,15 +9,16 @@ const VisPVS = 0 // VisPAS index const VisPAS = 1 -// MaxClusterSizePerVIS -const MaxClusterSizePerVIS = 8 - // Vis contains visibility data for clusters // Includes both PVS (Potential Visible Set) and PAS (Potential Audible Set) type Vis struct { + // NumClusters is number of computed cluster NumClusters int32 - ByteOffset [][2]int32 // Slice length = NumClusters [0]=offset to PVS bit array for cluster - BitVectors []byte // Compressed bit vectors, contains run-length compression PVS data + // ByteOffset contains offsets for cluster pvs and pas + // Slice length = NumClusters [0]=offset to PVS bit array for cluster + ByteOffset [][2]int32 + // BitVectors are compressed bit vectors, contains run-length compression PVS data + BitVectors []byte } // GetVisibleClusters returns all visible clusters ids for a given cluster diff --git a/reader.go b/reader.go index 91f0602..d075a18 100644 --- a/reader.go +++ b/reader.go @@ -98,7 +98,7 @@ func (r *Reader) readHeader(reader *bytes.Reader, header Header) (*Header, error // readLump Reads a single lumps data // Expect a byte reader containing the lump data, as well as the -// header and lump identifier (index) +// header and lump identifier (id) func (r *Reader) readLump(reader *bytes.Reader, header Header, index int) ([]byte, error) { //Limit lump data to declared size lumpHeader := header.Lumps[index]