Improve curseforge fetcher

This commit is contained in:
JFronny 2021-02-19 12:39:22 +01:00
parent 58ffdcaeb0
commit 0fb768a90e
7 changed files with 75 additions and 29 deletions

View File

@ -11,18 +11,17 @@ archivesBaseName = project.archives_base_name
version = project.mod_version
group = project.maven_group
repositories {
maven { url = "https://maven.terraformersmc.com/"; name = "ModMenu" }
}
dependencies {
//to change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
// You may need to force-disable transitiveness on them.
modCompile("io.github.prospector:modmenu:1.14.9+build.14")
modCompile "com.terraformersmc:modmenu:1.16.7"
}
processResources {

View File

@ -2,13 +2,13 @@
org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.16.4
yarn_mappings=1.16.4+build.7
loader_version=0.10.8
minecraft_version=1.16.5
yarn_mappings=1.16.5+build.4
loader_version=0.11.1
# Mod Properties
mod_version=1.2.0
mod_version=1.3.0
maven_group=io.gitlab.jfronny
archives_base_name=resclone
# Dependencies
# check this on https://modmuss50.me/fabric.html
fabric_version=0.25.1+build.416-1.16
fabric_version=0.30.3+1.16

View File

@ -31,6 +31,7 @@ public class Resclone implements ModInitializer, RescloneApi {
@Override
public void onInitialize() {
System.out.println("[resclone] Beginning init, will download packs");
conf.clear();
fetcherInstances.clear();
processors.clear();
@ -45,6 +46,7 @@ public class Resclone implements ModInitializer, RescloneApi {
}
addProcessor(new RemoveEmptyProcessor());
reload();
System.out.println("[resclone] Completed");
}
@Override

View File

@ -0,0 +1,9 @@
package io.gitlab.jfronny.resclone.data;
import java.util.Set;
public class CfAddon {
public String downloadUrl;
public String fileDate;
public Set<String> gameVersion;
}

View File

@ -1,8 +1,11 @@
package io.gitlab.jfronny.resclone.fetchers;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.gitlab.jfronny.resclone.data.CfAddon;
import io.gitlab.jfronny.resclone.data.RescloneException;
import net.minecraft.MinecraftVersion;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CurseforgeFetcher extends PackFetcher {
@Override
@ -13,8 +16,31 @@ public class CurseforgeFetcher extends PackFetcher {
@Override
public String getDownloadUrl(String baseUrl) throws RescloneException {
try {
JsonObject latest = (JsonObject)readJsonFromURL("https://addons-ecs.forgesvc.net/api/v2/addon/" + baseUrl + "/files", JsonArray.class).get(0);
return latest.get("downloadUrl").getAsString();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
String version = MinecraftVersion.field_25319.getName();
CfAddon latest = null;
Date latestDate = null;
boolean foundMatchingVersion = false;
for (CfAddon addon : readJsonFromURLSet("https://addons-ecs.forgesvc.net/api/v2/addon/" + baseUrl + "/files", CfAddon.class)) {
Date d = df.parse(addon.fileDate);
if (foundMatchingVersion && !addon.gameVersion.contains(version))
continue;
if (!foundMatchingVersion && addon.gameVersion.contains(version)) {
foundMatchingVersion = true;
latest = null;
}
if (latest == null || d.after(latestDate)) {
latest = addon;
latestDate = d;
}
}
if (!foundMatchingVersion)
System.err.println("Could not find pack for matching version, using latest");
return latest.downloadUrl;
} catch (Throwable e) {
throw new RescloneException("Could not get CF download for " + baseUrl, e);
}

View File

@ -1,6 +1,7 @@
package io.gitlab.jfronny.resclone.fetchers;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.gitlab.jfronny.resclone.Resclone;
import io.gitlab.jfronny.resclone.data.RescloneException;
@ -12,6 +13,7 @@ import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Scanner;
import java.util.Set;
public abstract class PackFetcher {
abstract public String getSourceTypeName(); // The name for users to specify in the config
@ -41,8 +43,13 @@ public abstract class PackFetcher {
return gson.fromJson(readStringFromURL(requestUrl), classOfT);
}
public <T> Set<T> readJsonFromURLSet(String requestUrl, Class<T> classOfT) throws IOException {
return gson.fromJson(readStringFromURL(requestUrl), TypeToken.getParameterized(Set.class, classOfT).getType());
}
public void get(String baseUrl, Path targetPath) throws RescloneException {
String url = getDownloadUrl(baseUrl);
System.out.println("Downloading pack " + url);
try (InputStream is = new URL(url).openStream()) {
FileOutputStream os = new FileOutputStream(targetPath.toFile());
byte[] dataBuffer = new byte[1024];
@ -50,6 +57,7 @@ public abstract class PackFetcher {
while ((bytesRead = is.read(dataBuffer, 0, 1024)) != -1) {
os.write(dataBuffer, 0, bytesRead);
}
System.out.println("Completed download");
}
catch (Throwable e) {
throw new RescloneException("Could not download pack", e);

View File

@ -21,21 +21,23 @@ public class PruneVanillaProcessor extends PackProcessor {
public void process(FileSystem p) throws RescloneException {
ClassLoader cl = MinecraftClient.class.getClassLoader();
try {
Files.walkFileTree(p.getPath("/assets/minecraft"), new PathPruneVisitor((s) -> {
if (Files.isDirectory(s))
return false;
try {
InputStream vn = cl.getResourceAsStream(p.getPath("/").relativize(s).toString());
if (vn != null) {
InputStream pk = Files.newInputStream(s, StandardOpenOption.READ);
return IOUtils.contentEquals(vn, pk);
if (Files.isDirectory(p.getPath("/assets/minecraft"))) {
Files.walkFileTree(p.getPath("/assets/minecraft"), new PathPruneVisitor((s) -> {
if (Files.isDirectory(s))
return false;
try {
InputStream vn = cl.getResourceAsStream(p.getPath("/").relativize(s).toString());
if (vn != null) {
InputStream pk = Files.newInputStream(s, StandardOpenOption.READ);
return IOUtils.contentEquals(vn, pk);
}
}
}
catch (Throwable e) {
e.printStackTrace();
}
return false;
}));
catch (Throwable e) {
e.printStackTrace();
}
return false;
}));
}
} catch (IOException e) {
throw new RescloneException("Could not prune vanilla files", e);
}