| 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.collect.Maps;
|
| 7 | import com.mojang.authlib.GameProfile;
|
| 8 | import com.mojang.authlib.minecraft.InsecureTextureException;
|
| 9 | import com.mojang.authlib.minecraft.MinecraftProfileTexture;
|
| 10 | import com.mojang.authlib.minecraft.MinecraftSessionService;
|
| 11 | import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
|
| 12 | import java.awt.image.BufferedImage;
|
| 13 | import java.io.File;
|
| 14 | import java.util.Map;
|
| 15 | import java.util.concurrent.ExecutorService;
|
| 16 | import java.util.concurrent.LinkedBlockingQueue;
|
| 17 | import java.util.concurrent.ThreadPoolExecutor;
|
| 18 | import java.util.concurrent.TimeUnit;
|
| 19 | import net.minecraft.client.Minecraft;
|
| 20 | import net.minecraft.client.renderer.IImageBuffer;
|
| 21 | import net.minecraft.client.renderer.ImageBufferDownload;
|
| 22 | import net.minecraft.client.renderer.ThreadDownloadImageData;
|
| 23 | import net.minecraft.client.renderer.texture.ITextureObject;
|
| 24 | import net.minecraft.client.renderer.texture.TextureManager;
|
| 25 | import net.minecraft.util.ResourceLocation;
|
| 26 |
|
| 27 | public class SkinManager
|
| 28 | {
|
| 29 | private static final ExecutorService THREAD_POOL = new ThreadPoolExecutor(0, 2, 1L, TimeUnit.MINUTES, new LinkedBlockingQueue());
|
| 30 | private final TextureManager textureManager;
|
| 31 | private final File skinCacheDir;
|
| 32 | private final MinecraftSessionService sessionService;
|
| 33 | private final LoadingCache<GameProfile, Map<Type, MinecraftProfileTexture>> skinCacheLoader;
|
| 34 |
|
| 35 | public SkinManager(TextureManager textureManagerInstance, File skinCacheDirectory, MinecraftSessionService sessionService)
|
| 36 | {
|
| 37 | this.textureManager = textureManagerInstance;
|
| 38 | this.skinCacheDir = skinCacheDirectory;
|
| 39 | this.sessionService = sessionService;
|
| 40 | this.skinCacheLoader = CacheBuilder.newBuilder().expireAfterAccess(15L, TimeUnit.SECONDS).<GameProfile, Map<Type, MinecraftProfileTexture>>build(new CacheLoader<GameProfile, Map<Type, MinecraftProfileTexture>>()
|
| 41 | {
|
| 42 | public Map<Type, MinecraftProfileTexture> load(GameProfile p_load_1_) throws Exception
|
| 43 | {
|
| 44 | return Minecraft.getMinecraft().getSessionService().getTextures(p_load_1_, false);
|
| 45 | }
|
| 46 | });
|
| 47 | }
|
| 48 |
|
| 49 | /**
|
| 50 | * Used in the Skull renderer to fetch a skin. May download the skin if it's not in the cache
|
| 51 | */
|
| 52 | public ResourceLocation loadSkin(MinecraftProfileTexture profileTexture, Type p_152792_2_)
|
| 53 | {
|
| 54 | return this.loadSkin(profileTexture, p_152792_2_, (SkinManager.SkinAvailableCallback)null);
|
| 55 | }
|
| 56 |
|
| 57 | /**
|
| 58 | * May download the skin if its not in the cache, can be passed a SkinManager#SkinAvailableCallback for handling
|
| 59 | */
|
| 60 | public ResourceLocation loadSkin(final MinecraftProfileTexture profileTexture, final Type p_152789_2_, final SkinManager.SkinAvailableCallback skinAvailableCallback)
|
| 61 | {
|
| 62 | final ResourceLocation resourcelocation = new ResourceLocation("skins/" + profileTexture.getHash());
|
| 63 | ITextureObject itextureobject = this.textureManager.getTexture(resourcelocation);
|
| 64 |
|
| 65 | if (itextureobject != null)
|
| 66 | {
|
| 67 | if (skinAvailableCallback != null)
|
| 68 | {
|
| 69 | skinAvailableCallback.skinAvailable(p_152789_2_, resourcelocation, profileTexture);
|
| 70 | }
|
| 71 | }
|
| 72 | else
|
| 73 | {
|
| 74 | File file1 = new File(this.skinCacheDir, profileTexture.getHash().length() > 2 ? profileTexture.getHash().substring(0, 2) : "xx");
|
| 75 | File file2 = new File(file1, profileTexture.getHash());
|
| 76 | final IImageBuffer iimagebuffer = p_152789_2_ == Type.SKIN ? new ImageBufferDownload() : null;
|
| 77 | ThreadDownloadImageData threaddownloadimagedata = new ThreadDownloadImageData(file2, profileTexture.getUrl(), DefaultPlayerSkin.getDefaultSkinLegacy(), new IImageBuffer()
|
| 78 | {
|
| 79 | public BufferedImage parseUserSkin(BufferedImage image)
|
| 80 | {
|
| 81 | if (iimagebuffer != null)
|
| 82 | {
|
| 83 | image = iimagebuffer.parseUserSkin(image);
|
| 84 | }
|
| 85 |
|
| 86 | return image;
|
| 87 | }
|
| 88 | public void skinAvailable()
|
| 89 | {
|
| 90 | if (iimagebuffer != null)
|
| 91 | {
|
| 92 | iimagebuffer.skinAvailable();
|
| 93 | }
|
| 94 |
|
| 95 | if (skinAvailableCallback != null)
|
| 96 | {
|
| 97 | skinAvailableCallback.skinAvailable(p_152789_2_, resourcelocation, profileTexture);
|
| 98 | }
|
| 99 | }
|
| 100 | });
|
| 101 | this.textureManager.loadTexture(resourcelocation, threaddownloadimagedata);
|
| 102 | }
|
| 103 |
|
| 104 | return resourcelocation;
|
| 105 | }
|
| 106 |
|
| 107 | public void loadProfileTextures(final GameProfile profile, final SkinManager.SkinAvailableCallback skinAvailableCallback, final boolean requireSecure)
|
| 108 | {
|
| 109 | THREAD_POOL.submit(new Runnable()
|
| 110 | {
|
| 111 | public void run()
|
| 112 | {
|
| 113 | final Map<Type, MinecraftProfileTexture> map = Maps.<Type, MinecraftProfileTexture>newHashMap();
|
| 114 |
|
| 115 | try
|
| 116 | {
|
| 117 | map.putAll(SkinManager.this.sessionService.getTextures(profile, requireSecure));
|
| 118 | }
|
| 119 | catch (InsecureTextureException var3)
|
| 120 | {
|
| 121 | ;
|
| 122 | }
|
| 123 |
|
| 124 | if (map.isEmpty() && profile.getId().equals(Minecraft.getMinecraft().getSession().getProfile().getId()))
|
| 125 | {
|
| 126 | profile.getProperties().clear();
|
| 127 | profile.getProperties().putAll(Minecraft.getMinecraft().getProfileProperties());
|
| 128 | map.putAll(SkinManager.this.sessionService.getTextures(profile, false));
|
| 129 | }
|
| 130 |
|
| 131 | Minecraft.getMinecraft().addScheduledTask(new Runnable()
|
| 132 | {
|
| 133 | public void run()
|
| 134 | {
|
| 135 | if (map.containsKey(Type.SKIN))
|
| 136 | {
|
| 137 | SkinManager.this.loadSkin((MinecraftProfileTexture)map.get(Type.SKIN), Type.SKIN, skinAvailableCallback);
|
| 138 | }
|
| 139 |
|
| 140 | if (map.containsKey(Type.CAPE))
|
| 141 | {
|
| 142 | SkinManager.this.loadSkin((MinecraftProfileTexture)map.get(Type.CAPE), Type.CAPE, skinAvailableCallback);
|
| 143 | }
|
| 144 | }
|
| 145 | });
|
| 146 | }
|
| 147 | });
|
| 148 | }
|
| 149 |
|
| 150 | public Map<Type, MinecraftProfileTexture> loadSkinFromCache(GameProfile profile)
|
| 151 | {
|
| 152 | return (Map)this.skinCacheLoader.getUnchecked(profile);
|
| 153 | }
|
| 154 |
|
| 155 | public interface SkinAvailableCallback
|
| 156 | {
|
| 157 | void skinAvailable(Type p_180521_1_, ResourceLocation location, MinecraftProfileTexture profileTexture);
|
| 158 | }
|
| 159 | }
|
| 160 |
|