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 02805a341..1356a6cad 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 @@ -11,12 +11,14 @@ import com.google.common.io.Files; import com.skcraft.launcher.model.modpack.FileInstall; import com.skcraft.launcher.model.modpack.Manifest; +import com.skcraft.launcher.util.HttpRequest; import lombok.NonNull; import lombok.extern.java.Log; import org.apache.commons.io.FilenameUtils; import java.io.File; import java.io.IOException; +import java.net.URL; import java.nio.charset.Charset; /** @@ -54,24 +56,70 @@ protected DirectoryBehavior getBehavior(@NonNull String name) { @Override protected void onFile(File file, String relPath) throws IOException { - if (file.getName().endsWith(FileInfoScanner.FILE_SUFFIX) || file.getName().endsWith(URL_FILE_SUFFIX)) { + ClientFileCollector.log.info(String.format("Get %s from %s...", relPath, file.getAbsolutePath())); + if ( + (file.getName().endsWith(FileInfoScanner.FILE_SUFFIX) || file.getName().endsWith(URL_FILE_SUFFIX)) + && new File(file.getAbsoluteFile().getParentFile(), file.getName().replace(URL_FILE_SUFFIX, "")).exists() + ) { + ClientFileCollector.log.info(String.format("Get %s ignored ...", relPath)); return; } - FileInstall entry = new FileInstall(); - String hash = Files.hash(file, hf).toString(); - String to = FilenameUtils.separatorsToUnix(FilenameUtils.normalize(relPath)); + File urlFile; + if(file.getName().endsWith(URL_FILE_SUFFIX)) + { + urlFile = file; + file = new File(file.getAbsoluteFile().getParentFile(), file.getName().replace(URL_FILE_SUFFIX, "")); + relPath = relPath.replace(URL_FILE_SUFFIX, ""); + } + else + { + urlFile = new File(file.getAbsoluteFile().getParentFile(), file.getName() + URL_FILE_SUFFIX); + } // url.txt override file - File urlFile = new File(file.getAbsoluteFile().getParentFile(), file.getName() + URL_FILE_SUFFIX); - String location; + String location, hash; boolean copy = true; - if (urlFile.exists() && !System.getProperty("com.skcraft.builder.ignoreURLOverrides", "false").equalsIgnoreCase("true")) { + if (urlFile.exists() && + ( + !file.exists() || !System.getProperty("com.skcraft.builder.ignoreURLOverrides", "false") + .equalsIgnoreCase("true") + ) + ) { location = Files.readFirstLine(urlFile, Charset.defaultCharset()); copy = false; + + if (file.exists()) + { + hash = Files.hash(file, hf).toString(); + } + else + { + ClientFileCollector.log.info(String.format("Download %s from %s...", relPath, location)); + File tempFile = File.createTempFile("com.skcraft.builder", null); + URL url = HttpRequest.url(location.trim()); + try { + HttpRequest.get(url) + .execute() + .expectResponseCode(200) + .saveContent(tempFile); + hash = Files.hash(tempFile, hf).toString(); + } catch (InterruptedException e) { + ClientFileCollector.log.warning(String.format("Download from %s failed! ", location)); + throw new IOException(e); + } finally { + if(!tempFile.delete()) { + ClientFileCollector.log.warning(String.format("Unable to delete %s! ", tempFile.getAbsolutePath())); + } + } + } } else { + hash = Files.hash(file, hf).toString(); location = hash.substring(0, 2) + "/" + hash.substring(2, 4) + "/" + hash; } + + FileInstall entry = new FileInstall(); + String to = FilenameUtils.separatorsToUnix(FilenameUtils.normalize(relPath)); File destPath = new File(destDir, location); entry.setHash(hash);