| 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.io.IOException;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Map.Entry;
|
| 9 | import java.util.concurrent.Callable;
|
| 10 | import net.minecraft.client.resources.IResourceManager;
|
| 11 | import net.minecraft.client.resources.IResourceManagerReloadListener;
|
| 12 | import net.minecraft.crash.CrashReport;
|
| 13 | import net.minecraft.crash.CrashReportCategory;
|
| 14 | import net.minecraft.util.ReportedException;
|
| 15 | import net.minecraft.util.ResourceLocation;
|
| 16 | import org.apache.logging.log4j.LogManager;
|
| 17 | import org.apache.logging.log4j.Logger;
|
| 18 |
|
| 19 | public class TextureManager implements ITickable, IResourceManagerReloadListener
|
| 20 | {
|
| 21 | private static final Logger logger = LogManager.getLogger();
|
| 22 | private final Map<ResourceLocation, ITextureObject> mapTextureObjects = Maps.<ResourceLocation, ITextureObject>newHashMap();
|
| 23 | private final List<ITickable> listTickables = Lists.<ITickable>newArrayList();
|
| 24 | private final Map<String, Integer> mapTextureCounters = Maps.<String, Integer>newHashMap();
|
| 25 | private IResourceManager theResourceManager;
|
| 26 |
|
| 27 | public TextureManager(IResourceManager resourceManager)
|
| 28 | {
|
| 29 | this.theResourceManager = resourceManager;
|
| 30 | }
|
| 31 |
|
| 32 | public void bindTexture(ResourceLocation resource)
|
| 33 | {
|
| 34 | ITextureObject itextureobject = (ITextureObject)this.mapTextureObjects.get(resource);
|
| 35 |
|
| 36 | if (itextureobject == null)
|
| 37 | {
|
| 38 | itextureobject = new SimpleTexture(resource);
|
| 39 | this.loadTexture(resource, itextureobject);
|
| 40 | }
|
| 41 |
|
| 42 | TextureUtil.bindTexture(itextureobject.getGlTextureId());
|
| 43 | }
|
| 44 |
|
| 45 | public boolean loadTickableTexture(ResourceLocation textureLocation, ITickableTextureObject textureObj)
|
| 46 | {
|
| 47 | if (this.loadTexture(textureLocation, textureObj))
|
| 48 | {
|
| 49 | this.listTickables.add(textureObj);
|
| 50 | return true;
|
| 51 | }
|
| 52 | else
|
| 53 | {
|
| 54 | return false;
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | public boolean loadTexture(ResourceLocation textureLocation, ITextureObject textureObj)
|
| 59 | {
|
| 60 | boolean flag = true;
|
| 61 |
|
| 62 | try
|
| 63 | {
|
| 64 | ((ITextureObject)textureObj).loadTexture(this.theResourceManager);
|
| 65 | }
|
| 66 | catch (IOException ioexception)
|
| 67 | {
|
| 68 | logger.warn((String)("Failed to load texture: " + textureLocation), (Throwable)ioexception);
|
| 69 | textureObj = TextureUtil.missingTexture;
|
| 70 | this.mapTextureObjects.put(textureLocation, (ITextureObject)textureObj);
|
| 71 | flag = false;
|
| 72 | }
|
| 73 | catch (Throwable throwable)
|
| 74 | {
|
| 75 | final ITextureObject textureObjf = textureObj;
|
| 76 | CrashReport crashreport = CrashReport.makeCrashReport(throwable, "Registering texture");
|
| 77 | CrashReportCategory crashreportcategory = crashreport.makeCategory("Resource location being registered");
|
| 78 | crashreportcategory.addCrashSection("Resource location", textureLocation);
|
| 79 | crashreportcategory.addCrashSectionCallable("Texture object class", new Callable<String>()
|
| 80 | {
|
| 81 | public String call() throws Exception
|
| 82 | {
|
| 83 | return textureObjf.getClass().getName();
|
| 84 | }
|
| 85 | });
|
| 86 | throw new ReportedException(crashreport);
|
| 87 | }
|
| 88 |
|
| 89 | this.mapTextureObjects.put(textureLocation, (ITextureObject)textureObj);
|
| 90 | return flag;
|
| 91 | }
|
| 92 |
|
| 93 | public ITextureObject getTexture(ResourceLocation textureLocation)
|
| 94 | {
|
| 95 | return (ITextureObject)this.mapTextureObjects.get(textureLocation);
|
| 96 | }
|
| 97 |
|
| 98 | public ResourceLocation getDynamicTextureLocation(String name, DynamicTexture texture)
|
| 99 | {
|
| 100 | Integer integer = (Integer)this.mapTextureCounters.get(name);
|
| 101 |
|
| 102 | if (integer == null)
|
| 103 | {
|
| 104 | integer = Integer.valueOf(1);
|
| 105 | }
|
| 106 | else
|
| 107 | {
|
| 108 | integer = Integer.valueOf(integer.intValue() + 1);
|
| 109 | }
|
| 110 |
|
| 111 | this.mapTextureCounters.put(name, integer);
|
| 112 | ResourceLocation resourcelocation = new ResourceLocation(String.format("dynamic/%s_%d", new Object[] {name, integer}));
|
| 113 | this.loadTexture(resourcelocation, texture);
|
| 114 | return resourcelocation;
|
| 115 | }
|
| 116 |
|
| 117 | public void tick()
|
| 118 | {
|
| 119 | for (ITickable itickable : this.listTickables)
|
| 120 | {
|
| 121 | itickable.tick();
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | public void deleteTexture(ResourceLocation textureLocation)
|
| 126 | {
|
| 127 | ITextureObject itextureobject = this.getTexture(textureLocation);
|
| 128 |
|
| 129 | if (itextureobject != null)
|
| 130 | {
|
| 131 | TextureUtil.deleteTexture(itextureobject.getGlTextureId());
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | public void onResourceManagerReload(IResourceManager resourceManager)
|
| 136 | {
|
| 137 | for (Entry<ResourceLocation, ITextureObject> entry : this.mapTextureObjects.entrySet())
|
| 138 | {
|
| 139 | this.loadTexture((ResourceLocation)entry.getKey(), (ITextureObject)entry.getValue());
|
| 140 | }
|
| 141 | }
|
| 142 | }
|
| 143 |
|