Rewrite RuntimeBlockMapping to use BlockStateSerializer

This commit is contained in:
Dylan K. Taylor 2022-02-01 17:34:32 +00:00
parent cab9b6c862
commit 979f6f3d57
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
5 changed files with 221 additions and 150 deletions

View 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)
));
}
}

View 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;
}
}

View File

@ -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";
}
}

View File

@ -23,13 +23,14 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\convert;
use pocketmine\block\Block;
use pocketmine\block\BlockLegacyIds;
use pocketmine\data\bedrock\LegacyBlockIdToStringIdMap;
use pocketmine\block\BlockFactory;
use pocketmine\block\UnknownBlock;
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\network\mcpe\protocol\serializer\NetworkNbtSerializer;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializerContext;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\SingletonTrait;
use pocketmine\utils\Utils;
use Webmozart\PathUtil\Path;
@ -41,98 +42,54 @@ use function file_get_contents;
final class RuntimeBlockMapping{
use SingletonTrait;
/** @var int[] */
private $legacyToRuntimeMap = [];
/** @var int[] */
private $runtimeToLegacyMap = [];
/** @var CompoundTag[] */
private $bedrockKnownStates;
private BlockStateDictionary $blockStateDictionary;
private BlockStateSerializer $blockStateSerializer;
/**
* @var int[]
* @phpstan-var array<int, int>
*/
private array $networkIdCache = [];
/**
* Used when a blockstate can't be correctly serialized (e.g. because it's unknown)
*/
private BlockStateData $fallbackStateData;
private function __construct(){
$stream = PacketSerializer::decoder(
Utils::assumeNotFalse(file_get_contents(Path::join(\pocketmine\BEDROCK_DATA_PATH, "canonical_block_states.nbt")), "Missing required resource file"),
0,
new PacketSerializerContext(GlobalItemTypeDictionary::getInstance()->getDictionary())
);
$list = [];
while(!$stream->feof()){
$list[] = $stream->getNbtCompoundRoot();
}
$this->bedrockKnownStates = $list;
$contents = Utils::assumeNotFalse(file_get_contents(Path::join(\pocketmine\BEDROCK_DATA_PATH, "canonical_block_states.nbt")), "Missing required resource file");
$this->blockStateDictionary = BlockStateDictionary::loadFromString($contents);
$this->blockStateSerializer = new BlockStateSerializer();
$this->setupLegacyMappings();
}
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");
}
$this->fallbackStateData = new BlockStateData(BlockTypeNames::INFO_UPDATE, CompoundTag::create(), BlockStateData::CURRENT_VERSION);
}
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{
return $this->runtimeToLegacyMap[$runtimeId];
}
public function getBlockStateDictionary() : BlockStateDictionary{ return $this->blockStateDictionary; }
private function registerMapping(int $staticRuntimeId, int $legacyId, int $legacyMeta) : void{
$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;
}
public function getFallbackStateData() : BlockStateData{ return $this->fallbackStateData; }
}

View File

@ -25,7 +25,10 @@ namespace pocketmine\network\mcpe\serializer;
use pocketmine\block\tile\Spawnable;
use pocketmine\data\bedrock\BiomeIds;
use pocketmine\data\bedrock\blockstate\BlockStateData;
use pocketmine\data\bedrock\blockstate\BlockTypeNames;
use pocketmine\data\bedrock\LegacyBiomeIdToStringIdMap;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\TreeRoot;
use pocketmine\network\mcpe\convert\RuntimeBlockMapping;
use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer;
@ -97,6 +100,8 @@ final class ChunkSerializer{
$stream->putByte(count($layers));
$blockStateDictionary = $blockMapper->getBlockStateDictionary();
foreach($layers as $blocks){
$bitsPerBlock = $blocks->getBitsPerBlock();
$words = $blocks->getWordArray();
@ -113,7 +118,13 @@ final class ChunkSerializer{
if($persistentBlockStates){
$nbtSerializer = new NetworkNbtSerializer();
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{
foreach($palette as $p){