mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 17:59:48 +00:00
BlockStateData: use array<Tag> instead of CompoundTag to store state properties
this reduces the footprint of RuntimeBlockMapping by a further 1 MB, as well as simplifying various parts of the code, and solidifying the immutability guarantee of BlockStateData.
This commit is contained in:
@ -25,6 +25,8 @@ namespace pocketmine\data\bedrock\block;
|
||||
|
||||
use pocketmine\nbt\NbtException;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
use pocketmine\nbt\tag\Tag;
|
||||
use pocketmine\utils\Utils;
|
||||
use function array_keys;
|
||||
use function count;
|
||||
use function implode;
|
||||
@ -46,15 +48,27 @@ final class BlockStateData{
|
||||
public const TAG_STATES = "states";
|
||||
public const TAG_VERSION = "version";
|
||||
|
||||
/**
|
||||
* @param Tag[] $states
|
||||
* @phpstan-param array<string, Tag> $states
|
||||
*/
|
||||
public function __construct(
|
||||
private string $name,
|
||||
private CompoundTag $states,
|
||||
private array $states,
|
||||
private int $version
|
||||
){}
|
||||
|
||||
public function getName() : string{ return $this->name; }
|
||||
|
||||
public function getStates() : CompoundTag{ return $this->states; }
|
||||
/**
|
||||
* @return Tag[]
|
||||
* @phpstan-return array<string, Tag>
|
||||
*/
|
||||
public function getStates() : array{ return $this->states; }
|
||||
|
||||
public function getState(string $name) : ?Tag{
|
||||
return $this->states[$name] ?? null;
|
||||
}
|
||||
|
||||
public function getVersion() : int{ return $this->version; }
|
||||
|
||||
@ -76,20 +90,30 @@ final class BlockStateData{
|
||||
throw new BlockStateDeserializeException("Unexpected extra keys: " . implode(", ", array_keys($allKeys)));
|
||||
}
|
||||
|
||||
return new self($name, $states, $version);
|
||||
return new self($name, $states->getValue(), $version);
|
||||
}
|
||||
|
||||
public function toNbt() : CompoundTag{
|
||||
$statesTag = CompoundTag::create();
|
||||
foreach(Utils::stringifyKeys($this->states) as $key => $value){
|
||||
$statesTag->setTag($key, $value);
|
||||
}
|
||||
return CompoundTag::create()
|
||||
->setString(self::TAG_NAME, $this->name)
|
||||
->setInt(self::TAG_VERSION, $this->version)
|
||||
->setTag(self::TAG_STATES, $this->states);
|
||||
->setTag(self::TAG_STATES, $statesTag);
|
||||
}
|
||||
|
||||
public function equals(self $that) : bool{
|
||||
return
|
||||
$this->name === $that->name &&
|
||||
$this->states->equals($that->states) &&
|
||||
$this->version === $that->version;
|
||||
if($this->name !== $that->name || count($this->states) !== count($that->states)){
|
||||
return false;
|
||||
}
|
||||
foreach(Utils::stringifyKeys($this->states) as $k => $v){
|
||||
if(!isset($that->states[$k]) || !$that->states[$k]->equals($v)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\data\bedrock\block;
|
||||
|
||||
use function count;
|
||||
|
||||
final class CachingBlockStateDeserializer implements DelegatingBlockStateDeserializer{
|
||||
|
||||
/**
|
||||
@ -36,7 +38,7 @@ final class CachingBlockStateDeserializer implements DelegatingBlockStateDeseria
|
||||
){}
|
||||
|
||||
public function deserialize(BlockStateData $stateData) : int{
|
||||
if($stateData->getStates()->count() === 0){
|
||||
if(count($stateData->getStates()) === 0){
|
||||
//if a block has zero properties, we can keep a map of string ID -> internal blockstate ID
|
||||
return $this->simpleCache[$stateData->getName()] ??= $this->realDeserializer->deserialize($stateData);
|
||||
}
|
||||
|
@ -38,6 +38,7 @@ use pocketmine\nbt\tag\ByteTag;
|
||||
use pocketmine\nbt\tag\IntTag;
|
||||
use pocketmine\nbt\tag\StringTag;
|
||||
use pocketmine\nbt\tag\Tag;
|
||||
use pocketmine\utils\Utils;
|
||||
use function get_class;
|
||||
|
||||
final class BlockStateReader{
|
||||
@ -66,7 +67,7 @@ final class BlockStateReader{
|
||||
/** @throws BlockStateDeserializeException */
|
||||
public function readBool(string $name) : bool{
|
||||
$this->usedStates[$name] = true;
|
||||
$tag = $this->data->getStates()->getTag($name);
|
||||
$tag = $this->data->getState($name);
|
||||
if($tag instanceof ByteTag){
|
||||
switch($tag->getValue()){
|
||||
case 0: return false;
|
||||
@ -80,7 +81,7 @@ final class BlockStateReader{
|
||||
/** @throws BlockStateDeserializeException */
|
||||
public function readInt(string $name) : int{
|
||||
$this->usedStates[$name] = true;
|
||||
$tag = $this->data->getStates()->getTag($name);
|
||||
$tag = $this->data->getState($name);
|
||||
if($tag instanceof IntTag){
|
||||
return $tag->getValue();
|
||||
}
|
||||
@ -100,7 +101,7 @@ final class BlockStateReader{
|
||||
public function readString(string $name) : string{
|
||||
$this->usedStates[$name] = true;
|
||||
//TODO: only allow a specific set of values (strings are primarily used for enums)
|
||||
$tag = $this->data->getStates()->getTag($name);
|
||||
$tag = $this->data->getState($name);
|
||||
if($tag instanceof StringTag){
|
||||
return $tag->getValue();
|
||||
}
|
||||
@ -314,7 +315,7 @@ final class BlockStateReader{
|
||||
* Explicitly mark a property as unused, so it doesn't get flagged as an error when debug mode is enabled
|
||||
*/
|
||||
public function ignored(string $name) : void{
|
||||
if($this->data->getStates()->getTag($name) !== null){
|
||||
if($this->data->getState($name) !== null){
|
||||
$this->usedStates[$name] = true;
|
||||
}else{
|
||||
throw $this->missingOrWrongTypeException($name, null);
|
||||
@ -332,7 +333,7 @@ final class BlockStateReader{
|
||||
* @throws BlockStateDeserializeException
|
||||
*/
|
||||
public function checkUnreadProperties() : void{
|
||||
foreach($this->data->getStates() as $name => $tag){
|
||||
foreach(Utils::stringifyKeys($this->data->getStates()) as $name => $tag){
|
||||
if(!isset($this->usedStates[$name])){
|
||||
throw new BlockStateDeserializeException("Unread property \"$name\"");
|
||||
}
|
||||
|
@ -35,17 +35,22 @@ use pocketmine\data\bedrock\block\BlockStateSerializeException;
|
||||
use pocketmine\data\bedrock\block\BlockStateStringValues as StringValues;
|
||||
use pocketmine\math\Axis;
|
||||
use pocketmine\math\Facing;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
use pocketmine\nbt\tag\ByteTag;
|
||||
use pocketmine\nbt\tag\IntTag;
|
||||
use pocketmine\nbt\tag\StringTag;
|
||||
use pocketmine\nbt\tag\Tag;
|
||||
|
||||
final class BlockStateWriter{
|
||||
|
||||
private CompoundTag $states;
|
||||
/**
|
||||
* @var Tag[]
|
||||
* @phpstan-var array<string, Tag>
|
||||
*/
|
||||
private array $states = [];
|
||||
|
||||
public function __construct(
|
||||
private string $id
|
||||
){
|
||||
$this->states = CompoundTag::create();
|
||||
}
|
||||
){}
|
||||
|
||||
public static function create(string $id) : self{
|
||||
return new self($id);
|
||||
@ -53,19 +58,19 @@ final class BlockStateWriter{
|
||||
|
||||
/** @return $this */
|
||||
public function writeBool(string $name, bool $value) : self{
|
||||
$this->states->setByte($name, $value ? 1 : 0);
|
||||
$this->states[$name] = new ByteTag($value ? 1 : 0);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** @return $this */
|
||||
public function writeInt(string $name, int $value) : self{
|
||||
$this->states->setInt($name, $value);
|
||||
$this->states[$name] = new IntTag($value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** @return $this */
|
||||
public function writeString(string $name, string $value) : self{
|
||||
$this->states->setString($name, $value);
|
||||
$this->states[$name] = new StringTag($value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -51,7 +51,7 @@ final class BlockDataUpgrader{
|
||||
$blockStateData = $this->upgradeStringIdMeta($id, $data);
|
||||
if($blockStateData === null){
|
||||
//unknown block, invalid ID
|
||||
$blockStateData = new BlockStateData(BlockTypeNames::INFO_UPDATE, CompoundTag::create(), BlockStateData::CURRENT_VERSION);
|
||||
$blockStateData = new BlockStateData(BlockTypeNames::INFO_UPDATE, [], BlockStateData::CURRENT_VERSION);
|
||||
}
|
||||
}else{
|
||||
//Modern (post-1.13) blockstate
|
||||
|
@ -23,15 +23,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\data\bedrock\block\upgrade;
|
||||
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
use pocketmine\nbt\tag\Tag;
|
||||
use pocketmine\utils\Utils;
|
||||
|
||||
final class BlockStateUpgradeSchemaBlockRemap{
|
||||
|
||||
public CompoundTag $oldState;
|
||||
public CompoundTag $newState;
|
||||
|
||||
/**
|
||||
* @param Tag[] $oldState
|
||||
* @param Tag[] $newState
|
||||
@ -39,17 +33,8 @@ final class BlockStateUpgradeSchemaBlockRemap{
|
||||
* @phpstan-param array<string, Tag> $newState
|
||||
*/
|
||||
public function __construct(
|
||||
array $oldState,
|
||||
public array $oldState,
|
||||
public string $newName,
|
||||
array $newState
|
||||
){
|
||||
$this->oldState = CompoundTag::create();
|
||||
$this->newState = CompoundTag::create();
|
||||
foreach(Utils::stringifyKeys($oldState) as $k => $v){
|
||||
$this->oldState->setTag($k, $v);
|
||||
}
|
||||
foreach(Utils::stringifyKeys($newState) as $k => $v){
|
||||
$this->newState->setTag($k, $v);
|
||||
}
|
||||
}
|
||||
public array $newState
|
||||
){}
|
||||
}
|
||||
|
@ -240,9 +240,9 @@ final class BlockStateUpgradeSchemaUtils{
|
||||
foreach(Utils::stringifyKeys($schema->remappedStates) as $oldBlockName => $remaps){
|
||||
foreach($remaps as $remap){
|
||||
$result->remappedStates[$oldBlockName][] = new BlockStateUpgradeSchemaModelBlockRemap(
|
||||
array_map(fn(Tag $tag) => self::tagToJsonModel($tag), $remap->oldState->getValue()),
|
||||
array_map(fn(Tag $tag) => self::tagToJsonModel($tag), $remap->oldState),
|
||||
$remap->newName,
|
||||
array_map(fn(Tag $tag) => self::tagToJsonModel($tag), $remap->newState->getValue()),
|
||||
array_map(fn(Tag $tag) => self::tagToJsonModel($tag), $remap->newState),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -24,9 +24,9 @@ declare(strict_types=1);
|
||||
namespace pocketmine\data\bedrock\block\upgrade;
|
||||
|
||||
use pocketmine\data\bedrock\block\BlockStateData;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
use pocketmine\nbt\tag\Tag;
|
||||
use pocketmine\utils\Utils;
|
||||
use function count;
|
||||
use function ksort;
|
||||
use const SORT_NUMERIC;
|
||||
|
||||
@ -68,12 +68,20 @@ final class BlockStateUpgrader{
|
||||
}
|
||||
foreach($schemas as $schema){
|
||||
$oldName = $blockStateData->getName();
|
||||
$oldState = $blockStateData->getStates();
|
||||
if(isset($schema->remappedStates[$oldName])){
|
||||
foreach($schema->remappedStates[$oldName] as $remap){
|
||||
if($blockStateData->getStates()->equals($remap->oldState)){
|
||||
$blockStateData = new BlockStateData($remap->newName, clone $remap->newState, $resultVersion);
|
||||
continue 2;
|
||||
if(count($oldState) !== count($remap->oldState)){
|
||||
continue; //try next state
|
||||
}
|
||||
foreach(Utils::stringifyKeys($oldState) as $k => $v){
|
||||
if(!isset($remap->oldState[$k]) || !$remap->oldState[$k]->equals($v)){
|
||||
continue 2; //try next state
|
||||
}
|
||||
}
|
||||
|
||||
$blockStateData = new BlockStateData($remap->newName, $remap->newState, $resultVersion);
|
||||
continue 2; //try next schema
|
||||
}
|
||||
}
|
||||
$newName = $schema->renamedIds[$oldName] ?? null;
|
||||
@ -96,42 +104,44 @@ final class BlockStateUpgrader{
|
||||
return $blockStateData;
|
||||
}
|
||||
|
||||
private function cloneIfNeeded(CompoundTag $states, int &$stateChanges) : CompoundTag{
|
||||
if($stateChanges === 0){
|
||||
$states = clone $states;
|
||||
/**
|
||||
* @param Tag[] $states
|
||||
* @phpstan-param array<string, Tag> $states
|
||||
*
|
||||
* @return Tag[]
|
||||
* @phpstan-return array<string, Tag>
|
||||
*/
|
||||
private function applyPropertyAdded(BlockStateUpgradeSchema $schema, string $oldName, array $states, int &$stateChanges) : array{
|
||||
if(isset($schema->addedProperties[$oldName])){
|
||||
foreach(Utils::stringifyKeys($schema->addedProperties[$oldName]) as $propertyName => $value){
|
||||
if(!isset($states[$propertyName])){
|
||||
$stateChanges++;
|
||||
$states[$propertyName] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
$stateChanges++;
|
||||
|
||||
return $states;
|
||||
}
|
||||
|
||||
private function applyPropertyAdded(BlockStateUpgradeSchema $schema, string $oldName, CompoundTag $states, int &$stateChanges) : CompoundTag{
|
||||
$newStates = $states;
|
||||
if(isset($schema->addedProperties[$oldName])){
|
||||
foreach(Utils::stringifyKeys($schema->addedProperties[$oldName]) as $propertyName => $value){
|
||||
$oldValue = $states->getTag($propertyName);
|
||||
if($oldValue === null){
|
||||
$newStates = $this->cloneIfNeeded($newStates, $stateChanges);
|
||||
$newStates->setTag($propertyName, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $newStates;
|
||||
}
|
||||
|
||||
private function applyPropertyRemoved(BlockStateUpgradeSchema $schema, string $oldName, CompoundTag $states, int &$stateChanges) : CompoundTag{
|
||||
$newStates = $states;
|
||||
/**
|
||||
* @param Tag[] $states
|
||||
* @phpstan-param array<string, Tag> $states
|
||||
*
|
||||
* @return Tag[]
|
||||
* @phpstan-return array<string, Tag>
|
||||
*/
|
||||
private function applyPropertyRemoved(BlockStateUpgradeSchema $schema, string $oldName, array $states, int &$stateChanges) : array{
|
||||
if(isset($schema->removedProperties[$oldName])){
|
||||
foreach($schema->removedProperties[$oldName] as $propertyName){
|
||||
if($states->getTag($propertyName) !== null){
|
||||
$newStates = $this->cloneIfNeeded($newStates, $stateChanges);
|
||||
$newStates->removeTag($propertyName);
|
||||
if(isset($states[$propertyName])){
|
||||
$stateChanges++;
|
||||
unset($states[$propertyName]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $newStates;
|
||||
return $states;
|
||||
}
|
||||
|
||||
private function locateNewPropertyValue(BlockStateUpgradeSchema $schema, string $oldName, string $oldPropertyName, Tag $oldValue) : Tag{
|
||||
@ -146,18 +156,25 @@ final class BlockStateUpgrader{
|
||||
return $oldValue;
|
||||
}
|
||||
|
||||
private function applyPropertyRenamedOrValueChanged(BlockStateUpgradeSchema $schema, string $oldName, CompoundTag $states, int &$stateChanges) : CompoundTag{
|
||||
/**
|
||||
* @param Tag[] $states
|
||||
* @phpstan-param array<string, Tag> $states
|
||||
*
|
||||
* @return Tag[]
|
||||
* @phpstan-return array<string, Tag>
|
||||
*/
|
||||
private function applyPropertyRenamedOrValueChanged(BlockStateUpgradeSchema $schema, string $oldName, array $states, int &$stateChanges) : array{
|
||||
if(isset($schema->renamedProperties[$oldName])){
|
||||
foreach(Utils::stringifyKeys($schema->renamedProperties[$oldName]) as $oldPropertyName => $newPropertyName){
|
||||
$oldValue = $states->getTag($oldPropertyName);
|
||||
$oldValue = $states[$oldPropertyName] ?? null;
|
||||
if($oldValue !== null){
|
||||
$states = $this->cloneIfNeeded($states, $stateChanges);
|
||||
$states->removeTag($oldPropertyName);
|
||||
$stateChanges++;
|
||||
unset($states[$oldPropertyName]);
|
||||
|
||||
//If a value remap is needed, we need to do it here, since we won't be able to locate the property
|
||||
//after it's been renamed - value remaps are always indexed by old property name for the sake of
|
||||
//being able to do changes in any order.
|
||||
$states->setTag($newPropertyName, $this->locateNewPropertyValue($schema, $oldName, $oldPropertyName, $oldValue));
|
||||
$states[$newPropertyName] = $this->locateNewPropertyValue($schema, $oldName, $oldPropertyName, $oldValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -165,15 +182,22 @@ final class BlockStateUpgrader{
|
||||
return $states;
|
||||
}
|
||||
|
||||
private function applyPropertyValueChanged(BlockStateUpgradeSchema $schema, string $oldName, CompoundTag $states, int &$stateChanges) : CompoundTag{
|
||||
/**
|
||||
* @param Tag[] $states
|
||||
* @phpstan-param array<string, Tag> $states
|
||||
*
|
||||
* @return Tag[]
|
||||
* @phpstan-return array<string, Tag>
|
||||
*/
|
||||
private function applyPropertyValueChanged(BlockStateUpgradeSchema $schema, string $oldName, array $states, int &$stateChanges) : array{
|
||||
if(isset($schema->remappedPropertyValues[$oldName])){
|
||||
foreach(Utils::stringifyKeys($schema->remappedPropertyValues[$oldName]) as $oldPropertyName => $remappedValues){
|
||||
$oldValue = $states->getTag($oldPropertyName);
|
||||
$oldValue = $states[$oldPropertyName] ?? null;
|
||||
if($oldValue !== null){
|
||||
$newValue = $this->locateNewPropertyValue($schema, $oldName, $oldPropertyName, $oldValue);
|
||||
if($newValue !== $oldValue){
|
||||
$states = $this->cloneIfNeeded($states, $stateChanges);
|
||||
$states->setTag($oldPropertyName, $newValue);
|
||||
$stateChanges++;
|
||||
$states[$oldPropertyName] = $newValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user