2019-09-25 21:52:28 +00:00
|
|
|
package org.geysermc.connector.utils;
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
import lombok.Getter;
|
|
|
|
import org.geysermc.api.Geyser;
|
2019-10-02 20:45:29 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2019-09-25 21:52:28 +00:00
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
2019-10-02 20:45:29 +00:00
|
|
|
import java.awt.*;
|
2019-09-25 21:52:28 +00:00
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
|
|
|
public class SkinProvider {
|
2019-10-02 20:45:29 +00:00
|
|
|
public static final Gson GSON = new GsonBuilder().create();
|
|
|
|
public static final boolean ALLOW_THIRD_PARTY_CAPES = ((GeyserConnector)Geyser.getConnector()).getConfig().isAllowThirdPartyCapes();
|
|
|
|
private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(ALLOW_THIRD_PARTY_CAPES ? 21 : 14);
|
2019-09-25 21:52:28 +00:00
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
public static final byte[] STEVE_SKIN = new ProvidedSkin("bedrock/skin/skin_steve.png").getSkin();
|
2019-11-19 20:31:24 +00:00
|
|
|
public static final Skin EMPTY_SKIN = new Skin(-1, "steve", STEVE_SKIN);
|
2019-09-25 21:52:28 +00:00
|
|
|
private static Map<UUID, Skin> cachedSkins = new ConcurrentHashMap<>();
|
2019-10-02 20:45:29 +00:00
|
|
|
private static Map<UUID, CompletableFuture<Skin>> requestedSkins = new ConcurrentHashMap<>();
|
|
|
|
|
2019-11-19 20:31:24 +00:00
|
|
|
public static final Cape EMPTY_CAPE = new Cape("", "no-cape", new byte[0], -1, true);
|
2019-09-25 21:52:28 +00:00
|
|
|
private static Map<String, Cape> cachedCapes = new ConcurrentHashMap<>();
|
2019-10-02 20:45:29 +00:00
|
|
|
private static Map<String, CompletableFuture<Cape>> requestedCapes = new ConcurrentHashMap<>();
|
2019-09-25 21:52:28 +00:00
|
|
|
|
|
|
|
private static final int CACHE_INTERVAL = 8 * 60 * 1000; // 8 minutes
|
|
|
|
|
|
|
|
public static boolean hasSkinCached(UUID uuid) {
|
|
|
|
return cachedSkins.containsKey(uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean hasCapeCached(String capeUrl) {
|
|
|
|
return cachedCapes.containsKey(capeUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Skin getCachedSkin(UUID uuid) {
|
2019-10-10 21:16:07 +00:00
|
|
|
return cachedSkins.getOrDefault(uuid, EMPTY_SKIN);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Cape getCachedCape(String capeUrl) {
|
2019-10-10 21:16:07 +00:00
|
|
|
return capeUrl != null ? cachedCapes.getOrDefault(capeUrl, EMPTY_CAPE) : EMPTY_CAPE;
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 18:39:38 +00:00
|
|
|
public static CompletableFuture<SkinAndCape> requestSkinAndCape(UUID playerId, String skinUrl, String capeUrl) {
|
2019-09-25 21:52:28 +00:00
|
|
|
return CompletableFuture.supplyAsync(() -> {
|
|
|
|
long time = System.currentTimeMillis();
|
|
|
|
|
|
|
|
SkinAndCape skinAndCape = new SkinAndCape(
|
2019-10-09 18:39:38 +00:00
|
|
|
getOrDefault(requestSkin(playerId, skinUrl, false), EMPTY_SKIN, 5),
|
|
|
|
getOrDefault(requestCape(capeUrl, false), EMPTY_CAPE, 5)
|
2019-09-25 21:52:28 +00:00
|
|
|
);
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
Geyser.getLogger().debug("Took " + (System.currentTimeMillis() - time) + "ms for " + playerId);
|
2019-09-25 21:52:28 +00:00
|
|
|
return skinAndCape;
|
2019-10-02 20:45:29 +00:00
|
|
|
}, EXECUTOR_SERVICE);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 18:39:38 +00:00
|
|
|
public static CompletableFuture<Skin> requestSkin(UUID playerId, String textureUrl, boolean newThread) {
|
2019-09-25 21:52:28 +00:00
|
|
|
if (textureUrl == null || textureUrl.isEmpty()) return CompletableFuture.completedFuture(EMPTY_SKIN);
|
|
|
|
if (requestedSkins.containsKey(playerId)) return requestedSkins.get(playerId); // already requested
|
|
|
|
|
|
|
|
if ((System.currentTimeMillis() - CACHE_INTERVAL) < cachedSkins.getOrDefault(playerId, EMPTY_SKIN).getRequestedOn()) {
|
|
|
|
// no need to update, still cached
|
|
|
|
return CompletableFuture.completedFuture(cachedSkins.get(playerId));
|
|
|
|
}
|
|
|
|
|
|
|
|
CompletableFuture<Skin> future;
|
|
|
|
if (newThread) {
|
2019-10-02 20:45:29 +00:00
|
|
|
future = CompletableFuture.supplyAsync(() -> supplySkin(playerId, textureUrl), EXECUTOR_SERVICE)
|
2019-09-25 21:52:28 +00:00
|
|
|
.whenCompleteAsync((skin, throwable) -> {
|
|
|
|
if (!cachedSkins.getOrDefault(playerId, EMPTY_SKIN).getTextureUrl().equals(textureUrl)) {
|
|
|
|
skin.updated = true;
|
|
|
|
cachedSkins.put(playerId, skin);
|
|
|
|
}
|
|
|
|
requestedSkins.remove(skin.getSkinOwner());
|
|
|
|
});
|
|
|
|
requestedSkins.put(playerId, future);
|
|
|
|
} else {
|
|
|
|
Skin skin = supplySkin(playerId, textureUrl);
|
|
|
|
future = CompletableFuture.completedFuture(skin);
|
|
|
|
cachedSkins.put(playerId, skin);
|
|
|
|
}
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2019-10-09 18:39:38 +00:00
|
|
|
public static CompletableFuture<Cape> requestCape(String capeUrl, boolean newThread) {
|
2019-09-25 21:52:28 +00:00
|
|
|
if (capeUrl == null || capeUrl.isEmpty()) return CompletableFuture.completedFuture(EMPTY_CAPE);
|
|
|
|
if (requestedCapes.containsKey(capeUrl)) return requestedCapes.get(capeUrl); // already requested
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
boolean officialCape = capeUrl.startsWith("https://textures.minecraft.net");
|
|
|
|
boolean validCache = (System.currentTimeMillis() - CACHE_INTERVAL) < cachedCapes.getOrDefault(capeUrl, EMPTY_CAPE).getRequestedOn();
|
|
|
|
|
|
|
|
if ((cachedCapes.containsKey(capeUrl) && officialCape) || validCache) {
|
|
|
|
// the cape is an official cape (static) or the cape doesn't need a update yet
|
2019-09-25 21:52:28 +00:00
|
|
|
return CompletableFuture.completedFuture(cachedCapes.get(capeUrl));
|
|
|
|
}
|
|
|
|
|
|
|
|
CompletableFuture<Cape> future;
|
|
|
|
if (newThread) {
|
2019-10-02 20:45:29 +00:00
|
|
|
future = CompletableFuture.supplyAsync(() -> supplyCape(capeUrl), EXECUTOR_SERVICE)
|
2019-09-25 21:52:28 +00:00
|
|
|
.whenCompleteAsync((cape, throwable) -> {
|
|
|
|
cachedCapes.put(capeUrl, cape);
|
|
|
|
requestedCapes.remove(capeUrl);
|
|
|
|
});
|
|
|
|
requestedCapes.put(capeUrl, future);
|
|
|
|
} else {
|
|
|
|
Cape cape = supplyCape(capeUrl); // blocking
|
|
|
|
future = CompletableFuture.completedFuture(cape);
|
|
|
|
cachedCapes.put(capeUrl, cape);
|
|
|
|
}
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2019-10-09 18:39:38 +00:00
|
|
|
public static CompletableFuture<Cape> requestUnofficialCape(Cape officialCape, UUID playerId,
|
|
|
|
String username, boolean newThread) {
|
2019-10-02 20:45:29 +00:00
|
|
|
if (officialCape.isFailed() && ALLOW_THIRD_PARTY_CAPES) {
|
|
|
|
for (UnofficalCape cape : UnofficalCape.VALUES) {
|
|
|
|
Cape cape1 = getOrDefault(
|
2019-10-09 18:39:38 +00:00
|
|
|
requestCape(cape.getUrlFor(playerId, username), newThread),
|
2019-10-02 20:45:29 +00:00
|
|
|
EMPTY_CAPE, 4
|
|
|
|
);
|
|
|
|
if (!cape1.isFailed()) {
|
|
|
|
return CompletableFuture.completedFuture(cape1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CompletableFuture.completedFuture(officialCape);
|
|
|
|
}
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
private static Skin supplySkin(UUID uuid, String textureUrl) {
|
|
|
|
byte[] skin = EMPTY_SKIN.getSkinData();
|
|
|
|
try {
|
2019-10-02 20:45:29 +00:00
|
|
|
skin = requestImage(textureUrl, false);
|
2019-09-25 21:52:28 +00:00
|
|
|
} catch (Exception ignored) {} // just ignore I guess
|
|
|
|
return new Skin(uuid, textureUrl, skin, System.currentTimeMillis(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Cape supplyCape(String capeUrl) {
|
2019-10-02 20:45:29 +00:00
|
|
|
byte[] cape = new byte[0];
|
2019-09-25 21:52:28 +00:00
|
|
|
try {
|
2019-10-02 20:45:29 +00:00
|
|
|
cape = requestImage(capeUrl, true);
|
2019-09-25 21:52:28 +00:00
|
|
|
} catch (Exception ignored) {} // just ignore I guess
|
2019-10-02 20:45:29 +00:00
|
|
|
|
2019-11-19 20:31:24 +00:00
|
|
|
String[] urlSection = capeUrl.split("/"); // A real url is expected at this stage
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
return new Cape(
|
|
|
|
capeUrl,
|
2019-11-19 20:31:24 +00:00
|
|
|
urlSection[urlSection.length - 1], // get the texture id and use it as cape id
|
2019-10-02 20:45:29 +00:00
|
|
|
cape.length > 0 ? cape : EMPTY_CAPE.getCapeData(),
|
|
|
|
System.currentTimeMillis(),
|
|
|
|
cape.length == 0
|
|
|
|
);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
private static byte[] requestImage(String imageUrl, boolean cape) throws Exception {
|
2019-09-25 21:52:28 +00:00
|
|
|
BufferedImage image = ImageIO.read(new URL(imageUrl));
|
|
|
|
Geyser.getLogger().debug("Downloaded " + imageUrl);
|
|
|
|
|
2019-12-17 03:26:35 +00:00
|
|
|
if (cape) {
|
|
|
|
image = image.getWidth() > 64 ? scale(image) : image;
|
2019-12-15 02:12:12 +00:00
|
|
|
BufferedImage newImage = new BufferedImage(64, 32, BufferedImage.TYPE_INT_RGB);
|
|
|
|
Graphics g = newImage.createGraphics();
|
|
|
|
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
|
|
|
|
g.dispose();
|
|
|
|
image = newImage;
|
|
|
|
}
|
2019-10-02 20:45:29 +00:00
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(image.getWidth() * 4 + image.getHeight() * 4);
|
|
|
|
try {
|
|
|
|
for (int y = 0; y < image.getHeight(); y++) {
|
|
|
|
for (int x = 0; x < image.getWidth(); x++) {
|
|
|
|
int rgba = image.getRGB(x, y);
|
|
|
|
outputStream.write((rgba >> 16) & 0xFF);
|
|
|
|
outputStream.write((rgba >> 8) & 0xFF);
|
|
|
|
outputStream.write(rgba & 0xFF);
|
|
|
|
outputStream.write((rgba >> 24) & 0xFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
image.flush();
|
|
|
|
return outputStream.toByteArray();
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
outputStream.close();
|
|
|
|
} catch (IOException ignored) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 02:12:12 +00:00
|
|
|
private static BufferedImage scale (BufferedImage bufferedImage) {
|
|
|
|
BufferedImage resized = new BufferedImage(bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2, BufferedImage.TYPE_INT_RGB);
|
|
|
|
Graphics2D g2 = resized.createGraphics();
|
|
|
|
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
|
|
|
g2.drawImage(bufferedImage, 0, 0, bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2, null);
|
|
|
|
g2.dispose();
|
|
|
|
return resized;
|
|
|
|
}
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
public static <T> T getOrDefault(CompletableFuture<T> future, T defaultValue, int timeoutInSeconds) {
|
|
|
|
try {
|
|
|
|
return future.get(timeoutInSeconds, TimeUnit.SECONDS);
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
@AllArgsConstructor
|
|
|
|
@Getter
|
|
|
|
public static class SkinAndCape {
|
|
|
|
private Skin skin;
|
|
|
|
private Cape cape;
|
|
|
|
}
|
|
|
|
|
|
|
|
@AllArgsConstructor
|
|
|
|
@Getter
|
|
|
|
public static class Skin {
|
|
|
|
private UUID skinOwner;
|
|
|
|
private String textureUrl;
|
2019-11-19 20:31:24 +00:00
|
|
|
private byte[] skinData;
|
2019-09-25 21:52:28 +00:00
|
|
|
private long requestedOn;
|
|
|
|
private boolean updated;
|
|
|
|
|
2019-11-19 20:31:24 +00:00
|
|
|
private Skin(long requestedOn, String textureUrl, byte[] skinData) {
|
2019-09-25 21:52:28 +00:00
|
|
|
this.requestedOn = requestedOn;
|
|
|
|
this.textureUrl = textureUrl;
|
2019-11-19 20:31:24 +00:00
|
|
|
this.skinData = skinData;
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@AllArgsConstructor
|
|
|
|
@Getter
|
|
|
|
public static class Cape {
|
|
|
|
private String textureUrl;
|
2019-11-19 20:31:24 +00:00
|
|
|
private String capeId;
|
2019-09-25 21:52:28 +00:00
|
|
|
private byte[] capeData;
|
2019-10-02 20:45:29 +00:00
|
|
|
private long requestedOn;
|
|
|
|
private boolean failed;
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
/*
|
|
|
|
* Sorted by 'priority'
|
|
|
|
*/
|
|
|
|
@AllArgsConstructor
|
|
|
|
@Getter
|
|
|
|
public enum UnofficalCape {
|
|
|
|
OPTIFINE("http://s.optifine.net/capes/%s.png", CapeUrlType.USERNAME),
|
|
|
|
LABYMOD("http://capes.labymod.net/capes/%s.png", CapeUrlType.UUID_DASHED),
|
|
|
|
FIVEZIG("http://textures.5zig.net/2/%s", CapeUrlType.UUID),
|
|
|
|
MINECRAFTCAPES("https://www.minecraftcapes.co.uk/getCape/%s", CapeUrlType.UUID);
|
|
|
|
|
|
|
|
public static final UnofficalCape[] VALUES = values();
|
|
|
|
private String url;
|
|
|
|
private CapeUrlType type;
|
|
|
|
|
|
|
|
public String getUrlFor(String type) {
|
|
|
|
return String.format(url, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getUrlFor(UUID uuid, String username) {
|
|
|
|
return getUrlFor(toRequestedType(type, uuid, username));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String toRequestedType(CapeUrlType type, UUID uuid, String username) {
|
|
|
|
switch (type) {
|
|
|
|
case UUID: return uuid.toString().replace("-", "");
|
|
|
|
case UUID_DASHED: return uuid.toString();
|
|
|
|
default: return username;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum CapeUrlType {
|
|
|
|
USERNAME,
|
|
|
|
UUID,
|
|
|
|
UUID_DASHED
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
}
|