Files
MCCustomSkinLoader/Common/source/customskinloader/fake/FakeSkinManager.java
T

119 lines
5.7 KiB
Java
Raw Normal View History

2016-12-25 17:05:30 +08:00
package customskinloader.fake;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.google.common.collect.Maps;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
import com.mojang.authlib.minecraft.MinecraftSessionService;
import customskinloader.CustomSkinLoader;
import customskinloader.utils.HttpTextureUtil;
import customskinloader.utils.HttpTextureUtil.HttpTextureInfo;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.IImageBuffer;
import net.minecraft.client.renderer.ThreadDownloadImageData;
import net.minecraft.client.renderer.texture.ITextureObject;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.resources.DefaultPlayerSkin;
2016-12-31 22:41:36 +08:00
import net.minecraft.client.resources.SkinManager.SkinAvailableCallback;
2016-12-25 17:05:30 +08:00
import net.minecraft.util.ResourceLocation;
2016-12-31 22:20:29 +08:00
public class FakeSkinManager{
2018-06-25 00:34:32 +08:00
private static final ExecutorService THREAD_POOL = new ThreadPoolExecutor(0, 2, 1L, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>());
2018-02-12 12:20:28 +08:00
private final TextureManager textureManager;
2016-12-25 17:05:30 +08:00
2018-02-12 12:20:28 +08:00
public FakeSkinManager(TextureManager textureManagerInstance, File skinCacheDirectory, MinecraftSessionService sessionService) {
this.textureManager=textureManagerInstance;
HttpTextureUtil.defaultCacheDir=skinCacheDirectory;
}
public ResourceLocation loadSkin(MinecraftProfileTexture profileTexture, Type textureType){
return this.loadSkin(profileTexture, textureType, null);
}
public ResourceLocation loadSkin(final MinecraftProfileTexture profileTexture, final Type textureType, final SkinAvailableCallback skinAvailableCallback){
HttpTextureInfo info=HttpTextureUtil.toHttpTextureInfo(profileTexture.getUrl());
final ResourceLocation resourcelocation = new ResourceLocation("skins/" + info.hash);
ITextureObject itextureobject = this.textureManager.getTexture(resourcelocation);
2016-12-25 17:05:30 +08:00
2018-02-12 12:20:28 +08:00
if (itextureobject != null)//Have already loaded
makeCallback(skinAvailableCallback,textureType, resourcelocation, profileTexture);
else{
2018-06-25 00:34:32 +08:00
ThreadDownloadImageData threaddownloadimagedata = new ThreadDownloadImageData(info.cacheFile, info.url, DefaultPlayerSkin.getDefaultSkinLegacy(),
2018-02-12 12:20:28 +08:00
new BaseBuffer(skinAvailableCallback, textureType, resourcelocation, profileTexture));
2018-07-02 00:15:03 +08:00
if(skinAvailableCallback instanceof FakeClientPlayer.LegacyBuffer)//Cache for client player
FakeClientPlayer.textureCache.put(resourcelocation,threaddownloadimagedata);
2018-02-12 12:20:28 +08:00
this.textureManager.loadTexture(resourcelocation, threaddownloadimagedata);
}
return resourcelocation;
}
public void loadProfileTextures(final GameProfile profile, final SkinAvailableCallback skinAvailableCallback, final boolean requireSecure){
THREAD_POOL.submit(new Runnable(){
public void run(){
2018-06-25 00:34:32 +08:00
final Map<Type, MinecraftProfileTexture> map = Maps.newHashMap();
2018-02-12 12:20:28 +08:00
map.putAll(customskinloader.CustomSkinLoader.loadProfile(profile));
2016-12-25 17:05:30 +08:00
2018-02-12 12:20:28 +08:00
Minecraft.getMinecraft().addScheduledTask(new Runnable(){
public void run(){
for(Type type:Type.values())
if(map.containsKey(type))
2018-06-25 00:34:32 +08:00
FakeSkinManager.this.loadSkin(map.get(type), type, skinAvailableCallback);
2018-02-12 12:20:28 +08:00
}
});
}
});
}
public Map<Type, MinecraftProfileTexture> loadSkinFromCache(GameProfile profile){
return CustomSkinLoader.loadProfileFromCache(profile);
}
private static void makeCallback(SkinAvailableCallback callback,Type type, ResourceLocation location, MinecraftProfileTexture texture){
if(callback!=null)
callback.skinAvailable(type, location, texture);
}
private class BaseBuffer implements IImageBuffer{
private IImageBuffer buffer;
private SkinAvailableCallback callback;
private Type type;
private ResourceLocation location;
private MinecraftProfileTexture texture;
public BaseBuffer(SkinAvailableCallback callback,Type type,ResourceLocation location,MinecraftProfileTexture texture){
this.buffer=(type==Type.SKIN?new FakeSkinBuffer():null);
this.callback=callback;
this.type=type;
this.location=location;
this.texture=texture;
}
2018-07-25 21:06:16 +08:00
public net.minecraft.client.renderer.texture.NativeImage func_195786_a(net.minecraft.client.renderer.texture.NativeImage image){
return buffer instanceof FakeSkinBuffer ? ((FakeSkinBuffer) buffer).func_195786_a(image) : image;
}
2018-02-12 12:20:28 +08:00
public BufferedImage parseUserSkin(BufferedImage image){
return buffer==null?image:buffer.parseUserSkin(image);
}
public void skinAvailable(){
2018-06-24 22:59:46 +08:00
if (buffer != null){
2018-02-12 12:20:28 +08:00
buffer.skinAvailable();
2018-06-24 22:59:46 +08:00
if ("auto".equals(texture.getMetadata("model")) && buffer instanceof FakeSkinBuffer){
//Auto judge skin type
2018-06-25 00:34:32 +08:00
Map<String, String> metadata = Maps.newHashMap();
2018-06-24 22:59:46 +08:00
metadata.put("model", ((FakeSkinBuffer) buffer).judgeType());
texture = new MinecraftProfileTexture(texture.getUrl(), metadata);
}
2018-05-18 21:06:28 +08:00
}
2016-12-25 17:05:30 +08:00
2018-02-12 12:20:28 +08:00
FakeSkinManager.makeCallback(callback,type, location, texture);
}
}
2016-12-25 17:05:30 +08:00
}