From 67f3fc1fa4d80efa1a55d12ecea7944941f18593 Mon Sep 17 00:00:00 2001 From: Henry Le Grys Date: Mon, 25 Jan 2021 14:38:26 +0000 Subject: [PATCH] Add a scanner to download missing targets of .url.txt files --- .../launcher/builder/ClientFileCollector.java | 15 ++--- .../launcher/builder/FileUrlRedirect.java | 43 ++++++++++++++ .../launcher/builder/FileUrlScanner.java | 57 +++++++++++++++++++ .../launcher/builder/PackageBuilder.java | 10 +++- .../com/skcraft/launcher/LauncherUtils.java | 21 +++---- 5 files changed, 123 insertions(+), 23 deletions(-) create mode 100644 launcher-builder/src/main/java/com/skcraft/launcher/builder/FileUrlRedirect.java create mode 100644 launcher-builder/src/main/java/com/skcraft/launcher/builder/FileUrlScanner.java diff --git a/launcher-builder/src/main/java/com/skcraft/launcher/builder/ClientFileCollector.java b/launcher-builder/src/main/java/com/skcraft/launcher/builder/ClientFileCollector.java index 02805a3..79c9dd8 100644 --- a/launcher-builder/src/main/java/com/skcraft/launcher/builder/ClientFileCollector.java +++ b/launcher-builder/src/main/java/com/skcraft/launcher/builder/ClientFileCollector.java @@ -17,7 +17,6 @@ import org.apache.commons.io.FilenameUtils; import java.io.File; import java.io.IOException; -import java.nio.charset.Charset; /** * Walks a path and adds hashed path versions to the given @@ -26,8 +25,6 @@ import java.nio.charset.Charset; @Log public class ClientFileCollector extends DirectoryWalker { - public static final String URL_FILE_SUFFIX = ".url.txt"; - private final Manifest manifest; private final PropertiesApplicator applicator; private final File destDir; @@ -54,7 +51,8 @@ public class ClientFileCollector extends DirectoryWalker { @Override protected void onFile(File file, String relPath) throws IOException { - if (file.getName().endsWith(FileInfoScanner.FILE_SUFFIX) || file.getName().endsWith(URL_FILE_SUFFIX)) { + if (file.getName().endsWith(FileInfoScanner.FILE_SUFFIX) + || file.getName().endsWith(FileUrlScanner.URL_FILE_SUFFIX)) { return; } @@ -63,11 +61,14 @@ public class ClientFileCollector extends DirectoryWalker { String to = FilenameUtils.separatorsToUnix(FilenameUtils.normalize(relPath)); // url.txt override file - File urlFile = new File(file.getAbsoluteFile().getParentFile(), file.getName() + URL_FILE_SUFFIX); + File urlFile = new File(file.getAbsoluteFile().getParentFile(), + file.getName() + FileUrlScanner.URL_FILE_SUFFIX); String location; boolean copy = true; - if (urlFile.exists() && !System.getProperty("com.skcraft.builder.ignoreURLOverrides", "false").equalsIgnoreCase("true")) { - location = Files.readFirstLine(urlFile, Charset.defaultCharset()); + if (urlFile.exists() && FileUrlScanner.isEnabled()) { + FileUrlRedirect redirect = FileUrlRedirect.fromFile(urlFile); + + location = redirect.getUrl().toString(); copy = false; } else { location = hash.substring(0, 2) + "/" + hash.substring(2, 4) + "/" + hash; diff --git a/launcher-builder/src/main/java/com/skcraft/launcher/builder/FileUrlRedirect.java b/launcher-builder/src/main/java/com/skcraft/launcher/builder/FileUrlRedirect.java new file mode 100644 index 0000000..6115f56 --- /dev/null +++ b/launcher-builder/src/main/java/com/skcraft/launcher/builder/FileUrlRedirect.java @@ -0,0 +1,43 @@ +package com.skcraft.launcher.builder; + +import com.google.common.io.Files; +import lombok.Data; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.nio.charset.Charset; +import java.util.List; + +import static com.skcraft.launcher.util.HttpRequest.url; + +@Data +public class FileUrlRedirect { + private URL url; + private String hash; + + public void readFromFile(File file) throws IOException { + List lines = Files.readLines(file, Charset.defaultCharset()); + this.url = url(lines.get(0)); + + if (lines.size() > 1) { + String hash = lines.get(1); + + if (!hash.isEmpty()) { + this.hash = hash; + } + } + } + + public void writeToFile(File file) throws IOException { + String entry = url.toString() + '\n' + hash; + + Files.write(entry, file, Charset.defaultCharset()); + } + + public static FileUrlRedirect fromFile(File file) throws IOException { + FileUrlRedirect entry = new FileUrlRedirect(); + entry.readFromFile(file); + return entry; + } +} diff --git a/launcher-builder/src/main/java/com/skcraft/launcher/builder/FileUrlScanner.java b/launcher-builder/src/main/java/com/skcraft/launcher/builder/FileUrlScanner.java new file mode 100644 index 0000000..52076fd --- /dev/null +++ b/launcher-builder/src/main/java/com/skcraft/launcher/builder/FileUrlScanner.java @@ -0,0 +1,57 @@ +package com.skcraft.launcher.builder; + +import com.google.common.io.Files; +import com.skcraft.launcher.util.FileUtils; +import com.skcraft.launcher.util.HttpRequest; +import lombok.extern.java.Log; + +import java.io.File; +import java.io.IOException; + +@Log +public class FileUrlScanner extends DirectoryWalker { + public static final String URL_FILE_SUFFIX = ".url.txt"; + + public static boolean isEnabled() { + return !System.getProperty("com.skcraft.builder.ignoreURLOverrides", "false") + .equalsIgnoreCase("true"); + } + + @Override + protected void onFile(File file, String relPath) throws IOException { + if (!file.getName().endsWith(URL_FILE_SUFFIX)) return; + + log.info("Found URL file " + file.getName()); + + File targetFile = new File(file.getAbsoluteFile().getParentFile(), + file.getName().replace(URL_FILE_SUFFIX, "")); + FileUrlRedirect info = FileUrlRedirect.fromFile(file); + + if (targetFile.exists()) { + String localHash = FileUtils.getShaHash(targetFile); + if (info.getHash() == null) { + info.setHash(localHash); + info.writeToFile(file); + return; + } + + // If everything matches, skip this file + if (info.getHash().equals(localHash)) return; + } + + File tempFile = File.createTempFile("launcherlib", null); + + try { + log.info("Downloading file " + targetFile.getName() + " from " + info.getUrl() + "..."); + HttpRequest.get(info.getUrl()) + .execute() + .expectResponseCode(200) + .saveContent(tempFile); + } catch (InterruptedException e) { + throw new IOException(e); + } + + Files.move(tempFile, targetFile); + log.info("Updated " + targetFile.getName() + " from " + file.getName()); + } +} diff --git a/launcher-builder/src/main/java/com/skcraft/launcher/builder/PackageBuilder.java b/launcher-builder/src/main/java/com/skcraft/launcher/builder/PackageBuilder.java index 44600f2..65ddc91 100644 --- a/launcher-builder/src/main/java/com/skcraft/launcher/builder/PackageBuilder.java +++ b/launcher-builder/src/main/java/com/skcraft/launcher/builder/PackageBuilder.java @@ -104,11 +104,15 @@ public class PackageBuilder { public void scan(File dir) throws IOException { logSection("Scanning for .info.json files..."); - FileInfoScanner scanner = new FileInfoScanner(mapper); - scanner.walk(dir); - for (FeaturePattern pattern : scanner.getPatterns()) { + FileInfoScanner infoScanner = new FileInfoScanner(mapper); + infoScanner.walk(dir); + for (FeaturePattern pattern : infoScanner.getPatterns()) { applicator.register(pattern); } + + logSection("Scanning for .url.txt files..."); + FileUrlScanner urlScanner = new FileUrlScanner(); + urlScanner.walk(dir); } public void addFiles(File dir, File destDir) throws IOException { diff --git a/launcher/src/main/java/com/skcraft/launcher/LauncherUtils.java b/launcher/src/main/java/com/skcraft/launcher/LauncherUtils.java index caf9537..6520ef7 100644 --- a/launcher/src/main/java/com/skcraft/launcher/LauncherUtils.java +++ b/launcher/src/main/java/com/skcraft/launcher/LauncherUtils.java @@ -88,6 +88,10 @@ public final class LauncherUtils { public static void interruptibleDelete(File file, List failures) throws IOException, InterruptedException { checkInterrupted(); + if (!file.exists()) { + throw new FileNotFoundException("Does not exist: " + file); + } + if (file.isDirectory()) { File[] files = file.listFiles(); @@ -98,20 +102,11 @@ public final class LauncherUtils { for (File f : files) { interruptibleDelete(f, failures); } + } - if (!file.delete()) { - log.warning("Failed to delete " + file.getAbsolutePath()); - failures.add(file); - } - } else { - if (!file.exists()) { - throw new FileNotFoundException("Does not exist: " + file); - } - - if (!file.delete()) { - log.warning("Failed to delete " + file.getAbsolutePath()); - failures.add(file); - } + if (!file.delete()) { + log.warning("Failed to delete " + file.getAbsolutePath()); + failures.add(file); } }