Checking in BlockStateUpgrader and a bunch of unit tests

This commit is contained in:
Dylan K. Taylor
2022-02-04 00:16:48 +00:00
parent f85f2cae98
commit 0cc997f531
8 changed files with 838 additions and 0 deletions

View File

@ -85,4 +85,11 @@ final class BlockStateData{
->setInt(self::TAG_VERSION, $this->version)
->setTag(self::TAG_STATES, $this->states);
}
public function equals(self $that) : bool{
return
$this->name === $that->name &&
$this->states->equals($that->states) &&
$this->version === $that->version;
}
}

View File

@ -0,0 +1,70 @@
<?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\blockstate\upgrade;
use pocketmine\data\bedrock\blockstate\upgrade\BlockStateUpgradeSchemaValueRemap as ValueRemap;
use pocketmine\nbt\tag\Tag;
final class BlockStateUpgradeSchema{
/**
* @var string[]
* @phpstan-var array<string, string>
*/
public array $renamedIds = [];
/**
* @var Tag[][]
* @phpstan-var array<string, array<string, Tag>>
*/
public array $addedProperties = [];
/**
* @var string[][]
* @phpstan-var array<string, list<string>>
*/
public array $removedProperties = [];
/**
* @var string[][]
* @phpstan-var array<string, array<string, string>>
*/
public array $renamedProperties = [];
/**
* @var ValueRemap[][][]
* @phpstan-var array<string, array<string, list<ValueRemap>>>
*/
public array $remappedPropertyValues = [];
public function __construct(
public int $maxVersionMajor,
public int $maxVersionMinor,
public int $maxVersionPatch,
public int $maxVersionRevision
){}
public function getVersionId() : int{
return ($this->maxVersionMajor << 24) | ($this->maxVersionMinor << 16) | ($this->maxVersionPatch << 8) | $this->maxVersionRevision;
}
}

View File

