Mojang cannot be relied on ...

This commit is contained in:
Dylan K. Taylor 2022-05-13 20:50:38 +01:00
parent 643556a366
commit a75bc5d537
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
4 changed files with 22 additions and 45 deletions

View File

@ -38,9 +38,9 @@ final class BlockStateData{
*/
public const CURRENT_VERSION =
(1 << 24) | //major
(16 << 16) | //minor
(210 << 8) | //patch
(3); //revision
(18 << 16) | //minor
(10 << 8) | //patch
(1); //revision
public const TAG_NAME = "name";
public const TAG_STATES = "states";

View File

@ -70,13 +70,16 @@ final class BlockStateUpgradeSchema{
public int $maxVersionMajor,
public int $maxVersionMinor,
public int $maxVersionPatch,
public int $maxVersionRevision
public int $maxVersionRevision,
private int $priority
){}
public function getVersionId() : int{
return ($this->maxVersionMajor << 24) | ($this->maxVersionMinor << 16) | ($this->maxVersionPatch << 8) | $this->maxVersionRevision;
}
public function getPriority() : int{ return $this->priority; }
public function isEmpty() : bool{
foreach([
$this->renamedIds,
@ -93,22 +96,4 @@ final class BlockStateUpgradeSchema{
return true;
}
public function isBackwardsCompatible() : bool{
if($this->backwardsCompatible === null){
$this->backwardsCompatible = true;
foreach([
$this->renamedIds,
$this->removedProperties,
$this->remappedPropertyValues,
$this->remappedStates
] as $bcBreakingRules){
if(count($bcBreakingRules) !== 0){
$this->backwardsCompatible = false;
}
}
}
//schemas which only add properties are backwards compatible
return $this->backwardsCompatible;
}
}

View File

@ -111,12 +111,13 @@ final class BlockStateUpgradeSchemaUtils{
};
}
public static function fromJsonModel(BlockStateUpgradeSchemaModel $model) : BlockStateUpgradeSchema{
public static function fromJsonModel(BlockStateUpgradeSchemaModel $model, int $priority) : BlockStateUpgradeSchema{
$result = new BlockStateUpgradeSchema(
$model->maxVersionMajor,
$model->maxVersionMinor,
$model->maxVersionPatch,
$model->maxVersionRevision
$model->maxVersionRevision,
$priority
);
$result->renamedIds = $model->renamedIds ?? [];
$result->renamedProperties = $model->renamedProperties ?? [];
@ -280,7 +281,7 @@ final class BlockStateUpgradeSchemaUtils{
}
try{
$schema = self::loadSchemaFromString($raw);
$schema = self::loadSchemaFromString($raw, $priority);
}catch(\RuntimeException $e){
throw new \RuntimeException("Loading schema file $fullPath: " . $e->getMessage(), 0, $e);
}
@ -298,7 +299,7 @@ final class BlockStateUpgradeSchemaUtils{
return $result;
}
public static function loadSchemaFromString(string $raw) : BlockStateUpgradeSchema{
public static function loadSchemaFromString(string $raw, int $priority) : BlockStateUpgradeSchema{
try{
$json = json_decode($raw, false, flags: JSON_THROW_ON_ERROR);
}catch(\JsonException $e){
@ -315,6 +316,6 @@ final class BlockStateUpgradeSchemaUtils{
throw new \RuntimeException($e->getMessage(), 0, $e);
}
return self::fromJsonModel($model);
return self::fromJsonModel($model, $priority);
}
}

View File

@ -27,7 +27,6 @@ use pocketmine\data\bedrock\blockstate\BlockStateData;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\Tag;
use pocketmine\utils\Utils;
use function array_unshift;
use function ksort;
use const SORT_NUMERIC;
@ -46,19 +45,16 @@ final class BlockStateUpgrader{
}
public function addSchema(BlockStateUpgradeSchema $schema) : void{
if(!$schema->isBackwardsCompatible()){
$schemaList = $this->upgradeSchemas[$schema->getVersionId()] ?? [];
foreach($schemaList as $otherSchema){
if(!$otherSchema->isBackwardsCompatible()){
throw new \InvalidArgumentException("Cannot add two backwards-incompatible schemas with the same version");
}
}
array_unshift($schemaList, $schema);
$this->upgradeSchemas[$schema->getVersionId()] = $schemaList;
}else{
//Backwards-compatible schemas can be added in any order
$this->upgradeSchemas[$schema->getVersionId()][] = $schema;
$schemaList = $this->upgradeSchemas[$schema->getVersionId()] ?? [];
$priority = $schema->getPriority();
if(isset($schemaList[$priority])){
throw new \InvalidArgumentException("Cannot add two schemas to the same version with the same priority");
}
$schemaList[$priority] = $schema;
ksort($schemaList, SORT_NUMERIC);
$this->upgradeSchemas[$schema->getVersionId()] = $schemaList;
ksort($this->upgradeSchemas, SORT_NUMERIC);
}
@ -71,11 +67,6 @@ final class BlockStateUpgrader{
continue;
}
foreach($schemas as $schema){
if(!$schema->isBackwardsCompatible() && $resultVersion === $version){
//backwards-compatible updates typically don't bump version and must always be applied because we
//can't tell any different, but backwards-incompatible ones SHOULD always get their own version bump
continue;
}
$oldName = $blockStateData->getName();
if(isset($schema->remappedStates[$oldName])){
foreach($schema->remappedStates[$oldName] as $remap){