Reapply 470a3e1a3: tools/generate-blockstate-upgrade-schema: reduce dependencies for generating blockstate mappings

This commit is contained in:
Dylan K. Taylor 2023-06-26 12:40:17 +01:00
parent 0b0b72f596
commit 882d50b14e
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -280,15 +280,13 @@ function processStateGroup(string $oldName, array $upgradeTable, BlockStateUpgra
* This significantly reduces the output size during flattening when the flattened block has many permutations * This significantly reduces the output size during flattening when the flattened block has many permutations
* (e.g. walls). * (e.g. walls).
* *
* @param BlockStateUpgradeSchemaBlockRemap[] $stateRemaps * @param BlockStateMapping[] $upgradeTable
* @param BlockStateMapping[] $upgradeTable
* @phpstan-param list<BlockStateUpgradeSchemaBlockRemap> $stateRemaps
* @phpstan-param array<string, BlockStateMapping> $upgradeTable * @phpstan-param array<string, BlockStateMapping> $upgradeTable
* *
* @return BlockStateUpgradeSchemaBlockRemap[] * @return BlockStateUpgradeSchemaBlockRemap[]
* @phpstan-return list<BlockStateUpgradeSchemaBlockRemap> * @phpstan-return list<BlockStateUpgradeSchemaBlockRemap>
*/ */
function compressRemappedStates(array $upgradeTable, array $stateRemaps) : array{ function processRemappedStates(array $upgradeTable) : array{
$unchangedStatesByNewName = []; $unchangedStatesByNewName = [];
foreach($upgradeTable as $pair){ foreach($upgradeTable as $pair){
@ -331,21 +329,26 @@ function compressRemappedStates(array $upgradeTable, array $stateRemaps) : array
$compressedRemaps = []; $compressedRemaps = [];
foreach($stateRemaps as $remap){ foreach($upgradeTable as $remap){
$oldState = $remap->oldState; $oldState = $remap->old->getStates();
$newState = $remap->newState; $newState = $remap->new->getStates();
if($oldState === null || $newState === null){ if(count($oldState) === 0 || count($newState) === 0){
//no unchanged states - no compression possible //all states have changed in some way - compression not possible
assert(!isset($unchangedStatesByNewName[$remap->newName])); assert(!isset($unchangedStatesByNewName[$remap->new->getName()]));
$compressedRemaps[$remap->newName][] = $remap; $compressedRemaps[$remap->new->getName()][] = new BlockStateUpgradeSchemaBlockRemap(
$oldState,
$remap->new->getName(),
$newState,
[]
);
continue; continue;
} }
$cleanedOldState = $oldState; $cleanedOldState = $oldState;
$cleanedNewState = $newState; $cleanedNewState = $newState;
foreach($unchangedStatesByNewName[$remap->newName] as $propertyName){ foreach($unchangedStatesByNewName[$remap->new->getName()] as $propertyName){
unset($cleanedOldState[$propertyName]); unset($cleanedOldState[$propertyName]);
unset($cleanedNewState[$propertyName]); unset($cleanedNewState[$propertyName]);
} }
@ -353,10 +356,8 @@ function compressRemappedStates(array $upgradeTable, array $stateRemaps) : array
ksort($cleanedNewState); ksort($cleanedNewState);
$duplicate = false; $duplicate = false;
$compressedRemaps[$remap->newName] ??= []; $compressedRemaps[$remap->new->getName()] ??= [];
foreach($compressedRemaps[$remap->newName] as $k => $compressedRemap){ foreach($compressedRemaps[$remap->new->getName()] as $k => $compressedRemap){
assert($compressedRemap->oldState !== null && $compressedRemap->newState !== null);
if( if(
count($compressedRemap->oldState) !== count($cleanedOldState) || count($compressedRemap->oldState) !== count($cleanedOldState) ||
count($compressedRemap->newState) !== count($cleanedNewState) count($compressedRemap->newState) !== count($cleanedNewState)
@ -379,11 +380,11 @@ function compressRemappedStates(array $upgradeTable, array $stateRemaps) : array
break; break;
} }
if(!$duplicate){ if(!$duplicate){
$compressedRemaps[$remap->newName][] = new BlockStateUpgradeSchemaBlockRemap( $compressedRemaps[$remap->new->getName()][] = new BlockStateUpgradeSchemaBlockRemap(
$cleanedOldState, $cleanedOldState,
$remap->newName, $remap->new->getName(),
$cleanedNewState, $cleanedNewState,
$unchangedStatesByNewName[$remap->newName] $unchangedStatesByNewName[$remap->new->getName()]
); );
} }
} }
@ -455,21 +456,9 @@ function generateBlockStateUpgradeSchema(array $upgradeTable) : BlockStateUpgrad
} }
} }
//block mapped to multiple different new IDs; we can't guess these, so we just do a plain old remap //block mapped to multiple different new IDs; we can't guess these, so we just do a plain old remap
foreach($blockStateMappings as $mapping){ $result->remappedStates[$oldName] = processRemappedStates($blockStateMappings);
if(!$mapping->old->equals($mapping->new)){
$result->remappedStates[$mapping->old->getName()][] = new BlockStateUpgradeSchemaBlockRemap(
$mapping->old->getStates(),
$mapping->new->getName(),
$mapping->new->getStates(),
[]
);
}
}
} }
} }
foreach(Utils::stringifyKeys($result->remappedStates) as $oldName => $remap){
$result->remappedStates[$oldName] = compressRemappedStates($upgradeTable[$oldName], $remap);
}
return $result; return $result;
} }