@ -0,0 +1,221 @@
<?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\blockstate\upgrade;
use pocketmine\data\bedrock\blockstate\upgrade\model\BlockStateUpgradeSchemaModel;
use pocketmine\data\bedrock\blockstate\upgrade\model\BlockStateUpgradeSchemaModelTag;
use pocketmine\data\bedrock\blockstate\upgrade\model\BlockStateUpgradeSchemaModelValueRemap;
use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\nbt\tag\Tag;
use pocketmine\utils\Utils;
use Webmozart\PathUtil\Path;
use function file_get_contents;
use function get_class;
use function implode;
use function is_int;
use function is_object;
use function is_string;
use function json_decode;
use function ksort;
use function var_dump;
use const JSON_THROW_ON_ERROR;
use const SORT_NUMERIC;
final class BlockStateUpgradeSchemaUtils{
public static function describe(BlockStateUpgradeSchema $schema) : string{
$lines = [];
$lines[] = "Renames:";
foreach($schema->renamedIds as $rename){
$lines[] = "- $rename";
}
$lines[] = "Added properties:";
foreach(Utils::stringifyKeys($schema->addedProperties) as $blockName => $tags){
foreach(Utils::stringifyKeys($tags) as $k => $v){
$lines[] = "- $blockName has $k added: $v";
}
}
$lines[] = "Removed properties:";
foreach(Utils::stringifyKeys($schema->removedProperties) as $blockName => $tagNames){
foreach($tagNames as $tagName){
$lines[] = "- $blockName has $tagName removed";
}
}
$lines[] = "Renamed properties:";
foreach(Utils::stringifyKeys($schema->renamedProperties) as $blockName => $tagNames){
foreach(Utils::stringifyKeys($tagNames) as $oldTagName => $newTagName){
$lines[] = "- $blockName has $oldTagName renamed to $newTagName";
}
}
$lines[] = "Remapped property values:";
foreach(Utils::stringifyKeys($schema->remappedPropertyValues) as $blockName => $remaps){
foreach(Utils::stringifyKeys($remaps) as $tagName => $oldNewList){
foreach($oldNewList as $oldNew){
$lines[] = "- $blockName has $tagName value changed from $oldNew->old to $oldNew->new";
}
}
}
return implode("\n", $lines);
}
private static function tagToJsonModel(Tag $tag) : BlockStateUpgradeSchemaModelTag{
$type = match(get_class($tag)){
IntTag::class => "int",
StringTag::class => "string",
ByteTag::class => "byte",
default => throw new \UnexpectedValueException()
};
return new BlockStateUpgradeSchemaModelTag($type, $tag->getValue());
}
private static function jsonModelToTag(BlockStateUpgradeSchemaModelTag $model) : Tag{
if($model->type === "int"){
if(!is_int($model->value)){
throw new \UnexpectedValueException("Value for type int must be an int");
}
return new IntTag($model->value);
}elseif($model->type === "byte"){
if(!is_int($model->value)){
throw new \UnexpectedValueException("Value for type byte must be an int");
}
return new ByteTag($model->value);
}elseif($model->type === "string"){
if(!is_string($model->value)){
throw new \UnexpectedValueException("Value for type string must be a string");
}
return new StringTag($model->value);
}else{
throw new \UnexpectedValueException("Unknown blockstate value type $model->type");
}
}
public static function fromJsonModel(BlockStateUpgradeSchemaModel $model) : BlockStateUpgradeSchema{
$result = new BlockStateUpgradeSchema(
$model->maxVersionMajor,
$model->maxVersionMinor,
$model->maxVersionPatch,
$model->maxVersionRevision
);
$result->renamedIds = $model->renamedIds ?? [];
$result->renamedProperties = $model->renamedProperties ?? [];
$result->removedProperties = $model->removedProperties ?? [];
foreach(Utils::stringifyKeys($model->addedProperties ?? []) as $blockName => $properties){
foreach(Utils::stringifyKeys($properties) as $propertyName => $propertyValue){
$result->addedProperties[$blockName][$propertyName] = self::jsonModelToTag($propertyValue);
}
}
foreach(Utils::stringifyKeys($model->remappedPropertyValues ?? []) as $blockName => $properties){
foreach(Utils::stringifyKeys($properties) as $property => $mappedValuesKey){
foreach($mappedValuesKey as $oldNew){
$result->remappedPropertyValues[$blockName][$property][] = new BlockStateUpgradeSchemaValueRemap(
self::jsonModelToTag($oldNew->old),
self::jsonModelToTag($oldNew->new)
);
}
}
}
return $result;
}
public static function toJsonModel(BlockStateUpgradeSchema $schema) : BlockStateUpgradeSchemaModel{
$result = new BlockStateUpgradeSchemaModel();
$result->maxVersionMajor = $schema->maxVersionMajor;
$result->maxVersionMinor = $schema->maxVersionMinor;
$result->maxVersionPatch = $schema->maxVersionPatch;
$result->maxVersionRevision = $schema->maxVersionRevision;
$result->renamedIds = $schema->renamedIds;
$result->renamedProperties = $schema->renamedProperties;
$result->removedProperties = $schema->removedProperties;
foreach(Utils::stringifyKeys($schema->addedProperties) as $blockName => $properties){
foreach(Utils::stringifyKeys($properties) as $propertyName => $propertyValue){
$result->addedProperties[$blockName][$propertyName] = self::tagToJsonModel($propertyValue);
}
}
foreach(Utils::stringifyKeys($schema->remappedPropertyValues) as $blockName => $properties){
foreach(Utils::stringifyKeys($properties) as $property => $propertyValues){
foreach($propertyValues as $oldNew){
$result->remappedPropertyValues[$blockName][$property][] = (array) new BlockStateUpgradeSchemaModelValueRemap(
self::tagToJsonModel($oldNew->old),
self::tagToJsonModel($oldNew->new)
);
}
}
}
return $result;
}
/**
* Returns a list of schemas ordered by priority. Oldest schemas appear first.
*
* @return BlockStateUpgradeSchema[]
*/
public static function loadSchemas(string $path) : array{
$iterator = new \RegexIterator(
new \FilesystemIterator(
$path,
\FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS
),
'/\/mapping_schema_(\d{4}).*\.json$/',
\RegexIterator::GET_MATCH
);
$result = [];
$jsonMapper = new \JsonMapper();
/** @var string[] $matches */
foreach($iterator as $matches){
$filename = $matches[0];
$priority = (int) $matches[1];
var_dump($filename);
$fullPath = Path::join($path, $filename);
//TODO: should we bother handling exceptions in here?
$raw = ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents($fullPath));
$json = json_decode($raw, false, flags: JSON_THROW_ON_ERROR);
if(!is_object($json)){
throw new \RuntimeException("Unexpected root type of schema file $fullPath");
}
$model = $jsonMapper->map($json, new BlockStateUpgradeSchemaModel());
$result[$priority] = self::fromJsonModel($model);
}
ksort($result, SORT_NUMERIC);
return $result;
}
}

