| 1 | package net.minecraft.client.resources;
|
| 2 |
|
| 3 | import com.google.common.base.Function;
|
| 4 | import com.google.common.base.Joiner;
|
| 5 | import com.google.common.collect.Iterables;
|
| 6 | import com.google.common.collect.Lists;
|
| 7 | import com.google.common.collect.Maps;
|
| 8 | import com.google.common.collect.Sets;
|
| 9 | import java.io.FileNotFoundException;
|
| 10 | import java.io.IOException;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Map;
|
| 13 | import java.util.Set;
|
| 14 | import net.minecraft.client.resources.data.IMetadataSerializer;
|
| 15 | import net.minecraft.util.ResourceLocation;
|
| 16 | import org.apache.logging.log4j.LogManager;
|
| 17 | import org.apache.logging.log4j.Logger;
|
| 18 |
|
| 19 | public class SimpleReloadableResourceManager implements IReloadableResourceManager
|
| 20 | {
|
| 21 | private static final Logger logger = LogManager.getLogger();
|
| 22 | private static final Joiner joinerResourcePacks = Joiner.on(", ");
|
| 23 | private final Map<String, FallbackResourceManager> domainResourceManagers = Maps.<String, FallbackResourceManager>newHashMap();
|
| 24 | private final List<IResourceManagerReloadListener> reloadListeners = Lists.<IResourceManagerReloadListener>newArrayList();
|
| 25 | private final Set<String> setResourceDomains = Sets.<String>newLinkedHashSet();
|
| 26 | private final IMetadataSerializer rmMetadataSerializer;
|
| 27 |
|
| 28 | public SimpleReloadableResourceManager(IMetadataSerializer rmMetadataSerializerIn)
|
| 29 | {
|
| 30 | this.rmMetadataSerializer = rmMetadataSerializerIn;
|
| 31 | }
|
| 32 |
|
| 33 | public void reloadResourcePack(IResourcePack resourcePack)
|
| 34 | {
|
| 35 | for (String s : resourcePack.getResourceDomains())
|
| 36 | {
|
| 37 | this.setResourceDomains.add(s);
|
| 38 | FallbackResourceManager fallbackresourcemanager = (FallbackResourceManager)this.domainResourceManagers.get(s);
|
| 39 |
|
| 40 | if (fallbackresourcemanager == null)
|
| 41 | {
|
| 42 | fallbackresourcemanager = new FallbackResourceManager(this.rmMetadataSerializer);
|
| 43 | this.domainResourceManagers.put(s, fallbackresourcemanager);
|
| 44 | }
|
| 45 |
|
| 46 | fallbackresourcemanager.addResourcePack(resourcePack);
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | public Set<String> getResourceDomains()
|
| 51 | {
|
| 52 | return this.setResourceDomains;
|
| 53 | }
|
| 54 |
|
| 55 | public IResource getResource(ResourceLocation location) throws IOException
|
| 56 | {
|
| 57 | IResourceManager iresourcemanager = (IResourceManager)this.domainResourceManagers.get(location.getResourceDomain());
|
| 58 |
|
| 59 | if (iresourcemanager != null)
|
| 60 | {
|
| 61 | return iresourcemanager.getResource(location);
|
| 62 | }
|
| 63 | else
|
| 64 | {
|
| 65 | throw new FileNotFoundException(location.toString());
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | public List<IResource> getAllResources(ResourceLocation location) throws IOException
|
| 70 | {
|
| 71 | IResourceManager iresourcemanager = (IResourceManager)this.domainResourceManagers.get(location.getResourceDomain());
|
| 72 |
|
| 73 | if (iresourcemanager != null)
|
| 74 | {
|
| 75 | return iresourcemanager.getAllResources(location);
|
| 76 | }
|
| 77 | else
|
| 78 | {
|
| 79 | throw new FileNotFoundException(location.toString());
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | private void clearResources()
|
| 84 | {
|
| 85 | this.domainResourceManagers.clear();
|
| 86 | this.setResourceDomains.clear();
|
| 87 | }
|
| 88 |
|
| 89 | public void reloadResources(List<IResourcePack> resourcesPacksList)
|
| 90 | {
|
| 91 | this.clearResources();
|
| 92 | logger.info("Reloading ResourceManager: " + joinerResourcePacks.join(Iterables.transform(resourcesPacksList, new Function<IResourcePack, String>()
|
| 93 | {
|
| 94 | public String apply(IResourcePack p_apply_1_)
|
| 95 | {
|
| 96 | return p_apply_1_.getPackName();
|
| 97 | }
|
| 98 | })));
|
| 99 |
|
| 100 | for (IResourcePack iresourcepack : resourcesPacksList)
|
| 101 | {
|
| 102 | this.reloadResourcePack(iresourcepack);
|
| 103 | }
|
| 104 |
|
| 105 | this.notifyReloadListeners();
|
| 106 | }
|
| 107 |
|
| 108 | public void registerReloadListener(IResourceManagerReloadListener reloadListener)
|
| 109 | {
|
| 110 | this.reloadListeners.add(reloadListener);
|
| 111 | reloadListener.onResourceManagerReload(this);
|
| 112 | }
|
| 113 |
|
| 114 | private void notifyReloadListeners()
|
| 115 | {
|
| 116 | for (IResourceManagerReloadListener iresourcemanagerreloadlistener : this.reloadListeners)
|
| 117 | {
|
| 118 | iresourcemanagerreloadlistener.onResourceManagerReload(this);
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|