Make Block(De)Serializer much less nasty to interact with

this makes it a lot less inconvenient to access the primary blockstate serializer/deserializer, which is necessary for registering new blocks.
This commit is contained in:
Dylan K. Taylor 2022-09-24 13:30:53 +01:00
parent c24370b8ac
commit 590eb74703
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
7 changed files with 27 additions and 163 deletions

View File

@ -1,51 +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\data\bedrock\block;
use function count;
final class CachingBlockStateDeserializer implements DelegatingBlockStateDeserializer{
/**
* @var int[]
* @phpstan-var array<string, int>
*/
private array $simpleCache = [];
public function __construct(
private BlockStateDeserializer $realDeserializer
){}
public function deserialize(BlockStateData $stateData) : int{
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);
}
//we can't cache blocks that have properties - go ahead and deserialize the slow way
return $this->realDeserializer->deserialize($stateData);
}
public function getRealDeserializer() : BlockStateDeserializer{ return $this->realDeserializer; }
}

View File

@ -1,43 +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\data\bedrock\block;
final class CachingBlockStateSerializer implements DelegatingBlockStateSerializer{
/**
* @var BlockStateData[]
* @phpstan-var array<int, BlockStateData>
*/
private array $cache = [];
public function __construct(
private BlockStateSerializer $realSerializer
){}
public function serialize(int $stateId) : BlockStateData{
return $this->cache[$stateId] ??= $this->realSerializer->serialize($stateId);
}
public function getRealSerializer() : BlockStateSerializer{ return $this->realSerializer; }
}

View File

@ -1,29 +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\data\bedrock\block;
interface DelegatingBlockStateDeserializer extends BlockStateDeserializer{
public function getRealDeserializer() : BlockStateDeserializer;
}

View File

@ -1,29 +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\data\bedrock\block;
interface DelegatingBlockStateSerializer extends BlockStateSerializer{
public function getRealSerializer() : BlockStateSerializer;
}

View File

@ -179,6 +179,12 @@ final class BlockObjectToBlockStateSerializer implements BlockStateSerializer{
*/
private array $serializers = [];
/**
* @var BlockStateData[]
* @phpstan-var array<int, BlockStateData>
*/
private array $cache = [];
public function __construct(){
$this->registerCandleSerializers();
$this->registerCauldronSerializers();
@ -188,7 +194,8 @@ final class BlockObjectToBlockStateSerializer implements BlockStateSerializer{
public function serialize(int $stateId) : BlockStateData{
//TODO: singleton usage not ideal
return $this->serializeBlock(BlockFactory::getInstance()->fromStateId($stateId));
//TODO: we may want to deduplicate cache entries to avoid wasting memory
return $this->cache[$stateId] ??= $this->serializeBlock(BlockFactory::getInstance()->fromStateId($stateId));
}
/**

View File

@ -51,6 +51,7 @@ use pocketmine\data\bedrock\block\convert\BlockStateReader as Reader;
use pocketmine\math\Axis;
use pocketmine\math\Facing;
use function array_key_exists;
use function count;
use function min;
final class BlockStateToBlockObjectDeserializer implements BlockStateDeserializer{
@ -61,6 +62,12 @@ final class BlockStateToBlockObjectDeserializer implements BlockStateDeserialize
*/
private array $deserializeFuncs = [];
/**
* @var int[]
* @phpstan-var array<string, int>
*/
private array $simpleCache = [];
public function __construct(){
$this->registerCandleDeserializers();
$this->registerCauldronDeserializers();
@ -69,6 +76,12 @@ final class BlockStateToBlockObjectDeserializer implements BlockStateDeserialize
}
public function deserialize(BlockStateData $stateData) : int{
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->deserializeBlock($stateData)->getStateId();
}
//we can't cache blocks that have properties - go ahead and deserialize the slow way
return $this->deserializeBlock($stateData)->getStateId();
}

View File

@ -24,11 +24,7 @@ declare(strict_types=1);
namespace pocketmine\world\format\io;
use pocketmine\data\bedrock\block\BlockStateData;
use pocketmine\data\bedrock\block\BlockStateDeserializer;
use pocketmine\data\bedrock\block\BlockStateSerializer;
use pocketmine\data\bedrock\block\BlockTypeNames;
use pocketmine\data\bedrock\block\CachingBlockStateDeserializer;
use pocketmine\data\bedrock\block\CachingBlockStateSerializer;
use pocketmine\data\bedrock\block\convert\BlockObjectToBlockStateSerializer;
use pocketmine\data\bedrock\block\convert\BlockStateToBlockObjectDeserializer;
use pocketmine\data\bedrock\block\upgrade\BlockDataUpgrader;
@ -49,20 +45,20 @@ use const pocketmine\BEDROCK_BLOCK_UPGRADE_SCHEMA_PATH;
*/
final class GlobalBlockStateHandlers{
private static ?BlockStateSerializer $blockStateSerializer = null;
private static ?BlockObjectToBlockStateSerializer $blockStateSerializer = null;
private static ?BlockStateDeserializer $blockStateDeserializer = null;
private static ?BlockStateToBlockObjectDeserializer $blockStateDeserializer = null;
private static ?BlockDataUpgrader $blockDataUpgrader = null;
private static ?BlockStateData $unknownBlockStateData = null;
public static function getDeserializer() : BlockStateDeserializer{
return self::$blockStateDeserializer ??= new CachingBlockStateDeserializer(new BlockStateToBlockObjectDeserializer());
public static function getDeserializer() : BlockStateToBlockObjectDeserializer{
return self::$blockStateDeserializer ??= new BlockStateToBlockObjectDeserializer();
}
public static function getSerializer() : BlockStateSerializer{
return self::$blockStateSerializer ??= new CachingBlockStateSerializer(new BlockObjectToBlockStateSerializer());
public static function getSerializer() : BlockObjectToBlockStateSerializer{
return self::$blockStateSerializer ??= new BlockObjectToBlockStateSerializer();
}
public static function getUpgrader() : BlockDataUpgrader{