View File

@ -0,0 +1,34 @@
<?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\blockstate\upgrade;
use pocketmine\nbt\tag\Tag;
final class BlockStateUpgradeSchemaValueRemap{
public function __construct(
public Tag $old,
public Tag $new
){}
}

View File

@ -0,0 +1,163 @@
<?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\blockstate\upgrade;
use pocketmine\data\bedrock\blockstate\BlockStateData;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\Tag;
use pocketmine\utils\Utils;
use function ksort;
use const SORT_ASC;
use const SORT_NUMERIC;
final class BlockStateUpgrader{
/** @var BlockStateUpgradeSchema[][] */
private array $upgradeSchemas = [];
public function addSchema(BlockStateUpgradeSchema $schema, int $priority) : void{
if(isset($this->upgradeSchemas[$schema->getVersionId()][$priority])){
throw new \InvalidArgumentException("Another schema already has this priority");
}
$this->upgradeSchemas[$schema->getVersionId()][$priority] = $schema;
ksort($this->upgradeSchemas, SORT_NUMERIC | SORT_ASC);
ksort($this->upgradeSchemas[$schema->getVersionId()], SORT_NUMERIC | SORT_ASC);
}
public function upgrade(BlockStateData $blockStateData) : BlockStateData{
$oldName = $blockStateData->getName();
$version = $blockStateData->getVersion();
foreach($this->upgradeSchemas as $resultVersion => $schemas){
if($version > $resultVersion){
//even if this is actually the same version, we have to apply it anyway because mojang are dumb and
//didn't always bump the blockstate version when changing it :(
continue;
}
foreach($schemas as $schema){
$newName = $schema->renamedIds[$oldName] ?? null;
$stateChanges = 0;
$states = $blockStateData->getStates();
$states = $this->applyPropertyAdded($schema, $oldName, $states, $stateChanges);
$states = $this->applyPropertyRemoved($schema, $oldName, $states, $stateChanges);
$states = $this->applyPropertyRenamedOrValueChanged($schema, $oldName, $states, $stateChanges);
$states = $this->applyPropertyValueChanged($schema, $oldName, $states, $stateChanges);
if($newName !== null || $stateChanges > 0){
$blockStateData = new BlockStateData($newName ?? $oldName, $states, $resultVersion);
//don't break out; we may need to further upgrade the state
}
}
}
return $blockStateData;
}
private function cloneIfNeeded(CompoundTag $states, int &$stateChanges) : CompoundTag{
if($stateChanges === 0){
$states = clone $states;
}
$stateChanges++;
return $states;
}
private function applyPropertyAdded(BlockStateUpgradeSchema $schema, string $oldName, CompoundTag $states, int &$stateChanges) : CompoundTag{
$newStates = $states;
if(isset($schema->addedProperties[$oldName])){
foreach(Utils::stringifyKeys($schema->addedProperties[$oldName]) as $propertyName => $value){
$oldValue = $states->getTag($propertyName);
if($oldValue === null){
$newStates = $this->cloneIfNeeded($newStates, $stateChanges);
$newStates->setTag($propertyName, $value);
}
}
}
return $newStates;
}
private function applyPropertyRemoved(BlockStateUpgradeSchema $schema, string $oldName, CompoundTag $states, int &$stateChanges) : CompoundTag{
$newStates = $states;
if(isset($schema->removedProperties[$oldName])){
foreach($schema->removedProperties[$oldName] as $propertyName){
if($states->getTag($propertyName) !== null){
$newStates = $this->cloneIfNeeded($newStates, $stateChanges);
$newStates->removeTag($propertyName);
}
}
}
return $newStates;
}
private function locateNewPropertyValue(BlockStateUpgradeSchema $schema, string $oldName, string $oldPropertyName, Tag $oldValue) : Tag{
if(isset($schema->remappedPropertyValues[$oldName][$oldPropertyName])){
foreach($schema->remappedPropertyValues[$oldName][$oldPropertyName] as $mappedPair){
if($mappedPair->old->equals($oldValue)){
return $mappedPair->new;
}
}
}
return $oldValue;
}
private function applyPropertyRenamedOrValueChanged(BlockStateUpgradeSchema $schema, string $oldName, CompoundTag $states, int &$stateChanges) : CompoundTag{
if(isset($schema->renamedProperties[$oldName])){
foreach(Utils::stringifyKeys($schema->renamedProperties[$oldName]) as $oldPropertyName => $newPropertyName){
$oldValue = $states->getTag($oldPropertyName);
if($oldValue !== null){
$states = $this->cloneIfNeeded($states, $stateChanges);
$states->removeTag($oldPropertyName);
//If a value remap is needed, we need to do it here, since we won't be able to locate the property
//after it's been renamed - value remaps are always indexed by old property name for the sake of
//being able to do changes in any order.
$states->setTag($newPropertyName, $this->locateNewPropertyValue($schema, $oldName, $oldPropertyName, $oldValue));
}
}
}
return $states;
}
private function applyPropertyValueChanged(BlockStateUpgradeSchema $schema, string $oldName, CompoundTag $states, int &$stateChanges) : CompoundTag{
if(isset($schema->remappedPropertyValues[$oldName])){
foreach(Utils::stringifyKeys($schema->remappedPropertyValues[$oldName]) as $oldPropertyName => $remappedValues){
$oldValue = $states->getTag($oldPropertyName);
if($oldValue !== null){
$newValue = $this->locateNewPropertyValue($schema, $oldName, $oldPropertyName, $oldValue);
if($newValue !== $oldValue){
$states = $this->cloneIfNeeded($states, $stateChanges);
$states->setTag($oldPropertyName, $newValue);
}
}
}
}
return $states;
}
}

