[FEATURE] ServerWrapper 1.18 support

This commit is contained in:
Gravita 2021-12-02 16:45:36 +07:00
parent 9cc1cf5feb
commit 9c289cd1cb
2 changed files with 22 additions and 8 deletions

View file

@ -141,14 +141,16 @@ public void run(String... args) throws Throwable {
} }
Class<?> mainClass; Class<?> mainClass;
if (config.classpath != null && !config.classpath.isEmpty()) { if (config.classpath != null && !config.classpath.isEmpty()) {
if (!ServerAgent.isAgentStarted()) { if(config.classLoaderConfig == ClientProfile.ClassLoaderConfig.LAUNCHER) {
LogHelper.warning("JavaAgent not found. Using URLClassLoader");
URL[] urls = config.classpath.stream().map(Paths::get).map(IOHelper::toURL).toArray(URL[]::new); URL[] urls = config.classpath.stream().map(Paths::get).map(IOHelper::toURL).toArray(URL[]::new);
ucp = new PublicURLClassLoader(urls); ucp = new PublicURLClassLoader(urls);
Thread.currentThread().setContextClassLoader(ucp); Thread.currentThread().setContextClassLoader(ucp);
loader = ucp; loader = ucp;
} else { } else if(config.classLoaderConfig == ClientProfile.ClassLoaderConfig.AGENT) {
LogHelper.info("Found %d custom classpath elements", config.classpath.size()); if (!ServerAgent.isAgentStarted()) {
LogHelper.error("JavaAgent not found");
System.exit(-1);
}
for (String c : config.classpath) for (String c : config.classpath)
ServerAgent.addJVMClassPath(c); ServerAgent.addJVMClassPath(c);
} }
@ -207,6 +209,7 @@ public Config getDefaultConfig() {
newConfig.args = new ArrayList<>(); newConfig.args = new ArrayList<>();
newConfig.classpath = new ArrayList<>(); newConfig.classpath = new ArrayList<>();
newConfig.address = "ws://localhost:9274/api"; newConfig.address = "ws://localhost:9274/api";
newConfig.classLoaderConfig = ClientProfile.ClassLoaderConfig.SYSTEM_ARGS;
newConfig.env = LauncherConfig.LauncherEnvironment.STD; newConfig.env = LauncherConfig.LauncherEnvironment.STD;
return newConfig; return newConfig;
} }
@ -219,6 +222,7 @@ public static final class Config {
public boolean autoloadLibraries; public boolean autoloadLibraries;
public String logFile; public String logFile;
public List<String> classpath; public List<String> classpath;
public ClientProfile.ClassLoaderConfig classLoaderConfig;
public String librariesDir; public String librariesDir;
public String mainclass; public String mainclass;
public List<String> args; public List<String> args;

View file

@ -1,5 +1,6 @@
package pro.gravit.launcher.server.setup; package pro.gravit.launcher.server.setup;
import pro.gravit.launcher.profiles.ClientProfile;
import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.StdWebSocketService; import pro.gravit.launcher.request.websockets.StdWebSocketService;
import pro.gravit.launcher.server.ServerWrapper; import pro.gravit.launcher.server.ServerWrapper;
@ -57,6 +58,7 @@ public void run() throws Exception {
System.out.println("Print your server name:"); System.out.println("Print your server name:");
wrapper.config.serverName = commands.commandHandler.readLine(); wrapper.config.serverName = commands.commandHandler.readLine();
wrapper.config.mainclass = mainClassName; wrapper.config.mainclass = mainClassName;
boolean altMode = false;
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
if(!Request.isAvailable() || Request.getRequestService().isClosed()) { if(!Request.isAvailable() || Request.getRequestService().isClosed()) {
System.out.println("Print launchserver websocket host( ws://host:port/api ):"); System.out.println("Print launchserver websocket host( ws://host:port/api ):");
@ -84,6 +86,12 @@ public void run() throws Exception {
((AutoCloseable) Request.getRequestService()).close(); ((AutoCloseable) Request.getRequestService()).close();
} }
} }
if(wrapper.profile != null && wrapper.profile.getVersion().compareTo(ClientProfile.Version.MC118) >= 0) {
LogHelper.info("Switch to alternative start mode (1.18)");
wrapper.config.classpath.add(jarName);
wrapper.config.classLoaderConfig = ClientProfile.ClassLoaderConfig.LAUNCHER;
altMode = true;
}
} }
wrapper.saveConfig(); wrapper.saveConfig();
LogHelper.info("Generate start script"); LogHelper.info("Generate start script");
@ -116,10 +124,12 @@ public void run() throws Exception {
writer.append("-cp "); writer.append("-cp ");
String pathServerWrapper = IOHelper.getCodeSource(ServerWrapper.class).getFileName().toString(); String pathServerWrapper = IOHelper.getCodeSource(ServerWrapper.class).getFileName().toString();
writer.append(pathServerWrapper); writer.append(pathServerWrapper);
if(!altMode) {
if (JVMHelper.OS_TYPE == JVMHelper.OS.MUSTDIE) { if (JVMHelper.OS_TYPE == JVMHelper.OS.MUSTDIE) {
writer.append(";"); writer.append(";");
} else writer.append(":"); } else writer.append(":");
writer.append(jarName); writer.append(jarName);
}
writer.append(" "); writer.append(" ");
writer.append(ServerWrapper.class.getName()); writer.append(ServerWrapper.class.getName());
writer.append("\n"); writer.append("\n");