Split up ItemDataUpgrader (preparing for code backport)

This commit is contained in:
Dylan K. Taylor 2023-02-02 16:08:49 +00:00
parent e9b994cbc3
commit 6b7a4e2c41
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
3 changed files with 87 additions and 44 deletions

View File

@ -36,40 +36,19 @@ use pocketmine\nbt\tag\ShortTag;
use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\tag\StringTag;
use pocketmine\utils\Binary; use pocketmine\utils\Binary;
use function assert; use function assert;
use function ksort;
use const SORT_NUMERIC;
final class ItemDataUpgrader{ final class ItemDataUpgrader{
private const TAG_LEGACY_ID = "id"; //TAG_Short (or TAG_String for Java itemstacks) private const TAG_LEGACY_ID = "id"; //TAG_Short (or TAG_String for Java itemstacks)
/** /**
* @var ItemIdMetaUpgradeSchema[]
* @phpstan-var array<int, ItemIdMetaUpgradeSchema>
*/
private array $idMetaUpgradeSchemas = [];
/**
* @param ItemIdMetaUpgradeSchema[] $idMetaUpgradeSchemas
* @phpstan-param array<int, ItemIdMetaUpgradeSchema> $idMetaUpgradeSchemas * @phpstan-param array<int, ItemIdMetaUpgradeSchema> $idMetaUpgradeSchemas
*/ */
public function __construct( public function __construct(
private ItemIdMetaUpgrader $idMetaUpgrader,
private LegacyItemIdToStringIdMap $legacyIntToStringIdMap, private LegacyItemIdToStringIdMap $legacyIntToStringIdMap,
private R12ItemIdToBlockIdMap $r12ItemIdToBlockIdMap, private R12ItemIdToBlockIdMap $r12ItemIdToBlockIdMap,
private BlockDataUpgrader $blockDataUpgrader, private BlockDataUpgrader $blockDataUpgrader,
array $idMetaUpgradeSchemas ){}
){
foreach($idMetaUpgradeSchemas as $schema){
$this->addIdMetaUpgradeSchema($schema);
}
}
public function addIdMetaUpgradeSchema(ItemIdMetaUpgradeSchema $schema) : void{
if(isset($this->idMetaUpgradeSchemas[$schema->getSchemaId()])){
throw new \InvalidArgumentException("Already have a schema with priority " . $schema->getSchemaId());
}
$this->idMetaUpgradeSchemas[$schema->getSchemaId()] = $schema;
ksort($this->idMetaUpgradeSchemas, SORT_NUMERIC);
}
/** /**
* This function replaces the legacy ItemFactory::get(). * This function replaces the legacy ItemFactory::get().
@ -87,7 +66,7 @@ final class ItemDataUpgrader{
$blockStateData = null; $blockStateData = null;
} }
[$newNameId, $newMeta] = $this->upgradeItemStringIdMeta($rawNameId, $meta); [$newNameId, $newMeta] = $this->idMetaUpgrader->upgradeStringIdMeta($rawNameId, $meta);
//TODO: this won't account for spawn eggs from before 1.16.100 - perhaps we're lucky and they just left the meta in there anyway? //TODO: this won't account for spawn eggs from before 1.16.100 - perhaps we're lucky and they just left the meta in there anyway?
@ -107,6 +86,8 @@ final class ItemDataUpgrader{
* @throws SavedDataLoadingException if the legacy numeric ID doesn't map to a string ID * @throws SavedDataLoadingException if the legacy numeric ID doesn't map to a string ID
*/ */
public function upgradeItemTypeDataInt(int $legacyNumericId, int $meta, int $count, ?CompoundTag $nbt) : SavedItemStackData{ public function upgradeItemTypeDataInt(int $legacyNumericId, int $meta, int $count, ?CompoundTag $nbt) : SavedItemStackData{
//do not upgrade the ID beyond this initial step - we need the 1.12 ID for the item ID -> block ID map in the
//next step
$rawNameId = $this->legacyIntToStringIdMap->legacyToString($legacyNumericId); $rawNameId = $this->legacyIntToStringIdMap->legacyToString($legacyNumericId);
if($rawNameId === null){ if($rawNameId === null){
throw new SavedDataLoadingException("Unmapped legacy item ID $legacyNumericId"); throw new SavedDataLoadingException("Unmapped legacy item ID $legacyNumericId");
@ -158,7 +139,7 @@ final class ItemDataUpgrader{
$blockStateData = null; $blockStateData = null;
} }
[$newNameId, $newMeta] = $this->upgradeItemStringIdMeta($rawNameId, $meta); [$newNameId, $newMeta] = $this->idMetaUpgrader->upgradeStringIdMeta($rawNameId, $meta);
//TODO: this won't account for spawn eggs from before 1.16.100 - perhaps we're lucky and they just left the meta in there anyway? //TODO: this won't account for spawn eggs from before 1.16.100 - perhaps we're lucky and they just left the meta in there anyway?
@ -217,21 +198,5 @@ final class ItemDataUpgrader{
); );
} }
/** public function getIdMetaUpgrader() : ItemIdMetaUpgrader{ return $this->idMetaUpgrader; }
* @phpstan-return array{string, int}
*/
public function upgradeItemStringIdMeta(string $id, int $meta) : array{
$newId = $id;
$newMeta = $meta;
foreach($this->idMetaUpgradeSchemas as $schema){
if(($remappedMetaId = $schema->remapMeta($newId, $newMeta)) !== null){
$newId = $remappedMetaId;
$newMeta = 0;
}elseif(($renamedId = $schema->renameId($newId)) !== null){
$newId = $renamedId;
}
}
return [$newId, $newMeta];
}
} }

