| 1 | package net.minecraft.client.renderer;
|
| 2 |
|
| 3 | import java.awt.image.BufferedImage;
|
| 4 | import java.io.File;
|
| 5 | import java.io.IOException;
|
| 6 | import java.net.HttpURLConnection;
|
| 7 | import java.net.URL;
|
| 8 | import java.util.concurrent.atomic.AtomicInteger;
|
| 9 | import javax.imageio.ImageIO;
|
| 10 | import net.minecraft.client.Minecraft;
|
| 11 | import net.minecraft.client.renderer.texture.SimpleTexture;
|
| 12 | import net.minecraft.client.renderer.texture.TextureUtil;
|
| 13 | import net.minecraft.client.resources.IResourceManager;
|
| 14 | import net.minecraft.util.ResourceLocation;
|
| 15 | import org.apache.commons.io.FileUtils;
|
| 16 | import org.apache.logging.log4j.LogManager;
|
| 17 | import org.apache.logging.log4j.Logger;
|
| 18 |
|
| 19 | public class ThreadDownloadImageData extends SimpleTexture
|
| 20 | {
|
| 21 | private static final Logger logger = LogManager.getLogger();
|
| 22 | private static final AtomicInteger threadDownloadCounter = new AtomicInteger(0);
|
| 23 | private final File cacheFile;
|
| 24 | private final String imageUrl;
|
| 25 | private final IImageBuffer imageBuffer;
|
| 26 | private BufferedImage bufferedImage;
|
| 27 | private Thread imageThread;
|
| 28 | private boolean textureUploaded;
|
| 29 |
|
| 30 | public ThreadDownloadImageData(File cacheFileIn, String imageUrlIn, ResourceLocation textureResourceLocation, IImageBuffer imageBufferIn)
|
| 31 | {
|
| 32 | super(textureResourceLocation);
|
| 33 | this.cacheFile = cacheFileIn;
|
| 34 | this.imageUrl = imageUrlIn;
|
| 35 | this.imageBuffer = imageBufferIn;
|
| 36 | }
|
| 37 |
|
| 38 | private void checkTextureUploaded()
|
| 39 | {
|
| 40 | if (!this.textureUploaded)
|
| 41 | {
|
| 42 | if (this.bufferedImage != null)
|
| 43 | {
|
| 44 | if (this.textureLocation != null)
|
| 45 | {
|
| 46 | this.deleteGlTexture();
|
| 47 | }
|
| 48 |
|
| 49 | TextureUtil.uploadTextureImage(super.getGlTextureId(), this.bufferedImage);
|
| 50 | this.textureUploaded = true;
|
| 51 | }
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | public int getGlTextureId()
|
| 56 | {
|
| 57 | this.checkTextureUploaded();
|
| 58 | return super.getGlTextureId();
|
| 59 | }
|
| 60 |
|
| 61 | public void setBufferedImage(BufferedImage bufferedImageIn)
|
| 62 | {
|
| 63 | this.bufferedImage = bufferedImageIn;
|
| 64 |
|
| 65 | if (this.imageBuffer != null)
|
| 66 | {
|
| 67 | this.imageBuffer.skinAvailable();
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | public void loadTexture(IResourceManager resourceManager) throws IOException
|
| 72 | {
|
| 73 | if (this.bufferedImage == null && this.textureLocation != null)
|
| 74 | {
|
| 75 | super.loadTexture(resourceManager);
|
| 76 | }
|
| 77 |
|
| 78 | if (this.imageThread == null)
|
| 79 | {
|
| 80 | if (this.cacheFile != null && this.cacheFile.isFile())
|
| 81 | {
|
| 82 | logger.debug("Loading http texture from local cache ({})", new Object[] {this.cacheFile});
|
| 83 |
|
| 84 | try
|
| 85 | {
|
| 86 | this.bufferedImage = ImageIO.read(this.cacheFile);
|
| 87 |
|
| 88 | if (this.imageBuffer != null)
|
| 89 | {
|
| 90 | this.setBufferedImage(this.imageBuffer.parseUserSkin(this.bufferedImage));
|
| 91 | }
|
| 92 | }
|
| 93 | catch (IOException ioexception)
|
| 94 | {
|
| 95 | logger.error((String)("Couldn\'t load skin " + this.cacheFile), (Throwable)ioexception);
|
| 96 | this.loadTextureFromServer();
|
| 97 | }
|
| 98 | }
|
| 99 | else
|
| 100 | {
|
| 101 | this.loadTextureFromServer();
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | protected void loadTextureFromServer()
|
| 107 | {
|
| 108 | this.imageThread = new Thread("Texture Downloader #" + threadDownloadCounter.incrementAndGet())
|
| 109 | {
|
| 110 | public void run()
|
| 111 | {
|
| 112 | HttpURLConnection httpurlconnection = null;
|
| 113 | ThreadDownloadImageData.logger.debug("Downloading http texture from {} to {}", new Object[] {ThreadDownloadImageData.this.imageUrl, ThreadDownloadImageData.this.cacheFile});
|
| 114 |
|
| 115 | try
|
| 116 | {
|
| 117 | httpurlconnection = (HttpURLConnection)(new URL(ThreadDownloadImageData.this.imageUrl)).openConnection(Minecraft.getMinecraft().getProxy());
|
| 118 | httpurlconnection.setDoInput(true);
|
| 119 | httpurlconnection.setDoOutput(false);
|
| 120 | httpurlconnection.connect();
|
| 121 |
|
| 122 | if (httpurlconnection.getResponseCode() / 100 == 2)
|
| 123 | {
|
| 124 | BufferedImage bufferedimage;
|
| 125 |
|
| 126 | if (ThreadDownloadImageData.this.cacheFile != null)
|
| 127 | {
|
| 128 | FileUtils.copyInputStreamToFile(httpurlconnection.getInputStream(), ThreadDownloadImageData.this.cacheFile);
|
| 129 | bufferedimage = ImageIO.read(ThreadDownloadImageData.this.cacheFile);
|
| 130 | }
|
| 131 | else
|
| 132 | {
|
| 133 | bufferedimage = TextureUtil.readBufferedImage(httpurlconnection.getInputStream());
|
| 134 | }
|
| 135 |
|
| 136 | if (ThreadDownloadImageData.this.imageBuffer != null)
|
| 137 | {
|
| 138 | bufferedimage = ThreadDownloadImageData.this.imageBuffer.parseUserSkin(bufferedimage);
|
| 139 | }
|
| 140 |
|
| 141 | ThreadDownloadImageData.this.setBufferedImage(bufferedimage);
|
| 142 | return;
|
| 143 | }
|
| 144 | }
|
| 145 | catch (Exception exception)
|
| 146 | {
|
| 147 | ThreadDownloadImageData.logger.error((String)"Couldn\'t download http texture", (Throwable)exception);
|
| 148 | return;
|
| 149 | }
|
| 150 | finally
|
| 151 | {
|
| 152 | if (httpurlconnection != null)
|
| 153 | {
|
| 154 | httpurlconnection.disconnect();
|
| 155 | }
|
| 156 | }
|
| 157 | }
|
| 158 | };
|
| 159 | this.imageThread.setDaemon(true);
|
| 160 | this.imageThread.start();
|
| 161 | }
|
| 162 | }
|
| 163 |
|