Implement basic caching: Try to download, use cache on failure

This commit is contained in:
JFronny 2020-12-29 16:32:47 +01:00
parent 059a093db2
commit 9beed53d0b
1 changed files with 18 additions and 3 deletions

View File

@ -81,7 +81,13 @@ public class Resclone implements ClientModInitializer, RescloneApi {
fileName += Integer.toHexString(s.source.hashCode());
PackMetaLoaded p = new PackMetaLoaded(getConfigPath().resolve("cache").resolve(fileName), s.name);
//If file is not present: download and process pack
if (!Files.isRegularFile(p.zipPath)) {
Path p1 = p.zipPath;
if (Files.exists(p.zipPath)) {
p1 = p.zipPath.getParent().resolve(p.zipPath.getFileName() + ".bak");
Files.deleteIfExists(p1);
Files.move(p.zipPath, p1);
}
try {
//Download
fetcherInstances.get(s.fetcher).get(s.source, p.zipPath);
//Process
@ -89,7 +95,6 @@ public class Resclone implements ClientModInitializer, RescloneApi {
props.put("create", "false");
URI zipfile = URI.create("jar:" + p.zipPath.toUri().toString());
try (FileSystem zipfs = FileSystems.newFileSystem(zipfile, props)) {
//ZipFile file = new ZipFile(meta.zipPath.toFile());
for (PackProcessor processor : processors) {
processor.process(zipfs);
}
@ -98,8 +103,18 @@ public class Resclone implements ClientModInitializer, RescloneApi {
e.printStackTrace();
}
}
catch (Throwable e) {
if (!p1.equals(p.zipPath)) {
System.err.println("Failed to download, using cache");
Files.deleteIfExists(p.zipPath);
Files.move(p1, p.zipPath);
} else
throw new RescloneException("Failed to download initial");
}
if (!p1.equals(p.zipPath))
Files.deleteIfExists(p1);
metas.add(p);
} catch (RescloneException e) {
} catch (Throwable e) {
e.printStackTrace();
}
}