Added support for compressing blockstate remaps using copiedState

this significantly reduces the size of schemas when state remaps are used (see pmmp/BedrockBlockUpgradeSchema@85b83b360e).

in addition, this will likely offer a substantial performance and memory saving when walls get flattened, which will eventually happen.
This commit is contained in:
Dylan K. Taylor
2023-04-28 20:34:06 +01:00
parent 263e1e9950
commit a1d44de487
5 changed files with 166 additions and 10 deletions

View File

@ -27,14 +27,18 @@ use pocketmine\nbt\tag\Tag;
final class BlockStateUpgradeSchemaBlockRemap{
/**
* @param Tag[] $oldState
* @param Tag[] $newState
* @param Tag[] $oldState
* @param Tag[] $newState
* @param string[] $copiedState
*
* @phpstan-param array<string, Tag> $oldState
* @phpstan-param array<string, Tag> $newState
* @phpstan-param list<string> $copiedState
*/
public function __construct(
public array $oldState,
public string $newName,
public array $newState
public array $newState,
public array $copiedState
){}
}

View File

@ -157,6 +157,7 @@ final class BlockStateUpgradeSchemaUtils{
array_map(fn(BlockStateUpgradeSchemaModelTag $tag) => self::jsonModelToTag($tag), $remap->oldState ?? []),
$remap->newName,
array_map(fn(BlockStateUpgradeSchemaModelTag $tag) => self::jsonModelToTag($tag), $remap->newState ?? []),
$remap->copiedState ?? []
);
}
}
@ -277,7 +278,11 @@ final class BlockStateUpgradeSchemaUtils{
array_map(fn(Tag $tag) => self::tagToJsonModel($tag), $remap->oldState),
$remap->newName,
array_map(fn(Tag $tag) => self::tagToJsonModel($tag), $remap->newState),
$remap->copiedState
);
if(count($modelRemap->copiedState) === 0){
unset($modelRemap->copiedState); //avoid polluting the JSON
}
$key = json_encode($modelRemap);
assert(!isset($keyedRemaps[$key]));
if(isset($keyedRemaps[$key])){

View File

@ -70,16 +70,23 @@ final class BlockStateUpgrader{
$oldState = $blockStateData->getStates();
if(isset($schema->remappedStates[$oldName])){
foreach($schema->remappedStates[$oldName] as $remap){
if(count($oldState) !== count($remap->oldState)){
if(count($remap->oldState) > count($oldState)){
//match criteria has more requirements than we have state properties
continue; //try next state
}
foreach(Utils::stringifyKeys($oldState) as $k => $v){
if(!isset($remap->oldState[$k]) || !$remap->oldState[$k]->equals($v)){
foreach(Utils::stringifyKeys($remap->oldState) as $k => $v){
if(!isset($oldState[$k]) || !$oldState[$k]->equals($v)){
continue 2; //try next state
}
}
$newState = $remap->newState;
foreach($remap->copiedState as $stateName){
if(isset($oldState[$stateName])){
$newState[$stateName] = $oldState[$stateName];
}
}
$blockStateData = new BlockStateData($remap->newName, $remap->newState, $resultVersion);
$blockStateData = new BlockStateData($remap->newName, $newState, $resultVersion);
continue 2; //try next schema
}
}

View File

@ -43,15 +43,25 @@ final class BlockStateUpgradeSchemaModelBlockRemap{
*/
public ?array $newState;
/**
* @var string[]
* @phpstan-var list<string>
* May not be present in older schemas
*/
public array $copiedState;
/**
* @param BlockStateUpgradeSchemaModelTag[] $oldState
* @param BlockStateUpgradeSchemaModelTag[] $newState
* @param string[] $copiedState
* @phpstan-param array<string, BlockStateUpgradeSchemaModelTag> $oldState
* @phpstan-param array<string, BlockStateUpgradeSchemaModelTag> $newState
* @phpstan-param list<string> $copiedState
*/
public function __construct(array $oldState, string $newName, array $newState){
public function __construct(array $oldState, string $newName, array $newState, array $copiedState){
$this->oldState = count($oldState) === 0 ? null : $oldState;
$this->newName = $newName;
$this->newState = count($newState) === 0 ? null : $newState;
$this->copiedState = $copiedState;
}
}