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