-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
PatchRelaunchClassLoader.java
216 lines (203 loc) · 6.8 KB
/
PatchRelaunchClassLoader.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package me.nallar.patched;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.relauncher.IClassTransformer;
import cpw.mods.fml.relauncher.RelaunchClassLoader;
import me.nallar.tickthreading.Log;
import sun.misc.URLClassPath;
public abstract class PatchRelaunchClassLoader extends RelaunchClassLoader {
private static volatile Map<String, byte[]> replacedClasses;
private File minecraftdir;
private File patchedModsFolder;
private URLClassPath ucp;
public PatchRelaunchClassLoader(URL[] sources) {
super(sources);
}
private static File locationOf(Class clazz) {
String path = clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
path = path.contains("!") ? path.substring(0, path.lastIndexOf('!')) : path;
if (!path.isEmpty() && path.charAt(0) == '/') {
path = "file:" + path;
}
try {
return new File(new URL(path).toURI());
} catch (Exception e) {
Log.severe("", e);
return new File(path);
}
}
public void construct() {
try {
Field field = URLClassLoader.class.getDeclaredField("ucp");
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, (field.getModifiers() & ~Modifier.FINAL) & ~Modifier.PRIVATE);
ucp = (URLClassPath) field.get(this);
minecraftdir = locationOf(net.minecraft.util.Tuple.class).getParentFile();
patchedModsFolder = new File(minecraftdir, "patchedMods");
boolean foundTT = false;
for (File file : new File(minecraftdir, "mods").listFiles()) {
if (file.getName().toLowerCase().contains("tickthreading") && file.getName().endsWith(".jar")) {
URL toAdd = file.toURI().toURL();
if (!sources.contains(toAdd)) {
sources.add(toAdd);
ucp.addURL(toAdd);
}
foundTT = true;
}
}
if (!foundTT) {
System.err.println("Failed to find TT jar in mods folder - make sure it has 'tickthreading' in its name!");
}
} catch (Throwable t) {
t.printStackTrace();
}
}
@Override
public void addURL(URL url) {
if (sources.contains(url)) {
FMLLog.warning("Added " + url.toString().replace("%", "%%") + " to classpath twice");
}
ucp.addURL(url);
sources.add(url);
if (replacedClasses == null) {
getReplacementClassBytes("");
}
synchronized (RelaunchClassLoader.class) {
loadPatchedClasses(url, replacedClasses);
}
}
private void loadPatchedClasses(URL url_, Map<String, byte[]> replacedClasses) {
if (replacedClasses == null) {
return;
}
String url = url_.toString();
File patchedModFile;
try {
patchedModFile = new File(patchedModsFolder, url.substring(url.lastIndexOf('/') + 1, url.length()));
} catch (Exception e) {
return;
}
try {
if (patchedModFile.exists()) {
ZipFile zipFile = new ZipFile(patchedModFile);
try {
Enumeration<ZipEntry> zipEntryEnumeration = (Enumeration<ZipEntry>) zipFile.entries();
while (zipEntryEnumeration.hasMoreElements()) {
ZipEntry zipEntry = zipEntryEnumeration.nextElement();
String name = zipEntry.getName();
if (!name.toLowerCase().endsWith(".class") || replacedClasses.containsKey(name)) {
continue;
}
byte[] contents = readFully(zipFile.getInputStream(zipEntry));
replacedClasses.put(name, contents);
}
} finally {
zipFile.close();
}
}
} catch (Exception e) {
System.out.println("Failed to load patched classes for " + patchedModFile.getName());
e.printStackTrace();
}
}
private byte[] getReplacementClassBytes(String file) {
try {
Map<String, byte[]> replacedClasses = PatchRelaunchClassLoader.replacedClasses;
if (replacedClasses == null) {
synchronized (RelaunchClassLoader.class) {
replacedClasses = PatchRelaunchClassLoader.replacedClasses;
if (replacedClasses == null && minecraftdir != null) {
replacedClasses = new ConcurrentHashMap<String, byte[]>();
for (URL url_ : sources) {
loadPatchedClasses(url_, replacedClasses);
}
PatchRelaunchClassLoader.replacedClasses = replacedClasses;
}
}
}
return replacedClasses == null ? null : replacedClasses.get(file);
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
@Override
public byte[] getClassBytes(String name) throws IOException {
byte[] data;
if (name.indexOf('.') == -1) {
for (String res : RESERVED) {
if (name.toUpperCase(Locale.ENGLISH).startsWith(res)) {
data = getClassBytes('_' + name);
if (data != null) {
return data;
}
}
}
}
InputStream classStream = null;
try {
URL classResource = findResource(name.replace('.', '/') + ".class");
if (classResource == null) {
if (DEBUG_CLASSLOADING) {
FMLLog.finest("Failed to find class resource %s", name.replace('.', '/') + ".class");
}
return null;
}
classStream = classResource.openStream();
if (DEBUG_CLASSLOADING) {
FMLLog.finest("Loading class %s from resource %s", name, classResource.toString());
}
data = readFully(classStream);
byte[] data2 = getReplacementClassBytes(name.replace('.', '/') + ".class");
return data2 == null ? data : data2;
} finally {
if (classStream != null) {
try {
classStream.close();
} catch (IOException ignored) {
}
}
}
}
@Override
protected byte[] runTransformers(String name, byte[] basicClass) {
if (basicClass == null) {
if (DEBUG_CLASSLOADING) {
FMLLog.log(DEBUG_CLASSLOADING ? Level.WARNING : Level.FINE, "Could not find the class " + name + ". This is not necessarily an issue.");
}
}
for (IClassTransformer transformer : transformers) {
try {
byte[] oldClass = basicClass;
basicClass = transformer.transform(name, basicClass);
if (basicClass == null) {
basicClass = oldClass;
FMLLog.severe(transformer.getClass() + " returned a null class during transformation, ignoring.");
}
} catch (Throwable throwable) {
if (throwable.getMessage().contains("for invalid side")) {
invalidClasses.add(name);
throw (RuntimeException) throwable;
} else if (basicClass != null || DEBUG_CLASSLOADING) {
FMLLog.log((DEBUG_CLASSLOADING && basicClass != null) ? Level.WARNING : Level.FINE, throwable, "Failed to transform " + name);
}
}
}
return basicClass;
}
}