Add DowngradeLevelChunkPacket
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
|
||||
"github.com/akmalfairuz/legacy-version/internal"
|
||||
"github.com/akmalfairuz/legacy-version/internal/chunk"
|
||||
"github.com/akmalfairuz/legacy-version/mapping"
|
||||
"github.com/df-mc/dragonfly/server/block/cube"
|
||||
@@ -31,6 +30,10 @@ type BlockTranslator interface {
|
||||
DowngradeBlockPackets([]packet.Packet, *minecraft.Conn) (result []packet.Packet)
|
||||
// UpgradeBlockPackets upgrades the input block packets to the latest block packets.
|
||||
UpgradeBlockPackets([]packet.Packet, *minecraft.Conn) (result []packet.Packet)
|
||||
// DowngradeLevelChunk downgrades the given LevelChunk packet to a legacy format.
|
||||
DowngradeLevelChunk(*packet.LevelChunk) error
|
||||
// BlockMapping returns the block mapping used by this translator.
|
||||
BlockMapping() mapping.Block
|
||||
}
|
||||
|
||||
type DefaultBlockTranslator struct {
|
||||
@@ -45,18 +48,18 @@ func NewBlockTranslator(mapping mapping.Block, latestMapping mapping.Block, pse
|
||||
return &DefaultBlockTranslator{mapping: mapping, latest: latestMapping, pse: pse, pe: pe, oldFormat: oldFormat}
|
||||
}
|
||||
|
||||
func (t *DefaultBlockTranslator) downgradeLevelChunkPacket(pk *packet.LevelChunk) error {
|
||||
func (t *DefaultBlockTranslator) BlockMapping() mapping.Block {
|
||||
return t.mapping
|
||||
}
|
||||
|
||||
func (t *DefaultBlockTranslator) DowngradeLevelChunk(pk *packet.LevelChunk) error {
|
||||
count := int(pk.SubChunkCount)
|
||||
if count == protocol.SubChunkRequestModeLimitless || count == protocol.SubChunkRequestModeLimited {
|
||||
return nil
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(pk.RawPayload)
|
||||
writeBuf := internal.BufferPool.Get().(*bytes.Buffer)
|
||||
defer func() {
|
||||
writeBuf.Reset()
|
||||
internal.BufferPool.Put(writeBuf)
|
||||
}()
|
||||
writeBuf := bytes.NewBuffer(nil)
|
||||
if !pk.CacheEnabled {
|
||||
c, err := chunk.NetworkDecode(t.latest.Air(), buf, count, false, world.Overworld.Range(), LatestNetworkPersistentEncoding, LatestBlockPaletteEncoding)
|
||||
if err != nil {
|
||||
@@ -76,12 +79,12 @@ func (t *DefaultBlockTranslator) downgradeLevelChunkPacket(pk *packet.LevelChunk
|
||||
countBorder, err := buf.ReadByte()
|
||||
if err != nil {
|
||||
pk.RawPayload = append(writeBuf.Bytes(), safeBytes...)
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
borderBytes := make([]byte, countBorder)
|
||||
if _, err = buf.Read(borderBytes); err != nil {
|
||||
pk.RawPayload = append(writeBuf.Bytes(), safeBytes...)
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
writeBuf.WriteByte(countBorder)
|
||||
writeBuf.Write(borderBytes)
|
||||
@@ -107,8 +110,17 @@ func (t *DefaultBlockTranslator) DowngradeBlockPackets(pks []packet.Packet, conn
|
||||
for _, pk := range pks {
|
||||
switch pk := pk.(type) {
|
||||
case *packet.LevelChunk:
|
||||
_ = t.downgradeLevelChunkPacket(pk)
|
||||
if !EnableChunkTranslation {
|
||||
break
|
||||
}
|
||||
if err := t.DowngradeLevelChunk(pk); err != nil {
|
||||
//fmt.Println(err)
|
||||
break
|
||||
}
|
||||
case *packet.SubChunk:
|
||||
if !EnableChunkTranslation {
|
||||
break
|
||||
}
|
||||
r := world.Overworld.Range()
|
||||
if t.oldFormat {
|
||||
r = cube.Range{0, 255}
|
||||
@@ -147,25 +159,27 @@ func (t *DefaultBlockTranslator) DowngradeBlockPackets(pks []packet.Packet, conn
|
||||
pk.SubChunkEntries[i] = entry
|
||||
}
|
||||
}
|
||||
case *packet.ClientCacheMissResponse:
|
||||
r := world.Overworld.Range()
|
||||
if t.oldFormat {
|
||||
r = cube.Range{0, 255}
|
||||
}
|
||||
|
||||
for i, blob := range pk.Blobs {
|
||||
buf := bytes.NewBuffer(blob.Payload)
|
||||
ind := byte(0)
|
||||
subChunk, err := chunk.DecodeSubChunk(t.latest.Air(), r, buf, &ind, chunk.NetworkEncoding, LatestNetworkPersistentEncoding, LatestBlockPaletteEncoding)
|
||||
if err != nil {
|
||||
// Has a possibility to be a biome, ignore then
|
||||
continue
|
||||
}
|
||||
t.DowngradeSubChunk(subChunk)
|
||||
|
||||
blob.Payload = append(chunk.EncodeSubChunk(subChunk, chunk.NetworkEncoding, t.pe, chunk.SubChunkVersion9, r, int(ind)), buf.Bytes()...)
|
||||
pk.Blobs[i] = blob
|
||||
}
|
||||
//case *packet.ClientCacheMissResponse:
|
||||
// if EnableChunkTranslation {
|
||||
// r := world.Overworld.Range()
|
||||
// if t.oldFormat {
|
||||
// r = cube.Range{0, 255}
|
||||
// }
|
||||
//
|
||||
// for i, blob := range pk.Blobs {
|
||||
// buf := bytes.NewBuffer(blob.Payload)
|
||||
// ind := byte(0)
|
||||
// subChunk, err := chunk.DecodeSubChunk(t.latest.Air(), r, buf, &ind, chunk.NetworkEncoding, LatestNetworkPersistentEncoding, LatestBlockPaletteEncoding)
|
||||
// if err != nil {
|
||||
// // Has a possibility to be a biome, ignore then
|
||||
// continue
|
||||
// }
|
||||
// t.DowngradeSubChunk(subChunk)
|
||||
//
|
||||
// blob.Payload = append(chunk.EncodeSubChunk(subChunk, chunk.NetworkEncoding, t.pe, chunk.SubChunkVersion9, r, int(ind)), buf.Bytes()...)
|
||||
// pk.Blobs[i] = blob
|
||||
// }
|
||||
// }
|
||||
case *packet.UpdateSubChunkBlocks:
|
||||
for i, block := range pk.Blocks {
|
||||
block.BlockRuntimeID = t.DowngradeBlockRuntimeID(block.BlockRuntimeID)
|
||||
|
||||
Reference in New Issue
Block a user