-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
PatchRelaunchClassLoader.java
309 lines (288 loc) · 10.2 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package me.nallar.patched.forge;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
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.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import cpw.mods.fml.relauncher.FMLRelaunchLog;
import cpw.mods.fml.relauncher.IClassTransformer;
import cpw.mods.fml.relauncher.RelaunchClassLoader;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.patcher.Declare;
import me.nallar.unsafe.UnsafeUtil;
import sun.misc.URLClassPath;
public abstract class PatchRelaunchClassLoader extends RelaunchClassLoader {
@Declare
public static int patchedClasses_;
@Declare
public static int usedPatchedClasses_;
private static volatile Map<String, byte[]> replacedClasses;
private File minecraftdir;
private File patchedModsFolder;
private URLClassPath ucp;
private static PrintStream err;
private Set<String> patchedURLs;
private ThreadLocal<byte[]> buffer;
private static void log(Level level, Throwable t, String message) {
try {
FMLRelaunchLog.log(level, t, message);
} catch (Throwable t_) {
t_.printStackTrace(err);
}
}
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);
}
}
@SuppressWarnings ("ZeroLengthArrayAllocation")
public static void staticConstruct() {
EMPTY_BYTE_ARRAY = new byte[0];
err = new PrintStream(new FileOutputStream(FileDescriptor.err));
}
public void construct() {
try {
buffer = new ThreadLocal<byte[]>();
patchedURLs = new HashSet<String>();
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 = new File(".").getAbsoluteFile();
FMLRelaunchLog.info("This server is patched with @MOD_NAME@ v@MOD_VERSION@ for MC@MC_VERSION@");
log(Level.INFO, null, "Searching for patched mods in minecraft dir " + minecraftdir);
patchedModsFolder = new File(minecraftdir, "patchedMods");
if (!patchedModsFolder.exists()) {
minecraftdir = locationOf(net.minecraft.util.Tuple.class).getParentFile();
log(Level.INFO, null, "Searching for patched mods in minecraft dir " + minecraftdir);
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 = new URL(file.toURI().toURL().toString().replace("/./", "/"));
if (!sources.contains(toAdd)) {
sources.add(toAdd);
ucp.addURL(toAdd);
}
log(Level.INFO, null, "Added tickthreading jar " + file + " to classpath");
foundTT = true;
}
}
for (URL url : ucp.getURLs()) {
if (url.toString().contains("patchedMods")) {
log(Level.WARNING, null, url.toString().replace("%", "%%") + " is in the classpath, but it appears to be in the patchedMods directory");
}
}
if (!foundTT) {
log(Level.SEVERE, null, "Failed to find TT jar in mods folder - make sure it has 'tickthreading' in its name!");
}
} catch (Throwable t) {
log(Level.SEVERE, t, "Failed to initialise RelaunchClassLoader");
}
}
@Override
public void addURL(URL url) {
if (url.toString().contains("patchedMods")) {
log(Level.WARNING, null, "Adding " + url.toString().replace("%", "%%") + " to classpath when it appears to be in the patchedMods directory");
}
if (sources.contains(url)) {
log(Level.WARNING, null, "Attempted to add " + url.toString().replace("%", "%%") + " to classpath twice");
return;
}
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()));
if (!patchedURLs.add(patchedModFile.getAbsolutePath())) {
return;
}
} catch (Exception e) {
return;
}
try {
if (patchedModFile.exists()) {
ZipFile zipFile = new ZipFile(patchedModFile);
try {
Enumeration<ZipEntry> zipEntryEnumeration = (Enumeration<ZipEntry>) zipFile.entries();
int patchedClasses = 0;
while (zipEntryEnumeration.hasMoreElements()) {
ZipEntry zipEntry = zipEntryEnumeration.nextElement();
String name = zipEntry.getName();
if (!name.toLowerCase().endsWith(".class") || replacedClasses.containsKey(name)) {
continue;
}
patchedClasses++;
byte[] contents = readFully(zipFile.getInputStream(zipEntry));
replacedClasses.put(name, contents);
}
RelaunchClassLoader.patchedClasses += patchedClasses;
log(Level.INFO, null, "Loaded " + patchedClasses + " patched classes for " + patchedModFile.getName());
} finally {
zipFile.close();
}
}
} catch (Exception e) {
log(Level.SEVERE, e, "Failed to load patched classes for " + patchedModFile.getName());
}
}
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);
}
if (patchedModsFolder.isDirectory()) {
for (File file_ : patchedModsFolder.listFiles()) {
loadPatchedClasses(file_.toURI().toURL(), replacedClasses);
}
}
PatchRelaunchClassLoader.replacedClasses = replacedClasses;
}
}
}
if (replacedClasses == null) {
return null;
}
byte[] data = replacedClasses.get(file);
if (data != null) {
usedPatchedClasses++;
}
return data;
} catch (Throwable t) {
log(Level.SEVERE, t, "Exception getting patched class for " + file);
}
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) {
FMLRelaunchLog.finest("Failed to find class resource %s", name.replace('.', '/') + ".class");
}
return null;
}
classStream = classResource.openStream();
if (DEBUG_CLASSLOADING) {
FMLRelaunchLog.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) {
FMLRelaunchLog.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 && oldClass != null) {
basicClass = oldClass;
FMLRelaunchLog.severe(transformer.getClass() + " returned a null class during transformation, ignoring.");
}
} catch (Throwable throwable) {
String message = throwable.getMessage();
if ((message == null ? "" : message).contains("for invalid side")) {
invalidClasses.add(name);
throw UnsafeUtil.throwIgnoreChecked(throwable);
} else if (basicClass != null || DEBUG_CLASSLOADING) {
FMLRelaunchLog.log((DEBUG_CLASSLOADING && basicClass != null) ? Level.WARNING : Level.FINE, throwable, "Failed to transform " + name);
}
}
}
return basicClass;
}
private static byte[] EMPTY_BYTE_ARRAY;
@Override
protected byte[] readFully(InputStream stream) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream(stream.available());
int readBytes;
byte[] buffer = this.buffer.get();
if (buffer == null) {
buffer = new byte[1048576];
this.buffer.set(buffer);
}
while ((readBytes = stream.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, readBytes);
}
return bos.toByteArray();
} catch (Throwable t) {
FMLRelaunchLog.log(Level.SEVERE, t, "Problem loading class");
return EMPTY_BYTE_ARRAY;
}
}
}