From 2cdedf5b2ac58cbb6e143b6121717577ed586350 Mon Sep 17 00:00:00 2001 From: Zaxar163 Date: Fri, 19 Jul 2019 19:25:08 +0300 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=9A=D1=8D=D1=88=20=D0=B8=D0=B7?= =?UTF-8?q?=20HashedDir=D0=BE=D0=B2.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pro/gravit/launchserver/LaunchServer.java | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java index d8870755..7449dd00 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java @@ -7,6 +7,7 @@ import java.io.Writer; import java.lang.ProcessBuilder.Redirect; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Type; import java.nio.file.DirectoryStream; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -40,6 +41,15 @@ import org.bouncycastle.crypto.util.PrivateKeyFactory; import org.bouncycastle.crypto.util.PrivateKeyInfoFactory; import org.bouncycastle.operator.OperatorCreationException; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.internal.Streams; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + import pro.gravit.launcher.Launcher; import pro.gravit.launcher.LauncherConfig; import pro.gravit.launcher.NeedGarbageCollection; @@ -468,6 +478,10 @@ public static void main(String... args) throws Throwable { public static Class defaultLauncherEXEBinaryClass = null; public final Path optimizedUpdatesDir; + + public final Path updatesCache; + + public final JsonParser parser; public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException, InvalidKeySpecException { this.dir = dir; @@ -475,6 +489,8 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException taskPool = new Timer("Timered task worker thread", true); launcherLibraries = dir.resolve("launcher-libraries"); launcherLibrariesCompile = dir.resolve("launcher-libraries-compile"); + updatesCache = dir.resolve("cache-updates.hdir.json"); + parser = new JsonParser(); this.args = Arrays.asList(args); if(IOHelper.exists(dir.resolve("LaunchServer.conf"))) { @@ -942,7 +958,15 @@ public void syncProfilesDir() throws IOException { public void syncUpdatesDir(Collection dirs) throws IOException { LogHelper.info("Syncing updates dir"); Map newUpdatesDirMap = new HashMap<>(16); - boolean work = updatesDirMap != null; + if (updatesDirMap == null && IOHelper.exists(updatesCache)) { + try (JsonReader r = Launcher.gsonManager.configGson.newJsonReader(IOHelper.newReader(updatesCache))) { + parser.parse(r).getAsJsonObject().entrySet().forEach(e -> { + newUpdatesDirMap.put(e.getKey(), Launcher.gsonManager.configGson.fromJson(e.getValue(), HashedDir.class)); + }); + } + updatesDirMap = Collections.unmodifiableMap(newUpdatesDirMap); + return; + } try (DirectoryStream dirStream = Files.newDirectoryStream(updatesDir)) { for (final Path updateDir : dirStream) { if (Files.isHidden(updateDir)) @@ -968,7 +992,7 @@ public void syncUpdatesDir(Collection dirs) throws IOException { // Sync and sign update dir LogHelper.info("Syncing '%s' update dir", name); HashedDir updateHDir = new HashedDir(updateDir, null, true, true); - if (work && config.zipDownload) processUpdate(updateDir, updateHDir, name); + if (config.zipDownload) processUpdate(updateDir, updateHDir, name); HashedDir old = newUpdatesDirMap.put(name, updateHDir); if (old != null) { HashedDir.Diff diff = old.diff(updateHDir, null); @@ -987,6 +1011,14 @@ public void syncUpdatesDir(Collection dirs) throws IOException { } } } + try (JsonWriter w = Launcher.gsonManager.configGson.newJsonWriter(IOHelper.newWriter(updatesCache))) { + w.setLenient(true); + JsonObject o = new JsonObject(); + newUpdatesDirMap.entrySet().forEach(e -> { + o.add(e.getKey(), parser.parse(Launcher.gsonManager.configGson.toJson(e.getValue(), HashedDir.class))); + }); + Streams.write(o, w); + } updatesDirMap = Collections.unmodifiableMap(newUpdatesDirMap); }