PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
LegacySkinAdapter.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\network\mcpe\convert;
25
30use function is_array;
31use function is_string;
32use function json_decode;
33use function json_encode;
34use function random_bytes;
35use function str_repeat;
36use const JSON_THROW_ON_ERROR;
37
39
40 public function toSkinData(Skin $skin) : SkinData{
41 $capeData = $skin->getCapeData();
42 $capeImage = $capeData === "" ? new SkinImage(0, 0, "") : new SkinImage(32, 64, $capeData);
43 $geometryName = $skin->getGeometryName();
44 if($geometryName === ""){
45 $geometryName = "geometry.humanoid.custom";
46 }
47 return new SkinData(
48 $skin->getSkinId(),
49 "", //TODO: playfab ID
50 json_encode(["geometry" => ["default" => $geometryName]], JSON_THROW_ON_ERROR),
51 SkinImage::fromLegacy($skin->getSkinData()), [],
52 $capeImage,
53 $skin->getGeometryData()
54 );
55 }
56
57 public function fromSkinData(SkinData $data) : Skin{
58 if($data->isPersona()){
59 return new Skin("Standard_Custom", str_repeat(random_bytes(3) . "\xff", 4096));
60 }
61
62 $capeData = $data->isPersonaCapeOnClassic() ? "" : $data->getCapeImage()->getData();
63
64 $resourcePatch = json_decode($data->getResourcePatch(), true);
65 if(is_array($resourcePatch) && isset($resourcePatch["geometry"]["default"]) && is_string($resourcePatch["geometry"]["default"])){
66 $geometryName = $resourcePatch["geometry"]["default"];
67 }else{
68 throw new InvalidSkinException("Missing geometry name field");
69 }
70
71 return new Skin($data->getSkinId(), $data->getSkinImage()->getData(), $capeData, $geometryName, $data->getGeometryData());
72 }
73}