Add fuzz testing for Section encoding/decoding with multiple edge cases
This commit is contained in:
+21
-42
@@ -1,54 +1,33 @@
|
||||
package level
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"math"
|
||||
import "io"
|
||||
|
||||
"github.com/admin-else/strom/data"
|
||||
"github.com/admin-else/strom/util"
|
||||
)
|
||||
|
||||
type BlockState struct {
|
||||
Id string
|
||||
Properties map[string]string
|
||||
type Chunk struct {
|
||||
Sections []Section
|
||||
version string
|
||||
}
|
||||
|
||||
const (
|
||||
ChunkWidth = 16
|
||||
ChunkColumns = ChunkWidth * ChunkWidth
|
||||
BlocksPerChunkSection = ChunkWidth * ChunkWidth * ChunkWidth
|
||||
ChunkBiomesWidth = 4
|
||||
BiomesPerChunkSection = ChunkBiomesWidth * ChunkBiomesWidth * ChunkBiomesWidth
|
||||
)
|
||||
func ReadChunkFromChunkPacketData(r io.Reader, version string, worldHeight int) (err error) {
|
||||
c := &Chunk{}
|
||||
c.version = version
|
||||
nSections := worldHeight / ChunkWidth
|
||||
|
||||
type Section struct {
|
||||
Blocks *Storage[*BlockState]
|
||||
Biomes *Storage[string]
|
||||
}
|
||||
|
||||
func PossibleBpesBlocks(version string) (ret []uint8) {
|
||||
maxBpe := util.BpeByNum(float64(len(data.BlocksForVersion(version))))
|
||||
ret = []uint8{0, 4, 5, 6, 7, 8, maxBpe}
|
||||
return
|
||||
}
|
||||
|
||||
func PossibleBpesBiomes(version string) (ret []uint8) {
|
||||
maxBpe := uint8(math.Ceil(math.Log2(float64(len(data.BiomesForVersion(version))))))
|
||||
ret = []uint8{0, 1, 2, 3, maxBpe}
|
||||
c.Sections = make([]Section, nSections)
|
||||
for i := range c.Sections {
|
||||
c.Sections[i], err = SectionDecodePacket(r, version)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SectionFromPacket(r io.Reader, version string) (s *Section, err error) {
|
||||
s = &Section{}
|
||||
s.Blocks = NewStorage[*BlockState](BlocksPerChunkSection, PossibleBpesBlocks(version))
|
||||
s.Biomes = NewStorage[string](BiomesPerChunkSection, PossibleBpesBiomes(version))
|
||||
|
||||
var bitsPerEntryBlocks uint8
|
||||
err = binary.Read(r, binary.BigEndian, &bitsPerEntryBlocks)
|
||||
if err != nil {
|
||||
return
|
||||
func (c *Chunk) WriteChunkData(w io.Writer) (err error) {
|
||||
for _, s := range c.Sections {
|
||||
err = SectionEncodePacket(w, s)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user