| 1 | package net.minecraft.client.resources;
|
| 2 |
|
| 3 | import com.google.common.base.Charsets;
|
| 4 | import com.google.gson.JsonObject;
|
| 5 | import com.google.gson.JsonParseException;
|
| 6 | import com.google.gson.JsonParser;
|
| 7 | import java.awt.image.BufferedImage;
|
| 8 | import java.io.BufferedReader;
|
| 9 | import java.io.File;
|
| 10 | import java.io.IOException;
|
| 11 | import java.io.InputStream;
|
| 12 | import java.io.InputStreamReader;
|
| 13 | import java.io.Reader;
|
| 14 | import net.minecraft.client.renderer.texture.TextureUtil;
|
| 15 | import net.minecraft.client.resources.data.IMetadataSection;
|
| 16 | import net.minecraft.client.resources.data.IMetadataSerializer;
|
| 17 | import net.minecraft.util.ResourceLocation;
|
| 18 | import org.apache.commons.io.IOUtils;
|
| 19 | import org.apache.logging.log4j.LogManager;
|
| 20 | import org.apache.logging.log4j.Logger;
|
| 21 |
|
| 22 | public abstract class AbstractResourcePack implements IResourcePack
|
| 23 | {
|
| 24 | private static final Logger resourceLog = LogManager.getLogger();
|
| 25 | protected final File resourcePackFile;
|
| 26 |
|
| 27 | public AbstractResourcePack(File resourcePackFileIn)
|
| 28 | {
|
| 29 | this.resourcePackFile = resourcePackFileIn;
|
| 30 | }
|
| 31 |
|
| 32 | private static String locationToName(ResourceLocation location)
|
| 33 | {
|
| 34 | return String.format("%s/%s/%s", new Object[] {"assets", location.getResourceDomain(), location.getResourcePath()});
|
| 35 | }
|
| 36 |
|
| 37 | protected static String getRelativeName(File p_110595_0_, File p_110595_1_)
|
| 38 | {
|
| 39 | return p_110595_0_.toURI().relativize(p_110595_1_.toURI()).getPath();
|
| 40 | }
|
| 41 |
|
| 42 | public InputStream getInputStream(ResourceLocation location) throws IOException
|
| 43 | {
|
| 44 | return this.getInputStreamByName(locationToName(location));
|
| 45 | }
|
| 46 |
|
| 47 | public boolean resourceExists(ResourceLocation location)
|
| 48 | {
|
| 49 | return this.hasResourceName(locationToName(location));
|
| 50 | }
|
| 51 |
|
| 52 | protected abstract InputStream getInputStreamByName(String name) throws IOException;
|
| 53 |
|
| 54 | protected abstract boolean hasResourceName(String name);
|
| 55 |
|
| 56 | protected void logNameNotLowercase(String name)
|
| 57 | {
|
| 58 | resourceLog.warn("ResourcePack: ignored non-lowercase namespace: %s in %s", new Object[] {name, this.resourcePackFile});
|
| 59 | }
|
| 60 |
|
| 61 | public <T extends IMetadataSection> T getPackMetadata(IMetadataSerializer metadataSerializer, String metadataSectionName) throws IOException
|
| 62 | {
|
| 63 | return readMetadata(metadataSerializer, this.getInputStreamByName("pack.mcmeta"), metadataSectionName);
|
| 64 | }
|
| 65 |
|
| 66 | static <T extends IMetadataSection> T readMetadata(IMetadataSerializer p_110596_0_, InputStream p_110596_1_, String p_110596_2_)
|
| 67 | {
|
| 68 | JsonObject jsonobject = null;
|
| 69 | BufferedReader bufferedreader = null;
|
| 70 |
|
| 71 | try
|
| 72 | {
|
| 73 | bufferedreader = new BufferedReader(new InputStreamReader(p_110596_1_, Charsets.UTF_8));
|
| 74 | jsonobject = (new JsonParser()).parse((Reader)bufferedreader).getAsJsonObject();
|
| 75 | }
|
| 76 | catch (RuntimeException runtimeexception)
|
| 77 | {
|
| 78 | throw new JsonParseException(runtimeexception);
|
| 79 | }
|
| 80 | finally
|
| 81 | {
|
| 82 | IOUtils.closeQuietly((Reader)bufferedreader);
|
| 83 | }
|
| 84 |
|
| 85 | return p_110596_0_.parseMetadataSection(p_110596_2_, jsonobject);
|
| 86 | }
|
| 87 |
|
| 88 | public BufferedImage getPackImage() throws IOException
|
| 89 | {
|
| 90 | return TextureUtil.readBufferedImage(this.getInputStreamByName("pack.png"));
|
| 91 | }
|
| 92 |
|
| 93 | public String getPackName()
|
| 94 | {
|
| 95 | return this.resourcePackFile.getName();
|
| 96 | }
|
| 97 | }
|
| 98 |
|