[FIX] Исправления алгоритма.

This commit is contained in:
zaxar163 2019-07-22 12:26:27 +03:00
parent 2cdedf5b2a
commit f7a0743e24

View file

@ -4,6 +4,7 @@
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.Reader;
import java.io.Writer; import java.io.Writer;
import java.lang.ProcessBuilder.Redirect; import java.lang.ProcessBuilder.Redirect;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -37,19 +38,10 @@
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LogLevel;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.crypto.util.PrivateKeyFactory; import org.bouncycastle.crypto.util.PrivateKeyFactory;
import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;
import org.bouncycastle.operator.OperatorCreationException; import org.bouncycastle.operator.OperatorCreationException;
import com.google.gson.Gson; import com.google.gson.reflect.TypeToken;
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.Launcher;
import pro.gravit.launcher.LauncherConfig; import pro.gravit.launcher.LauncherConfig;
import pro.gravit.launcher.NeedGarbageCollection; import pro.gravit.launcher.NeedGarbageCollection;
@ -481,8 +473,6 @@ public static void main(String... args) throws Throwable {
public final Path updatesCache; public final Path updatesCache;
public final JsonParser parser;
public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException, InvalidKeySpecException { public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException, InvalidKeySpecException {
this.dir = dir; this.dir = dir;
this.testEnv = testEnv; this.testEnv = testEnv;
@ -490,7 +480,6 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException
launcherLibraries = dir.resolve("launcher-libraries"); launcherLibraries = dir.resolve("launcher-libraries");
launcherLibrariesCompile = dir.resolve("launcher-libraries-compile"); launcherLibrariesCompile = dir.resolve("launcher-libraries-compile");
updatesCache = dir.resolve("cache-updates.hdir.json"); updatesCache = dir.resolve("cache-updates.hdir.json");
parser = new JsonParser();
this.args = Arrays.asList(args); this.args = Arrays.asList(args);
if(IOHelper.exists(dir.resolve("LaunchServer.conf"))) if(IOHelper.exists(dir.resolve("LaunchServer.conf")))
{ {
@ -959,10 +948,15 @@ public void syncUpdatesDir(Collection<String> dirs) throws IOException {
LogHelper.info("Syncing updates dir"); LogHelper.info("Syncing updates dir");
Map<String, HashedDir> newUpdatesDirMap = new HashMap<>(16); Map<String, HashedDir> newUpdatesDirMap = new HashMap<>(16);
if (updatesDirMap == null && IOHelper.exists(updatesCache)) { if (updatesDirMap == null && IOHelper.exists(updatesCache)) {
try (JsonReader r = Launcher.gsonManager.configGson.newJsonReader(IOHelper.newReader(updatesCache))) { if (IOHelper.exists(updatesCache)) {
parser.parse(r).getAsJsonObject().entrySet().forEach(e -> { LogHelper.info("Load sessions from %s", IOHelper.getFileName(updatesCache));
newUpdatesDirMap.put(e.getKey(), Launcher.gsonManager.configGson.fromJson(e.getValue(), HashedDir.class)); Type setType = new TypeToken<HashMap<String, HashedDir>>() {
}); }.getType();
try (Reader reader = IOHelper.newReader(updatesCache)) {
newUpdatesDirMap = Launcher.gsonManager.configGson.fromJson(reader, setType);
} catch (IOException e) {
LogHelper.error(e);
}
} }
updatesDirMap = Collections.unmodifiableMap(newUpdatesDirMap); updatesDirMap = Collections.unmodifiableMap(newUpdatesDirMap);
return; return;
@ -1011,13 +1005,11 @@ public void syncUpdatesDir(Collection<String> dirs) throws IOException {
} }
} }
} }
try (JsonWriter w = Launcher.gsonManager.configGson.newJsonWriter(IOHelper.newWriter(updatesCache))) { try (Writer writer = IOHelper.newWriter(updatesCache)) {
w.setLenient(true); LogHelper.info("Write sessions to %s", IOHelper.getFileName(updatesCache));
JsonObject o = new JsonObject(); Launcher.gsonManager.configGson.toJson(newUpdatesDirMap, writer);
newUpdatesDirMap.entrySet().forEach(e -> { } catch (IOException e) {
o.add(e.getKey(), parser.parse(Launcher.gsonManager.configGson.toJson(e.getValue(), HashedDir.class))); LogHelper.error(e);
});
Streams.write(o, w);
} }
updatesDirMap = Collections.unmodifiableMap(newUpdatesDirMap); updatesDirMap = Collections.unmodifiableMap(newUpdatesDirMap);
} }