-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
PatchUtil.java
66 lines (59 loc) · 2.62 KB
/
PatchUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package nallar.tickthreading.util;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import com.google.common.io.Files;
import nallar.tickthreading.Log;
import nallar.tickthreading.minecraft.TickThreading;
import nallar.tickthreading.patcher.PatchMain;
import nallar.tickthreading.patcher.PatchManager;
import nallar.tickthreading.patcher.Patches;
import sun.tools.jar.resources.jar;
public enum PatchUtil {
;
private static boolean written = false;
public static synchronized void writePatchRunners() throws IOException {
if (written) {
return;
}
written = true;
String java = System.getProperties().getProperty("java.home") + File.separator + "bin" + File.separator + "java";
String CP = LocationUtil.locationOf(TickThreading.class).toString();
String MS = CollectionsUtil.join(LocationUtil.getJarLocations());
ZipFile zipFile = new ZipFile(new File(CP));
try {
CP += File.pathSeparator + new File(LocationUtil.getServerDirectory(), "lib/guava-14.0-rc3.jar") + File.pathSeparator + new File(LocationUtil.getServerDirectory(), "lib/asm-all-4.1.jar");
for (ZipEntry zipEntry : new IterableEnumerationWrapper<ZipEntry>((Enumeration<ZipEntry>) zipFile.entries())) {
if (zipEntry.getName().startsWith("patchrun/") && !(!zipEntry.getName().isEmpty() && zipEntry.getName().charAt(zipEntry.getName().length() - 1) == '/')) {
String data = new Scanner(zipFile.getInputStream(zipEntry), "UTF-8").useDelimiter("\\A").next();
data = data.replace("%JAVA%", java).replace("%CP%", CP).replace("%MS%", MS).replace("\r\n", "\n");
Files.write(data.getBytes("UTF-8"), new File(LocationUtil.getServerDirectory(), zipEntry.getName().replace("patchrun/", "")));
}
}
} finally {
zipFile.close();
}
}
public static boolean shouldPatch(Iterable<File> files) {
try {
PatchManager patchManager = new PatchManager(PatchMain.class.getResourceAsStream("/patches.xml"), Patches.class);
ArrayList<File> fileList = new ArrayList<File>();
for (File file : files) {
fileList.add(file.getAbsoluteFile());
}
patchManager.classRegistry.loadFiles(fileList);
patchManager.classRegistry.closeClassPath();
patchManager.classRegistry.loadPatchHashes(patchManager);
boolean result = patchManager.classRegistry.shouldPatch();
patchManager.classRegistry.clearClassInfo();
return result;
} catch (Exception e) {
Log.severe("Failed to determine whether patches should run", e);
}
return false;
}
}