1
0
mirror of https://github.com/SKCraft/Launcher.git synced 2024-11-27 12:46:22 +01:00

Add a scanner to download missing targets of .url.txt files

This commit is contained in:
Henry Le Grys 2021-01-25 14:38:26 +00:00
parent d0d8ad79ee
commit 67f3fc1fa4
5 changed files with 123 additions and 23 deletions

View File

@ -17,7 +17,6 @@ import org.apache.commons.io.FilenameUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
/** /**
* Walks a path and adds hashed path versions to the given * Walks a path and adds hashed path versions to the given
@ -26,8 +25,6 @@ import java.nio.charset.Charset;
@Log @Log
public class ClientFileCollector extends DirectoryWalker { public class ClientFileCollector extends DirectoryWalker {
public static final String URL_FILE_SUFFIX = ".url.txt";
private final Manifest manifest; private final Manifest manifest;
private final PropertiesApplicator applicator; private final PropertiesApplicator applicator;
private final File destDir; private final File destDir;
@ -54,7 +51,8 @@ public class ClientFileCollector extends DirectoryWalker {
@Override @Override
protected void onFile(File file, String relPath) throws IOException { 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; return;
} }
@ -63,11 +61,14 @@ public class ClientFileCollector extends DirectoryWalker {
String to = FilenameUtils.separatorsToUnix(FilenameUtils.normalize(relPath)); String to = FilenameUtils.separatorsToUnix(FilenameUtils.normalize(relPath));
// url.txt override file // 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; String location;
boolean copy = true; boolean copy = true;
if (urlFile.exists() && !System.getProperty("com.skcraft.builder.ignoreURLOverrides", "false").equalsIgnoreCase("true")) { if (urlFile.exists() && FileUrlScanner.isEnabled()) {
location = Files.readFirstLine(urlFile, Charset.defaultCharset()); FileUrlRedirect redirect = FileUrlRedirect.fromFile(urlFile);
location = redirect.getUrl().toString();
copy = false; copy = false;
} else { } else {
location = hash.substring(0, 2) + "/" + hash.substring(2, 4) + "/" + hash; location = hash.substring(0, 2) + "/" + hash.substring(2, 4) + "/" + hash;

View File

@ -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<String> 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;
}
}

View File

@ -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());
}
}

View File

@ -104,11 +104,15 @@ public class PackageBuilder {
public void scan(File dir) throws IOException { public void scan(File dir) throws IOException {
logSection("Scanning for .info.json files..."); logSection("Scanning for .info.json files...");
FileInfoScanner scanner = new FileInfoScanner(mapper); FileInfoScanner infoScanner = new FileInfoScanner(mapper);
scanner.walk(dir); infoScanner.walk(dir);
for (FeaturePattern pattern : scanner.getPatterns()) { for (FeaturePattern pattern : infoScanner.getPatterns()) {
applicator.register(pattern); 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 { public void addFiles(File dir, File destDir) throws IOException {

View File

@ -88,6 +88,10 @@ public final class LauncherUtils {
public static void interruptibleDelete(File file, List<File> failures) throws IOException, InterruptedException { public static void interruptibleDelete(File file, List<File> failures) throws IOException, InterruptedException {
checkInterrupted(); checkInterrupted();
if (!file.exists()) {
throw new FileNotFoundException("Does not exist: " + file);
}
if (file.isDirectory()) { if (file.isDirectory()) {
File[] files = file.listFiles(); File[] files = file.listFiles();
@ -98,20 +102,11 @@ public final class LauncherUtils {
for (File f : files) { for (File f : files) {
interruptibleDelete(f, failures); interruptibleDelete(f, failures);
} }
}
if (!file.delete()) { if (!file.delete()) {
log.warning("Failed to delete " + file.getAbsolutePath()); log.warning("Failed to delete " + file.getAbsolutePath());
failures.add(file); 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);
}
} }
} }