mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-04 09:10:00 +00:00
Rewrite RuntimeBlockMapping to use BlockStateSerializer
This commit is contained in:
parent
cab9b6c862
commit
979f6f3d57
74
src/network/mcpe/convert/BlockStateDictionary.php
Normal file
74
src/network/mcpe/convert/BlockStateDictionary.php
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\network\mcpe\convert;
|
||||||
|
|
||||||
|
use pocketmine\data\bedrock\blockstate\BlockStateData;
|
||||||
|
use pocketmine\nbt\TreeRoot;
|
||||||
|
use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer;
|
||||||
|
use function array_map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles translation of network block runtime IDs into blockstate data, and vice versa
|
||||||
|
*/
|
||||||
|
final class BlockStateDictionary{
|
||||||
|
|
||||||
|
private BlockStateLookupCache $lookupCache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param BlockStateData[] $states
|
||||||
|
*
|
||||||
|
* @phpstan-param list<BlockStateData> $states
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
private array $states
|
||||||
|
){
|
||||||
|
$this->lookupCache = new BlockStateLookupCache($this->states);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDataFromStateId(int $networkRuntimeId) : ?BlockStateData{
|
||||||
|
return $this->states[$networkRuntimeId] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches for the appropriate state ID which matches the given blockstate NBT.
|
||||||
|
* Returns null if there were no matches.
|
||||||
|
*/
|
||||||
|
public function lookupStateIdFromData(BlockStateData $data) : ?int{
|
||||||
|
return $this->lookupCache->lookupStateId($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array mapping runtime ID => blockstate data.
|
||||||
|
* @return BlockStateData[]
|
||||||
|
* @phpstan-return array<int, BlockStateData>
|
||||||
|
*/
|
||||||
|
public function getStates() : array{ return $this->states; }
|
||||||
|
|
||||||
|
public static function loadFromString(string $contents) : self{
|
||||||
|
return new self(array_map(
|
||||||
|
fn(TreeRoot $root) => BlockStateData::fromNbt($root->mustGetCompoundTag()),
|
||||||
|
(new NetworkNbtSerializer())->readMultiple($contents)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
87
src/network/mcpe/convert/BlockStateLookupCache.php
Normal file
87
src/network/mcpe/convert/BlockStateLookupCache.php
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\network\mcpe\convert;
|
||||||
|
|
||||||
|
use pocketmine\data\bedrock\blockstate\BlockStateData;
|
||||||
|
use pocketmine\utils\Utils;
|
||||||
|
use function array_key_first;
|
||||||
|
use function count;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Facilitates quickly looking up a block's state ID based on its NBT.
|
||||||
|
*/
|
||||||
|
final class BlockStateLookupCache{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int[][]
|
||||||
|
* @phpstan-var array<string, array<int, BlockStateData>>
|
||||||
|
*/
|
||||||
|
private array $nameToNetworkIdsLookup = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int[]
|
||||||
|
* @phpstan-var array<string, int>
|
||||||
|
*/
|
||||||
|
private array $nameToSingleNetworkIdLookup = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param BlockStateData[] $blockStates
|
||||||
|
* @phpstan-param list<BlockStateData> $blockStates
|
||||||
|
*/
|
||||||
|
public function __construct(array $blockStates){
|
||||||
|
foreach($blockStates as $stateId => $stateNbt){
|
||||||
|
$this->nameToNetworkIdsLookup[$stateNbt->getName()][$stateId] = $stateNbt;
|
||||||
|
}
|
||||||
|
|
||||||
|
//setup fast path for stateless blocks
|
||||||
|
foreach(Utils::stringifyKeys($this->nameToNetworkIdsLookup) as $name => $stateIds){
|
||||||
|
if(count($stateIds) === 1){
|
||||||
|
$this->nameToSingleNetworkIdLookup[$name] = array_key_first($stateIds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches for the appropriate state ID which matches the given blockstate NBT.
|
||||||
|
* Returns null if there were no matches.
|
||||||
|
*/
|
||||||
|
public function lookupStateId(BlockStateData $data) : ?int{
|
||||||
|
$name = $data->getName();
|
||||||
|
|
||||||
|
if(isset($this->nameToSingleNetworkIdLookup[$name])){
|
||||||
|
return $this->nameToSingleNetworkIdLookup[$name];
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isset($this->nameToNetworkIdsLookup[$name])){
|
||||||
|
$states = $data->getStates();
|
||||||
|
foreach($this->nameToNetworkIdsLookup[$name] as $stateId => $stateNbt){
|
||||||
|
if($stateNbt->getStates()->equals($states)){
|
||||||
|
return $stateId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -1,58 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
*
|
|
||||||
* ____ _ _ __ __ _ __ __ ____
|
|
||||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
|
||||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
|
||||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
|
||||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* @author PocketMine Team
|
|
||||||
* @link http://www.pocketmine.net/
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace pocketmine\network\mcpe\convert;
|
|
||||||
|
|
||||||
use pocketmine\nbt\tag\CompoundTag;
|
|
||||||
|
|
||||||
final class R12ToCurrentBlockMapEntry{
|
|
||||||
|
|
||||||
/** @var string */
|
|
||||||
private $id;
|
|
||||||
/** @var int */
|
|
||||||
private $meta;
|
|
||||||
/** @var CompoundTag */
|
|
||||||
private $blockState;
|
|
||||||
|
|
||||||
public function __construct(string $id, int $meta, CompoundTag $blockState){
|
|
||||||
$this->id = $id;
|
|
||||||
$this->meta = $meta;
|
|
||||||
$this->blockState = $blockState;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getId() : string{
|
|
||||||
return $this->id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getMeta() : int{
|
|
||||||
return $this->meta;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getBlockState() : CompoundTag{
|
|
||||||
return $this->blockState;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __toString(){
|
|
||||||
return "id=$this->id, meta=$this->meta, nbt=$this->blockState";
|
|
||||||
}
|
|
||||||
}
|
|
@ -23,13 +23,14 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\network\mcpe\convert;
|
namespace pocketmine\network\mcpe\convert;
|
||||||
|
|
||||||
use pocketmine\block\Block;
|
use pocketmine\block\BlockFactory;
|
||||||
use pocketmine\block\BlockLegacyIds;
|
use pocketmine\block\UnknownBlock;
|
||||||
use pocketmine\data\bedrock\LegacyBlockIdToStringIdMap;
|
use pocketmine\data\bedrock\blockstate\BlockStateData;
|
||||||
|
use pocketmine\data\bedrock\blockstate\BlockStateSerializeException;
|
||||||
|
use pocketmine\data\bedrock\blockstate\BlockStateSerializer;
|
||||||
|
use pocketmine\data\bedrock\blockstate\BlockTypeNames;
|
||||||
use pocketmine\nbt\tag\CompoundTag;
|
use pocketmine\nbt\tag\CompoundTag;
|
||||||
use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer;
|
use pocketmine\utils\AssumptionFailedError;
|
||||||
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
|
|
||||||
use pocketmine\network\mcpe\protocol\serializer\PacketSerializerContext;
|
|
||||||
use pocketmine\utils\SingletonTrait;
|
use pocketmine\utils\SingletonTrait;
|
||||||
use pocketmine\utils\Utils;
|
use pocketmine\utils\Utils;
|
||||||
use Webmozart\PathUtil\Path;
|
use Webmozart\PathUtil\Path;
|
||||||
@ -41,98 +42,54 @@ use function file_get_contents;
|
|||||||
final class RuntimeBlockMapping{
|
final class RuntimeBlockMapping{
|
||||||
use SingletonTrait;
|
use SingletonTrait;
|
||||||
|
|
||||||
/** @var int[] */
|
private BlockStateDictionary $blockStateDictionary;
|
||||||
private $legacyToRuntimeMap = [];
|
private BlockStateSerializer $blockStateSerializer;
|
||||||
/** @var int[] */
|
/**
|
||||||
private $runtimeToLegacyMap = [];
|
* @var int[]
|
||||||
/** @var CompoundTag[] */
|
* @phpstan-var array<int, int>
|
||||||
private $bedrockKnownStates;
|
*/
|
||||||
|
private array $networkIdCache = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used when a blockstate can't be correctly serialized (e.g. because it's unknown)
|
||||||
|
*/
|
||||||
|
private BlockStateData $fallbackStateData;
|
||||||
|
|
||||||
private function __construct(){
|
private function __construct(){
|
||||||
$stream = PacketSerializer::decoder(
|
$contents = Utils::assumeNotFalse(file_get_contents(Path::join(\pocketmine\BEDROCK_DATA_PATH, "canonical_block_states.nbt")), "Missing required resource file");
|
||||||
Utils::assumeNotFalse(file_get_contents(Path::join(\pocketmine\BEDROCK_DATA_PATH, "canonical_block_states.nbt")), "Missing required resource file"),
|
$this->blockStateDictionary = BlockStateDictionary::loadFromString($contents);
|
||||||
0,
|
$this->blockStateSerializer = new BlockStateSerializer();
|
||||||
new PacketSerializerContext(GlobalItemTypeDictionary::getInstance()->getDictionary())
|
|
||||||
);
|
|
||||||
$list = [];
|
|
||||||
while(!$stream->feof()){
|
|
||||||
$list[] = $stream->getNbtCompoundRoot();
|
|
||||||
}
|
|
||||||
$this->bedrockKnownStates = $list;
|
|
||||||
|
|
||||||
$this->setupLegacyMappings();
|
$this->fallbackStateData = new BlockStateData(BlockTypeNames::INFO_UPDATE, CompoundTag::create(), BlockStateData::CURRENT_VERSION);
|
||||||
}
|
|
||||||
|
|
||||||
private function setupLegacyMappings() : void{
|
|
||||||
$legacyIdMap = LegacyBlockIdToStringIdMap::getInstance();
|
|
||||||
/** @var R12ToCurrentBlockMapEntry[] $legacyStateMap */
|
|
||||||
$legacyStateMap = [];
|
|
||||||
$legacyStateMapReader = PacketSerializer::decoder(
|
|
||||||
Utils::assumeNotFalse(file_get_contents(Path::join(\pocketmine\BEDROCK_DATA_PATH, "r12_to_current_block_map.bin")), "Missing required resource file"),
|
|
||||||
0,
|
|
||||||
new PacketSerializerContext(GlobalItemTypeDictionary::getInstance()->getDictionary())
|
|
||||||
);
|
|
||||||
$nbtReader = new NetworkNbtSerializer();
|
|
||||||
while(!$legacyStateMapReader->feof()){
|
|
||||||
$id = $legacyStateMapReader->getString();
|
|
||||||
$meta = $legacyStateMapReader->getLShort();
|
|
||||||
|
|
||||||
$offset = $legacyStateMapReader->getOffset();
|
|
||||||
$state = $nbtReader->read($legacyStateMapReader->getBuffer(), $offset)->mustGetCompoundTag();
|
|
||||||
$legacyStateMapReader->setOffset($offset);
|
|
||||||
$legacyStateMap[] = new R12ToCurrentBlockMapEntry($id, $meta, $state);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int[][] $idToStatesMap string id -> int[] list of candidate state indices
|
|
||||||
*/
|
|
||||||
$idToStatesMap = [];
|
|
||||||
foreach($this->bedrockKnownStates as $k => $state){
|
|
||||||
$idToStatesMap[$state->getString("name")][] = $k;
|
|
||||||
}
|
|
||||||
foreach($legacyStateMap as $pair){
|
|
||||||
$id = $legacyIdMap->stringToLegacy($pair->getId());
|
|
||||||
if($id === null){
|
|
||||||
throw new \RuntimeException("No legacy ID matches " . $pair->getId());
|
|
||||||
}
|
|
||||||
$data = $pair->getMeta();
|
|
||||||
if($data > 15){
|
|
||||||
//we can't handle metadata with more than 4 bits
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$mappedState = $pair->getBlockState();
|
|
||||||
$mappedName = $mappedState->getString("name");
|
|
||||||
if(!isset($idToStatesMap[$mappedName])){
|
|
||||||
throw new \RuntimeException("Mapped new state does not appear in network table");
|
|
||||||
}
|
|
||||||
foreach($idToStatesMap[$mappedName] as $k){
|
|
||||||
$networkState = $this->bedrockKnownStates[$k];
|
|
||||||
if($mappedState->equals($networkState)){
|
|
||||||
$this->registerMapping($k, $id, $data);
|
|
||||||
continue 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new \RuntimeException("Mapped new state does not appear in network table");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toRuntimeId(int $internalStateId) : int{
|
public function toRuntimeId(int $internalStateId) : int{
|
||||||
return $this->legacyToRuntimeMap[$internalStateId] ?? $this->legacyToRuntimeMap[BlockLegacyIds::INFO_UPDATE << Block::INTERNAL_METADATA_BITS];
|
if(isset($this->networkIdCache[$internalStateId])){
|
||||||
|
return $this->networkIdCache[$internalStateId];
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: singleton usage not ideal
|
||||||
|
$block = BlockFactory::getInstance()->fromFullBlock($internalStateId);
|
||||||
|
if($block instanceof UnknownBlock){
|
||||||
|
$blockStateData = $this->fallbackStateData;
|
||||||
|
}else{
|
||||||
|
try{
|
||||||
|
$blockStateData = $this->blockStateSerializer->serialize($block);
|
||||||
|
}catch(BlockStateSerializeException $e){
|
||||||
|
throw new AssumptionFailedError("Invalid serializer for block $block", 0, $e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$networkId = $this->blockStateDictionary->lookupStateIdFromData($blockStateData);
|
||||||
|
|
||||||
|
if($networkId === null){
|
||||||
|
throw new AssumptionFailedError("Unmapped blockstate returned by blockstate serializer: " . $blockStateData->toNbt());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->networkIdCache[$internalStateId] = $networkId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fromRuntimeId(int $runtimeId) : int{
|
public function getBlockStateDictionary() : BlockStateDictionary{ return $this->blockStateDictionary; }
|
||||||
return $this->runtimeToLegacyMap[$runtimeId];
|
|
||||||
}
|
|
||||||
|
|
||||||
private function registerMapping(int $staticRuntimeId, int $legacyId, int $legacyMeta) : void{
|
public function getFallbackStateData() : BlockStateData{ return $this->fallbackStateData; }
|
||||||
$this->legacyToRuntimeMap[($legacyId << Block::INTERNAL_METADATA_BITS) | $legacyMeta] = $staticRuntimeId;
|
|
||||||
$this->runtimeToLegacyMap[$staticRuntimeId] = ($legacyId << Block::INTERNAL_METADATA_BITS) | $legacyMeta;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return CompoundTag[]
|
|
||||||
*/
|
|
||||||
public function getBedrockKnownStates() : array{
|
|
||||||
return $this->bedrockKnownStates;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,10 @@ namespace pocketmine\network\mcpe\serializer;
|
|||||||
|
|
||||||
use pocketmine\block\tile\Spawnable;
|
use pocketmine\block\tile\Spawnable;
|
||||||
use pocketmine\data\bedrock\BiomeIds;
|
use pocketmine\data\bedrock\BiomeIds;
|
||||||
|
use pocketmine\data\bedrock\blockstate\BlockStateData;
|
||||||
|
use pocketmine\data\bedrock\blockstate\BlockTypeNames;
|
||||||
use pocketmine\data\bedrock\LegacyBiomeIdToStringIdMap;
|
use pocketmine\data\bedrock\LegacyBiomeIdToStringIdMap;
|
||||||
|
use pocketmine\nbt\tag\CompoundTag;
|
||||||
use pocketmine\nbt\TreeRoot;
|
use pocketmine\nbt\TreeRoot;
|
||||||
use pocketmine\network\mcpe\convert\RuntimeBlockMapping;
|
use pocketmine\network\mcpe\convert\RuntimeBlockMapping;
|
||||||
use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer;
|
use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer;
|
||||||
@ -97,6 +100,8 @@ final class ChunkSerializer{
|
|||||||
|
|
||||||
$stream->putByte(count($layers));
|
$stream->putByte(count($layers));
|
||||||
|
|
||||||
|
$blockStateDictionary = $blockMapper->getBlockStateDictionary();
|
||||||
|
|
||||||
foreach($layers as $blocks){
|
foreach($layers as $blocks){
|
||||||
$bitsPerBlock = $blocks->getBitsPerBlock();
|
$bitsPerBlock = $blocks->getBitsPerBlock();
|
||||||
$words = $blocks->getWordArray();
|
$words = $blocks->getWordArray();
|
||||||
@ -113,7 +118,13 @@ final class ChunkSerializer{
|
|||||||
if($persistentBlockStates){
|
if($persistentBlockStates){
|
||||||
$nbtSerializer = new NetworkNbtSerializer();
|
$nbtSerializer = new NetworkNbtSerializer();
|
||||||
foreach($palette as $p){
|
foreach($palette as $p){
|
||||||
$stream->put($nbtSerializer->write(new TreeRoot($blockMapper->getBedrockKnownStates()[$blockMapper->toRuntimeId($p)])));
|
//TODO: introduce a binary cache for this
|
||||||
|
$state = $blockStateDictionary->getDataFromStateId($blockMapper->toRuntimeId($p));
|
||||||
|
if($state === null){
|
||||||
|
$state = $blockMapper->getFallbackStateData();
|
||||||
|
}
|
||||||
|
|
||||||
|
$stream->put($nbtSerializer->write(new TreeRoot($state->toNbt())));
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
foreach($palette as $p){
|
foreach($palette as $p){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user