-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
PatchLauncher.java
121 lines (110 loc) · 4.06 KB
/
PatchLauncher.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
package nallar.tickthreading.patcher;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.util.*;
public class PatchLauncher {
public static void main(String[] args) {
try {
run(args);
} catch (Throwable t) {
t.printStackTrace(System.err);
}
}
public static String[] startupArgs;
private static final String serverJarArgument = "--serverjar=";
private static void run(String[] args) throws Exception {
ClassLoader classLoader = PatchLauncher.class.getClassLoader();
String loc = null;
ArrayList<String> argsList = new ArrayList<String>(Arrays.asList(args));
for (Iterator<String> i$ = argsList.iterator(); i$.hasNext(); ) {
String arg = i$.next();
if (arg.toLowerCase().startsWith(serverJarArgument)) {
loc = arg.substring(serverJarArgument.length());
i$.remove();
break;
}
}
startupArgs = args = argsList.toArray(new String[argsList.size()]);
if (System.getProperty("tickthreading.launcherWaitForKeyPress") != null) {
System.out.println("Waiting for enter key press to continue;");
new Scanner(System.in).nextLine();
}
if (System.getProperty("tickthreading.debug") == null) {
System.out.println("THIS IS AN UNSTABLE BUILD OF TICKTHREADING.");
System.out.println("It may cause world corruption, exceptions everywhere or even blow up your server.");
Thread.sleep(5000);
}
addLibraries((URLClassLoader) classLoader, loc);
Class<?> launchwrapper;
try {
launchwrapper = Class.forName("net.minecraft.launchwrapper.Launch", true, classLoader);
System.out.println(String.valueOf(launchwrapper.getClassLoader()));
Class.forName("org.objectweb.asm.Type", true, classLoader);
Method main = launchwrapper.getMethod("main", String[].class);
String[] allArgs = new String[args.length + 2];
allArgs[0] = "--tweakClass";
allArgs[1] = "cpw.mods.fml.common.launcher.FMLServerTweaker";
System.arraycopy(args, 0, allArgs, 2, args.length);
main.invoke(null, (Object) allArgs);
} catch (ClassNotFoundException e) {
System.err.println(e.toString());
System.exit(1);
} catch (Throwable t) {
System.err.println("A problem occurred running the Server launcher.");
t.printStackTrace(System.err);
System.exit(1);
}
}
private static void addLibraries(URLClassLoader classLoader, String loc) {
if (loc == null) {
System.err.println("You have not specified a server jar");
System.err.println("Please add --serverJar=<minecraft/forge/mcpc jar name here> at the end of your java arguments.");
System.err.println("Example: java -Xmx=2G -XX:MaxPermSize=256m -XX:+AgressiveOpts -jar TT.jar --serverJar=mcpc953.jar");
System.exit(1);
}
File locFile = new File(loc);
try {
locFile = locFile.getCanonicalFile();
} catch (IOException e) {
e.printStackTrace(System.err);
}
if (locFile.exists()) {
System.out.println("Adding specified server jar: " + loc + " @ " + locFile + " to libraries.");
addPathToClassLoader(locFile, classLoader);
} else {
System.err.println("Could not find specified server jar: " + loc + " @ " + locFile);
System.exit(1);
}
File libraries = new File("libraries");
addLibraries(classLoader, libraries);
}
private static void addLibraries(URLClassLoader classLoader, File file) {
File[] files = file.listFiles();
if (files != null) {
for (File inner : files) {
addLibraries(classLoader, inner);
}
}
addPathToClassLoader(file, classLoader);
}
private static final Method addUrlMethod = getAddURLMethod();
private static Method getAddURLMethod() {
Method method;
try {
method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
method.setAccessible(true);
return method;
}
public static void addPathToClassLoader(File path, URLClassLoader classLoader) {
try {
URL u = path.toURI().toURL();
addUrlMethod.invoke(classLoader, u);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
}