Allow limiting max schema ID loaded for item ID upgrading

This commit is contained in:
Dylan K. Taylor 2023-02-02 15:29:45 +00:00
parent 0e15a8698a
commit e9b994cbc3
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
4 changed files with 16 additions and 12 deletions

View File

@ -64,10 +64,10 @@ final class ItemDataUpgrader{
} }
public function addIdMetaUpgradeSchema(ItemIdMetaUpgradeSchema $schema) : void{ public function addIdMetaUpgradeSchema(ItemIdMetaUpgradeSchema $schema) : void{
if(isset($this->idMetaUpgradeSchemas[$schema->getPriority()])){ if(isset($this->idMetaUpgradeSchemas[$schema->getSchemaId()])){
throw new \InvalidArgumentException("Already have a schema with priority " . $schema->getPriority()); throw new \InvalidArgumentException("Already have a schema with priority " . $schema->getSchemaId());
} }
$this->idMetaUpgradeSchemas[$schema->getPriority()] = $schema; $this->idMetaUpgradeSchemas[$schema->getSchemaId()] = $schema;
ksort($this->idMetaUpgradeSchemas, SORT_NUMERIC); ksort($this->idMetaUpgradeSchemas, SORT_NUMERIC);
} }

View File

@ -36,10 +36,10 @@ final class ItemIdMetaUpgradeSchema{
public function __construct( public function __construct(
private array $renamedIds, private array $renamedIds,
private array $remappedMetas, private array $remappedMetas,
private int $priority private int $schemaId
){} ){}
public function getPriority() : int{ return $this->priority; } public function getSchemaId() : int{ return $this->schemaId; }
public function renameId(string $id) : ?string{ public function renameId(string $id) : ?string{
return $this->renamedIds[mb_strtolower($id, 'US-ASCII')] ?? null; return $this->renamedIds[mb_strtolower($id, 'US-ASCII')] ?? null;

View File

@ -39,7 +39,7 @@ final class ItemIdMetaUpgradeSchemaUtils{
* @return ItemIdMetaUpgradeSchema[] * @return ItemIdMetaUpgradeSchema[]
* @phpstan-return array<int, ItemIdMetaUpgradeSchema> * @phpstan-return array<int, ItemIdMetaUpgradeSchema>
*/ */
public static function loadSchemas(string $path) : array{ public static function loadSchemas(string $path, int $maxSchemaId) : array{
$iterator = new \RegexIterator( $iterator = new \RegexIterator(
new \FilesystemIterator( new \FilesystemIterator(
$path, $path,
@ -55,26 +55,29 @@ final class ItemIdMetaUpgradeSchemaUtils{
/** @var string[] $matches */ /** @var string[] $matches */
foreach($iterator as $matches){ foreach($iterator as $matches){
$filename = $matches[0]; $filename = $matches[0];
$priority = (int) $matches[1]; $schemaId = (int) $matches[1];
if($schemaId > $maxSchemaId){
continue;
}
$fullPath = Path::join($path, $filename); $fullPath = Path::join($path, $filename);
$raw = Filesystem::fileGetContents($fullPath); $raw = Filesystem::fileGetContents($fullPath);
try{ try{
$schema = self::loadSchemaFromString($raw, $priority); $schema = self::loadSchemaFromString($raw, $schemaId);
}catch(\RuntimeException $e){ }catch(\RuntimeException $e){
throw new \RuntimeException("Loading schema file $fullPath: " . $e->getMessage(), 0, $e); throw new \RuntimeException("Loading schema file $fullPath: " . $e->getMessage(), 0, $e);
} }
$result[$priority] = $schema; $result[$schemaId] = $schema;
} }
ksort($result, SORT_NUMERIC); ksort($result, SORT_NUMERIC);
return $result; return $result;
} }
public static function loadSchemaFromString(string $raw, int $priority) : ItemIdMetaUpgradeSchema{ public static function loadSchemaFromString(string $raw, int $schemaId) : ItemIdMetaUpgradeSchema{
try{ try{
$json = json_decode($raw, false, flags: JSON_THROW_ON_ERROR); $json = json_decode($raw, false, flags: JSON_THROW_ON_ERROR);
}catch(\JsonException $e){ }catch(\JsonException $e){
@ -91,6 +94,6 @@ final class ItemIdMetaUpgradeSchemaUtils{
throw new \RuntimeException($e->getMessage(), 0, $e); throw new \RuntimeException($e->getMessage(), 0, $e);
} }
return new ItemIdMetaUpgradeSchema($model->renamedIds, $model->remappedMetas, $priority); return new ItemIdMetaUpgradeSchema($model->renamedIds, $model->remappedMetas, $schemaId);
} }
} }

View File

@ -33,6 +33,7 @@ use Symfony\Component\Filesystem\Path;
use const pocketmine\BEDROCK_ITEM_UPGRADE_SCHEMA_PATH; use const pocketmine\BEDROCK_ITEM_UPGRADE_SCHEMA_PATH;
final class GlobalItemDataHandlers{ final class GlobalItemDataHandlers{
public const MAX_ITEM_ID_UPGRADE_SCHEMA_ID = 81; //0081_1.18.30_to_1.19.30.34_beta.json
private static ?ItemSerializer $itemSerializer = null; private static ?ItemSerializer $itemSerializer = null;
@ -53,7 +54,7 @@ final class GlobalItemDataHandlers{
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')) ItemIdMetaUpgradeSchemaUtils::loadSchemas(Path::join(BEDROCK_ITEM_UPGRADE_SCHEMA_PATH, 'id_meta_upgrade_schema'), self::MAX_ITEM_ID_UPGRADE_SCHEMA_ID)
); );
} }
} }