Launcher/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java

297 lines
12 KiB
Java
Raw Normal View History

package pro.gravit.launcher.server;
2018-09-17 10:07:32 +03:00
import pro.gravit.launcher.ClientPermissions;
import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.LauncherConfig;
import pro.gravit.launcher.config.JsonConfigurable;
2020-12-17 13:52:25 +03:00
import pro.gravit.launcher.events.request.AuthRequestEvent;
import pro.gravit.launcher.events.request.ProfilesRequestEvent;
import pro.gravit.launcher.modules.events.PostInitPhase;
import pro.gravit.launcher.modules.events.PreConfigPhase;
2019-06-03 10:58:10 +03:00
import pro.gravit.launcher.profiles.ClientProfile;
2020-12-17 13:52:25 +03:00
import pro.gravit.launcher.profiles.PlayerProfile;
import pro.gravit.launcher.profiles.optional.actions.OptionalAction;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.auth.AuthRequest;
2021-04-13 15:30:06 +03:00
import pro.gravit.launcher.request.auth.GetAvailabilityAuthRequest;
import pro.gravit.launcher.request.update.ProfilesRequest;
2019-06-03 10:58:10 +03:00
import pro.gravit.launcher.server.setup.ServerWrapperSetup;
import pro.gravit.utils.PublicURLClassLoader;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;
2018-12-20 18:45:01 +03:00
2019-10-19 19:46:04 +03:00
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Type;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.UUID;
2019-10-19 19:46:04 +03:00
public class ServerWrapper extends JsonConfigurable<ServerWrapper.Config> {
2019-10-19 19:43:25 +03:00
public static final Path modulesDir = Paths.get(System.getProperty("serverwrapper.modulesDir", "modules"));
public static final Path modulesConfigDir = Paths.get(System.getProperty("serverwrapper.modulesConfigDir", "modules-config"));
public static final Path configFile = Paths.get(System.getProperty("serverwrapper.configFile", "ServerWrapperConfig.json"));
public static final boolean disableSetup = Boolean.parseBoolean(System.getProperty("serverwrapper.disableSetup", "false"));
2020-04-05 10:27:04 +03:00
public static ServerWrapperModulesManager modulesManager;
public static ServerWrapper wrapper;
public Config config;
public PublicURLClassLoader ucp;
public ClassLoader loader;
public ClientPermissions permissions;
public ClientProfile profile;
2020-12-17 13:52:25 +03:00
public PlayerProfile playerProfile;
public ClientProfile.ServerProfile serverProfile;
2018-11-08 15:30:16 +03:00
public ServerWrapper(Type type, Path configPath) {
super(type, configPath);
}
2020-04-05 10:27:04 +03:00
public static void initGson(ServerWrapperModulesManager modulesManager) {
Launcher.gsonManager = new ServerWrapperGsonManager(modulesManager);
Launcher.gsonManager.initGson();
}
public static void main(String... args) throws Throwable {
LogHelper.printVersion("ServerWrapper");
LogHelper.printLicense("ServerWrapper");
modulesManager = new ServerWrapperModulesManager(modulesDir, modulesConfigDir);
modulesManager.autoload();
modulesManager.initModules(null);
ServerWrapper.wrapper = new ServerWrapper(ServerWrapper.Config.class, configFile);
ServerWrapper.wrapper.run(args);
}
public boolean auth() {
try {
2019-11-28 20:01:04 +03:00
Launcher.getConfig();
AuthRequest request = new AuthRequest(config.login, config.password, config.auth_id, AuthRequest.ConnectTypes.API);
2020-12-17 13:52:25 +03:00
AuthRequestEvent authResult = request.request();
2021-05-25 12:17:29 +03:00
if (config.saveSession) {
if (authResult.oauth != null) {
Request.setOAuth(config.auth_id, authResult.oauth);
config.oauth = authResult.oauth;
config.oauthExpireTime = Request.getTokenExpiredTime();
} else {
Request.setSession(authResult.session);
}
saveConfig();
}
2020-12-17 13:52:25 +03:00
permissions = authResult.permissions;
playerProfile = authResult.playerProfile;
return true;
} catch (Throwable e) {
LogHelper.error(e);
if (config.stopOnError) System.exit(-1);
return false;
}
}
public ProfilesRequestEvent getProfiles() {
try {
ProfilesRequestEvent result = new ProfilesRequest().request();
for (ClientProfile p : result.profiles) {
LogHelper.debug("Get profile: %s", p.getTitle());
2020-12-17 13:52:25 +03:00
boolean isFound = false;
2021-03-24 15:47:38 +03:00
for (ClientProfile.ServerProfile srv : p.getServers()) {
if (srv != null && srv.name.equals(config.serverName)) {
2020-12-17 13:52:25 +03:00
this.serverProfile = srv;
this.profile = p;
Launcher.profile = p;
LogHelper.debug("Found profile: %s", Launcher.profile.getTitle());
isFound = true;
break;
}
}
2021-03-24 15:47:38 +03:00
if (isFound) break;
}
if (profile == null) {
2020-12-17 13:52:25 +03:00
LogHelper.warning("Not connected to ServerProfile. May be serverName incorrect?");
}
return result;
2018-11-08 15:30:16 +03:00
} catch (Throwable e) {
LogHelper.error(e);
if (config.stopOnError) System.exit(-1);
return null;
}
}
2018-11-08 15:30:16 +03:00
2019-04-03 16:27:40 +03:00
public void run(String... args) throws Throwable {
initGson(modulesManager);
2020-01-20 08:21:59 +03:00
AuthRequest.registerProviders();
2021-04-13 15:30:06 +03:00
GetAvailabilityAuthRequest.registerProviders();
OptionalAction.registerProviders();
2019-04-03 16:27:40 +03:00
if (args.length > 0 && args[0].equals("setup") && !disableSetup) {
2019-04-03 11:46:18 +03:00
LogHelper.debug("Read ServerWrapperConfig.json");
loadConfig();
2019-04-03 11:46:18 +03:00
ServerWrapperSetup setup = new ServerWrapperSetup();
setup.run();
System.exit(0);
2019-04-03 11:46:18 +03:00
}
modulesManager.invokeEvent(new PreConfigPhase());
LogHelper.debug("Read ServerWrapperConfig.json");
loadConfig();
updateLauncherConfig();
2019-04-03 16:27:40 +03:00
if (config.env != null) Launcher.applyLauncherEnv(config.env);
else Launcher.applyLauncherEnv(LauncherConfig.LauncherEnvironment.STD);
2019-01-15 06:35:39 +03:00
if (config.logFile != null) LogHelper.addOutput(IOHelper.newWriter(Paths.get(config.logFile), true));
{
2021-05-25 12:17:29 +03:00
if (config.saveSession) {
boolean needRestore = false;
2021-05-25 12:17:29 +03:00
if (config.oauth != null) {
Request.setOAuth(config.auth_id, config.oauth, config.oauthExpireTime);
needRestore = true;
} else if (config.session != null) {
Request.setSession(config.session);
needRestore = true;
} else {
auth();
}
try {
if (needRestore)
Request.restore();
} catch (Exception e) {
LogHelper.error(e);
auth();
}
} else {
auth();
}
getProfiles();
}
modulesManager.invokeEvent(new ServerWrapperInitPhase(this));
2019-01-03 17:22:59 +03:00
String classname = (config.mainclass == null || config.mainclass.isEmpty()) ? args[0] : config.mainclass;
2018-12-20 18:45:01 +03:00
if (classname.length() == 0) {
LogHelper.error("MainClass not found. Please set MainClass for ServerWrapper.cfg or first commandline argument");
2019-01-15 06:35:39 +03:00
if (config.stopOnError) System.exit(-1);
}
Class<?> mainClass;
2018-12-20 18:45:01 +03:00
if (config.customClassPath) {
2019-01-15 06:35:39 +03:00
if (config.classpath == null)
throw new UnsupportedOperationException("classpath is null, customClassPath not available");
String[] cp = config.classpath.split(":");
2018-12-20 18:45:01 +03:00
if (!ServerAgent.isAgentStarted()) {
LogHelper.warning("JavaAgent not found. Using URLClassLoader");
URL[] urls = Arrays.stream(cp).map(Paths::get).map(IOHelper::toURL).toArray(URL[]::new);
ucp = new PublicURLClassLoader(urls);
Thread.currentThread().setContextClassLoader(ucp);
loader = ucp;
2018-12-20 18:45:01 +03:00
} else {
LogHelper.info("Found %d custom classpath elements", cp.length);
for (String c : cp)
ServerAgent.addJVMClassPath(c);
}
}
2018-12-20 18:45:01 +03:00
if (config.autoloadLibraries) {
if (!ServerAgent.isAgentStarted()) {
throw new UnsupportedOperationException("JavaAgent not found, autoloadLibraries not available");
}
2019-01-15 06:35:39 +03:00
if (config.librariesDir == null)
throw new UnsupportedOperationException("librariesDir is null, autoloadLibraries not available");
Path librariesDir = Paths.get(config.librariesDir);
LogHelper.info("Load libraries");
ServerAgent.loadLibraries(librariesDir);
}
2018-12-20 18:45:01 +03:00
if (loader != null) mainClass = Class.forName(classname, true, loader);
else mainClass = Class.forName(classname);
2018-09-17 10:07:32 +03:00
MethodHandle mainMethod = MethodHandles.publicLookup().findStatic(mainClass, "main", MethodType.methodType(void.class, String[].class));
modulesManager.invokeEvent(new PostInitPhase());
Request.service.reconnectCallback = () ->
{
LogHelper.debug("WebSocket connect closed. Try reconnect");
2021-05-25 12:17:29 +03:00
if (config.saveSession) {
try {
Request.restore();
} catch (Exception e) {
auth();
}
} else {
auth();
}
getProfiles();
};
2020-12-17 13:52:25 +03:00
LogHelper.info("ServerWrapper: Project %s, LaunchServer address: %s. Title: %s", config.projectname, config.address, Launcher.profile != null ? Launcher.profile.getTitle() : "unknown");
LogHelper.info("Minecraft Version (for profile): %s", wrapper.profile == null ? "unknown" : wrapper.profile.getVersion().name);
LogHelper.info("Start Minecraft Server");
LogHelper.debug("Invoke main method %s", mainClass.getName());
2019-04-03 16:27:40 +03:00
if (config.args == null) {
2019-04-03 11:46:18 +03:00
String[] real_args;
2019-04-03 16:27:40 +03:00
if (args.length > 0) {
2019-04-03 11:46:18 +03:00
real_args = new String[args.length - 1];
System.arraycopy(args, 1, real_args, 0, args.length - 1);
} else real_args = args;
mainMethod.invoke(real_args);
2019-04-03 16:27:40 +03:00
} else {
mainMethod.invoke(config.args);
}
2018-09-17 10:07:32 +03:00
}
2019-04-03 16:27:40 +03:00
public void updateLauncherConfig() {
2021-04-26 18:21:28 +03:00
LauncherConfig cfg = new LauncherConfig(config.address, null, null, new HashMap<>(), config.projectname);
Launcher.setConfig(cfg);
}
2018-11-08 15:30:16 +03:00
@Override
public Config getConfig() {
return config;
}
2020-04-05 10:27:04 +03:00
@Override
public void setConfig(Config config) {
this.config = config;
}
@Override
public Config getDefaultConfig() {
2019-01-15 06:35:39 +03:00
Config newConfig = new Config();
2020-12-17 13:52:25 +03:00
newConfig.serverName = "your server name";
newConfig.projectname = "MineCraft";
newConfig.login = "login";
newConfig.password = "password";
2019-01-03 17:22:59 +03:00
newConfig.mainclass = "";
newConfig.syncAuth = true;
newConfig.stopOnError = true;
newConfig.saveSession = true;
newConfig.reconnectCount = 10;
newConfig.reconnectSleep = 1000;
newConfig.address = "ws://localhost:9274/api";
newConfig.env = LauncherConfig.LauncherEnvironment.STD;
return newConfig;
}
2018-09-24 14:30:42 +03:00
public static final class Config {
2020-12-17 13:52:25 +03:00
@Deprecated
2018-09-24 14:30:42 +03:00
public String title;
public String projectname;
public String address;
2020-12-17 13:52:25 +03:00
public String serverName;
public int reconnectCount;
public int reconnectSleep;
public boolean customClassPath;
public boolean autoloadLibraries;
public boolean stopOnError;
public boolean syncAuth;
public String logFile;
public String classpath;
public String librariesDir;
public String mainclass;
public String login;
public String[] args;
public String password;
public String auth_id = "";
public boolean saveSession;
public AuthRequestEvent.OAuthRequestEvent oauth;
public long oauthExpireTime;
public UUID session;
public LauncherConfig.LauncherEnvironment env;
2018-09-24 14:30:42 +03:00
}
2019-05-15 14:11:22 +03:00
public static final class WebSocketConf {
}
2018-09-17 10:07:32 +03:00
}