| 1 | package net.minecraft.client.resources;
|
| 2 |
|
| 3 | import com.google.common.cache.CacheBuilder;
|
| 4 | import com.google.common.cache.CacheLoader;
|
| 5 | import com.google.common.cache.LoadingCache;
|
| 6 | import com.google.common.hash.Hashing;
|
| 7 | import com.mojang.authlib.GameProfile;
|
| 8 | import com.mojang.authlib.SignatureState;
|
| 9 | import com.mojang.authlib.minecraft.MinecraftProfileTexture;
|
| 10 | import com.mojang.authlib.minecraft.MinecraftProfileTextures;
|
| 11 | import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
|
| 12 | import com.mojang.authlib.properties.Property;
|
| 13 | import com.mojang.logging.LogUtils;
|
| 14 | import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
| 15 | import java.nio.file.Path;
|
| 16 | import java.time.Duration;
|
| 17 | import java.util.Map;
|
| 18 | import java.util.Optional;
|
| 19 | import java.util.UUID;
|
| 20 | import java.util.concurrent.CompletableFuture;
|
| 21 | import java.util.concurrent.Executor;
|
| 22 | import java.util.function.Supplier;
|
| 23 | import net.fabricmc.api.EnvType;
|
| 24 | import net.fabricmc.api.Environment;
|
| 25 | import net.minecraft.SharedConstants;
|
| 26 | import net.minecraft.client.renderer.texture.SkinTextureDownloader;
|
| 27 | import net.minecraft.core.ClientAsset.Texture;
|
| 28 | import net.minecraft.resources.Identifier;
|
| 29 | import net.minecraft.server.Services;
|
| 30 | import net.minecraft.util.Util;
|
| 31 | import net.minecraft.world.entity.player.PlayerModelType;
|
| 32 | import net.minecraft.world.entity.player.PlayerSkin;
|
| 33 | import org.jspecify.annotations.Nullable;
|
| 34 | import org.slf4j.Logger;
|
| 35 |
|
| 36 | @Environment(EnvType.CLIENT)
|
| 37 | public class SkinManager {
|
| 38 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 39 | private final Services services;
|
| 40 | private final SkinTextureDownloader skinTextureDownloader;
|
| 41 | private final LoadingCache<SkinManager.CacheKey, CompletableFuture<Optional<PlayerSkin>>> skinCache;
|
| 42 | private final SkinManager.TextureCache skinTextures;
|
| 43 | private final SkinManager.TextureCache capeTextures;
|
| 44 | private final SkinManager.TextureCache elytraTextures;
|
| 45 |
|
| 46 | public SkinManager(final Path skinsDirectory, final Services services, final SkinTextureDownloader skinTextureDownloader, final Executor mainThreadExecutor) {
|
| 47 | this.services = services;
|
| 48 | this.skinTextureDownloader = skinTextureDownloader;
|
| 49 | this.skinTextures = new SkinManager.TextureCache(skinsDirectory, Type.SKIN);
|
| 50 | this.capeTextures = new SkinManager.TextureCache(skinsDirectory, Type.CAPE);
|
| 51 | this.elytraTextures = new SkinManager.TextureCache(skinsDirectory, Type.ELYTRA);
|
| 52 | this.skinCache = CacheBuilder.newBuilder()
|
| 53 | .expireAfterAccess(Duration.ofSeconds(15L))
|
| 54 | .build(
|
| 55 | new CacheLoader<SkinManager.CacheKey, CompletableFuture<Optional<PlayerSkin>>>() {
|
| 56 | public CompletableFuture<Optional<PlayerSkin>> load(final SkinManager.CacheKey key) {
|
| 57 | return CompletableFuture.<MinecraftProfileTextures>supplyAsync(() -> {
|
| 58 | Property packedTextures = key.packedTextures();
|
| 59 | if (packedTextures == null) {
|
| 60 | return MinecraftProfileTextures.EMPTY;
|
| 61 | }
|
| 62 |
|
| 63 | MinecraftProfileTextures textures = services.sessionService().unpackTextures(packedTextures);
|
| 64 | if (textures.signatureState() == SignatureState.INVALID) {
|
| 65 | SkinManager.LOGGER.warn("Profile contained invalid signature for textures property (profile id: {})", key.profileId());
|
| 66 | }
|
| 67 |
|
| 68 | return textures;
|
| 69 | }, Util.backgroundExecutor().forName("unpackSkinTextures"))
|
| 70 | .thenComposeAsync(textures -> SkinManager.this.registerTextures(key.profileId(), textures), mainThreadExecutor)
|
| 71 | .handle((playerSkin, throwable) -> {
|
| 72 | if (throwable != null) {
|
| 73 | SkinManager.LOGGER.warn("Failed to load texture for profile {}", key.profileId, throwable);
|
| 74 | }
|
| 75 |
|
| 76 | return Optional.ofNullable(playerSkin);
|
| 77 | });
|
| 78 | }
|
| 79 | }
|
| 80 | );
|
| 81 | }
|
| 82 |
|
| 83 | public Supplier<PlayerSkin> createLookup(final GameProfile profile, final boolean requireSecure) {
|
| 84 | CompletableFuture<Optional<PlayerSkin>> future = this.get(profile);
|
| 85 | PlayerSkin defaultSkin = DefaultPlayerSkin.get(profile);
|
| 86 | if (SharedConstants.DEBUG_DEFAULT_SKIN_OVERRIDE) {
|
| 87 | return () -> defaultSkin;
|
| 88 | } else {
|
| 89 | Optional<PlayerSkin> currentValue = future.getNow(null);
|
| 90 | if (currentValue != null) {
|
| 91 | PlayerSkin playerSkin = currentValue.filter(skin -> !requireSecure || skin.secure()).orElse(defaultSkin);
|
| 92 | return () -> playerSkin;
|
| 93 | } else {
|
| 94 | return () -> future.getNow(Optional.empty()).filter(skin -> !requireSecure || skin.secure()).orElse(defaultSkin);
|
| 95 | }
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | public CompletableFuture<Optional<PlayerSkin>> get(final GameProfile profile) {
|
| 100 | if (SharedConstants.DEBUG_DEFAULT_SKIN_OVERRIDE) {
|
| 101 | PlayerSkin defaultSkin = DefaultPlayerSkin.get(profile);
|
| 102 | return CompletableFuture.completedFuture(Optional.of(defaultSkin));
|
| 103 | } else {
|
| 104 | Property packedTextures = this.services.sessionService().getPackedTextures(profile);
|
| 105 | return this.skinCache.getUnchecked(new SkinManager.CacheKey(profile.id(), packedTextures));
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | private CompletableFuture<PlayerSkin> registerTextures(final UUID profileId, final MinecraftProfileTextures textures) {
|
| 110 | MinecraftProfileTexture skinInfo = textures.skin();
|
| 111 | CompletableFuture<Texture> skinTexture;
|
| 112 | PlayerModelType model;
|
| 113 | if (skinInfo != null) {
|
| 114 | skinTexture = this.skinTextures.getOrLoad(skinInfo);
|
| 115 | model = PlayerModelType.byLegacyServicesName(skinInfo.getMetadata("model"));
|
| 116 | } else {
|
| 117 | PlayerSkin defaultSkin = DefaultPlayerSkin.get(profileId);
|
| 118 | skinTexture = CompletableFuture.completedFuture(defaultSkin.body());
|
| 119 | model = defaultSkin.model();
|
| 120 | }
|
| 121 |
|
| 122 | MinecraftProfileTexture capeInfo = textures.cape();
|
| 123 | CompletableFuture<Texture> capeTexture = capeInfo != null ? this.capeTextures.getOrLoad(capeInfo) : CompletableFuture.completedFuture(null);
|
| 124 | MinecraftProfileTexture elytraInfo = textures.elytra();
|
| 125 | CompletableFuture<Texture> elytraTexture = elytraInfo != null ? this.elytraTextures.getOrLoad(elytraInfo) : CompletableFuture.completedFuture(null);
|
| 126 | return CompletableFuture.allOf(skinTexture, capeTexture, elytraTexture)
|
| 127 | .thenApply(unused -> new PlayerSkin(skinTexture.join(), capeTexture.join(), elytraTexture.join(), model, textures.signatureState() == SignatureState.SIGNED));
|
| 128 | }
|
| 129 |
|
| 130 | @Environment(EnvType.CLIENT)
|
| 131 | private record CacheKey(UUID profileId, @Nullable Property packedTextures) {
|
| 132 | }
|
| 133 |
|
| 134 | @Environment(EnvType.CLIENT)
|
| 135 | private class TextureCache {
|
| 136 | private final Path root;
|
| 137 | private final Type type;
|
| 138 | private final Map<String, CompletableFuture<Texture>> textures = new Object2ObjectOpenHashMap<>();
|
| 139 |
|
| 140 | private TextureCache(final Path root, final Type type) {
|
| 141 | this.root = root;
|
| 142 | this.type = type;
|
| 143 | }
|
| 144 |
|
| 145 | public CompletableFuture<Texture> getOrLoad(final MinecraftProfileTexture texture) {
|
| 146 | String hash = texture.getHash();
|
| 147 | CompletableFuture<Texture> future = this.textures.get(hash);
|
| 148 | if (future == null) {
|
| 149 | future = this.registerTexture(texture);
|
| 150 | this.textures.put(hash, future);
|
| 151 | }
|
| 152 |
|
| 153 | return future;
|
| 154 | }
|
| 155 |
|
| 156 | private CompletableFuture<Texture> registerTexture(final MinecraftProfileTexture textureInfo) {
|
| 157 | String hash = Hashing.sha1().hashUnencodedChars(textureInfo.getHash()).toString();
|
| 158 | Identifier textureId = this.getTextureLocation(hash);
|
| 159 | Path file = this.root.resolve(hash.length() > 2 ? hash.substring(0, 2) : "xx").resolve(hash);
|
| 160 | return SkinManager.this.skinTextureDownloader.downloadAndRegisterSkin(textureId, file, textureInfo.getUrl(), this.type == Type.SKIN);
|
| 161 | }
|
| 162 |
|
| 163 | private Identifier getTextureLocation(final String textureHash) {
|
| 164 | String root = switch (this.type) {
|
| 165 | case SKIN -> "skins";
|
| 166 | case CAPE -> "capes";
|
| 167 | case ELYTRA -> "elytra";
|
| 168 | };
|
| 169 | return Identifier.withDefaultNamespace(root + "/" + textureHash);
|
| 170 | }
|
| 171 | }
|
| 172 | }
|
| 173 |
|