| 1 | package net.minecraft.client.resources;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.google.common.hash.Hashing;
|
| 6 | import com.google.common.io.Files;
|
| 7 | import com.google.common.util.concurrent.FutureCallback;
|
| 8 | import com.google.common.util.concurrent.Futures;
|
| 9 | import com.google.common.util.concurrent.ListenableFuture;
|
| 10 | import com.google.common.util.concurrent.SettableFuture;
|
| 11 | import java.awt.image.BufferedImage;
|
| 12 | import java.io.Closeable;
|
| 13 | import java.io.File;
|
| 14 | import java.io.FileFilter;
|
| 15 | import java.io.IOException;
|
| 16 | import java.util.Arrays;
|
| 17 | import java.util.Collections;
|
| 18 | import java.util.Iterator;
|
| 19 | import java.util.List;
|
| 20 | import java.util.Map;
|
| 21 | import java.util.concurrent.locks.ReentrantLock;
|
| 22 | import net.minecraft.client.Minecraft;
|
| 23 | import net.minecraft.client.gui.GuiScreenWorking;
|
| 24 | import net.minecraft.client.renderer.texture.DynamicTexture;
|
| 25 | import net.minecraft.client.renderer.texture.TextureManager;
|
| 26 | import net.minecraft.client.resources.data.IMetadataSerializer;
|
| 27 | import net.minecraft.client.resources.data.PackMetadataSection;
|
| 28 | import net.minecraft.client.settings.GameSettings;
|
| 29 | import net.minecraft.util.EnumChatFormatting;
|
| 30 | import net.minecraft.util.HttpUtil;
|
| 31 | import net.minecraft.util.ResourceLocation;
|
| 32 | import org.apache.commons.io.FileUtils;
|
| 33 | import org.apache.commons.io.IOUtils;
|
| 34 | import org.apache.commons.io.comparator.LastModifiedFileComparator;
|
| 35 | import org.apache.commons.io.filefilter.IOFileFilter;
|
| 36 | import org.apache.commons.io.filefilter.TrueFileFilter;
|
| 37 | import org.apache.logging.log4j.LogManager;
|
| 38 | import org.apache.logging.log4j.Logger;
|
| 39 |
|
| 40 | public class ResourcePackRepository
|
| 41 | {
|
| 42 | private static final Logger logger = LogManager.getLogger();
|
| 43 | private static final FileFilter resourcePackFilter = new FileFilter()
|
| 44 | {
|
| 45 | public boolean accept(File p_accept_1_)
|
| 46 | {
|
| 47 | boolean flag = p_accept_1_.isFile() && p_accept_1_.getName().endsWith(".zip");
|
| 48 | boolean flag1 = p_accept_1_.isDirectory() && (new File(p_accept_1_, "pack.mcmeta")).isFile();
|
| 49 | return flag || flag1;
|
| 50 | }
|
| 51 | };
|
| 52 | private final File dirResourcepacks;
|
| 53 | public final IResourcePack rprDefaultResourcePack;
|
| 54 | private final File dirServerResourcepacks;
|
| 55 | public final IMetadataSerializer rprMetadataSerializer;
|
| 56 | private IResourcePack resourcePackInstance;
|
| 57 | private final ReentrantLock lock = new ReentrantLock();
|
| 58 | private ListenableFuture<Object> downloadingPacks;
|
| 59 | private List<ResourcePackRepository.Entry> repositoryEntriesAll = Lists.<ResourcePackRepository.Entry>newArrayList();
|
| 60 | private List<ResourcePackRepository.Entry> repositoryEntries = Lists.<ResourcePackRepository.Entry>newArrayList();
|
| 61 |
|
| 62 | public ResourcePackRepository(File dirResourcepacksIn, File dirServerResourcepacksIn, IResourcePack rprDefaultResourcePackIn, IMetadataSerializer rprMetadataSerializerIn, GameSettings settings)
|
| 63 | {
|
| 64 | this.dirResourcepacks = dirResourcepacksIn;
|
| 65 | this.dirServerResourcepacks = dirServerResourcepacksIn;
|
| 66 | this.rprDefaultResourcePack = rprDefaultResourcePackIn;
|
| 67 | this.rprMetadataSerializer = rprMetadataSerializerIn;
|
| 68 | this.fixDirResourcepacks();
|
| 69 | this.updateRepositoryEntriesAll();
|
| 70 | Iterator<String> iterator = settings.resourcePacks.iterator();
|
| 71 |
|
| 72 | while (iterator.hasNext())
|
| 73 | {
|
| 74 | String s = (String)iterator.next();
|
| 75 |
|
| 76 | for (ResourcePackRepository.Entry resourcepackrepository$entry : this.repositoryEntriesAll)
|
| 77 | {
|
| 78 | if (resourcepackrepository$entry.getResourcePackName().equals(s))
|
| 79 | {
|
| 80 | if (resourcepackrepository$entry.func_183027_f() == 1 || settings.incompatibleResourcePacks.contains(resourcepackrepository$entry.getResourcePackName()))
|
| 81 | {
|
| 82 | this.repositoryEntries.add(resourcepackrepository$entry);
|
| 83 | break;
|
| 84 | }
|
| 85 |
|
| 86 | iterator.remove();
|
| 87 | logger.warn("Removed selected resource pack {} because it\'s no longer compatible", new Object[] {resourcepackrepository$entry.getResourcePackName()});
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | private void fixDirResourcepacks()
|
| 94 | {
|
| 95 | if (this.dirResourcepacks.exists())
|
| 96 | {
|
| 97 | if (!this.dirResourcepacks.isDirectory() && (!this.dirResourcepacks.delete() || !this.dirResourcepacks.mkdirs()))
|
| 98 | {
|
| 99 | logger.warn("Unable to recreate resourcepack folder, it exists but is not a directory: " + this.dirResourcepacks);
|
| 100 | }
|
| 101 | }
|
| 102 | else if (!this.dirResourcepacks.mkdirs())
|
| 103 | {
|
| 104 | logger.warn("Unable to create resourcepack folder: " + this.dirResourcepacks);
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | private List<File> getResourcePackFiles()
|
| 109 | {
|
| 110 | return this.dirResourcepacks.isDirectory() ? Arrays.asList(this.dirResourcepacks.listFiles(resourcePackFilter)) : Collections.<File>emptyList();
|
| 111 | }
|
| 112 |
|
| 113 | public void updateRepositoryEntriesAll()
|
| 114 | {
|
| 115 | List<ResourcePackRepository.Entry> list = Lists.<ResourcePackRepository.Entry>newArrayList();
|
| 116 |
|
| 117 | for (File file1 : this.getResourcePackFiles())
|
| 118 | {
|
| 119 | ResourcePackRepository.Entry resourcepackrepository$entry = new ResourcePackRepository.Entry(file1);
|
| 120 |
|
| 121 | if (!this.repositoryEntriesAll.contains(resourcepackrepository$entry))
|
| 122 | {
|
| 123 | try
|
| 124 | {
|
| 125 | resourcepackrepository$entry.updateResourcePack();
|
| 126 | list.add(resourcepackrepository$entry);
|
| 127 | }
|
| 128 | catch (Exception var6)
|
| 129 | {
|
| 130 | list.remove(resourcepackrepository$entry);
|
| 131 | }
|
| 132 | }
|
| 133 | else
|
| 134 | {
|
| 135 | int i = this.repositoryEntriesAll.indexOf(resourcepackrepository$entry);
|
| 136 |
|
| 137 | if (i > -1 && i < this.repositoryEntriesAll.size())
|
| 138 | {
|
| 139 | list.add(this.repositoryEntriesAll.get(i));
|
| 140 | }
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | this.repositoryEntriesAll.removeAll(list);
|
| 145 |
|
| 146 | for (ResourcePackRepository.Entry resourcepackrepository$entry1 : this.repositoryEntriesAll)
|
| 147 | {
|
| 148 | resourcepackrepository$entry1.closeResourcePack();
|
| 149 | }
|
| 150 |
|
| 151 | this.repositoryEntriesAll = list;
|
| 152 | }
|
| 153 |
|
| 154 | public List<ResourcePackRepository.Entry> getRepositoryEntriesAll()
|
| 155 | {
|
| 156 | return ImmutableList.copyOf(this.repositoryEntriesAll);
|
| 157 | }
|
| 158 |
|
| 159 | public List<ResourcePackRepository.Entry> getRepositoryEntries()
|
| 160 | {
|
| 161 | return ImmutableList.copyOf(this.repositoryEntries);
|
| 162 | }
|
| 163 |
|
| 164 | public void setRepositories(List<ResourcePackRepository.Entry> repositories)
|
| 165 | {
|
| 166 | this.repositoryEntries.clear();
|
| 167 | this.repositoryEntries.addAll(repositories);
|
| 168 | }
|
| 169 |
|
| 170 | public File getDirResourcepacks()
|
| 171 | {
|
| 172 | return this.dirResourcepacks;
|
| 173 | }
|
| 174 |
|
| 175 | public ListenableFuture<Object> downloadResourcePack(String url, String hash)
|
| 176 | {
|
| 177 | String s;
|
| 178 |
|
| 179 | if (hash.matches("^[a-f0-9]{40}$"))
|
| 180 | {
|
| 181 | s = hash;
|
| 182 | }
|
| 183 | else
|
| 184 | {
|
| 185 | s = "legacy";
|
| 186 | }
|
| 187 |
|
| 188 | final File file1 = new File(this.dirServerResourcepacks, s);
|
| 189 | this.lock.lock();
|
| 190 |
|
| 191 | try
|
| 192 | {
|
| 193 | this.clearResourcePack();
|
| 194 |
|
| 195 | if (file1.exists() && hash.length() == 40)
|
| 196 | {
|
| 197 | try
|
| 198 | {
|
| 199 | String s1 = Hashing.sha1().hashBytes(Files.toByteArray(file1)).toString();
|
| 200 |
|
| 201 | if (s1.equals(hash))
|
| 202 | {
|
| 203 | ListenableFuture listenablefuture1 = this.setResourcePackInstance(file1);
|
| 204 | return listenablefuture1;
|
| 205 | }
|
| 206 |
|
| 207 | logger.warn("File " + file1 + " had wrong hash (expected " + hash + ", found " + s1 + "). Deleting it.");
|
| 208 | FileUtils.deleteQuietly(file1);
|
| 209 | }
|
| 210 | catch (IOException ioexception)
|
| 211 | {
|
| 212 | logger.warn((String)("File " + file1 + " couldn\'t be hashed. Deleting it."), (Throwable)ioexception);
|
| 213 | FileUtils.deleteQuietly(file1);
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | this.deleteOldServerResourcesPacks();
|
| 218 | final GuiScreenWorking guiscreenworking = new GuiScreenWorking();
|
| 219 | Map<String, String> map = Minecraft.getSessionInfo();
|
| 220 | final Minecraft minecraft = Minecraft.getMinecraft();
|
| 221 | Futures.getUnchecked(minecraft.addScheduledTask(new Runnable()
|
| 222 | {
|
| 223 | public void run()
|
| 224 | {
|
| 225 | minecraft.displayGuiScreen(guiscreenworking);
|
| 226 | }
|
| 227 | }));
|
| 228 | final SettableFuture<Object> settablefuture = SettableFuture.<Object>create();
|
| 229 | this.downloadingPacks = HttpUtil.downloadResourcePack(file1, url, map, 52428800, guiscreenworking, minecraft.getProxy());
|
| 230 | Futures.addCallback(this.downloadingPacks, new FutureCallback<Object>()
|
| 231 | {
|
| 232 | public void onSuccess(Object p_onSuccess_1_)
|
| 233 | {
|
| 234 | ResourcePackRepository.this.setResourcePackInstance(file1);
|
| 235 | settablefuture.set((Object)null);
|
| 236 | }
|
| 237 | public void onFailure(Throwable p_onFailure_1_)
|
| 238 | {
|
| 239 | settablefuture.setException(p_onFailure_1_);
|
| 240 | }
|
| 241 | });
|
| 242 | ListenableFuture listenablefuture = this.downloadingPacks;
|
| 243 | return listenablefuture;
|
| 244 | }
|
| 245 | finally
|
| 246 | {
|
| 247 | this.lock.unlock();
|
| 248 | }
|
| 249 | }
|
| 250 |
|
| 251 | /**
|
| 252 | * Keep only the 10 most recent resources packs, delete the others
|
| 253 | */
|
| 254 | private void deleteOldServerResourcesPacks()
|
| 255 | {
|
| 256 | List<File> list = Lists.newArrayList(FileUtils.listFiles(this.dirServerResourcepacks, TrueFileFilter.TRUE, (IOFileFilter)null));
|
| 257 | Collections.sort(list, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
|
| 258 | int i = 0;
|
| 259 |
|
| 260 | for (File file1 : list)
|
| 261 | {
|
| 262 | if (i++ >= 10)
|
| 263 | {
|
| 264 | logger.info("Deleting old server resource pack " + file1.getName());
|
| 265 | FileUtils.deleteQuietly(file1);
|
| 266 | }
|
| 267 | }
|
| 268 | }
|
| 269 |
|
| 270 | public ListenableFuture<Object> setResourcePackInstance(File resourceFile)
|
| 271 | {
|
| 272 | this.resourcePackInstance = new FileResourcePack(resourceFile);
|
| 273 | return Minecraft.getMinecraft().scheduleResourcesRefresh();
|
| 274 | }
|
| 275 |
|
| 276 | /**
|
| 277 | * Getter for the IResourcePack instance associated with this ResourcePackRepository
|
| 278 | */
|
| 279 | public IResourcePack getResourcePackInstance()
|
| 280 | {
|
| 281 | return this.resourcePackInstance;
|
| 282 | }
|
| 283 |
|
| 284 | public void clearResourcePack()
|
| 285 | {
|
| 286 | this.lock.lock();
|
| 287 |
|
| 288 | try
|
| 289 | {
|
| 290 | if (this.downloadingPacks != null)
|
| 291 | {
|
| 292 | this.downloadingPacks.cancel(true);
|
| 293 | }
|
| 294 |
|
| 295 | this.downloadingPacks = null;
|
| 296 |
|
| 297 | if (this.resourcePackInstance != null)
|
| 298 | {
|
| 299 | this.resourcePackInstance = null;
|
| 300 | Minecraft.getMinecraft().scheduleResourcesRefresh();
|
| 301 | }
|
| 302 | }
|
| 303 | finally
|
| 304 | {
|
| 305 | this.lock.unlock();
|
| 306 | }
|
| 307 | }
|
| 308 |
|
| 309 | public class Entry
|
| 310 | {
|
| 311 | private final File resourcePackFile;
|
| 312 | private IResourcePack reResourcePack;
|
| 313 | private PackMetadataSection rePackMetadataSection;
|
| 314 | private BufferedImage texturePackIcon;
|
| 315 | private ResourceLocation locationTexturePackIcon;
|
| 316 |
|
| 317 | private Entry(File resourcePackFileIn)
|
| 318 | {
|
| 319 | this.resourcePackFile = resourcePackFileIn;
|
| 320 | }
|
| 321 |
|
| 322 | public void updateResourcePack() throws IOException
|
| 323 | {
|
| 324 | this.reResourcePack = (IResourcePack)(this.resourcePackFile.isDirectory() ? new FolderResourcePack(this.resourcePackFile) : new FileResourcePack(this.resourcePackFile));
|
| 325 | this.rePackMetadataSection = (PackMetadataSection)this.reResourcePack.getPackMetadata(ResourcePackRepository.this.rprMetadataSerializer, "pack");
|
| 326 |
|
| 327 | try
|
| 328 | {
|
| 329 | this.texturePackIcon = this.reResourcePack.getPackImage();
|
| 330 | }
|
| 331 | catch (IOException var2)
|
| 332 | {
|
| 333 | ;
|
| 334 | }
|
| 335 |
|
| 336 | if (this.texturePackIcon == null)
|
| 337 | {
|
| 338 | this.texturePackIcon = ResourcePackRepository.this.rprDefaultResourcePack.getPackImage();
|
| 339 | }
|
| 340 |
|
| 341 | this.closeResourcePack();
|
| 342 | }
|
| 343 |
|
| 344 | public void bindTexturePackIcon(TextureManager textureManagerIn)
|
| 345 | {
|
| 346 | if (this.locationTexturePackIcon == null)
|
| 347 | {
|
| 348 | this.locationTexturePackIcon = textureManagerIn.getDynamicTextureLocation("texturepackicon", new DynamicTexture(this.texturePackIcon));
|
| 349 | }
|
| 350 |
|
| 351 | textureManagerIn.bindTexture(this.locationTexturePackIcon);
|
| 352 | }
|
| 353 |
|
| 354 | public void closeResourcePack()
|
| 355 | {
|
| 356 | if (this.reResourcePack instanceof Closeable)
|
| 357 | {
|
| 358 | IOUtils.closeQuietly((Closeable)this.reResourcePack);
|
| 359 | }
|
| 360 | }
|
| 361 |
|
| 362 | public IResourcePack getResourcePack()
|
| 363 | {
|
| 364 | return this.reResourcePack;
|
| 365 | }
|
| 366 |
|
| 367 | public String getResourcePackName()
|
| 368 | {
|
| 369 | return this.reResourcePack.getPackName();
|
| 370 | }
|
| 371 |
|
| 372 | public String getTexturePackDescription()
|
| 373 | {
|
| 374 | return this.rePackMetadataSection == null ? EnumChatFormatting.RED + "Invalid pack.mcmeta (or missing \'pack\' section)" : this.rePackMetadataSection.getPackDescription().getFormattedText();
|
| 375 | }
|
| 376 |
|
| 377 | public int func_183027_f()
|
| 378 | {
|
| 379 | return this.rePackMetadataSection.getPackFormat();
|
| 380 | }
|
| 381 |
|
| 382 | public boolean equals(Object p_equals_1_)
|
| 383 | {
|
| 384 | return this == p_equals_1_ ? true : (p_equals_1_ instanceof ResourcePackRepository.Entry ? this.toString().equals(p_equals_1_.toString()) : false);
|
| 385 | }
|
| 386 |
|
| 387 | public int hashCode()
|
| 388 | {
|
| 389 | return this.toString().hashCode();
|
| 390 | }
|
| 391 |
|
| 392 | public String toString()
|
| 393 | {
|
| 394 | return String.format("%s:%s:%d", new Object[] {this.resourcePackFile.getName(), this.resourcePackFile.isDirectory() ? "folder" : "zip", Long.valueOf(this.resourcePackFile.lastModified())});
|
| 395 | }
|
| 396 | }
|
| 397 | }
|
| 398 |
|