start work on mca to packet conversion

This commit is contained in:
adman
2026-05-03 18:20:36 +02:00
parent 94d15258d8
commit 5d3860b91e
8 changed files with 47711 additions and 341 deletions
+30 -12
View File
@@ -33,13 +33,22 @@ func (c Chunk) ToStorage(version string) (toChunk *level.Chunk, err error) {
}
}
// this is probably fine
uintSlice := unsafe.Slice((*uint64)(unsafe.Pointer(unsafe.SliceData(s.BlockStates.Data))), len(s.BlockStates.Data))
toChunk.Sections[i].Blocks, err = f.Import(uintSlice, util.BpeByNum(float64(len(s.BlockStates.Palette))), pallete)
if err != nil {
return
if s.BlockStates.Data != nil {
uintSlice := unsafe.Slice((*uint64)(unsafe.Pointer(unsafe.SliceData(s.BlockStates.Data))), len(s.BlockStates.Data))
bpe := util.BpeByNum(float64(len(s.BlockStates.Palette)))
toChunk.Sections[i].Blocks, err = f.Import(uintSlice, bpe, pallete)
if err != nil {
return
}
toChunk.Sections[i].BlockCount = int16(level.CountStorage(toChunk.Sections[i].Blocks, 0))
} else {
toChunk.Sections[i].Blocks, err = f.FullWith(0) //TODO PALLETE index 0 if avial
if err != nil {
return
}
toChunk.Sections[i].BlockCount = int16(f.Len)
}
toChunk.Sections[i].BlockCount = int16(level.CountStorage(toChunk.Sections[i].Blocks, 0))
}
for i, s := range c.Sections {
@@ -53,12 +62,21 @@ func (c Chunk) ToStorage(version string) (toChunk *level.Chunk, err error) {
}
pallete[j] = b.Id
}
// this is probably fine
uintSlice := unsafe.Slice((*uint64)(unsafe.Pointer(unsafe.SliceData(s.Biomes.Data))), len(s.Biomes.Data))
toChunk.Sections[i].Biomes, err = f.Import(uintSlice, util.BpeByNum(float64(len(pallete))), pallete)
if err != nil {
return
if s.Biomes.Data != nil {
// this is probably fine
uintSlice := unsafe.Slice((*uint64)(unsafe.Pointer(unsafe.SliceData(s.Biomes.Data))), len(s.Biomes.Data))
bpe := util.BpeByNum(float64(len(pallete)))
toChunk.Sections[i].Biomes, err = f.Import(uintSlice, bpe, pallete)
if err != nil {
return
}
} else {
toChunk.Sections[i].Biomes, err = f.FullWith(0) //TODO PALLETE index 0 if avial
if err != nil {
return
}
}
}
return
@@ -72,9 +90,9 @@ type Section struct {
Name string
Properties map[string]string `nbt:"properties,omitempty"`
}
}
} `nbt:"block_states,omitempty"`
Biomes struct {
Data []int64 `nbt:"data,omitempty"`
Palette []string
}
} `nbt:"biomes,omitempty"`
}
+40 -1
View File
@@ -1,13 +1,17 @@
package anvil_test
import (
"bytes"
"encoding/json"
"fmt"
"os"
"testing"
"github.com/admin-else/strom/level"
"github.com/admin-else/strom/level/anvil"
"github.com/admin-else/strom/nbt"
"github.com/admin-else/strom/util"
"github.com/google/go-cmp/cmp"
)
func TestLevel(t *testing.T) {
@@ -45,10 +49,45 @@ func TestRegionFile(t *testing.T) {
}
func TestConverToPacket(t *testing.T) {
f, err := os.Open("./level/anvil/testdata/r.0.0.mca")
f, err := os.Open("./testdata/r.0.0.mca")
if err != nil {
t.Errorf("Failed to open file: %v", err)
return
}
defer f.Close()
mca, err := anvil.ReadChunkHeader(f)
if err != nil {
t.Errorf("Failed to read chunk header: %v", err)
return
}
var n *nbt.Tag
n, err = mca.ChunkAt(f, 0, 0)
if err != nil {
t.Errorf("Failed to read chunk: %v", err)
return
}
var chunk anvil.Chunk
err = nbt.Format.Decode(n.Value, &chunk)
if err != nil {
t.Errorf("Failed to decode chunk: %v", err)
return
}
var packetChunk *level.Chunk
packetChunk, err = chunk.ToStorage("1.21.11")
if err != nil {
t.Errorf("Failed to convert chunk: %v", err)
return
}
buffer := bytes.NewBuffer(nil)
err = packetChunk.WriteChunkData(buffer)
if err != nil {
t.Errorf("Failed to write chunk data: %v", err)
return
}
a := chunk00.ChunkData.Val
b := buffer.Bytes()
if !bytes.Equal(a, b) {
t.Errorf("Chunk data is different: %v", cmp.Diff(a, b))
}
}
Binary file not shown.
File diff suppressed because it is too large Load Diff
Binary file not shown.
File diff suppressed because it is too large Load Diff