moved skin-parsing code from LoginPacketHandler to its own dedicated helper class

This commit is contained in:
Dylan K. Taylor 2020-06-17 21:24:12 +01:00
parent 55e6b2dfbc
commit 503782d67c
2 changed files with 88 additions and 44 deletions

View File

@ -38,6 +38,7 @@ use pocketmine\network\mcpe\protocol\types\login\AuthenticationData;
use pocketmine\network\mcpe\protocol\types\login\ClientData;
use pocketmine\network\mcpe\protocol\types\login\ClientDataPersonaPieceTintColor;
use pocketmine\network\mcpe\protocol\types\login\ClientDataPersonaSkinPiece;
use pocketmine\network\mcpe\protocol\types\login\ClientDataToSkinDataHelper;
use pocketmine\network\mcpe\protocol\types\login\JwtChain;
use pocketmine\network\mcpe\protocol\types\PersonaPieceTintColor;
use pocketmine\network\mcpe\protocol\types\PersonaSkinPiece;
@ -109,51 +110,8 @@ class LoginPacketHandler extends PacketHandler{
}
$clientData = $this->parseClientData($packet->clientDataJwt);
$safeB64Decode = static function(string $base64, string $context) : string{
$result = base64_decode($base64, true);
if($result === false){
throw new \InvalidArgumentException("$context: Malformed base64, cannot be decoded");
}
return $result;
};
try{
/** @var SkinAnimation[] $animations */
$animations = [];
foreach($clientData->AnimatedImageData as $k => $animation){
$animations[] = new SkinAnimation(
new SkinImage(
$animation->ImageHeight,
$animation->ImageWidth,
$safeB64Decode($animation->Image, "AnimatedImageData.$k.Image")
),
$animation->Type,
$animation->Frames
);
}
$skinData = new SkinData(
$clientData->SkinId,
$safeB64Decode($clientData->SkinResourcePatch, "SkinResourcePatch"),
new SkinImage($clientData->SkinImageHeight, $clientData->SkinImageWidth, $safeB64Decode($clientData->SkinData, "SkinData")),
$animations,
new SkinImage($clientData->CapeImageHeight, $clientData->CapeImageWidth, $safeB64Decode($clientData->CapeData, "CapeData")),
$safeB64Decode($clientData->SkinGeometryData, "SkinGeometryData"),
$safeB64Decode($clientData->SkinAnimationData, "SkinAnimationData"),
$clientData->PremiumSkin,
$clientData->PersonaSkin,
$clientData->CapeOnClassicSkin,
$clientData->CapeId,
null,
$clientData->ArmSize,
$clientData->SkinColor,
array_map(function(ClientDataPersonaSkinPiece $piece) : PersonaSkinPiece{
return new PersonaSkinPiece($piece->PieceId, $piece->PieceType, $piece->PackId, $piece->IsDefault, $piece->ProductId);
}, $clientData->PersonaPieces),
array_map(function(ClientDataPersonaPieceTintColor $tint) : PersonaPieceTintColor{
return new PersonaPieceTintColor($tint->PieceType, $tint->Colors);
}, $clientData->PieceTintColors)
);
$skin = SkinAdapterSingleton::get()->fromSkinData($skinData);
$skin = SkinAdapterSingleton::get()->fromSkinData(ClientDataToSkinDataHelper::getInstance()->fromClientData($clientData));
}catch(\InvalidArgumentException $e){
$this->session->getLogger()->debug("Invalid skin: " . $e->getMessage());
$this->session->disconnect("disconnectionScreen.invalidSkin");

View File

@ -0,0 +1,86 @@
<?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\network\mcpe\protocol\types\login;
use pocketmine\network\mcpe\protocol\types\PersonaPieceTintColor;
use pocketmine\network\mcpe\protocol\types\PersonaSkinPiece;
use pocketmine\network\mcpe\protocol\types\SkinAnimation;
use pocketmine\network\mcpe\protocol\types\SkinData;
use pocketmine\network\mcpe\protocol\types\SkinImage;
use pocketmine\utils\SingletonTrait;
use function array_map;
use function base64_decode;
final class ClientDataToSkinDataHelper{
use SingletonTrait;
/**
* @throws \InvalidArgumentException
*/
public function fromClientData(ClientData $clientData) : SkinData{
$safeB64Decode = static function(string $base64, string $context) : string{
$result = base64_decode($base64, true);
if($result === false){
throw new \InvalidArgumentException("$context: Malformed base64, cannot be decoded");
}
return $result;
};
/** @var SkinAnimation[] $animations */
$animations = [];
foreach($clientData->AnimatedImageData as $k => $animation){
$animations[] = new SkinAnimation(
new SkinImage(
$animation->ImageHeight,
$animation->ImageWidth,
$safeB64Decode($animation->Image, "AnimatedImageData.$k.Image")
),
$animation->Type,
$animation->Frames
);
}
return new SkinData(
$clientData->SkinId,
$safeB64Decode($clientData->SkinResourcePatch, "SkinResourcePatch"),
new SkinImage($clientData->SkinImageHeight, $clientData->SkinImageWidth, $safeB64Decode($clientData->SkinData, "SkinData")),
$animations,
new SkinImage($clientData->CapeImageHeight, $clientData->CapeImageWidth, $safeB64Decode($clientData->CapeData, "CapeData")),
$safeB64Decode($clientData->SkinGeometryData, "SkinGeometryData"),
$safeB64Decode($clientData->SkinAnimationData, "SkinAnimationData"),
$clientData->PremiumSkin,
$clientData->PersonaSkin,
$clientData->CapeOnClassicSkin,
$clientData->CapeId,
null,
$clientData->ArmSize,
$clientData->SkinColor,
array_map(function(ClientDataPersonaSkinPiece $piece) : PersonaSkinPiece{
return new PersonaSkinPiece($piece->PieceId, $piece->PieceType, $piece->PackId, $piece->IsDefault, $piece->ProductId);
}, $clientData->PersonaPieces),
array_map(function(ClientDataPersonaPieceTintColor $tint) : PersonaPieceTintColor{
return new PersonaPieceTintColor($tint->PieceType, $tint->Colors);
}, $clientData->PieceTintColors)
);
}
}