Check consistency of block remaps

This commit is contained in:
Dylan K. Taylor 2021-06-21 20:30:27 +01:00
parent 85ee628a74
commit 2a6009f8bf
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
4 changed files with 46 additions and 12 deletions

View File

@ -56,6 +56,7 @@ use function array_fill;
use function array_filter;
use function get_class;
use function min;
use function var_dump;
/**
* Manages deserializing block types from their legacy blockIDs and metadata.

View File

@ -145,14 +145,26 @@ class BlockTest extends TestCase{
public function testConsistency() : void{
$list = json_decode(file_get_contents(__DIR__ . '/block_factory_consistency_check.json'), true);
$knownStates = $list["knownStates"];
$remaps = $list["remaps"];
$states = $this->blockFactory->getAllKnownStates();
foreach($states as $k => $state){
self::assertArrayHasKey($k, $list, "New block state $k (" . $state->getName() . ") - consistency check may need regenerating");
self::assertSame($list[$k], $state->getName());
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());
}
}
foreach($list as $k => $name){
foreach($knownStates as $k => $name){
self::assertArrayHasKey($k, $states, "Missing previously-known block state $k ($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

@ -26,14 +26,19 @@ require dirname(__DIR__, 3) . '/vendor/autoload.php';
/* This script needs to be re-run after any intentional blockfactory change (adding or removing a block state). */
$factory = new \pocketmine\block\BlockFactory();
$remaps = [];
$new = [];
foreach($factory->getAllKnownStates() as $index => $block){
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);
$old = $oldTable["knownStates"];
$oldRemaps = $oldTable["remaps"];
$old = json_decode(file_get_contents(__DIR__ . '/block_factory_consistency_check.json'), true);
$new = array_map(
function(\pocketmine\block\Block $block) : string{
return $block->getName();
},
$factory->getAllKnownStates()
);
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";
@ -46,6 +51,22 @@ foreach($new as $k => $name){
echo "Name changed (" . ($k >> \pocketmine\block\Block::INTERNAL_METADATA_BITS) . ":" . ($k & \pocketmine\block\Block::INTERNAL_METADATA_MASK) . "): " . $old[$k] . " -> " . $name . "\n";
}
}
foreach($oldRemaps as $index => $mapped){
if(!isset($remaps[$index])){
echo "Removed remap of " . ($index >> 4) . ":" . ($index & 0xf) . "\n";
}
}
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";
}
}
file_put_contents(__DIR__ . '/block_factory_consistency_check.json', json_encode(
$new
[
"knownStates" => $new,
"remaps" => $remaps
],
));