[FEATURE] SYSTEM_ARGS classLoaderConfig

This commit is contained in:
Gravita 2021-07-11 13:44:57 +07:00
parent b901a5b9e4
commit 1a8ec31a5b
3 changed files with 23 additions and 14 deletions

View file

@ -110,13 +110,7 @@ public static void main(String[] args) throws Throwable {
// Verify ClientLauncher sign and classpath // Verify ClientLauncher sign and classpath
LogHelper.debug("Verifying ClientLauncher sign and classpath"); LogHelper.debug("Verifying ClientLauncher sign and classpath");
List<URL> classpath = new LinkedList<>(); List<URL> classpath = resolveClassPath(clientDir, params.actions, params.profile).map(IOHelper::toURL).collect(Collectors.toList());
resolveClassPathStream(clientDir, params.profile.getClassPath()).map(IOHelper::toURL).collect(Collectors.toCollection(() -> classpath));
for (OptionalAction a : params.actions) {
if (a instanceof OptionalActionClassPath)
resolveClassPathStream(clientDir, ((OptionalActionClassPath) a).args).map(IOHelper::toURL).collect(Collectors.toCollection(() -> classpath));
}
// Start client with WatchService monitoring // Start client with WatchService monitoring
boolean digest = !profile.isUpdateFastCheck(); boolean digest = !profile.isUpdateFastCheck();
LogHelper.debug("Restore sessions"); LogHelper.debug("Restore sessions");
@ -132,19 +126,18 @@ public static void main(String[] args) throws Throwable {
throw new RequestException("Connection failed", e); throw new RequestException("Connection failed", e);
} }
}; };
if (params.profile.getClassLoaderConfig() == ClientProfile.ClassLoaderConfig.LAUNCHER) { ClientProfile.ClassLoaderConfig classLoaderConfig = profile.getClassLoaderConfig();
if (classLoaderConfig == ClientProfile.ClassLoaderConfig.LAUNCHER) {
ClientClassLoader classLoader = new ClientClassLoader(classpath.toArray(new URL[0]), ClassLoader.getSystemClassLoader()); ClientClassLoader classLoader = new ClientClassLoader(classpath.toArray(new URL[0]), ClassLoader.getSystemClassLoader());
ClientLauncherEntryPoint.classLoader = classLoader; ClientLauncherEntryPoint.classLoader = classLoader;
Thread.currentThread().setContextClassLoader(classLoader); Thread.currentThread().setContextClassLoader(classLoader);
classLoader.nativePath = clientDir.resolve("natives").toString(); classLoader.nativePath = clientDir.resolve("natives").toString();
LauncherEngine.modulesManager.invokeEvent(new ClientProcessClassLoaderEvent(engine, classLoader, profile)); LauncherEngine.modulesManager.invokeEvent(new ClientProcessClassLoaderEvent(engine, classLoader, profile));
AuthService.username = params.playerProfile.username;
AuthService.uuid = params.playerProfile.uuid;
ClientService.classLoader = classLoader; ClientService.classLoader = classLoader;
ClientService.nativePath = classLoader.nativePath; ClientService.nativePath = classLoader.nativePath;
classLoader.addURL(IOHelper.getCodeSource(ClientLauncherEntryPoint.class).toUri().toURL()); classLoader.addURL(IOHelper.getCodeSource(ClientLauncherEntryPoint.class).toUri().toURL());
ClientService.baseURLs = classLoader.getURLs(); ClientService.baseURLs = classLoader.getURLs();
} else if (params.profile.getClassLoaderConfig() == ClientProfile.ClassLoaderConfig.AGENT) { } else if (classLoaderConfig == ClientProfile.ClassLoaderConfig.AGENT) {
ClientLauncherEntryPoint.classLoader = ClassLoader.getSystemClassLoader(); ClientLauncherEntryPoint.classLoader = ClassLoader.getSystemClassLoader();
classpath.add(IOHelper.getCodeSource(ClientLauncherEntryPoint.class).toUri().toURL()); classpath.add(IOHelper.getCodeSource(ClientLauncherEntryPoint.class).toUri().toURL());
for (URL url : classpath) { for (URL url : classpath) {
@ -153,11 +146,15 @@ public static void main(String[] args) throws Throwable {
ClientService.instrumentation = LauncherAgent.inst; ClientService.instrumentation = LauncherAgent.inst;
ClientService.nativePath = clientDir.resolve("natives").toString(); ClientService.nativePath = clientDir.resolve("natives").toString();
LauncherEngine.modulesManager.invokeEvent(new ClientProcessClassLoaderEvent(engine, classLoader, profile)); LauncherEngine.modulesManager.invokeEvent(new ClientProcessClassLoaderEvent(engine, classLoader, profile));
AuthService.username = params.playerProfile.username;
AuthService.uuid = params.playerProfile.uuid;
ClientService.classLoader = classLoader; ClientService.classLoader = classLoader;
ClientService.baseURLs = classpath.toArray(new URL[0]); ClientService.baseURLs = classpath.toArray(new URL[0]);
} else if (classLoaderConfig == ClientProfile.ClassLoaderConfig.SYSTEM_ARGS) {
ClientLauncherEntryPoint.classLoader = ClassLoader.getSystemClassLoader();
ClientService.classLoader = ClassLoader.getSystemClassLoader();
ClientService.baseURLs = classpath.toArray(new URL[0]);
} }
AuthService.username = params.playerProfile.username;
AuthService.uuid = params.playerProfile.uuid;
if (params.profile.getRuntimeInClientConfig() != ClientProfile.RuntimeInClientConfig.NONE) { if (params.profile.getRuntimeInClientConfig() != ClientProfile.RuntimeInClientConfig.NONE) {
CommonHelper.newThread("Client Launcher Thread", true, () -> { CommonHelper.newThread("Client Launcher Thread", true, () -> {
try { try {
@ -269,6 +266,15 @@ private static Stream<Path> resolveClassPathStream(Path clientDir, String... cla
return builder.build(); return builder.build();
} }
public static Stream<Path> resolveClassPath(Path clientDir, Set<OptionalAction> actions, ClientProfile profile) throws IOException {
Stream<Path> result = resolveClassPathStream(clientDir, profile.getClassPath());
for (OptionalAction a : actions) {
if (a instanceof OptionalActionClassPath)
result = Stream.concat(result, resolveClassPathStream(clientDir, ((OptionalActionClassPath) a).args));
}
return result;
}
private static void launch(ClientProfile profile, ClientLauncherProcess.ClientParams params) throws Throwable { private static void launch(ClientProfile profile, ClientLauncherProcess.ClientParams params) throws Throwable {
// Add client args // Add client args
Collection<String> args = new LinkedList<>(); Collection<String> args = new LinkedList<>();

View file

@ -27,6 +27,7 @@
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
public class ClientLauncherProcess { public class ClientLauncherProcess {
public final ClientParams params = new ClientParams(); public final ClientParams params = new ClientParams();
@ -152,6 +153,8 @@ public void start(boolean pipeOutput) throws IOException, InterruptedException {
//ADD CLASSPATH //ADD CLASSPATH
if (params.profile.getClassLoaderConfig() == ClientProfile.ClassLoaderConfig.AGENT) { if (params.profile.getClassLoaderConfig() == ClientProfile.ClassLoaderConfig.AGENT) {
processArgs.add("-javaagent:".concat(IOHelper.getCodeSource(ClientLauncherEntryPoint.class).toAbsolutePath().toString())); processArgs.add("-javaagent:".concat(IOHelper.getCodeSource(ClientLauncherEntryPoint.class).toAbsolutePath().toString()));
} else if (params.profile.getClassLoaderConfig() == ClientProfile.ClassLoaderConfig.SYSTEM_ARGS) {
systemClassPath.addAll(ClientLauncherEntryPoint.resolveClassPath(workDir, params.actions, params.profile).map(Path::toString).collect(Collectors.toList()));
} }
if (useLegacyJavaClassPathProperty) { if (useLegacyJavaClassPathProperty) {
processArgs.add("-Djava.class.path=".concat(String.join(getPathSeparator(), systemClassPath))); processArgs.add("-Djava.class.path=".concat(String.join(getPathSeparator(), systemClassPath)));

View file

@ -521,7 +521,7 @@ public enum SecurityManagerConfig {
} }
public enum ClassLoaderConfig { public enum ClassLoaderConfig {
AGENT, LAUNCHER AGENT, LAUNCHER, SYSTEM_ARGS
} }
public enum SignedClientConfig { public enum SignedClientConfig {