2018-09-17 10:07:32 +03:00
|
|
|
package ru.gravit.launcher.server;
|
|
|
|
|
|
|
|
|
2018-09-24 14:30:42 +03:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.IOException;
|
2018-09-17 10:07:32 +03:00
|
|
|
import java.lang.invoke.MethodHandle;
|
|
|
|
import java.lang.invoke.MethodHandles;
|
|
|
|
import java.lang.invoke.MethodType;
|
2018-09-24 14:30:42 +03:00
|
|
|
import java.nio.file.Path;
|
2018-09-17 10:07:32 +03:00
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
|
|
|
import ru.gravit.launcher.Launcher;
|
|
|
|
import ru.gravit.launcher.LauncherConfig;
|
|
|
|
import ru.gravit.launcher.client.ClientLauncher;
|
2018-09-24 14:30:42 +03:00
|
|
|
import ru.gravit.launcher.serialize.config.ConfigObject;
|
|
|
|
import ru.gravit.launcher.serialize.config.TextConfigReader;
|
|
|
|
import ru.gravit.launcher.serialize.config.TextConfigWriter;
|
|
|
|
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
|
2018-09-24 14:55:39 +03:00
|
|
|
import ru.gravit.launcher.serialize.config.entry.BooleanConfigEntry;
|
2018-09-24 14:30:42 +03:00
|
|
|
import ru.gravit.launcher.serialize.config.entry.StringConfigEntry;
|
2018-09-17 10:20:34 +03:00
|
|
|
import ru.gravit.utils.helper.IOHelper;
|
|
|
|
import ru.gravit.utils.helper.LogHelper;
|
2018-09-17 10:07:32 +03:00
|
|
|
import ru.gravit.launcher.profiles.ClientProfile;
|
|
|
|
import ru.gravit.launcher.request.update.ProfilesRequest;
|
|
|
|
import ru.gravit.launcher.serialize.HInput;
|
|
|
|
import ru.gravit.launcher.serialize.signed.SignedObjectHolder;
|
|
|
|
|
|
|
|
public class ServerWrapper {
|
|
|
|
public static ModulesManager modulesManager;
|
2018-09-24 14:30:42 +03:00
|
|
|
public static Path configFile;
|
|
|
|
public static Config config;
|
2018-09-17 10:07:32 +03:00
|
|
|
public static void main(String[] args) throws Throwable {
|
|
|
|
ServerWrapper wrapper = new ServerWrapper();
|
|
|
|
modulesManager = new ModulesManager(wrapper);
|
|
|
|
modulesManager.autoload(Paths.get("modules"));
|
|
|
|
Launcher.modulesManager = modulesManager;
|
|
|
|
LauncherConfig cfg = new LauncherConfig(new HInput(IOHelper.newInput(IOHelper.getResourceURL(Launcher.CONFIG_FILE))));
|
2018-09-24 14:30:42 +03:00
|
|
|
configFile = IOHelper.WORKING_DIR.resolve("ServerWrapper.cfg");
|
2018-09-17 10:07:32 +03:00
|
|
|
modulesManager.preInitModules();
|
2018-09-24 14:30:42 +03:00
|
|
|
generateConfigIfNotExists();
|
|
|
|
try (BufferedReader reader = IOHelper.newReader(configFile)) {
|
|
|
|
config = new Config(TextConfigReader.read(reader, true));
|
|
|
|
}
|
2018-09-17 10:07:32 +03:00
|
|
|
ProfilesRequest.Result result = new ProfilesRequest(cfg).request();
|
2018-09-22 17:33:00 +03:00
|
|
|
for (SignedObjectHolder<ClientProfile> p : result.profiles) {
|
|
|
|
LogHelper.debug("Get profile: %s", p.object.getTitle());
|
2018-09-24 14:14:20 +03:00
|
|
|
if (p.object.getTitle().equals(ClientLauncher.profile.getTitle())) {
|
2018-09-17 10:07:32 +03:00
|
|
|
wrapper.profile = p.object;
|
2018-09-24 14:30:42 +03:00
|
|
|
ClientLauncher.setProfile(p.object);
|
2018-09-24 14:14:20 +03:00
|
|
|
LogHelper.debug("Found profile: %s", ClientLauncher.profile.getTitle());
|
2018-09-17 10:07:32 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
modulesManager.initModules();
|
2018-09-24 14:55:39 +03:00
|
|
|
String classname = config.mainclass.isEmpty() ? args[0] : config.mainclass;
|
|
|
|
Class<?> mainClass;
|
|
|
|
if(config.customClassLoader) {
|
|
|
|
Class<ClassLoader> classloader_class = (Class<ClassLoader>) Class.forName(config.classloader);
|
|
|
|
ClassLoader loader = classloader_class.getConstructor(ClassLoader.class).newInstance(ClassLoader.getSystemClassLoader());
|
|
|
|
Thread.currentThread().setContextClassLoader(loader);
|
|
|
|
mainClass = Class.forName(classname,false,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));
|
|
|
|
String[] real_args = new String[args.length - 1];
|
2018-09-22 17:33:00 +03:00
|
|
|
System.arraycopy(args, 1, real_args, 0, args.length - 1);
|
2018-09-17 10:07:32 +03:00
|
|
|
modulesManager.postInitModules();
|
|
|
|
mainMethod.invoke(real_args);
|
|
|
|
}
|
2018-09-24 14:30:42 +03:00
|
|
|
private static void generateConfigIfNotExists() throws IOException {
|
|
|
|
if (IOHelper.isFile(configFile))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create new config
|
|
|
|
LogHelper.info("Creating LaunchServer config");
|
|
|
|
Config newConfig;
|
|
|
|
try (BufferedReader reader = IOHelper.newReader(IOHelper.getResourceURL("ru/gravit/launcher/server/config.cfg"))) {
|
|
|
|
newConfig = new Config(TextConfigReader.read(reader, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
LogHelper.warning("Title is not set. Please show ServerWrapper.cfg");
|
|
|
|
|
|
|
|
// Write LaunchServer config
|
|
|
|
LogHelper.info("Writing LaunchServer config file");
|
|
|
|
try (BufferedWriter writer = IOHelper.newWriter(configFile)) {
|
|
|
|
TextConfigWriter.write(newConfig.block, writer, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static final class Config extends ConfigObject {
|
|
|
|
public String title;
|
2018-09-24 14:55:39 +03:00
|
|
|
public boolean customClassLoader;
|
|
|
|
public String classloader;
|
|
|
|
public String mainclass;
|
2018-09-24 14:30:42 +03:00
|
|
|
protected Config(BlockConfigEntry block) {
|
|
|
|
super(block);
|
|
|
|
title = block.getEntryValue("title",StringConfigEntry.class);
|
2018-09-24 14:55:39 +03:00
|
|
|
customClassLoader = block.getEntryValue("customClassLoader", BooleanConfigEntry.class);
|
|
|
|
if(customClassLoader)
|
|
|
|
classloader = block.getEntryValue("classloader",StringConfigEntry.class);
|
|
|
|
mainclass = block.getEntryValue("MainClass",StringConfigEntry.class);
|
2018-09-24 14:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
2018-09-22 17:33:00 +03:00
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
public ClientProfile profile;
|
|
|
|
}
|