Small URL tweaks + pack counter in log

This commit is contained in:
seasnail8169 2021-04-03 05:34:52 +01:00
parent d96ce14496
commit 060346cf3e
3 changed files with 28 additions and 14 deletions

View File

@ -36,6 +36,8 @@ public class Resclone implements ModInitializer, RescloneApi {
public static PackUrlCache urlCache;
public static int COUNT = 0;
@Override
public void onInitialize() {
LOGGER.info("Initialising Resclone.");
@ -57,7 +59,7 @@ public class Resclone implements ModInitializer, RescloneApi {
addProcessor(new RemoveEmptyProcessor());
reload();
LOGGER.info("Completed");
LOGGER.info("Installed {} resource pack{}.", COUNT, COUNT == 1 ? "" : "s");
}
@Override

View File

@ -4,6 +4,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.gitlab.jfronny.resclone.Resclone;
import io.gitlab.jfronny.resclone.util.UrlUtils;
import org.jetbrains.annotations.Nullable;
public class GitHubFetcher extends PackFetcher {
@ -22,8 +23,7 @@ public class GitHubFetcher extends PackFetcher {
/* "user/repo" - Gets from latest commit of main/master branch. */
else if (parts.length == 2) {
Resclone.LOGGER.info("Getting from main branch.");
return getFromMain(parts[0], parts[1]);
return getFromBranch(parts[0], parts[1], null);
}
/* "user/repo/branch/branchName" - Gets from latest commit of specified branch. */
@ -63,7 +63,6 @@ public class GitHubFetcher extends PackFetcher {
else if (parts[2].equalsIgnoreCase("tag")) {
if (parts.length < 4) throw new Exception("Missing tag number in source definition.");
else {
Resclone.LOGGER.info("Getting from " + parts[3] + " tag.");
return getFromTag(parts[0], parts[1], parts[3]);
}
}
@ -71,19 +70,26 @@ public class GitHubFetcher extends PackFetcher {
return null;
}
private String getFromMain(String user, String repo) {
String url = getFromBranch(user, repo, "main");
if (!UrlUtils.urlValid(url)) url = getFromBranch(user, repo, "master");
private String getFromBranch(String user, String repo, @Nullable String branch) {
boolean main = branch == null;
String url;
if (main) {
url = "https://codeload.github.com/" + user + "/" + repo + "/legacy.zip/refs/heads/main";
if (!UrlUtils.urlValid(url)) url = "https://codeload.github.com/" + user + "/" + repo + "/legacy.zip/refs/heads/master";
Resclone.LOGGER.info("Getting from main branch.");
}
else {
url = "https://codeload.github.com/" + user + "/" + repo + "/legacy.zip/refs/heads/" + branch;
Resclone.LOGGER.info("Getting from " + branch + " branch.");
}
return url;
}
private String getFromBranch(String user, String repo, String branch) {
return "https://github.com/" + user + "/" + repo + "/archive/refs/heads/" + branch + ".zip";
}
private String getFromTag(String user, String repo, String tag) {
return "https://github.com/" + user + "/" + repo + "/archive/refs/tags/" + tag + ".zip";
Resclone.LOGGER.info("Getting from tag " + tag + ".");
return "https://codeload.github.com/" + user + "/" + repo + "/legacy.zip/refs/tags/" + tag;
}
}

View File

@ -29,9 +29,14 @@ public abstract class PackFetcher {
}
}
Path p = targetDir.resolve(Integer.toHexString(url.hashCode()));
if (!forceDownload && Files.exists(p))
if (!forceDownload && Files.exists(p)) {
Resclone.COUNT++;
return new Result(p, false);
Resclone.LOGGER.info("Downloading pack " + url);
}
Resclone.LOGGER.info("Downloading pack: " + url);
try (InputStream is = new URL(url).openStream()) {
FileOutputStream os = new FileOutputStream(p.toFile());
byte[] dataBuffer = new byte[1024];
@ -43,6 +48,7 @@ public abstract class PackFetcher {
} catch (Throwable e) {
throw new Exception("Could not download pack", e);
}
Resclone.COUNT++;
return new Result(p, true);
}