View File

@ -0,0 +1,77 @@
<?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\item\upgrade;
use function ksort;
use const SORT_NUMERIC;
/**
* Upgrades old item string IDs and metas to newer ones according to the given schemas.
*/
final class ItemIdMetaUpgrader{
/**
* @var ItemIdMetaUpgradeSchema[]
* @phpstan-var array<int, ItemIdMetaUpgradeSchema>
*/
private array $idMetaUpgradeSchemas = [];
/**
* @param ItemIdMetaUpgradeSchema[] $idMetaUpgradeSchemas
* @phpstan-param array<int, ItemIdMetaUpgradeSchema> $idMetaUpgradeSchemas
*/
public function __construct(
array $idMetaUpgradeSchemas,
){
foreach($idMetaUpgradeSchemas as $schema){
$this->addIdMetaUpgradeSchema($schema);
}
}
public function addIdMetaUpgradeSchema(ItemIdMetaUpgradeSchema $schema) : void{
if(isset($this->idMetaUpgradeSchemas[$schema->getSchemaId()])){
throw new \InvalidArgumentException("Already have a schema with priority " . $schema->getSchemaId());
}
$this->idMetaUpgradeSchemas[$schema->getSchemaId()] = $schema;
ksort($this->idMetaUpgradeSchemas, SORT_NUMERIC);
}
/**
* @phpstan-return array{string, int}
*/
public function upgradeStringIdMeta(string $id, int $meta) : array{
$newId = $id;
$newMeta = $meta;
foreach($this->idMetaUpgradeSchemas as $schema){
if(($remappedMetaId = $schema->remapMeta($newId, $newMeta)) !== null){
$newId = $remappedMetaId;
$newMeta = 0;
}elseif(($renamedId = $schema->renameId($newId)) !== null){
$newId = $renamedId;
}
}
return [$newId, $newMeta];
}
}

View File

@ -26,6 +26,7 @@ namespace pocketmine\world\format\io;
use pocketmine\data\bedrock\item\ItemDeserializer; use pocketmine\data\bedrock\item\ItemDeserializer;
use pocketmine\data\bedrock\item\ItemSerializer; use pocketmine\data\bedrock\item\ItemSerializer;
use pocketmine\data\bedrock\item\upgrade\ItemDataUpgrader; use pocketmine\data\bedrock\item\upgrade\ItemDataUpgrader;
use pocketmine\data\bedrock\item\upgrade\ItemIdMetaUpgrader;
use pocketmine\data\bedrock\item\upgrade\ItemIdMetaUpgradeSchemaUtils; use pocketmine\data\bedrock\item\upgrade\ItemIdMetaUpgradeSchemaUtils;
use pocketmine\data\bedrock\item\upgrade\LegacyItemIdToStringIdMap; use pocketmine\data\bedrock\item\upgrade\LegacyItemIdToStringIdMap;
use pocketmine\data\bedrock\item\upgrade\R12ItemIdToBlockIdMap; use pocketmine\data\bedrock\item\upgrade\R12ItemIdToBlockIdMap;
@ -51,10 +52,10 @@ final class GlobalItemDataHandlers{
public static function getUpgrader() : ItemDataUpgrader{ public static function getUpgrader() : ItemDataUpgrader{
return self::$itemDataUpgrader ??= new ItemDataUpgrader( return self::$itemDataUpgrader ??= new ItemDataUpgrader(
new ItemIdMetaUpgrader(ItemIdMetaUpgradeSchemaUtils::loadSchemas(Path::join(BEDROCK_ITEM_UPGRADE_SCHEMA_PATH, 'id_meta_upgrade_schema'), self::MAX_ITEM_ID_UPGRADE_SCHEMA_ID)),
LegacyItemIdToStringIdMap::getInstance(), LegacyItemIdToStringIdMap::getInstance(),
R12ItemIdToBlockIdMap::getInstance(), R12ItemIdToBlockIdMap::getInstance(),
GlobalBlockStateHandlers::getUpgrader(), GlobalBlockStateHandlers::getUpgrader()
ItemIdMetaUpgradeSchemaUtils::loadSchemas(Path::join(BEDROCK_ITEM_UPGRADE_SCHEMA_PATH, 'id_meta_upgrade_schema'), self::MAX_ITEM_ID_UPGRADE_SCHEMA_ID)
); );
} }
} }