diff --git a/Launcher/src/main/java/ru/gravit/launcher/server/ServerWrapper.java b/Launcher/src/main/java/ru/gravit/launcher/server/ServerWrapper.java index 5885bff2..04155681 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/server/ServerWrapper.java +++ b/Launcher/src/main/java/ru/gravit/launcher/server/ServerWrapper.java @@ -1,14 +1,23 @@ package ru.gravit.launcher.server; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; +import java.nio.file.Path; import java.nio.file.Paths; import ru.gravit.launcher.Launcher; import ru.gravit.launcher.LauncherConfig; import ru.gravit.launcher.client.ClientLauncher; +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; +import ru.gravit.launcher.serialize.config.entry.StringConfigEntry; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; import ru.gravit.launcher.profiles.ClientProfile; @@ -18,19 +27,26 @@ public class ServerWrapper { public static ModulesManager modulesManager; - + public static Path configFile; + public static Config config; 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)))); + configFile = IOHelper.WORKING_DIR.resolve("ServerWrapper.cfg"); modulesManager.preInitModules(); + generateConfigIfNotExists(); + try (BufferedReader reader = IOHelper.newReader(configFile)) { + config = new Config(TextConfigReader.read(reader, true)); + } ProfilesRequest.Result result = new ProfilesRequest(cfg).request(); for (SignedObjectHolder p : result.profiles) { LogHelper.debug("Get profile: %s", p.object.getTitle()); if (p.object.getTitle().equals(ClientLauncher.profile.getTitle())) { wrapper.profile = p.object; + ClientLauncher.setProfile(p.object); LogHelper.debug("Found profile: %s", ClientLauncher.profile.getTitle()); break; } @@ -44,6 +60,32 @@ public static void main(String[] args) throws Throwable { modulesManager.postInitModules(); mainMethod.invoke(real_args); } + 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; + protected Config(BlockConfigEntry block) { + super(block); + title = block.getEntryValue("title",StringConfigEntry.class); + } + } public ClientProfile profile; } diff --git a/Launcher/src/main/resources/ru/gravit/launcher/server/config.cfg b/Launcher/src/main/resources/ru/gravit/launcher/server/config.cfg new file mode 100644 index 00000000..f8e11e10 --- /dev/null +++ b/Launcher/src/main/resources/ru/gravit/launcher/server/config.cfg @@ -0,0 +1 @@ +title: "x"; \ No newline at end of file