Skip to content

Commit

Permalink
made rawBytes private in favour of public Unimplemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Galaco committed Oct 2, 2023
1 parent 8c3e21b commit 883c4a7
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 60 deletions.
2 changes: 1 addition & 1 deletion lump/displightmapsampleposition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package lump

// DispLightmapSamplePosition is Lump 34: DispLightmapSamplePosition
// NOTE: This does NOT have a mapped format yet, and is readable as []byte only
type DispLightmapSamplePosition = RawBytes
type DispLightmapSamplePosition = rawBytes
80 changes: 41 additions & 39 deletions lump/lump.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,11 @@ import (
"unsafe"
)

// unmarshallBasicLump is a helper function for unmarshalling []byte to lumps that are just a single []T.
func unmarshallBasicLump[V any](raw []byte) ([]V, error) {
if len(raw) == 0 {
return nil, nil
}

var sampleV V
v := make([]V, len(raw)/int(unsafe.Sizeof(sampleV)))
if err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &v); err != nil {
return nil, err
}

return v, nil
}

// marshallBasicLump is a helper function for marshalling lumps that are just a single []T to []byte.
func marshallBasicLump(data any) ([]byte, error) {
var buf bytes.Buffer
err := binary.Write(&buf, binary.LittleEndian, data)
return buf.Bytes(), err
}
// Unimplemented is Lump n: Unimplemented lump type.
type Unimplemented = rawBytes

// Metadata is a Helper info for a lump
type Metadata struct {
length int
version int32
}

Expand All @@ -47,60 +27,81 @@ func (info *Metadata) SetVersion(version int32) {
info.version = version
}

