From ca70ee78d1569d9d7ab25fbfa78b183ae06ff2c0 Mon Sep 17 00:00:00 2001 From: Gravita <12893402+gravit0@users.noreply.github.com> Date: Mon, 5 Aug 2024 05:34:45 +0700 Subject: [PATCH] [FIX] Launcher update --- .../auth/updates/LocalUpdatesProvider.java | 32 +++++++++++++++---- .../auth/updates/UpdatesProvider.java | 2 ++ .../launchserver/binary/LauncherBinary.java | 13 +++++--- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/updates/LocalUpdatesProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/updates/LocalUpdatesProvider.java index dc429863..581b35ac 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/updates/LocalUpdatesProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/updates/LocalUpdatesProvider.java @@ -13,6 +13,7 @@ import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.util.*; import java.util.stream.Stream; @@ -130,24 +131,41 @@ public HashedDir getUpdatesDir(String updateName) { return updatesDirMap.get(updateName); } + private Path resolveUpdateName(String updateName) { + if(updateName == null) { + return Path.of(updatesDir); + } + return Path.of(updatesDir).resolve(updateName); + } + @Override public void upload(String updateName, Map files, boolean deleteAfterUpload) throws IOException { - var path = Path.of(updatesDir).resolve(updateName); + var path = resolveUpdateName(updateName); for(var e : files.entrySet()) { var target = path.resolve(e.getKey()); - var source = Path.of(e.getKey()); + var source = e.getValue(); IOHelper.createParentDirs(target); if(deleteAfterUpload) { - Files.move(source, target); + Files.move(source, target, StandardCopyOption.REPLACE_EXISTING); } else { - Files.copy(source, target); + Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); } } } + @Override + public Map download(String updateName, List files) { + var path = resolveUpdateName(updateName); + Map map = new HashMap<>(); + for(var e : files) { + map.put(e, path.resolve(e)); + } + return map; + } + @Override public void delete(String updateName, List files) throws IOException { - var path = Path.of(updatesDir).resolve(updateName); + var path = resolveUpdateName(updateName); for(var e : files) { var target = path.resolve(e); Files.delete(target); @@ -156,13 +174,13 @@ public void delete(String updateName, List files) throws IOException { @Override public void delete(String updateName) throws IOException { - var path = Path.of(updatesDir).resolve(updateName); + var path = resolveUpdateName(updateName); IOHelper.deleteDir(path, true); } @Override public void create(String updateName) throws IOException { - var path = Path.of(updatesDir).resolve(updateName); + var path = resolveUpdateName(updateName); Files.createDirectories(path); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/updates/UpdatesProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/updates/UpdatesProvider.java index 572b07d1..832f729c 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/updates/UpdatesProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/updates/UpdatesProvider.java @@ -37,6 +37,8 @@ public void sync() throws IOException { public abstract void upload(String updateName, Map files, boolean deleteAfterUpload) throws IOException; + public abstract Map download(String updateName, List files); + public abstract void delete(String updateName, List files) throws IOException; public abstract void delete(String updateName) throws IOException; diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/LauncherBinary.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/LauncherBinary.java index 6a2d4e97..a336c439 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/LauncherBinary.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/LauncherBinary.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.nio.file.Path; +import java.util.List; import java.util.Map; public abstract class LauncherBinary extends BinaryPipeline { @@ -56,9 +57,13 @@ public void init() { } public final boolean sync() throws IOException { - boolean exists = exists(); - digest = exists ? SecurityHelper.digest(SecurityHelper.DigestAlgorithm.SHA512, IOHelper.read(syncBinaryFile)) : null; - - return exists; + try { + var target = syncBinaryFile.toString(); + var path = server.config.updatesProvider.download(null, List.of(target)).get(target); + digest = SecurityHelper.digest(SecurityHelper.DigestAlgorithm.SHA512, IOHelper.read(path)); + return true; + } catch (Throwable e) { + return false; + } } }