    001package net.minecraft.client.texturepacks;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.awt.image.BufferedImage;
    006import java.io.BufferedReader;
    007import java.io.File;
    008import java.io.IOException;
    009import java.io.InputStream;
    010import java.io.InputStreamReader;
    011import javax.imageio.ImageIO;
    012import net.minecraft.client.renderer.RenderEngine;
    013import org.lwjgl.opengl.GL11;
    014
    015@SideOnly(Side.CLIENT)
    016public abstract class TexturePackImplementation implements ITexturePack
    017{
    018    /**
    019     * Texture pack ID as returnd by generateTexturePackID(). Used only internally and not visible to the user.
    020     */
    021    private final String texturePackID;
    022
    023    /**
    024     * The name of the texture pack's zip file/directory or "Default" for the builtin texture pack. Shown in the GUI.
    025     */
    026    private final String texturePackFileName;
    027
    028    /**
    029     * File object for the texture pack's zip file in TexturePackCustom or the directory in TexturePackFolder.
    030     */
    031    protected final File texturePackFile;
    032
    033    /**
    034     * First line of texture pack description (from /pack.txt) displayed in the GUI
    035     */
    036    protected String firstDescriptionLine;
    037
    038    /**
    039     * Second line of texture pack description (from /pack.txt) displayed in the GUI
    040     */
    041    protected String secondDescriptionLine;
    042    private final ITexturePack field_98141_g;
    043
    044    /** The texture pack's thumbnail image loaded from the /pack.png file. */
    045    protected BufferedImage thumbnailImage;
    046
    047    /** The texture id for this pcak's thumbnail image. */
    048    private int thumbnailTextureName = -1;
    049
    050    protected TexturePackImplementation(String par1, File par2File, String par3Str, ITexturePack par4ITexturePack)
    051    {
    052        this.texturePackID = par1;
    053        this.texturePackFileName = par3Str;
    054        this.texturePackFile = par2File;
    055        this.field_98141_g = par4ITexturePack;
    056        this.loadThumbnailImage();
    057        this.loadDescription();
    058    }
    059
    060    /**
    061     * Truncate strings to at most 34 characters. Truncates description lines
    062     */
    063    private static String trimStringToGUIWidth(String par0Str)
    064    {
    065        if (par0Str != null && par0Str.length() > 34)
    066        {
    067            par0Str = par0Str.substring(0, 34);
    068        }
    069
    070        return par0Str;
    071    }
    072
    073    /**
    074     * Load and initialize thumbnailImage from the the /pack.png file.
    075     */
    076    private void loadThumbnailImage()
    077    {
    078        InputStream inputstream = null;
    079
    080        try
    081        {
    082            inputstream = this.func_98137_a("/pack.png", false);
    083            this.thumbnailImage = ImageIO.read(inputstream);
    084        }
    085        catch (IOException ioexception)
    086        {
    087            ;
    088        }
    089        finally
    090        {
    091            try
    092            {
    093                if (inputstream != null)
    094                {
    095                    inputstream.close();
    096                }
    097            }
    098            catch (IOException ioexception1)
    099            {
    100                ;
    101            }
    102        }
    103    }
    104
    105    /**
    106     * Load texture pack description from /pack.txt file in the texture pack
    107     */
    108    protected void loadDescription()
    109    {
    110        InputStream inputstream = null;
    111        BufferedReader bufferedreader = null;
    112
    113        try
    114        {
    115            inputstream = this.func_98139_b("/pack.txt");
    116            bufferedreader = new BufferedReader(new InputStreamReader(inputstream));
    117            this.firstDescriptionLine = trimStringToGUIWidth(bufferedreader.readLine());
    118            this.secondDescriptionLine = trimStringToGUIWidth(bufferedreader.readLine());
    119        }
    120        catch (IOException ioexception)
    121        {
    122            ;
    123        }
    124        finally
    125        {
    126            try
    127            {
    128                if (bufferedreader != null)
    129                {
    130                    bufferedreader.close();
    131                }
    132
    133                if (inputstream != null)
    134                {
    135                    inputstream.close();
    136                }
    137            }
    138            catch (IOException ioexception1)
    139            {
    140                ;
    141            }
    142        }
    143    }
    144
    145    public InputStream func_98137_a(String par1Str, boolean par2) throws IOException
    146    {
    147        try
    148        {
    149            return this.func_98139_b(par1Str);
    150        }
    151        catch (IOException ioexception)
    152        {
    153            if (this.field_98141_g != null && par2)
    154            {
    155                return this.field_98141_g.func_98137_a(par1Str, true);
    156            }
    157            else
    158            {
    159                throw ioexception;
    160            }
    161        }
    162    }
    163
    164    /**
    165     * Gives a texture resource as InputStream.
    166     */
    167    public InputStream getResourceAsStream(String par1Str) throws IOException
    168    {
    169        return this.func_98137_a(par1Str, true);
    170    }
    171
    172    protected abstract InputStream func_98139_b(String s) throws IOException;
    173
    174    /**
    175     * Delete the OpenGL texture id of the pack's thumbnail image, and close the zip file in case of TexturePackCustom.
    176     */
    177    public void deleteTexturePack(RenderEngine par1RenderEngine)
    178    {
    179        if (this.thumbnailImage != null && this.thumbnailTextureName != -1)
    180        {
    181            par1RenderEngine.deleteTexture(this.thumbnailTextureName);
    182        }
    183    }
    184
    185    /**
    186     * Bind the texture id of the pack's thumbnail image, loading it if necessary.
    187     */
    188    public void bindThumbnailTexture(RenderEngine par1RenderEngine)
    189    {
    190        if (this.thumbnailImage != null)
    191        {
    192            if (this.thumbnailTextureName == -1)
    193            {
    194                this.thumbnailTextureName = par1RenderEngine.allocateAndSetupTexture(this.thumbnailImage);
    195            }
    196
    197            GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.thumbnailTextureName);
    198            par1RenderEngine.func_98185_a();
    199        }
    200        else
    201        {
    202            par1RenderEngine.func_98187_b("/gui/unknown_pack.png");
    203        }
    204    }
    205
    206    public boolean func_98138_b(String par1Str, boolean par2)
    207    {
    208        boolean flag1 = this.func_98140_c(par1Str);
    209        return !flag1 && par2 && this.field_98141_g != null ? this.field_98141_g.func_98138_b(par1Str, par2) : flag1;
    210    }
    211
    212    public abstract boolean func_98140_c(String s);
    213
    214    /**
    215     * Get the texture pack ID
    216     */
    217    public String getTexturePackID()
    218    {
    219        return this.texturePackID;
    220    }
    221
    222    /**
    223     * Get the file name of the texture pack, or Default if not from a custom texture pack
    224     */
    225    public String getTexturePackFileName()
    226    {
    227        return this.texturePackFileName;
    228    }
    229
    230    /**
    231     * Get the first line of the texture pack description (read from the pack.txt file)
    232     */
    233    public String getFirstDescriptionLine()
    234    {
    235        return this.firstDescriptionLine;
    236    }
    237
    238    /**
    239     * Get the second line of the texture pack description (read from the pack.txt file)
    240     */
    241    public String getSecondDescriptionLine()
    242    {
    243        return this.secondDescriptionLine;
    244    }
    245}