| 1 | package net.minecraft.client.renderer.texture;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import java.awt.image.BufferedImage;
|
| 6 | import java.io.IOException;
|
| 7 | import java.util.Iterator;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.Map.Entry;
|
| 11 | import java.util.concurrent.Callable;
|
| 12 | import net.minecraft.client.Minecraft;
|
| 13 | import net.minecraft.client.renderer.StitcherException;
|
| 14 | import net.minecraft.client.resources.IResource;
|
| 15 | import net.minecraft.client.resources.IResourceManager;
|
| 16 | import net.minecraft.client.resources.data.AnimationMetadataSection;
|
| 17 | import net.minecraft.client.resources.data.TextureMetadataSection;
|
| 18 | import net.minecraft.crash.CrashReport;
|
| 19 | import net.minecraft.crash.CrashReportCategory;
|
| 20 | import net.minecraft.util.MathHelper;
|
| 21 | import net.minecraft.util.ReportedException;
|
| 22 | import net.minecraft.util.ResourceLocation;
|
| 23 | import org.apache.logging.log4j.LogManager;
|
| 24 | import org.apache.logging.log4j.Logger;
|
| 25 |
|
| 26 | public class TextureMap extends AbstractTexture implements ITickableTextureObject
|
| 27 | {
|
| 28 | private static final Logger logger = LogManager.getLogger();
|
| 29 | public static final ResourceLocation LOCATION_MISSING_TEXTURE = new ResourceLocation("missingno");
|
| 30 | public static final ResourceLocation locationBlocksTexture = new ResourceLocation("textures/atlas/blocks.png");
|
| 31 | private final List<TextureAtlasSprite> listAnimatedSprites;
|
| 32 | private final Map<String, TextureAtlasSprite> mapRegisteredSprites;
|
| 33 | private final Map<String, TextureAtlasSprite> mapUploadedSprites;
|
| 34 | private final String basePath;
|
| 35 | private final IIconCreator iconCreator;
|
| 36 | private int mipmapLevels;
|
| 37 | private final TextureAtlasSprite missingImage;
|
| 38 |
|
| 39 | public TextureMap(String p_i46099_1_)
|
| 40 | {
|
| 41 | this(p_i46099_1_, (IIconCreator)null);
|
| 42 | }
|
| 43 |
|
| 44 | public TextureMap(String p_i46100_1_, IIconCreator iconCreatorIn)
|
| 45 | {
|
| 46 | this.listAnimatedSprites = Lists.<TextureAtlasSprite>newArrayList();
|
| 47 | this.mapRegisteredSprites = Maps.<String, TextureAtlasSprite>newHashMap();
|
| 48 | this.mapUploadedSprites = Maps.<String, TextureAtlasSprite>newHashMap();
|
| 49 | this.missingImage = new TextureAtlasSprite("missingno");
|
| 50 | this.basePath = p_i46100_1_;
|
| 51 | this.iconCreator = iconCreatorIn;
|
| 52 | }
|
| 53 |
|
| 54 | private void initMissingImage()
|
| 55 | {
|
| 56 | int[] aint = TextureUtil.missingTextureData;
|
| 57 | this.missingImage.setIconWidth(16);
|
| 58 | this.missingImage.setIconHeight(16);
|
| 59 | int[][] aint1 = new int[this.mipmapLevels + 1][];
|
| 60 | aint1[0] = aint;
|
| 61 | this.missingImage.setFramesTextureData(Lists.newArrayList(new int[][][] {aint1}));
|
| 62 | }
|
| 63 |
|
| 64 | public void loadTexture(IResourceManager resourceManager) throws IOException
|
| 65 | {
|
| 66 | if (this.iconCreator != null)
|
| 67 | {
|
| 68 | this.loadSprites(resourceManager, this.iconCreator);
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | public void loadSprites(IResourceManager resourceManager, IIconCreator p_174943_2_)
|
| 73 | {
|
| 74 | this.mapRegisteredSprites.clear();
|
| 75 | p_174943_2_.registerSprites(this);
|
| 76 | this.initMissingImage();
|
| 77 | this.deleteGlTexture();
|
| 78 | this.loadTextureAtlas(resourceManager);
|
| 79 | }
|
| 80 |
|
| 81 | public void loadTextureAtlas(IResourceManager resourceManager)
|
| 82 | {
|
| 83 | int i = Minecraft.getGLMaximumTextureSize();
|
| 84 | Stitcher stitcher = new Stitcher(i, i, true, 0, this.mipmapLevels);
|
| 85 | this.mapUploadedSprites.clear();
|
| 86 | this.listAnimatedSprites.clear();
|
| 87 | int j = Integer.MAX_VALUE;
|
| 88 | int k = 1 << this.mipmapLevels;
|
| 89 |
|
| 90 | for (Entry<String, TextureAtlasSprite> entry : this.mapRegisteredSprites.entrySet())
|
| 91 | {
|
| 92 | TextureAtlasSprite textureatlassprite = (TextureAtlasSprite)entry.getValue();
|
| 93 | ResourceLocation resourcelocation = new ResourceLocation(textureatlassprite.getIconName());
|
| 94 | ResourceLocation resourcelocation1 = this.completeResourceLocation(resourcelocation, 0);
|
| 95 |
|
| 96 | try
|
| 97 | {
|
| 98 | IResource iresource = resourceManager.getResource(resourcelocation1);
|
| 99 | BufferedImage[] abufferedimage = new BufferedImage[1 + this.mipmapLevels];
|
| 100 | abufferedimage[0] = TextureUtil.readBufferedImage(iresource.getInputStream());
|
| 101 | TextureMetadataSection texturemetadatasection = (TextureMetadataSection)iresource.getMetadata("texture");
|
| 102 |
|
| 103 | if (texturemetadatasection != null)
|
| 104 | {
|
| 105 | List<Integer> list = texturemetadatasection.getListMipmaps();
|
| 106 |
|
| 107 | if (!list.isEmpty())
|
| 108 | {
|
| 109 | int l = abufferedimage[0].getWidth();
|
| 110 | int i1 = abufferedimage[0].getHeight();
|
| 111 |
|
| 112 | if (MathHelper.roundUpToPowerOfTwo(l) != l || MathHelper.roundUpToPowerOfTwo(i1) != i1)
|
| 113 | {
|
| 114 | throw new RuntimeException("Unable to load extra miplevels, source-texture is not power of two");
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | Iterator iterator = list.iterator();
|
| 119 |
|
| 120 | while (iterator.hasNext())
|
| 121 | {
|
| 122 | int i2 = ((Integer)iterator.next()).intValue();
|
| 123 |
|
| 124 | if (i2 > 0 && i2 < abufferedimage.length - 1 && abufferedimage[i2] == null)
|
| 125 | {
|
| 126 | ResourceLocation resourcelocation2 = this.completeResourceLocation(resourcelocation, i2);
|
| 127 |
|
| 128 | try
|
| 129 | {
|
| 130 | abufferedimage[i2] = TextureUtil.readBufferedImage(resourceManager.getResource(resourcelocation2).getInputStream());
|
| 131 | }
|
| 132 | catch (IOException ioexception)
|
| 133 | {
|
| 134 | logger.error("Unable to load miplevel {} from: {}", new Object[] {Integer.valueOf(i2), resourcelocation2, ioexception});
|
| 135 | }
|
| 136 | }
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | AnimationMetadataSection animationmetadatasection = (AnimationMetadataSection)iresource.getMetadata("animation");
|
| 141 | textureatlassprite.loadSprite(abufferedimage, animationmetadatasection);
|
| 142 | }
|
| 143 | catch (RuntimeException runtimeexception)
|
| 144 | {
|
| 145 | logger.error((String)("Unable to parse metadata from " + resourcelocation1), (Throwable)runtimeexception);
|
| 146 | continue;
|
| 147 | }
|
| 148 | catch (IOException ioexception1)
|
| 149 | {
|
| 150 | logger.error((String)("Using missing texture, unable to load " + resourcelocation1), (Throwable)ioexception1);
|
| 151 | continue;
|
| 152 | }
|
| 153 |
|
| 154 | j = Math.min(j, Math.min(textureatlassprite.getIconWidth(), textureatlassprite.getIconHeight()));
|
| 155 | int l1 = Math.min(Integer.lowestOneBit(textureatlassprite.getIconWidth()), Integer.lowestOneBit(textureatlassprite.getIconHeight()));
|
| 156 |
|
| 157 | if (l1 < k)
|
| 158 | {
|
| 159 | logger.warn("Texture {} with size {}x{} limits mip level from {} to {}", new Object[] {resourcelocation1, Integer.valueOf(textureatlassprite.getIconWidth()), Integer.valueOf(textureatlassprite.getIconHeight()), Integer.valueOf(MathHelper.calculateLogBaseTwo(k)), Integer.valueOf(MathHelper.calculateLogBaseTwo(l1))});
|
| 160 | k = l1;
|
| 161 | }
|
| 162 |
|
| 163 | stitcher.addSprite(textureatlassprite);
|
| 164 | }
|
| 165 |
|
| 166 | int j1 = Math.min(j, k);
|
| 167 | int k1 = MathHelper.calculateLogBaseTwo(j1);
|
| 168 |
|
| 169 | if (k1 < this.mipmapLevels)
|
| 170 | {
|
| 171 | logger.warn("{}: dropping miplevel from {} to {}, because of minimum power of two: {}", new Object[] {this.basePath, Integer.valueOf(this.mipmapLevels), Integer.valueOf(k1), Integer.valueOf(j1)});
|
| 172 | this.mipmapLevels = k1;
|
| 173 | }
|
| 174 |
|
| 175 | for (final TextureAtlasSprite textureatlassprite1 : this.mapRegisteredSprites.values())
|
| 176 | {
|
| 177 | try
|
| 178 | {
|
| 179 | textureatlassprite1.generateMipmaps(this.mipmapLevels);
|
| 180 | }
|
| 181 | catch (Throwable throwable1)
|
| 182 | {
|
| 183 | CrashReport crashreport = CrashReport.makeCrashReport(throwable1, "Applying mipmap");
|
| 184 | CrashReportCategory crashreportcategory = crashreport.makeCategory("Sprite being mipmapped");
|
| 185 | crashreportcategory.addCrashSectionCallable("Sprite name", new Callable<String>()
|
| 186 | {
|
| 187 | public String call() throws Exception
|
| 188 | {
|
| 189 | return textureatlassprite1.getIconName();
|
| 190 | }
|
| 191 | });
|
| 192 | crashreportcategory.addCrashSectionCallable("Sprite size", new Callable<String>()
|
| 193 | {
|
| 194 | public String call() throws Exception
|
| 195 | {
|
| 196 | return textureatlassprite1.getIconWidth() + " x " + textureatlassprite1.getIconHeight();
|
| 197 | }
|
| 198 | });
|
| 199 | crashreportcategory.addCrashSectionCallable("Sprite frames", new Callable<String>()
|
| 200 | {
|
| 201 | public String call() throws Exception
|
| 202 | {
|
| 203 | return textureatlassprite1.getFrameCount() + " frames";
|
| 204 | }
|
| 205 | });
|
| 206 | crashreportcategory.addCrashSection("Mipmap levels", Integer.valueOf(this.mipmapLevels));
|
| 207 | throw new ReportedException(crashreport);
|
| 208 | }
|
| 209 | }
|
| 210 |
|
| 211 | this.missingImage.generateMipmaps(this.mipmapLevels);
|
| 212 | stitcher.addSprite(this.missingImage);
|
| 213 |
|
| 214 | try
|
| 215 | {
|
| 216 | stitcher.doStitch();
|
| 217 | }
|
| 218 | catch (StitcherException stitcherexception)
|
| 219 | {
|
| 220 | throw stitcherexception;
|
| 221 | }
|
| 222 |
|
| 223 | logger.info("Created: {}x{} {}-atlas", new Object[] {Integer.valueOf(stitcher.getCurrentWidth()), Integer.valueOf(stitcher.getCurrentHeight()), this.basePath});
|
| 224 | TextureUtil.allocateTextureImpl(this.getGlTextureId(), this.mipmapLevels, stitcher.getCurrentWidth(), stitcher.getCurrentHeight());
|
| 225 | Map<String, TextureAtlasSprite> map = Maps.<String, TextureAtlasSprite>newHashMap(this.mapRegisteredSprites);
|
| 226 |
|
| 227 | for (TextureAtlasSprite textureatlassprite2 : stitcher.getStichSlots())
|
| 228 | {
|
| 229 | String s = textureatlassprite2.getIconName();
|
| 230 | map.remove(s);
|
| 231 | this.mapUploadedSprites.put(s, textureatlassprite2);
|
| 232 |
|
| 233 | try
|
| 234 | {
|
| 235 | TextureUtil.uploadTextureMipmap(textureatlassprite2.getFrameTextureData(0), textureatlassprite2.getIconWidth(), textureatlassprite2.getIconHeight(), textureatlassprite2.getOriginX(), textureatlassprite2.getOriginY(), false, false);
|
| 236 | }
|
| 237 | catch (Throwable throwable)
|
| 238 | {
|
| 239 | CrashReport crashreport1 = CrashReport.makeCrashReport(throwable, "Stitching texture atlas");
|
| 240 | CrashReportCategory crashreportcategory1 = crashreport1.makeCategory("Texture being stitched together");
|
| 241 | crashreportcategory1.addCrashSection("Atlas path", this.basePath);
|
| 242 | crashreportcategory1.addCrashSection("Sprite", textureatlassprite2);
|
| 243 | throw new ReportedException(crashreport1);
|
| 244 | }
|
| 245 |
|
| 246 | if (textureatlassprite2.hasAnimationMetadata())
|
| 247 | {
|
| 248 | this.listAnimatedSprites.add(textureatlassprite2);
|
| 249 | }
|
| 250 | }
|
| 251 |
|
| 252 | for (TextureAtlasSprite textureatlassprite3 : map.values())
|
| 253 | {
|
| 254 | textureatlassprite3.copyFrom(this.missingImage);
|
| 255 | }
|
| 256 | }
|
| 257 |
|
| 258 | private ResourceLocation completeResourceLocation(ResourceLocation location, int p_147634_2_)
|
| 259 | {
|
| 260 | return p_147634_2_ == 0 ? new ResourceLocation(location.getResourceDomain(), String.format("%s/%s%s", new Object[] {this.basePath, location.getResourcePath(), ".png"})): new ResourceLocation(location.getResourceDomain(), String.format("%s/mipmaps/%s.%d%s", new Object[] {this.basePath, location.getResourcePath(), Integer.valueOf(p_147634_2_), ".png"}));
|
| 261 | }
|
| 262 |
|
| 263 | public TextureAtlasSprite getAtlasSprite(String iconName)
|
| 264 | {
|
| 265 | TextureAtlasSprite textureatlassprite = (TextureAtlasSprite)this.mapUploadedSprites.get(iconName);
|
| 266 |
|
| 267 | if (textureatlassprite == null)
|
| 268 | {
|
| 269 | textureatlassprite = this.missingImage;
|
| 270 | }
|
| 271 |
|
| 272 | return textureatlassprite;
|
| 273 | }
|
| 274 |
|
| 275 | public void updateAnimations()
|
| 276 | {
|
| 277 | TextureUtil.bindTexture(this.getGlTextureId());
|
| 278 |
|
| 279 | for (TextureAtlasSprite textureatlassprite : this.listAnimatedSprites)
|
| 280 | {
|
| 281 | textureatlassprite.updateAnimation();
|
| 282 | }
|
| 283 | }
|
| 284 |
|
| 285 | public TextureAtlasSprite registerSprite(ResourceLocation location)
|
| 286 | {
|
| 287 | if (location == null)
|
| 288 | {
|
| 289 | throw new IllegalArgumentException("Location cannot be null!");
|
| 290 | }
|
| 291 | else
|
| 292 | {
|
| 293 | TextureAtlasSprite textureatlassprite = (TextureAtlasSprite)this.mapRegisteredSprites.get(location);
|
| 294 |
|
| 295 | if (textureatlassprite == null)
|
| 296 | {
|
| 297 | textureatlassprite = TextureAtlasSprite.makeAtlasSprite(location);
|
| 298 | this.mapRegisteredSprites.put(location.toString(), textureatlassprite);
|
| 299 | }
|
| 300 |
|
| 301 | return textureatlassprite;
|
| 302 | }
|
| 303 | }
|
| 304 |
|
| 305 | public void tick()
|
| 306 | {
|
| 307 | this.updateAnimations();
|
| 308 | }
|
| 309 |
|
| 310 | public void setMipmapLevels(int mipmapLevelsIn)
|
| 311 | {
|
| 312 | this.mipmapLevels = mipmapLevelsIn;
|
| 313 | }
|
| 314 |
|
| 315 | public TextureAtlasSprite getMissingSprite()
|
| 316 | {
|
| 317 | return this.missingImage;
|
| 318 | }
|
| 319 | }
|
| 320 |
|