// RawBytes is Lump n: RawBytes lump type
// rawBytes is Lump n: rawBytes lump type
// the contents are just raw bytes, left up to the implementer to handle.
type RawBytes struct {
type rawBytes struct {
Metadata
Data []byte `json:"data"`
}

// FromBytes imports this lump from raw byte Data
func (lump *RawBytes) FromBytes(raw []byte) error {
func (lump *rawBytes) FromBytes(raw []byte) error {
lump.Data = raw

return nil
}

// Contents returns internal format structure Data
func (lump *RawBytes) Contents() []byte {
func (lump *rawBytes) Contents() []byte {
return lump.Data
}

// ToBytes converts this lump back to raw byte Data
func (lump *RawBytes) ToBytes() ([]byte, error) {
func (lump *rawBytes) ToBytes() ([]byte, error) {
return lump.Data, nil
}

type Unimplemented = RawBytes
// unmarshallBasicLump is a helper function for unmarshalling []byte to lumps that are just a single []T.
func unmarshallBasicLump[V any](raw []byte) ([]V, error) {
if len(raw) == 0 {
return nil, nil
}

var sampleV V
v := make([]V, len(raw)/int(unsafe.Sizeof(sampleV)))
if err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &v); err != nil {
return nil, err
}

return v, nil
}

// marshallBasicLump is a helper function for marshalling lumps that are just a single []T to []byte.
func marshallBasicLump(data any) ([]byte, error) {
var buf bytes.Buffer
err := binary.Write(&buf, binary.LittleEndian, data)
return buf.Bytes(), err
}

// unmarshallTaggedLump is a helper function for unmarshalling []byte to lumps that are just a single []T.
// It uses the "bsp" tag on struct fields to determine which fields to read.
func unmarshallTaggedLump[V any](raw []byte, version string) ([]V, error) {
// It is generally useful for lumps that have multiple versions and have different fields & lengths of T.
func unmarshallTaggedLump[T any](raw []byte, version string) ([]T, error) {
if len(raw) == 0 {
return nil, nil
}

var sampleV V
var sampleT T

// Figure out the length of our struct for our version.
var binarylenV int
for _, field := range reflect.VisibleFields(reflect.TypeOf(sampleV)) {
var binarylenT int
for _, field := range reflect.VisibleFields(reflect.TypeOf(sampleT)) {
// Fields that are in this version should contribute to the length.
if t := field.Tag.Get("bsp"); t == "" || t == version {
binarylenV += int(field.Type.Size())
binarylenT += int(field.Type.Size())
}
}
if len(raw)%binarylenV != 0 {
if len(raw)%binarylenT != 0 {
// length doesn't match exactly a multiple of our calculated struct size.
return nil, fmt.Errorf("lump length %d is not a multiple of %d", len(raw), binarylenV)
return nil, fmt.Errorf("lump length %d is not a multiple of %d", len(raw), binarylenT)
}

// Calculate the padding size to properly read the struct.
padSize := int(unsafe.Sizeof(sampleV)) - binarylenV
padSize := int(unsafe.Sizeof(sampleT)) - binarylenT

v := make([]V, len(raw)/binarylenV)
v := make([]T, len(raw)/binarylenT)
for i := range v {
b2 := append([]byte{}, raw[(binarylenV*i):(binarylenV*i)+binarylenV]...)
b2 := append([]byte{}, raw[(binarylenT*i):(binarylenT*i)+binarylenT]...)
buf := bytes.NewBuffer(append(b2, []byte(strings.Repeat("\x00", padSize))...))
if err := binary.Read(buf, binary.LittleEndian, &v[i]); err != nil {
return nil, err
Expand All @@ -112,7 +113,8 @@ func unmarshallTaggedLump[V any](raw []byte, version string) ([]V, error) {

// marshallTaggedLump is a helper function for marshalling lumps that are just a single []T to []byte.
// It uses the "bsp" tag on struct fields to determine which fields to write.
func marshallTaggedLump[V any](data []V, version string) ([]byte, error) {
// It is generally useful for lumps that have multiple versions and have different fields & lengths of V.
func marshallTaggedLump[T any](data []T, version string) ([]byte, error) {
if len(data) == 0 {
return []byte{}, nil
}
Expand Down
6 changes: 3 additions & 3 deletions lump/lump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func TestUnimplemented_GetData(t *testing.T) {
sut := RawBytes{}
sut := Unimplemented{}
data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6}
if err := sut.FromBytes(data); err != nil {
t.Error(err)
Expand All @@ -18,7 +18,7 @@ func TestUnimplemented_GetData(t *testing.T) {
}

func TestUnimplemented_Marshall(t *testing.T) {
sut := RawBytes{}
sut := Unimplemented{}
data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6}
if err := sut.FromBytes(data); err != nil {
t.Error(err)
Expand All @@ -40,7 +40,7 @@ func TestUnimplemented_Marshall(t *testing.T) {
}

func TestUnimplemented_FromBytes(t *testing.T) {
sut := RawBytes{}
sut := Unimplemented{}
data := []byte{0, 1, 2, 5, 43, 156, 146, 3, 3, 6}
if err := sut.FromBytes(data); err != nil {
t.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion lump/pakfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// Pakfile is Lump 40: Pakfile
type Pakfile struct {
RawBytes
rawBytes
zipReader *zip.Reader
}

Expand Down
2 changes: 1 addition & 1 deletion lump/physdisp.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lump

// PhysDisp is Lump 28: PhysDisp
type PhysDisp = RawBytes
type PhysDisp = rawBytes
28 changes: 14 additions & 14 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func LumpResolverByBSPVersion(id LumpId, header Header) (l Lump, err error) {
// @TODO: Implement v21.
l, err = getV20Lump(id)
default:
l, err = &lump.RawBytes{}, nil
l, err = &lump.Unimplemented{}, nil
}

l.SetVersion(header.Version)
Expand Down Expand Up @@ -80,29 +80,29 @@ func getV20Lump(index LumpId) (Lump, error) {
case 21:
return &lump.AreaPortal{}, nil
case 22:
return &lump.RawBytes{}, nil //portals | unused0 | propcollision
return &lump.Unimplemented{}, nil //portals | unused0 | propcollision
case 23:
return &lump.RawBytes{}, nil //clusters | unused1 | prophulls
return &lump.Unimplemented{}, nil //clusters | unused1 | prophulls
case 24:
return &lump.RawBytes{}, nil //portalverts | unused2 | prophullverts
return &lump.Unimplemented{}, nil //portalverts | unused2 | prophullverts
case 25:
return &lump.RawBytes{}, nil //clusterportals | unused3 | proptris
return &lump.Unimplemented{}, nil //clusterportals | unused3 | proptris
case 26:
return &lump.DispInfo{}, nil
case 27:
return &lump.Face{}, nil
case 28:
return &lump.PhysDisp{}, nil
case 29:
return &lump.RawBytes{}, nil //physcollide.
return &lump.Unimplemented{}, nil //physcollide.
case 30:
return &lump.VertNormal{}, nil
case 31:
return &lump.VertNormalIndice{}, nil
case 32:
// @TODO: This appears to be stripped by VRAD.
// Find samples to test.
return &lump.RawBytes{}, nil //disp lightmap alphas
return &lump.Unimplemented{}, nil //disp lightmap alphas
case 33:
return &lump.DispVert{}, nil
case 34:
Expand All @@ -112,7 +112,7 @@ func getV20Lump(index LumpId) (Lump, error) {
case 36:
return &lump.LeafWaterData{}, nil
case 37:
return &lump.RawBytes{}, nil //primitives @TODO - Appears to be 4bytes unaccounted for at end of lump?
return &lump.Unimplemented{}, nil //primitives @TODO - Appears to be 4bytes unaccounted for at end of lump?
case 38:
return &lump.PrimVert{}, nil
case 39:
Expand All @@ -136,9 +136,9 @@ func getV20Lump(index LumpId) (Lump, error) {
case 48:
return &lump.DispTris{}, nil
case 49:
return &lump.RawBytes{}, nil //physcollidesurface | prop blob
return &lump.Unimplemented{}, nil //physcollidesurface | prop blob
case 50:
return &lump.RawBytes{}, nil //wateroverlays
return &lump.Unimplemented{}, nil //wateroverlays
case 51:
return &lump.LeafAmbientIndexHDR{}, nil
case 52:
Expand All @@ -152,19 +152,19 @@ func getV20Lump(index LumpId) (Lump, error) {
case 56:
return &lump.LeafAmbientLighting{}, nil //leaf ambient lighting
case 57:
return &lump.RawBytes{}, nil //xzippakfile
return &lump.Unimplemented{}, nil //xzippakfile
case 58:
return &lump.FaceHDR{}, nil
case 59:
return &lump.MapFlags{}, nil
case 60:
return &lump.OverlayFade{}, nil
case 61:
return &lump.RawBytes{}, nil //overlay system levels
return &lump.Unimplemented{}, nil //overlay system levels
case 62:
return &lump.RawBytes{}, nil //physlevel
return &lump.Unimplemented{}, nil //physlevel
case 63:
return &lump.RawBytes{}, nil //disp multiblend
return &lump.Unimplemented{}, nil //disp multiblend
default:
return nil, fmt.Errorf("invalid lump id")
}
Expand Down
2 changes: 1 addition & 1 deletion version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestGetLumpForVersion(t *testing.T) {
name: "unknown version",
id: 4,
version: 987,
t: reflect.TypeOf(&lump.RawBytes{}),
t: reflect.TypeOf(&lump.Unimplemented{}),
},
{
name: "v19",
Expand Down

0 comments on commit 883c4a7

Please sign in to comment.