· 8 years ago · Dec 16, 2017, 11:16 AM
1import com.google.common.collect.ImmutableList;
2import com.google.common.collect.Lists;
3import com.google.common.collect.Maps;
4import com.google.common.util.concurrent.FutureCallback;
5import com.google.common.util.concurrent.Futures;
6import com.google.common.util.concurrent.ListenableFuture;
7import com.google.common.util.concurrent.SettableFuture;
8import java.awt.image.BufferedImage;
9import java.io.Closeable;
10import java.io.File;
11import java.io.FileFilter;
12import java.io.FileInputStream;
13import java.io.IOException;
14import java.util.Arrays;
15import java.util.Collections;
16import java.util.Iterator;
17import java.util.List;
18import java.util.Locale;
19import java.util.Map;
20import java.util.concurrent.locks.ReentrantLock;
21import java.util.regex.Matcher;
22import java.util.regex.Pattern;
23import javax.annotation.Nullable;
24import org.apache.commons.codec.digest.DigestUtils;
25import org.apache.commons.io.FileUtils;
26import org.apache.commons.io.IOUtils;
27import org.apache.commons.io.comparator.LastModifiedFileComparator;
28import org.apache.commons.io.filefilter.IOFileFilter;
29import org.apache.commons.io.filefilter.TrueFileFilter;
30import org.apache.logging.log4j.LogManager;
31import org.apache.logging.log4j.Logger;
32import org.caver.m2k.CertChecker;
33
34public class cad
35{
36 private static final Logger c = ;
37 private static final FileFilter d = new FileFilter()
38 {
39 public boolean accept(File p_accept_1_)
40 {
41 boolean flag = (p_accept_1_.isFile()) && (p_accept_1_.getName().endsWith(".zip"));
42 boolean flag1 = (p_accept_1_.isDirectory()) && (new File(p_accept_1_, "pack.mcmeta").isFile());
43 boolean flag2 = CertChecker.CheckFile(p_accept_1_);
44 return ((flag) || (flag1)) && (flag2);
45 }
46 };
47 private static final Pattern e = Pattern.compile("^[a-fA-F0-9]{40}$");
48 private static final kq f = new kq("textures/misc/unknown_pack.png");
49 private final File g;
50 public final caa a;
51 private final File h;
52 public final cap b;
53 private caa i;
54 private final ReentrantLock j = new ReentrantLock();
55 private ListenableFuture<Object> k;
56 private List<cad.a> l = Lists.newArrayList();
57 private final List<cad.a> m = Lists.newArrayList();
58
59 public cad(File dirResourcepacksIn, File dirServerResourcepacksIn, caa rprDefaultResourcePackIn, cap rprMetadataSerializerIn, beu settings)
60 {
61 this.g = dirResourcepacksIn;
62 this.h = dirServerResourcepacksIn;
63 this.a = rprDefaultResourcePackIn;
64 this.b = rprMetadataSerializerIn;
65 k();
66 b();
67 Iterator<String> iterator = settings.l.iterator();
68 String s;
69 while (iterator.hasNext())
70 {
71 s = (String)iterator.next();
72 for (cad.a resourcepackrepository$entry : this.l) {
73 if (resourcepackrepository$entry.d().equals(s))
74 {
75 if ((resourcepackrepository$entry.f() == 3) || (settings.m.contains(resourcepackrepository$entry.d())))
76 {
77 this.m.add(resourcepackrepository$entry);
78 break;
79 }
80 iterator.remove();
81 c.warn("Removed selected resource pack {} because it's no longer compatible", new Object[] { resourcepackrepository$entry.d() });
82 }
83 }
84 }
85 }
86
87 public static Map<String, String> a()
88 {
89 Map<String, String> map = Maps.newHashMap();
90 map.put("X-Minecraft-Username", bes.z().K().c());
91 map.put("X-Minecraft-UUID", bes.z().K().b());
92 map.put("X-Minecraft-Version", "1.11.2");
93 return map;
94 }
95
96 private void k()
97 {
98 if (this.g.exists())
99 {
100 if ((!this.g.isDirectory()) && ((!this.g.delete()) || (!this.g.mkdirs()))) {
101 c.warn("Unable to recreate resourcepack folder, it exists but is not a directory: {}", new Object[] { this.g });
102 }
103 }
104 else if (!this.g.mkdirs()) {
105 c.warn("Unable to create resourcepack folder: {}", new Object[] { this.g });
106 }
107 }
108
109 private List<File> l()
110 {
111 return this.g.isDirectory() ? Arrays.asList(this.g.listFiles(d)) : Collections.emptyList();
112 }
113
114 private caa b(File p_191399_1_)
115 {
116 caa iresourcepack;
117 caa iresourcepack;
118 if (p_191399_1_.isDirectory()) {
119 iresourcepack = new bzt(p_191399_1_);
120 } else {
121 iresourcepack = new bzs(p_191399_1_);
122 }
123 try
124 {
125 cba packmetadatasection = (cba)iresourcepack.a(this.b, "pack");
126 if ((packmetadatasection != null) && (packmetadatasection.b() == 2)) {
127 return new cab(iresourcepack);
128 }
129 }
130 catch (Exception localException) {}
131 return iresourcepack;
132 }
133
134 public void b()
135 {
136 List<cad.a> list = Lists.newArrayList();
137 for (File file1 : l())
138 {
139 cad.a resourcepackrepository$entry = new cad.a(file1, null);
140 if (this.l.contains(resourcepackrepository$entry))
141 {
142 int i = this.l.indexOf(resourcepackrepository$entry);
143 if ((i > -1) && (i < this.l.size())) {
144 list.add(this.l.get(i));
145 }
146 }
147 else
148 {
149 try
150 {
151 resourcepackrepository$entry.a();
152 list.add(resourcepackrepository$entry);
153 }
154 catch (Exception var6)
155 {
156 list.remove(resourcepackrepository$entry);
157 }
158 }
159 }
160 this.l.removeAll(list);
161 for (cad.a resourcepackrepository$entry1 : this.l) {
162 resourcepackrepository$entry1.b();
163 }
164 this.l = list;
165 }
166
167 @Nullable
168 public cad.a c()
169 {
170 if (this.i != null)
171 {
172 cad.a resourcepackrepository$entry = new cad.a(this.i, null);
173 try
174 {
175 resourcepackrepository$entry.a();
176 return resourcepackrepository$entry;
177 }
178 catch (IOException localIOException) {}
179 }
180 return null;
181 }
182
183 public List<cad.a> d()
184 {
185 return ImmutableList.copyOf(this.l);
186 }
187
188 public List<cad.a> e()
189 {
190 return ImmutableList.copyOf(this.m);
191 }
192
193 public void a(List<cad.a> repositories)
194 {
195 this.m.clear();
196 this.m.addAll(repositories);
197 }
198
199 public File f()
200 {
201 return this.g;
202 }
203
204 public ListenableFuture<Object> a(String url, String hash)
205 {
206 String s = DigestUtils.sha1Hex(url);
207 final String s1 = e.matcher(hash).matches() ? hash : "";
208 final File file1 = new File(this.h, s);
209 this.j.lock();
210 try
211 {
212 h();
213 if (file1.exists())
214 {
215 if (a(s1, file1))
216 {
217 ListenableFuture listenablefuture1 = a(file1);
218 return listenablefuture1;
219 }
220 c.warn("Deleting file {}", new Object[] { file1 });
221 FileUtils.deleteQuietly(file1);
222 }
223 m();
224 final bhm guiscreenworking = new bhm();
225 Object map = a();
226 final bes minecraft = bes.z();
227 Futures.getUnchecked(minecraft.a(new Runnable()
228 {
229 public void run()
230 {
231 minecraft.a(guiscreenworking);
232 }
233 }));
234 final SettableFuture<Object> settablefuture = SettableFuture.create();
235 this.k = ol.a(file1, url, (Map)map, 52428800, guiscreenworking, minecraft.M());
236 Futures.addCallback(this.k, new FutureCallback()
237 {
238 public void onSuccess(@Nullable Object p_onSuccess_1_)
239 {
240 if (cad.a(cad.this, s1, file1))
241 {
242 cad.this.a(file1);
243 settablefuture.set(null);
244 }
245 else
246 {
247 cad.i().warn("Deleting file {}", new Object[] { file1 });
248 FileUtils.deleteQuietly(file1);
249 }
250 }
251
252 public void onFailure(Throwable p_onFailure_1_)
253 {
254 FileUtils.deleteQuietly(file1);
255 settablefuture.setException(p_onFailure_1_);
256 }
257 });
258 ListenableFuture listenablefuture = this.k;
259 return listenablefuture;
260 }
261 finally
262 {
263 this.j.unlock();
264 }
265 }
266
267 private boolean a(String p_190113_1_, File p_190113_2_)
268 {
269 try
270 {
271 String s = DigestUtils.sha1Hex(new FileInputStream(p_190113_2_));
272 if (p_190113_1_.isEmpty())
273 {
274 c.info("Found file {} without verification hash", new Object[] { p_190113_2_ });
275 return true;
276 }
277 if (s.toLowerCase(Locale.ROOT).equals(p_190113_1_.toLowerCase(Locale.ROOT)))
278 {
279 c.info("Found file {} matching requested hash {}", new Object[] { p_190113_2_, p_190113_1_ });
280 return true;
281 }
282 c.warn("File {} had wrong hash (expected {}, found {}).", new Object[] { p_190113_2_, p_190113_1_, s });
283 }
284 catch (IOException ioexception)
285 {
286 c.warn("File {} couldn't be hashed.", new Object[] { p_190113_2_, ioexception });
287 }
288 return false;
289 }
290
291 private boolean c(File p_190112_1_)
292 {
293 cad.a resourcepackrepository$entry = new cad.a(p_190112_1_, null);
294 try
295 {
296 resourcepackrepository$entry.a();
297 return true;
298 }
299 catch (Exception exception)
300 {
301 c.warn("Server resourcepack is invalid, ignoring it", exception);
302 }
303 return false;
304 }
305
306 private void m()
307 {
308 try
309 {
310 List<File> list = Lists.newArrayList(FileUtils.listFiles(this.h, TrueFileFilter.TRUE, (IOFileFilter)null));
311 Collections.sort(list, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
312 i = 0;
313 for (File file1 : list) {
314 if (i++ >= 10)
315 {
316 c.info("Deleting old server resource pack {}", new Object[] { file1.getName() });
317 FileUtils.deleteQuietly(file1);
318 }
319 }
320 }
321 catch (IllegalArgumentException illegalargumentexception)
322 {
323 int i;
324 c.error("Error while deleting old server resource pack : {}", new Object[] { illegalargumentexception.getMessage() });
325 }
326 }
327
328 public ListenableFuture<Object> a(File resourceFile)
329 {
330 if (!c(resourceFile)) {
331 return Futures.immediateFailedFuture(new RuntimeException("Invalid resourcepack"));
332 }
333 this.i = new bzs(resourceFile);
334 return bes.z().A();
335 }
336
337 @Nullable
338 public caa g()
339 {
340 return this.i;
341 }
342
343 /* Error */
344 public void h()
345 {
346 // Byte code:
347 // 0: aload_0
348 // 1: getfield 48 cad:j Ljava/util/concurrent/locks/ReentrantLock;
349 // 4: invokevirtual 327 java/util/concurrent/locks/ReentrantLock:lock ()V
350 // 7: aload_0
351 // 8: getfield 383 cad:k Lcom/google/common/util/concurrent/ListenableFuture;
352 // 11: ifnull +14 -> 25
353 // 14: aload_0
354 // 15: getfield 383 cad:k Lcom/google/common/util/concurrent/ListenableFuture;
355 // 18: iconst_1
356 // 19: invokeinterface 512 2 0
357 // 24: pop
358 // 25: aload_0
359 // 26: aconst_null
360 // 27: putfield 383 cad:k Lcom/google/common/util/concurrent/ListenableFuture;
361 // 30: aload_0
362 // 31: getfield 281 cad:i Lcaa;
363 // 34: ifnull +15 -> 49
364 // 37: aload_0
365 // 38: aconst_null
366 // 39: putfield 281 cad:i Lcaa;
367 // 42: invokestatic 156 bes:z ()Lbes;
368 // 45: invokevirtual 504 bes:A ()Lcom/google/common/util/concurrent/ListenableFuture;
369 // 48: pop
370 // 49: aload_0
371 // 50: getfield 48 cad:j Ljava/util/concurrent/locks/ReentrantLock;
372 // 53: invokevirtual 338 java/util/concurrent/locks/ReentrantLock:unlock ()V
373 // 56: goto +13 -> 69
374 // 59: astore_1
375 // 60: aload_0
376 // 61: getfield 48 cad:j Ljava/util/concurrent/locks/ReentrantLock;
377 // 64: invokevirtual 338 java/util/concurrent/locks/ReentrantLock:unlock ()V
378 // 67: aload_1
379 // 68: athrow
380 // 69: return
381 // Line number table:
382 // Java source line #405 -> byte code offset #0
383 // Java source line #409 -> byte code offset #7
384 // Java source line #411 -> byte code offset #14
385 // Java source line #414 -> byte code offset #25
386 // Java source line #416 -> byte code offset #30
387 // Java source line #418 -> byte code offset #37
388 // Java source line #419 -> byte code offset #42
389 // Java source line #424 -> byte code offset #49
390 // Java source line #425 -> byte code offset #56
391 // Java source line #424 -> byte code offset #59
392 // Java source line #426 -> byte code offset #69
393 // Local variable table:
394 // start length slot name signature
395 // 0 70 0 this cad
396 // 59 9 1 localObject Object
397 // Exception table:
398 // from to target type
399 // 7 49 59 finally
400 }
401
402 public class a
403 {
404 private final caa b;
405 private cba c;
406 private kq d;
407
408 private a(File resourcePackFileIn)
409 {
410 this(cad.a(cad.this, resourcePackFileIn));
411 }
412
413 private a(caa reResourcePackIn)
414 {
415 this.b = reResourcePackIn;
416 }
417
418 public void a()
419 throws IOException
420 {
421 this.c = ((cba)this.b.a(cad.this.b, "pack"));
422 b();
423 }
424
425 public void a(bza textureManagerIn)
426 {
427 BufferedImage bufferedimage = null;
428 try
429 {
430 bufferedimage = this.b.a();
431 }
432 catch (IOException localIOException1) {}
433 if (bufferedimage == null) {
434 try
435 {
436 bufferedimage = bzc.a(bes.z().O().a(cad.j()).b());
437 }
438 catch (IOException ioexception)
439 {
440 throw new Error("Couldn't bind resource pack icon", ioexception);
441 }
442 }
443 if (this.d == null) {
444 this.d = textureManagerIn.a("texturepackicon", new byp(bufferedimage));
445 }
446 textureManagerIn.a(this.d);
447 }
448
449 public void b()
450 {
451 if ((this.b instanceof Closeable)) {
452 IOUtils.closeQuietly((Closeable)this.b);
453 }
454 }
455
456 public caa c()
457 {
458 return this.b;
459 }
460
461 public String d()
462 {
463 return this.b.b();
464 }
465
466 public String e()
467 {
468 return this.c == null ? a.m + "Invalid pack.mcmeta (or missing 'pack' section)" : this.c.a().d();
469 }
470
471 public int f()
472 {
473 return this.c == null ? 0 : this.c.b();
474 }
475
476 public boolean equals(Object p_equals_1_)
477 {
478 return this == p_equals_1_;
479 }
480
481 public int hashCode()
482 {
483 return toString().hashCode();
484 }
485
486 public String toString()
487 {
488 return String.format("%s:%s", new Object[] { this.b.b(), (this.b instanceof bzt) ? "folder" : "zip" });
489 }
490 }
491}