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