View File

@ -0,0 +1,64 @@
<?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\blockstate\upgrade;
use pocketmine\data\bedrock\blockstate\BlockStateData;
use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer;
use pocketmine\utils\BinaryStream;
/**
* Interface to an upgrade schema describing how to convert 1.12 id+meta into modern blockstate NBT.
*/
final class LegacyIdMetaToBlockStateDataMap{
/**
* @param BlockStateData[][] $mappingTable
* @phpstan-param array<string, array<int, BlockStateData>> $mappingTable
*/
public function __construct(
private array $mappingTable
){}
public function getDataFromLegacyIdMeta(string $id, int $meta) : ?BlockStateData{
return $this->mappingTable[$id][$meta] ?? null;
}
public static function loadFromString(string $data) : self{
$mappingTable = [];
$legacyStateMapReader = new BinaryStream($data);
$nbtReader = new NetworkNbtSerializer();
while(!$legacyStateMapReader->feof()){
$id = $legacyStateMapReader->get($legacyStateMapReader->getUnsignedVarInt());
$meta = $legacyStateMapReader->getLShort();
$offset = $legacyStateMapReader->getOffset();
$state = $nbtReader->read($legacyStateMapReader->getBuffer(), $offset)->mustGetCompoundTag();
$legacyStateMapReader->setOffset($offset);
$mappingTable[$id][$meta] = BlockStateData::fromNbt($state);
}
return new self($mappingTable);
}
}