Hit block legacy metadata with the biggest nuke you've ever seen

This commit completely revamps the way that blocks are represented in memory at runtime.

Instead of being represented by legacy Mojang block IDs and metadata, which are dated, limited and unchangeable, we now use custom PM block IDs, which are generated from VanillaBlocks.
This means we have full control of how they are assigned, which opens the doors to finally addressing inconsistencies like glazed terracotta, stripped logs handling, etc.

To represent state, BlockDataReader and BlockDataWriter have been introduced, and are used by blocks with state information to pack said information into a binary form that can be stored on a chunk at runtime.
Conceptually it's pretty similar to legacy metadata, but the actual format shares no resemblance whatsoever to legacy metadata, and is fully controlled by PM.
This means that the 'state data' may change in serialization format at any time, so it should **NOT** be stored on disk or in a config.

In the future, this will be improved using more auto-generated code and attributes, instead of hand-baked decodeState() and encodeState(). For now, this opens the gateway to a significant expansion of features.
It's not ideal, but it's a big step forwards.
This commit is contained in:
Dylan K. Taylor
2022-06-24 23:19:37 +01:00
parent be2fe160b3
commit f24f2d9ca9
149 changed files with 2234 additions and 2650 deletions

View File

@ -24,9 +24,11 @@ declare(strict_types=1);
namespace pocketmine\block;
use PHPUnit\Framework\TestCase;
use function asort;
use function file_get_contents;
use function is_array;
use function json_decode;
use function print_r;
class BlockTest extends TestCase{
@ -52,7 +54,7 @@ class BlockTest extends TestCase{
public function testDeliberateOverrideBlock() : void{
$block = new MyCustomBlock(new BlockIdentifier(BlockTypeIds::COBBLESTONE, BlockLegacyIds::COBBLESTONE, 0), "Cobblestone", BlockBreakInfo::instant());
$this->blockFactory->register($block, true);
self::assertInstanceOf(MyCustomBlock::class, $this->blockFactory->get($block->getId(), 0));
self::assertInstanceOf(MyCustomBlock::class, $this->blockFactory->get($block->getTypeId(), 0));
}
/**
@ -63,7 +65,7 @@ class BlockTest extends TestCase{
if(!$this->blockFactory->isRegistered($i)){
$b = new StrangeNewBlock(new BlockIdentifier(BlockTypeIds::FIRST_UNUSED_BLOCK_ID, $i, 0), "Strange New Block", BlockBreakInfo::instant());
$this->blockFactory->register($b);
self::assertInstanceOf(StrangeNewBlock::class, $this->blockFactory->get($b->getId(), 0));
self::assertInstanceOf(StrangeNewBlock::class, $this->blockFactory->get($b->getTypeId(), 0));
return;
}
}
@ -92,37 +94,6 @@ class BlockTest extends TestCase{
}
}
/**
* @return int[][]
* @phpstan-return list<array{int,int}>
*/
public function blockGetProvider() : array{
return [
[BlockLegacyIds::STONE, 5],
[BlockLegacyIds::GOLD_BLOCK, 0],
[BlockLegacyIds::WOODEN_PLANKS, 5],
[BlockLegacyIds::SAND, 0],
[BlockLegacyIds::GOLD_BLOCK, 0]
];
}
/**
* @dataProvider blockGetProvider
*/
public function testBlockGet(int $id, int $meta) : void{
$block = $this->blockFactory->get($id, $meta);
self::assertEquals($id, $block->getId());
self::assertEquals($meta, $block->getMeta());
}
public function testBlockIds() : void{
for($i = 0; $i < 256; ++$i){
$b = $this->blockFactory->get($i, 0);
self::assertContains($i, $b->getIdInfo()->getAllLegacyBlockIds());
}
}
/**
* Test that light filters in the static arrays have valid values. Wrong values can cause lots of unpleasant bugs
* (like freezes) when doing light population.
@ -137,33 +108,21 @@ class BlockTest extends TestCase{
public function testConsistency() : void{
$list = json_decode(file_get_contents(__DIR__ . '/block_factory_consistency_check.json'), true);
if(!is_array($list)){
throw new \pocketmine\utils\AssumptionFailedError("Old table should be array{knownStates: array<string, string>, remaps: array<string, int>}");
throw new \pocketmine\utils\AssumptionFailedError("Old table should be array{knownStates: array<string, string>, stateDataBits: int}");
}
$knownStates = $list["knownStates"];
$remaps = $list["remaps"];
$oldStateDataSize = $list["stateDataBits"];
self::assertSame($oldStateDataSize, Block::INTERNAL_STATE_DATA_BITS, "Changed number of state data bits - consistency check probably need regenerating");
$states = [];
for($k = 0; $k < 1024 << Block::INTERNAL_METADATA_BITS; $k++){
$state = $this->blockFactory->fromFullBlock($k);
if($state instanceof UnknownBlock){
continue;
}
$states[$k] = $state;
if($state->getFullId() !== $k){
self::assertArrayHasKey($k, $remaps, "New remap of state $k (" . $state->getName() . ") - consistency check may need regenerating");
self::assertSame($state->getFullId(), $remaps[$k], "Mismatched full IDs of remapped state $k");
}else{
self::assertArrayHasKey($k, $knownStates, "New block state $k (" . $state->getName() . ") - consistency check may need regenerating");
self::assertSame($knownStates[$k], $state->getName());
}
$states = BlockFactory::getInstance()->getAllKnownStates();
foreach(BlockFactory::getInstance()->getAllKnownStates() as $stateId => $state){
self::assertArrayHasKey($stateId, $knownStates, "New block state $stateId (" . $state->getTypeId() . ":" . $state->computeStateData() . ", " . print_r($state, true) . ") - consistency check may need regenerating");
self::assertSame($knownStates[$stateId], $state->getName());
}
asort($knownStates, SORT_STRING);
foreach($knownStates as $k => $name){
self::assertArrayHasKey($k, $states, "Missing previously-known block state $k ($name)");
self::assertArrayHasKey($k, $states, "Missing previously-known block state $k " . ($k >> Block::INTERNAL_STATE_DATA_BITS) . ":" . ($k & Block::INTERNAL_STATE_DATA_MASK) . " ($name)");
self::assertSame($name, $states[$k]->getName());
}
foreach($remaps as $origin => $destination){
self::assertArrayHasKey($origin, $states, "Missing previously-remapped state $origin");
self::assertSame($destination, $states[$origin]->getFullId());
}
}
}

File diff suppressed because one or more lines are too long

View File

@ -22,7 +22,8 @@
declare(strict_types=1);
use pocketmine\block\Block;
use pocketmine\block\UnknownBlock;
use pocketmine\block\BlockFactory;
use pocketmine\utils\AssumptionFailedError;
require dirname(__DIR__, 3) . '/vendor/autoload.php';
@ -31,53 +32,56 @@ require dirname(__DIR__, 3) . '/vendor/autoload.php';
$factory = new \pocketmine\block\BlockFactory();
$remaps = [];
$new = [];
for($index = 0; $index < 1024 << Block::INTERNAL_METADATA_BITS; $index++){
$block = $factory->fromFullBlock($index);
if($block instanceof UnknownBlock){
continue;
}
if($block->getFullId() !== $index){
$remaps[$index] = $block->getFullId();
}else{
$new[$index] = $block->getName();
}
}
$oldTable = json_decode(file_get_contents(__DIR__ . '/block_factory_consistency_check.json'), true);
if(!is_array($oldTable)){
throw new \pocketmine\utils\AssumptionFailedError("Old table should be array{knownStates: array<string, string>, remaps: array<string, int>}");
}
$old = $oldTable["knownStates"];
$oldRemaps = $oldTable["remaps"];
foreach($old as $k => $name){
if(!isset($new[$k])){
echo "Removed state for $name (" . ($k >> \pocketmine\block\Block::INTERNAL_METADATA_BITS) . ":" . ($k & \pocketmine\block\Block::INTERNAL_METADATA_MASK) . ")\n";
}
}
foreach($new as $k => $name){
if(!isset($old[$k])){
echo "Added state for $name (" . ($k >> \pocketmine\block\Block::INTERNAL_METADATA_BITS) . ":" . ($k & \pocketmine\block\Block::INTERNAL_METADATA_MASK) . ")\n";
}elseif($old[$k] !== $name){
echo "Name changed (" . ($k >> \pocketmine\block\Block::INTERNAL_METADATA_BITS) . ":" . ($k & \pocketmine\block\Block::INTERNAL_METADATA_MASK) . "): " . $old[$k] . " -> " . $name . "\n";
foreach(BlockFactory::getInstance()->getAllKnownStates() as $index => $block){
if($index !== $block->getStateId()){
throw new AssumptionFailedError("State index should always match state ID");
}
$new[$index] = $block->getName();
}
foreach($oldRemaps as $index => $mapped){
if(!isset($remaps[$index])){
echo "Removed remap of " . ($index >> 4) . ":" . ($index & 0xf) . "\n";
$oldTablePath = __DIR__ . '/block_factory_consistency_check.json';
if(file_exists($oldTablePath)){
$oldTable = json_decode(file_get_contents($oldTablePath), true);
if(!is_array($oldTable)){
throw new \pocketmine\utils\AssumptionFailedError("Old table should be array{knownStates: array<string, string>, stateDataBits: int}");
}
}
foreach($remaps as $index => $mapped){
if(!isset($oldRemaps[$index])){
echo "New remap of " . ($index >> 4) . ":" . ($index & 0xf) . " (" . ($mapped >> 4) . ":" . ($mapped & 0xf) . ") (" . $new[$mapped] . ")\n";
}elseif($oldRemaps[$index] !== $mapped){
echo "Remap changed for " . ($index >> 4) . ":" . ($index & 0xf) . " (" . ($oldRemaps[$index] >> 4) . ":" . ($oldRemaps[$index] & 0xf) . " (" . $old[$oldRemaps[$index]] . ") -> " . ($mapped >> 4) . ":" . ($mapped & 0xf) . " (" . $new[$mapped] . "))\n";
$old = $oldTable["knownStates"];
$oldStateDataSize = $oldTable["stateDataBits"];
$oldStateDataMask = ~(~0 << $oldStateDataSize);
if($oldStateDataSize !== Block::INTERNAL_STATE_DATA_BITS){
echo "State data bits changed from $oldStateDataSize to " . Block::INTERNAL_STATE_DATA_BITS . "\n";
}
foreach($old as $k => $name){
[$oldId, $oldStateData] = [$k >> $oldStateDataSize, $k & $oldStateDataMask];
$reconstructedK = ($oldId << Block::INTERNAL_STATE_DATA_BITS) | $oldStateData;
if(!isset($new[$reconstructedK])){
echo "Removed state for $name ($oldId:$oldStateData)\n";
}
}
foreach($new as $k => $name){
[$newId, $newStateData] = [$k >> Block::INTERNAL_STATE_DATA_BITS, $k & Block::INTERNAL_STATE_DATA_MASK];
if($newStateData > $oldStateDataMask){
echo "Added state for $name ($newId, $newStateData)\n";
}else{
$reconstructedK = ($newId << $oldStateDataSize) | $newStateData;
if(!isset($old[$reconstructedK])){
echo "Added state for $name ($newId:$newStateData)\n";
}elseif($old[$reconstructedK] !== $name){
echo "Name changed ($newId:$newStateData) " . $old[$reconstructedK] . " -> " . $name . "\n";
}
}
}
}else{
echo "WARNING: Unable to calculate diff, no previous consistency check file found\n";
}
file_put_contents(__DIR__ . '/block_factory_consistency_check.json', json_encode(
[
"knownStates" => $new,
"remaps" => $remaps
"stateDataBits" => Block::INTERNAL_STATE_DATA_BITS
],
JSON_THROW_ON_ERROR
));

View File

@ -27,6 +27,7 @@ use PHPUnit\Framework\TestCase;
use pocketmine\block\BlockFactory;
use pocketmine\data\bedrock\block\BlockStateDeserializeException;
use pocketmine\data\bedrock\block\BlockStateSerializeException;
use function print_r;
final class BlockSerializerDeserializerTest extends TestCase{
private BlockStateToBlockObjectDeserializer $deserializer;
@ -50,7 +51,7 @@ final class BlockSerializerDeserializerTest extends TestCase{
self::fail($e->getMessage());
}
self::assertSame($block->getFullId(), $newBlock->getFullId(), "Mismatch of blockstate for " . $block->getName());
self::assertSame($block->getStateId(), $newBlock->getStateId(), "Mismatch of blockstate for " . $block->getName() . ", " . print_r($block, true) . " vs " . print_r($newBlock, true));
}
}
}

View File

@ -33,7 +33,7 @@ class RuntimeBlockMappingTest extends TestCase{
*/
public function testAllBlockStatesSerialize() : void{
foreach(BlockFactory::getInstance()->getAllKnownStates() as $state){
RuntimeBlockMapping::getInstance()->toRuntimeId($state->getFullId());
RuntimeBlockMapping::getInstance()->toRuntimeId($state->getStateId());
}
}
}