/
LaunchTesting.java
166 lines (143 loc) · 6.72 KB
/
LaunchTesting.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
/*
* Minecraft Forge
* Copyright (c) 2016-2019.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.userdev;
import com.google.common.base.Strings;
import com.mojang.authlib.Agent;
import com.mojang.authlib.UserAuthentication;
import com.mojang.authlib.exceptions.AuthenticationException;
import com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService;
import cpw.mods.modlauncher.Launcher;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.Proxy;
import java.util.Arrays;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager;
public class LaunchTesting
{
public static void main(String... args) throws InterruptedException
{
final String markerselection = System.getProperty("forge.logging.markers", "");
Arrays.stream(markerselection.split(",")).forEach(marker-> System.setProperty("forge.logging.marker."+ marker.toLowerCase(Locale.ROOT), "ACCEPT"));
ArgumentList lst = ArgumentList.from(args);
String target = lst.getOrDefault("launchTarget", System.getenv().get("target"));
if (target == null) {
throw new IllegalArgumentException("Environment variable target must be set.");
}
lst.putLazy("gameDir", ".");
lst.putLazy("launchTarget", target);
lst.putLazy("fml.mcpVersion", System.getenv("MCP_VERSION"));
lst.putLazy("fml.mcVersion", System.getenv("MC_VERSION"));
lst.putLazy("fml.forgeGroup", System.getenv("FORGE_GROUP"));
lst.putLazy("fml.forgeVersion", System.getenv("FORGE_VERSION"));
if (target.contains("client")) {
hackNatives();
lst.putLazy("version", "MOD_DEV");
lst.putLazy("assetIndex", System.getenv("assetIndex"));
lst.putLazy("assetsDir", System.getenv().getOrDefault("assetDirectory", "assets"));
String assets = lst.get("assetsDir");
if (assets == null || !new File(assets).exists()) {
throw new IllegalArgumentException("Environment variable 'assetDirectory' must be set to a valid path.");
}
if (!lst.hasValue("accessToken")) {
if (!login(lst)) {
String username = lst.get("username");
if (username != null) { // Replace '#' placeholders with random numbers
Matcher m = Pattern.compile("#+").matcher(username);
StringBuffer replaced = new StringBuffer();
while (m.find()) {
m.appendReplacement(replaced, getRandomNumbers(m.group().length()));
}
m.appendTail(replaced);
lst.put("username", replaced.toString());
} else {
lst.putLazy("username", "Dev");
}
lst.put("accessToken", "DONT_CRASH");
lst.put("userProperties", "{}");
}
}
}
if (Arrays.asList(
"fmldevclient", "fmldevserver", "fmldevdata",
"fmluserdevclient", "fmluserdevserver", "fmluserdevdata"
).contains(target)) {
//nop
} else {
throw new IllegalArgumentException("Unknown value for 'target' property: " + target);
}
Launcher.main(lst.getArguments());
Thread.sleep(10000);// Why do we have this? -Lex 03/06/19
}
private static String getRandomNumbers(int length)
{ // Generate a time-based random number, to mimic how n.m.client.Main works
return Long.toString(System.nanoTime() % (int) Math.pow(10, length));
}
private static void hackNatives()
{
String paths = System.getProperty("java.library.path");
String nativesDir = System.getenv().get("nativesDirectory");
if (Strings.isNullOrEmpty(paths))
paths = nativesDir;
else
paths += File.pathSeparator + nativesDir;
System.setProperty("java.library.path", paths);
// hack the classloader now.
try
{
final Method initializePathMethod = ClassLoader.class.getDeclaredMethod("initializePath", String.class);
initializePathMethod.setAccessible(true);
final Object usrPathsValue = initializePathMethod.invoke(null, "java.library.path");
final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
usrPathsField.setAccessible(true);
usrPathsField.set(null, usrPathsValue);
}
catch(Throwable t) {}
}
/**
* Basic implementation of Mojang's 'Yggdrasil' login system, purely intended as a dev time bare bones login.
* Login errors are not handled.
* Do not use this unless you know what you are doing and must use it to debug things REQUIRING authentication.
* Forge is not responsible for any auth information passed in, saved to logs, run configs, etc...
* BE CAREFUL WITH YOUR LOGIN INFO
*/
private static boolean login(ArgumentList args) {
if (!args.hasValue("username") || !args.hasValue("password")) {
args.remove("password"); //Just in case, so it shouldn't show up anywhere.
return false;
}
UserAuthentication auth = new YggdrasilAuthenticationService(Proxy.NO_PROXY, "1").createUserAuthentication(Agent.MINECRAFT);
auth.setUsername(args.get("username"));
auth.setPassword(args.remove("password"));
try {
auth.logIn();
} catch (AuthenticationException e) {
LogManager.getLogger().error("Login failed!", e);
throw new RuntimeException(e); // don't set other variables
}
args.put("username", auth.getSelectedProfile().getName());
args.put("uuid", auth.getSelectedProfile().getId().toString().replace("-", ""));
args.put("accessToken", auth.getAuthenticatedToken());
args.put("userProperties", auth.getUserProperties().toString());
return true;
}
}