mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-17 11:18:52 +00:00
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:
parent
c24370b8ac
commit
590eb74703
@ -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; }
|
||||
}
|
@ -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; }
|
||||
}
|
@ -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;
|
||||
}
|
@ -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;
|
||||
}
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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{
|
||||
|
Loading…
x
Reference in New Issue
Block a user