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

@ -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
));