2020-01-09 03:05:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2019-10-09 18:39:38 +00:00
|
|
|
package org.geysermc.connector.utils;
|
|
|
|
|
|
|
|
import com.github.steveice10.mc.auth.data.GameProfile;
|
|
|
|
import com.google.gson.JsonObject;
|
2019-10-31 02:14:23 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.ImageData;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.SerializedSkin;
|
2019-10-09 18:39:38 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.PlayerListPacket;
|
2019-12-21 17:35:48 +00:00
|
|
|
|
2019-10-09 18:39:38 +00:00
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
import lombok.Getter;
|
2019-12-21 17:35:48 +00:00
|
|
|
|
2020-01-04 05:58:58 +00:00
|
|
|
import org.geysermc.common.AuthType;
|
2019-11-27 01:52:13 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2019-10-09 18:39:38 +00:00
|
|
|
import org.geysermc.connector.entity.PlayerEntity;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
|
2019-12-01 21:16:52 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2019-10-09 18:39:38 +00:00
|
|
|
import java.util.Base64;
|
2019-11-16 03:25:43 +00:00
|
|
|
import java.util.Collections;
|
2019-10-09 18:39:38 +00:00
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
|
public class SkinUtils {
|
2020-01-04 05:58:58 +00:00
|
|
|
|
2019-10-09 18:39:38 +00:00
|
|
|
public static PlayerListPacket.Entry buildCachedEntry(GameProfile profile, long geyserId) {
|
|
|
|
GameProfileData data = GameProfileData.from(profile);
|
2019-11-19 20:31:24 +00:00
|
|
|
SkinProvider.Cape cape = SkinProvider.getCachedCape(data.getCapeUrl());
|
2019-10-09 18:39:38 +00:00
|
|
|
|
|
|
|
return buildEntryManually(
|
|
|
|
profile.getId(),
|
|
|
|
profile.getName(),
|
|
|
|
geyserId,
|
|
|
|
profile.getIdAsString(),
|
|
|
|
SkinProvider.getCachedSkin(profile.getId()).getSkinData(),
|
2019-11-19 20:31:24 +00:00
|
|
|
cape.getCapeId(),
|
|
|
|
cape.getCapeData(),
|
2019-11-06 00:55:59 +00:00
|
|
|
getLegacySkinGeometry("geometry.humanoid.custom" + (data.isAlex() ? "Slim" : "")),
|
2019-10-09 18:39:38 +00:00
|
|
|
""
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static PlayerListPacket.Entry buildDefaultEntry(GameProfile profile, long geyserId) {
|
|
|
|
return buildEntryManually(
|
|
|
|
profile.getId(),
|
|
|
|
profile.getName(),
|
|
|
|
geyserId,
|
|
|
|
profile.getIdAsString(),
|
|
|
|
SkinProvider.STEVE_SKIN,
|
2019-11-19 20:31:24 +00:00
|
|
|
SkinProvider.EMPTY_CAPE.getCapeId(),
|
2019-10-09 18:39:38 +00:00
|
|
|
SkinProvider.EMPTY_CAPE.getCapeData(),
|
2019-11-06 00:55:59 +00:00
|
|
|
getLegacySkinGeometry("geometry.humanoid"),
|
2019-10-09 18:39:38 +00:00
|
|
|
""
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static PlayerListPacket.Entry buildEntryManually(UUID uuid, String username, long geyserId,
|
2019-11-19 20:31:24 +00:00
|
|
|
String skinId, byte[] skinData,
|
|
|
|
String capeId, byte[] capeData,
|
2019-10-09 18:39:38 +00:00
|
|
|
String geometryName, String geometryData) {
|
2019-11-19 20:31:24 +00:00
|
|
|
SerializedSkin serializedSkin = SerializedSkin.of(
|
|
|
|
skinId, geometryName, ImageData.of(skinData), Collections.emptyList(),
|
|
|
|
ImageData.of(capeData), geometryData, "", true, false, false, capeId, uuid.toString()
|
|
|
|
);
|
2019-10-31 02:14:23 +00:00
|
|
|
|
2019-10-09 18:39:38 +00:00
|
|
|
PlayerListPacket.Entry entry = new PlayerListPacket.Entry(uuid);
|
|
|
|
entry.setName(username);
|
|
|
|
entry.setEntityId(geyserId);
|
2019-10-31 02:14:23 +00:00
|
|
|
entry.setSkin(serializedSkin);
|
2019-10-09 18:39:38 +00:00
|
|
|
entry.setXuid("");
|
|
|
|
entry.setPlatformChatId("");
|
2019-11-06 00:55:59 +00:00
|
|
|
entry.setTeacher(false);
|
2019-10-09 18:39:38 +00:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
@AllArgsConstructor
|
|
|
|
@Getter
|
|
|
|
public static class GameProfileData {
|
|
|
|
private String skinUrl;
|
|
|
|
private String capeUrl;
|
|
|
|
private boolean alex;
|
|
|
|
|
|
|
|
public static GameProfileData from(GameProfile profile) {
|
2019-10-10 21:27:30 +00:00
|
|
|
try {
|
|
|
|
GameProfile.Property skinProperty = profile.getProperty("textures");
|
2019-10-09 18:39:38 +00:00
|
|
|
|
2019-12-01 21:16:52 +00:00
|
|
|
JsonObject skinObject = SkinProvider.GSON.fromJson(new String(Base64.getDecoder().decode(skinProperty.getValue()), StandardCharsets.UTF_8), JsonObject.class);
|
2019-10-10 21:27:30 +00:00
|
|
|
JsonObject textures = skinObject.getAsJsonObject("textures");
|
2019-10-09 18:39:38 +00:00
|
|
|
|
2019-10-10 21:27:30 +00:00
|
|
|
JsonObject skinTexture = textures.getAsJsonObject("SKIN");
|
|
|
|
String skinUrl = skinTexture.get("url").getAsString();
|
2019-10-09 18:39:38 +00:00
|
|
|
|
2019-10-10 21:27:30 +00:00
|
|
|
boolean isAlex = skinTexture.has("metadata");
|
2019-10-09 18:39:38 +00:00
|
|
|
|
2019-10-10 21:27:30 +00:00
|
|
|
String capeUrl = null;
|
|
|
|
if (textures.has("CAPE")) {
|
|
|
|
JsonObject capeTexture = textures.getAsJsonObject("CAPE");
|
|
|
|
capeUrl = capeTexture.get("url").getAsString();
|
|
|
|
}
|
2019-10-09 18:39:38 +00:00
|
|
|
|
2019-10-10 21:27:30 +00:00
|
|
|
return new GameProfileData(skinUrl, capeUrl, isAlex);
|
|
|
|
} catch (Exception exception) {
|
2020-01-04 05:58:58 +00:00
|
|
|
if (GeyserConnector.getInstance().getAuthType() != AuthType.OFFLINE) {
|
2019-12-21 17:35:48 +00:00
|
|
|
GeyserConnector.getInstance().getLogger().debug("Got invalid texture data for " + profile.getName() + " " + exception.getMessage());
|
2019-11-27 01:52:13 +00:00
|
|
|
}
|
2019-10-10 21:27:30 +00:00
|
|
|
// return default skin with default cape when texture data is invalid
|
2019-11-28 03:22:27 +00:00
|
|
|
return new GameProfileData(SkinProvider.EMPTY_SKIN.getTextureUrl(), SkinProvider.EMPTY_CAPE.getTextureUrl(), false);
|
2019-10-10 21:27:30 +00:00
|
|
|
}
|
2019-10-09 18:39:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void requestAndHandleSkinAndCape(PlayerEntity entity, GeyserSession session,
|
|
|
|
Consumer<SkinProvider.SkinAndCape> skinAndCapeConsumer) {
|
2019-12-21 17:35:48 +00:00
|
|
|
GeyserConnector.getInstance().getGeneralThreadPool().execute(() -> {
|
2019-11-19 20:31:24 +00:00
|
|
|
GameProfileData data = GameProfileData.from(entity.getProfile());
|
2019-10-09 18:39:38 +00:00
|
|
|
|
|
|
|
SkinProvider.requestSkinAndCape(entity.getUuid(), data.getSkinUrl(), data.getCapeUrl())
|
|
|
|
.whenCompleteAsync((skinAndCape, throwable) -> {
|
|
|
|
try {
|
|
|
|
SkinProvider.Skin skin = skinAndCape.getSkin();
|
|
|
|
SkinProvider.Cape cape = skinAndCape.getCape();
|
|
|
|
|
|
|
|
if (cape.isFailed() && SkinProvider.ALLOW_THIRD_PARTY_CAPES) {
|
|
|
|
cape = SkinProvider.getOrDefault(SkinProvider.requestUnofficialCape(
|
|
|
|
cape, entity.getUuid(),
|
|
|
|
entity.getUsername(), false
|
|
|
|
), SkinProvider.EMPTY_CAPE, SkinProvider.UnofficalCape.VALUES.length * 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entity.getLastSkinUpdate() < skin.getRequestedOn()) {
|
|
|
|
entity.setLastSkinUpdate(skin.getRequestedOn());
|
|
|
|
|
|
|
|
if (session.getUpstream().isInitialized()) {
|
2019-11-19 20:31:24 +00:00
|
|
|
PlayerListPacket.Entry updatedEntry = buildEntryManually(
|
2019-10-09 18:39:38 +00:00
|
|
|
entity.getUuid(),
|
|
|
|
entity.getUsername(),
|
|
|
|
entity.getGeyserId(),
|
|
|
|
entity.getUuid().toString(),
|
|
|
|
skin.getSkinData(),
|
2019-11-19 20:31:24 +00:00
|
|
|
cape.getCapeId(),
|
2019-10-09 18:39:38 +00:00
|
|
|
cape.getCapeData(),
|
2019-11-06 00:55:59 +00:00
|
|
|
getLegacySkinGeometry("geometry.humanoid.custom" + (data.isAlex() ? "Slim" : "")),
|
2019-10-09 18:39:38 +00:00
|
|
|
""
|
|
|
|
);
|
|
|
|
|
|
|
|
PlayerListPacket playerRemovePacket = new PlayerListPacket();
|
2020-02-06 00:55:34 +00:00
|
|
|
playerRemovePacket.setAction(PlayerListPacket.Action.REMOVE);
|
2019-10-09 18:39:38 +00:00
|
|
|
playerRemovePacket.getEntries().add(updatedEntry);
|
|
|
|
session.getUpstream().sendPacket(playerRemovePacket);
|
|
|
|
|
|
|
|
PlayerListPacket playerAddPacket = new PlayerListPacket();
|
2020-02-06 00:55:34 +00:00
|
|
|
playerAddPacket.setAction(PlayerListPacket.Action.ADD);
|
2019-10-09 18:39:38 +00:00
|
|
|
playerAddPacket.getEntries().add(updatedEntry);
|
|
|
|
session.getUpstream().sendPacket(playerAddPacket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2019-12-21 17:35:48 +00:00
|
|
|
GeyserConnector.getInstance().getLogger().error("Failed getting skin for " + entity.getUuid(), e);
|
2019-10-09 18:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (skinAndCapeConsumer != null) skinAndCapeConsumer.accept(skinAndCape);
|
|
|
|
});
|
2020-01-04 05:58:58 +00:00
|
|
|
|
2019-10-09 18:39:38 +00:00
|
|
|
});
|
|
|
|
}
|
2019-11-06 00:55:59 +00:00
|
|
|
|
|
|
|
private static String getLegacySkinGeometry(String geometryName) {
|
|
|
|
return "{\"geometry\" :{\"default\" :\"" + geometryName + "\"}}";
|
|
|
|
}
|
2019-10-09 18:39:38 +00:00
|
|
|
}
|