    001package net.minecraft.client.renderer.texture;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.awt.image.BufferedImage;
    006import java.io.File;
    007import java.io.FileNotFoundException;
    008import java.io.IOException;
    009import java.util.ArrayList;
    010import java.util.HashMap;
    011import java.util.List;
    012import javax.imageio.ImageIO;
    013import net.minecraft.client.Minecraft;
    014import net.minecraft.client.texturepacks.ITexturePack;
    015
    016@SideOnly(Side.CLIENT)
    017public class TextureManager
    018{
    019    private static TextureManager instance;
    020    private int nextTextureID = 0;
    021    private final HashMap texturesMap = new HashMap();
    022    private final HashMap mapNameToId = new HashMap();
    023
    024    public static void init()
    025    {
    026        instance = new TextureManager();
    027    }
    028
    029    public static TextureManager instance()
    030    {
    031        return instance;
    032    }
    033
    034    public int getNextTextureId()
    035    {
    036        return this.nextTextureID++;
    037    }
    038
    039    public void registerTexture(String par1Str, Texture par2Texture)
    040    {
    041        this.mapNameToId.put(par1Str, Integer.valueOf(par2Texture.getTextureId()));
    042
    043        if (!this.texturesMap.containsKey(Integer.valueOf(par2Texture.getTextureId())))
    044        {
    045            this.texturesMap.put(Integer.valueOf(par2Texture.getTextureId()), par2Texture);
    046        }
    047    }
    048
    049    public void registerTexture(Texture par1Texture)
    050    {
    051        if (this.texturesMap.containsValue(par1Texture))
    052        {
    053            Minecraft.getMinecraft().getLogAgent().logWarning("TextureManager.registerTexture called, but this texture has already been registered. ignoring.");
    054        }
    055        else
    056        {
    057            this.texturesMap.put(Integer.valueOf(par1Texture.getTextureId()), par1Texture);
    058        }
    059    }
    060
    061    public Stitcher createStitcher(String par1Str)
    062    {
    063        int i = Minecraft.getGLMaximumTextureSize();
    064        return new Stitcher(par1Str, i, i, true);
    065    }
    066
    067    public List createTexture(String par1Str)
    068    {
    069        return createNewTexture(par1Str, par1Str, null);
    070    }
    071
    072    public List createNewTexture(String textureName, String textureFile, TextureStitched stitched)
    073    {
    074        String par1Str = textureFile;
    075        ArrayList arraylist = new ArrayList();
    076        ITexturePack itexturepack = Minecraft.getMinecraft().texturePackList.getSelectedTexturePack();
    077
    078        try
    079        {
    080            BufferedImage bufferedimage = null;
    081            int i = 0;
    082            int j = 0;
    083            FileNotFoundException fnfe = null;
    084            try
    085            {
    086                bufferedimage = ImageIO.read(itexturepack.getResourceAsStream("/" + textureFile));
    087                i = bufferedimage.getHeight();
    088                j = bufferedimage.getWidth();
    089            }
    090            catch (FileNotFoundException e)
    091            {
    092                fnfe = e;
    093            }
    094            String s1 = textureName;
    095
    096            if (stitched != null && stitched.loadTexture(this, itexturepack, textureName, textureFile, bufferedimage, arraylist))
    097            {
    098                ;
    099            }
    100            else if (fnfe != null)
    101            {
    102                throw fnfe;
    103            }
    104            else if (this.hasAnimationTxt(par1Str, itexturepack))
    105            {
    106                int k = j;
    107                int l = j;
    108                int i1 = i / j;
    109
    110                for (int j1 = 0; j1 < i1; ++j1)
    111                {
    112                    Texture texture = this.makeTexture(s1, 2, k, l, 10496, 6408, 9728, 9728, false, bufferedimage.getSubimage(0, l * j1, k, l));
    113                    arraylist.add(texture);
    114                }
    115            }
    116            else if (j == i)
    117            {
    118                arraylist.add(this.makeTexture(s1, 2, j, i, 10496, 6408, 9728, 9728, false, bufferedimage));
    119            }
    120            else
    121            {
    122                Minecraft.getMinecraft().getLogAgent().logWarning("TextureManager.createTexture: Skipping " + par1Str + " because of broken aspect ratio and not animation");
    123            }
    124
    125            return arraylist;
    126        }
    127        catch (FileNotFoundException filenotfoundexception)
    128        {
    129            Minecraft.getMinecraft().getLogAgent().logWarning("TextureManager.createTexture called for file " + par1Str + ", but that file does not exist. Ignoring.");
    130        }
    131        catch (IOException ioexception)
    132        {
    133            Minecraft.getMinecraft().getLogAgent().logWarning("TextureManager.createTexture encountered an IOException when trying to read file " + par1Str + ". Ignoring.");
    134        }
    135
    136        return arraylist;
    137    }
    138
    139    /**
    140     * Strips directory and file extension from the specified path, returning only the filename
    141     */
    142    private String getBasename(String par1Str)
    143    {
    144        File file1 = new File(par1Str);
    145        return file1.getName().substring(0, file1.getName().lastIndexOf(46));
    146    }
    147
    148    /**
    149     * Returns true if specified texture pack contains animation data for the specified texture file
    150     */
    151    private boolean hasAnimationTxt(String par1Str, ITexturePack par2ITexturePack)
    152    {
    153        String s1 = "/" + par1Str.substring(0, par1Str.lastIndexOf(46)) + ".txt";
    154        boolean flag = par2ITexturePack.func_98138_b("/" + par1Str, false);
    155        return Minecraft.getMinecraft().texturePackList.getSelectedTexturePack().func_98138_b(s1, !flag);
    156    }
    157
    158    public Texture makeTexture(String par1Str, int par2, int par3, int par4, int par5, int par6, int par7, int par8, boolean par9, BufferedImage par10BufferedImage)
    159    {
    160        Texture texture = new Texture(par1Str, par2, par3, par4, par5, par6, par7, par8, par10BufferedImage);
    161        this.registerTexture(texture);
    162        return texture;
    163    }
    164
    165    public Texture createEmptyTexture(String par1Str, int par2, int par3, int par4, int par5)
    166    {
    167        return this.makeTexture(par1Str, par2, par3, par4, 10496, par5, 9728, 9728, false, (BufferedImage)null);
    168    }
    169}