From dcf160301e355a73a35abdf03122ef319f426501 Mon Sep 17 00:00:00 2001 From: zaxar163 Date: Thu, 3 Jan 2019 10:30:12 +0400 Subject: [PATCH] UpdManager p1. --- .../ru/gravit/launchserver/LaunchServer.java | 8 +- .../ru/gravit/launchserver/UpdateManager.java | 102 ++++++++++++++++++ .../gravit/launchserver/defaults/config.cfg | 1 + .../java/ru/gravit/launcher/Launcher.java | 10 +- modules | 2 +- 5 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/UpdateManager.java diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index 58237d2b..ca7fc968 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -89,6 +89,10 @@ public static final class Config { public String[] mirrors; + public String updateMirror; + + public boolean criticalCallbacks; + public String binaryName; public LauncherConfig.LauncherEnvironment env; @@ -399,7 +403,6 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException { reconfigurableManager = new ReconfigurableManager(); socketHookManager = new SocketHookManager(); authHookManager = new AuthHookManager(); - GarbageManager.registerNeedGC(sessionManager); GarbageManager.registerNeedGC(limiter); if(config.permissionsHandler instanceof Reloadable) @@ -468,6 +471,7 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException { // post init modules modulesManager.postInitModules(); + new UpdateManager(this); } public static void initGson() @@ -541,6 +545,8 @@ private void generateConfigIfNotExists() throws IOException { LogHelper.info("Creating LaunchServer config"); Config newConfig = new Config(); newConfig.mirrors = new String[]{"http://mirror.gravitlauncher.ml/"}; + newConfig.updateMirror = "gravitlauncher.ml:57977"; + newConfig.criticalCallbacks = true; newConfig.launch4j = new ExeConf(); newConfig.launch4j.copyright = "© GravitLauncher Team"; newConfig.launch4j.fileDesc = "GravitLauncher ".concat(Launcher.getVersion().getVersionString()); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/UpdateManager.java b/LaunchServer/src/main/java/ru/gravit/launchserver/UpdateManager.java new file mode 100644 index 00000000..1007fe0a --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/UpdateManager.java @@ -0,0 +1,102 @@ +package ru.gravit.launchserver; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.Socket; +import java.net.URL; +import java.util.Timer; +import java.util.TimerTask; + +import ru.gravit.launcher.Launcher; +import ru.gravit.launcher.serialize.HInput; +import ru.gravit.utils.Version; +import ru.gravit.utils.helper.LogHelper; + +public final class UpdateManager extends TimerTask { + private final LaunchServer srv; + private final FCL cl; + public final Timer t; + private final URL updU; + private int lastNum = 0; + + public interface Callback { + void prep(LaunchServer lsrv); + void define(FCL loader); + void post(LaunchServer lsrv); + } + + UpdateManager(LaunchServer lsrv) throws MalformedURLException { + this.srv = lsrv; + this.cl = new FCL(srv); + t = new Timer("Updater", true); + t.schedule(this, 60000, 60000); + updU = new URL(srv.config.updateMirror); + checkVer(); + } + + public static class FCL extends ClassLoader { + private FCL(LaunchServer lsrv) { + super(lsrv.modulesManager.classloader); + } + + public Class define(String name, byte[] data) { + return defineClass(name, data, 0, data.length); + } + } + + @Override + public void run() { + try { + Socket s = getSocket(); + s.getOutputStream().write(2); + @SuppressWarnings("resource") // s.close() closes it. + HInput in = new HInput(s.getInputStream()); + if (in.readBoolean()) { + int num = in.readInt(); + if (num != lastNum) lastNum = num; + while (in.stream.available() > 0) { + String classN = in.readString(1024); + byte[] classB = in.readByteArray(256*1024); + try { + Callback c = (Callback)cl.define(classN, classB).newInstance(); + c.prep(srv); + c.define(cl); + c.post(srv); + } catch (InstantiationException | IllegalAccessException e) { + LogHelper.error(e); + } + } + + } + s.close(); + } catch (IOException e) { + } + + } + + private Socket getSocket() throws IOException { + return new Socket(updU.getHost(), updU.getPort()); + } + + private void checkVer() { + try { + Socket s = getSocket(); + s.getOutputStream().write(1); + @SuppressWarnings("resource") // s.close() closes it. + HInput in = new HInput(s.getInputStream()); + int major = in.readInt(); + int minor = in.readInt(); + int patch = in.readInt(); + int build = in.readInt(); + Version launcher = Launcher.getVersion(); + if (major > launcher.major || minor > launcher.minor || patch > launcher.patch || build > launcher.build) { + LogHelper.info("Updates avaliable download it from github."); + LogHelper.info("New version: " + new Version(major, minor, patch, build).toString()); + } + s.close(); + } catch (IOException e) { + LogHelper.error("Can not check version."); + LogHelper.error(e); + } + } +} diff --git a/LaunchServer/src/main/resources/ru/gravit/launchserver/defaults/config.cfg b/LaunchServer/src/main/resources/ru/gravit/launchserver/defaults/config.cfg index 76431adc..cf3fec52 100644 --- a/LaunchServer/src/main/resources/ru/gravit/launchserver/defaults/config.cfg +++ b/LaunchServer/src/main/resources/ru/gravit/launchserver/defaults/config.cfg @@ -47,6 +47,7 @@ "mirrors": [ "http://mirror.gravitlauncher.ml/" ], + "updateMirror": "gravitlauncher.ml:57977", "binaryName": "Launcher", "address": "localhost", "bindAddress": "0.0.0.0", diff --git a/libLauncher/src/main/java/ru/gravit/launcher/Launcher.java b/libLauncher/src/main/java/ru/gravit/launcher/Launcher.java index ae27933b..41cc81d1 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/Launcher.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/Launcher.java @@ -58,11 +58,11 @@ public final class Launcher { public static final String CONFIG_SCRIPT_FILE = "config.js"; private static final Pattern UUID_PATTERN = Pattern.compile("-", Pattern.LITERAL); - public static int MAJOR = 4; - public static int MINOR = 1; - public static int PATCH = 0; - public static int BUILD = 7; - public static Version.Type RELEASE = Version.Type.BETA; + public static final int MAJOR = 4; + public static final int MINOR = 1; + public static final int PATCH = 0; + public static final int BUILD = 7; + public static final Version.Type RELEASE = Version.Type.BETA; public static GsonBuilder gsonBuilder; public static Gson gson; diff --git a/modules b/modules index eec958f2..ec143160 160000 --- a/modules +++ b/modules @@ -1 +1 @@ -Subproject commit eec958f2c5edbc7c053686ab7cebaab8dbca2778 +Subproject commit ec1431605c4951ace5cbd2ab392b67cea25bddd5