diff --git a/lump/displightmapsampleposition.go b/lump/displightmapsampleposition.go index 2e94fd1..803aac9 100644 --- a/lump/displightmapsampleposition.go +++ b/lump/displightmapsampleposition.go @@ -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 diff --git a/lump/lump.go b/lump/lump.go index 64c1a9d..7cfd06a 100644 --- a/lump/lump.go +++ b/lump/lump.go @@ -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 } @@ -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 @@ -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 } diff --git a/lump/lump_test.go b/lump/lump_test.go index 96d6b4d..33afb7f 100644 --- a/lump/lump_test.go +++ b/lump/lump_test.go @@ -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) @@ -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) @@ -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) diff --git a/lump/pakfile.go b/lump/pakfile.go index f169012..3266b9e 100644 --- a/lump/pakfile.go +++ b/lump/pakfile.go @@ -9,7 +9,7 @@ import ( // Pakfile is Lump 40: Pakfile type Pakfile struct { - RawBytes + rawBytes zipReader *zip.Reader } diff --git a/lump/physdisp.go b/lump/physdisp.go index 434746e..da824e7 100644 --- a/lump/physdisp.go +++ b/lump/physdisp.go @@ -1,4 +1,4 @@ package lump // PhysDisp is Lump 28: PhysDisp -type PhysDisp = RawBytes +type PhysDisp = rawBytes diff --git a/version.go b/version.go index c350c07..f955b89 100644 --- a/version.go +++ b/version.go @@ -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) @@ -80,13 +80,13 @@ 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: @@ -94,7 +94,7 @@ func getV20Lump(index LumpId) (Lump, error) { 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: @@ -102,7 +102,7 @@ func getV20Lump(index LumpId) (Lump, error) { 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: @@ -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: @@ -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: @@ -152,7 +152,7 @@ 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: @@ -160,11 +160,11 @@ func getV20Lump(index LumpId) (Lump, error) { 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") } diff --git a/version_test.go b/version_test.go index bd29251..dbcd261 100644 --- a/version_test.go +++ b/version_test.go @@ -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",