Compare commits

...

2 Commits

Author SHA1 Message Date
Johannes Frohnmeyer 981d10d426
chore: update to 1.20.5
ci/woodpecker/push/jfmod Pipeline was successful Details
ci/woodpecker/tag/jfmod Pipeline was successful Details
2024-04-25 20:44:31 +02:00
Johannes Frohnmeyer 6df388aad7
feat: rewrite config/network IO for 1.20.5 2024-04-25 20:42:31 +02:00
17 changed files with 188 additions and 105 deletions

View File

@ -1,5 +1,5 @@
plugins {
id("jfmod") version "1.5-SNAPSHOT"
id("jfmod") version "1.6-SNAPSHOT"
}
loom {
@ -9,13 +9,14 @@ loom {
allprojects { group = "io.gitlab.jfronny" }
base.archivesName = "resclone"
val fabricVersion = "0.91.1+1.20.4"
val modmenuVersion = "9.0.0-pre.1"
val modmenuVersion = "10.0.0-beta.1"
val commonsVersion = "1.7-SNAPSHOT"
jfMod {
minecraftVersion = "1.20.4"
minecraftVersion = "1.20.5"
yarn("build.1")
loaderVersion = "0.15.0"
libJfVersion = "3.14.1"
loaderVersion = "0.15.10"
libJfVersion = "3.15.4"
fabricApiVersion = "0.97.6+1.20.5"
modrinth {
projectId = "resclone"
@ -28,12 +29,19 @@ jfMod {
}
dependencies {
include(modImplementation("io.gitlab.jfronny.libjf:libjf-base:${jfMod.libJfVersion.get()}")!!) // for JfCommons
include(modImplementation("io.gitlab.jfronny.libjf:libjf-config-core-v2:${jfMod.libJfVersion.get()}")!!) // for JfCommons
include(modImplementation(fabricApi.module("fabric-resource-loader-v0", fabricVersion))!!)
include(modImplementation("io.gitlab.jfronny.libjf:libjf-base")!!) // for JfCommons
include(modImplementation("io.gitlab.jfronny.libjf:libjf-config-core-v2")!!) // for JfCommons
include(modImplementation("net.fabricmc.fabric-api:fabric-resource-loader-v0")!!)
compileOnly("io.gitlab.jfronny:commons-serialize-generator-annotations:$commonsVersion")
annotationProcessor("io.gitlab.jfronny:commons-serialize-generator:$commonsVersion")
// Dev env
modLocalRuntime("io.gitlab.jfronny.libjf:libjf-config-ui-tiny:${jfMod.libJfVersion.get()}")
modLocalRuntime("io.gitlab.jfronny.libjf:libjf-devutil:${jfMod.libJfVersion.get()}")
modLocalRuntime("io.gitlab.jfronny.libjf:libjf-config-ui-tiny")
modLocalRuntime("io.gitlab.jfronny.libjf:libjf-devutil")
modLocalRuntime("com.terraformersmc:modmenu:$modmenuVersion")
// for modmenu
modLocalRuntime("net.fabricmc.fabric-api:fabric-resource-loader-v0")
modLocalRuntime("net.fabricmc.fabric-api:fabric-screen-api-v1")
modLocalRuntime("net.fabricmc.fabric-api:fabric-key-binding-api-v1")
}

View File

@ -1,7 +1,6 @@
package io.gitlab.jfronny.resclone;
import io.gitlab.jfronny.commons.logging.Logger;
import io.gitlab.jfronny.commons.serialize.gson.api.v2.GsonHolders;
import io.gitlab.jfronny.commons.logger.SystemLoggerPlus;
import io.gitlab.jfronny.resclone.data.PackMetaLoaded;
import io.gitlab.jfronny.resclone.data.PackMetaUnloaded;
import io.gitlab.jfronny.resclone.fetchers.*;
@ -26,7 +25,7 @@ public class Resclone implements ModInitializer {
public static final Set<PackMetaLoaded> NEW_PACKS = new LinkedHashSet<>(); // Client-only!
public static final String MOD_ID = "resclone";
public static final Logger LOGGER = Logger.forName(MOD_ID);
public static final SystemLoggerPlus LOGGER = SystemLoggerPlus.forName(MOD_ID);
public static final String USER_AGENT = "jfmods/" + MOD_ID + "/" + FabricLoader.getInstance()
.getModContainer(MOD_ID).orElseThrow()
.getMetadata()
@ -40,7 +39,6 @@ public class Resclone implements ModInitializer {
@Override
public void onInitialize() {
LOGGER.info("Initialising Resclone.");
GsonHolders.registerSerializer();
urlCache = new PackUrlCache(getConfigPath().resolve("urlCache.properties"));
FETCHER_INSTANCES.clear();
@ -58,7 +56,7 @@ public class Resclone implements ModInitializer {
}
reload();
LOGGER.info("Installed {} resource pack{}.", packCount, packCount == 1 ? "" : "s");
LOGGER.info("Installed {0} resource pack{1}.", packCount, packCount == 1 ? "" : "s");
}
public void addFetcher(PackFetcher fetcher) {
@ -87,9 +85,7 @@ public class Resclone implements ModInitializer {
LOGGER.info("No resclone pack was specified, add one");
}
else {
try {
//TODO migrate this to try-with-resources once JVM is updated
ExecutorService pool = Executors.newFixedThreadPool(RescloneConfig.packs.size());
try (ExecutorService pool = Executors.newFixedThreadPool(RescloneConfig.packs.size())) {
for (PackMetaUnloaded s : RescloneConfig.packs) {
pool.submit(generateTask(s, metas));
}
@ -111,15 +107,15 @@ public class Resclone implements ModInitializer {
private Runnable generateTask(PackMetaUnloaded meta, Set<PackMetaLoaded> metas) {
return () -> {
try {
if (!FETCHER_INSTANCES.containsKey(meta.fetcher))
throw new Exception("Invalid fetcher: " + meta.fetcher);
if (!FETCHER_INSTANCES.containsKey(meta.fetcher()))
throw new Exception("Invalid fetcher: " + meta.fetcher());
Path cacheDir = getConfigPath().resolve("cache");
PackMetaLoaded p;
try {
boolean isNew = !urlCache.containsKey(meta.source);
boolean isNew = !urlCache.containsKey(meta.source());
//Download
PackFetcher.Result fr = FETCHER_INSTANCES.get(meta.fetcher).get(meta.source, cacheDir, meta.forceDownload);
p = new PackMetaLoaded(fr.downloadPath(), meta.name, meta.forceEnable);
PackFetcher.Result fr = FETCHER_INSTANCES.get(meta.fetcher()).get(meta.source(), cacheDir, meta.forceDownload());
p = new PackMetaLoaded(fr.downloadPath(), meta.name(), meta.forceEnable());
metas.add(p);
if (isNew && FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) NEW_PACKS.add(p);
if (fr.freshDownload()) {
@ -139,7 +135,7 @@ public class Resclone implements ModInitializer {
throw new Exception("Failed to download pack", e);
}
} catch (Throwable e) {
LOGGER.error("Encountered issue while preparing " + meta.name, e);
LOGGER.error("Encountered issue while preparing " + meta.name(), e);
}
};
}

View File

@ -1,15 +1,18 @@
package io.gitlab.jfronny.resclone;
import com.google.gson.reflect.TypeToken;
import io.gitlab.jfronny.commons.serialize.gson.api.v2.GsonHolders;
import io.gitlab.jfronny.gson.JsonParseException;
import io.gitlab.jfronny.gson.stream.*;
import io.gitlab.jfronny.commons.serialize.MalformedDataException;
import io.gitlab.jfronny.commons.serialize.Token;
import io.gitlab.jfronny.commons.serialize.json.JsonReader;
import io.gitlab.jfronny.commons.serialize.json.JsonWriter;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.config.api.v2.JfCustomConfig;
import io.gitlab.jfronny.libjf.config.api.v2.dsl.DSL;
import io.gitlab.jfronny.resclone.data.GC_PackMetaUnloaded;
import io.gitlab.jfronny.resclone.data.PackMetaUnloaded;
import io.gitlab.jfronny.resclone.util.ListAdaptation;
import java.io.*;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
@ -26,7 +29,6 @@ public class RescloneConfig implements JfCustomConfig {
private static final String PRUNE_UNUSED = "pruneUnused";
private static final String FILTER_PACKS = "filterPacks";
private static final String LOG_PROCESSING = "logProcessing";
private static final Type META_SET = new TypeToken<Set<PackMetaUnloaded>>(){}.getType();
private static void load(Path path) throws IOException {
if (!Files.exists(path)) {
@ -40,42 +42,46 @@ public class RescloneConfig implements JfCustomConfig {
boolean updateRequired = false;
try (BufferedReader br = Files.newBufferedReader(path);
JsonReader reader = GsonHolders.CONFIG.getGson().newJsonReader(br)) {
if (reader.peek() == JsonToken.BEGIN_ARRAY) {
JsonReader reader = LibJf.LENIENT_TRANSPORT.createReader(br)) {
if (reader.peek() == Token.BEGIN_ARRAY) {
// Legacy format compatibility
packs = GsonHolders.CONFIG.getGson().fromJson(reader, META_SET);
packs = ListAdaptation.deserializeSet(reader, GC_PackMetaUnloaded::deserialize);
updateRequired = true;
} else if (reader.peek() == JsonToken.BEGIN_OBJECT) {
} else if (reader.peek() == Token.BEGIN_OBJECT) {
// New format
reader.beginObject();
Set<PackMetaUnloaded> packs = null;
Boolean pruneUnused = null;
Boolean filterPacks = null;
Boolean logProcessing = null;
while (reader.peek() != JsonToken.END_OBJECT) {
while (reader.peek() != Token.END_OBJECT) {
final String name = reader.nextName();
switch (name) {
case PACKS -> {
if (packs != null) throw new JsonParseException(ERR_DUPLICATE.formatted(PACKS));
packs = GsonHolders.CONFIG.getGson().fromJson(reader, META_SET);
if (packs != null) throw new MalformedDataException(ERR_DUPLICATE.formatted(PACKS));
if (reader.peek() == Token.BEGIN_ARRAY) {
packs = ListAdaptation.deserializeSet(reader, GC_PackMetaUnloaded::deserialize);
} else {
packs = Set.of(GC_PackMetaUnloaded.deserialize(reader));
}
}
case PRUNE_UNUSED -> {
if (pruneUnused != null) throw new JsonParseException(ERR_DUPLICATE.formatted(PRUNE_UNUSED));
if (pruneUnused != null) throw new MalformedDataException(ERR_DUPLICATE.formatted(PRUNE_UNUSED));
pruneUnused = reader.nextBoolean();
}
case FILTER_PACKS -> {
if (filterPacks != null) throw new JsonParseException(ERR_DUPLICATE.formatted(FILTER_PACKS));
if (filterPacks != null) throw new MalformedDataException(ERR_DUPLICATE.formatted(FILTER_PACKS));
filterPacks = reader.nextBoolean();
}
case LOG_PROCESSING -> {
if (logProcessing != null) throw new JsonParseException(ERR_DUPLICATE.formatted(LOG_PROCESSING));
if (logProcessing != null) throw new MalformedDataException(ERR_DUPLICATE.formatted(LOG_PROCESSING));
logProcessing = reader.nextBoolean();
}
default -> throw new JsonParseException("Unexpected element: \"" + name + "\" in Resclone config");
default -> throw new MalformedDataException("Unexpected element: \"" + name + "\" in Resclone config");
}
}
reader.endObject();
if (packs == null) throw new JsonParseException("Expected Resclone config object to contain packs");
if (packs == null) throw new MalformedDataException("Expected Resclone config object to contain packs");
if (pruneUnused == null) {
pruneUnused = true;
updateRequired = true;
@ -92,19 +98,23 @@ public class RescloneConfig implements JfCustomConfig {
RescloneConfig.pruneUnused = pruneUnused;
RescloneConfig.filterPacks = filterPacks;
RescloneConfig.logProcessing = logProcessing;
} else throw new JsonParseException("Expected Resclone config to be an object or array");
} else throw new MalformedDataException("Expected Resclone config to be an object or array");
}
if (updateRequired) write(path);
}
private static void write(Path path) throws IOException {
try (BufferedWriter bw = Files.newBufferedWriter(path);
JsonWriter writer = GsonHolders.CONFIG.getGson().newJsonWriter(bw)) {
JsonWriter writer = LibJf.LENIENT_TRANSPORT.createWriter(bw)) {
writer.beginObject()
.comment("The packs to be loaded by resclone")
.name(PACKS);
GsonHolders.CONFIG.getGson().toJson(packs, META_SET, writer);
writer.comment("Automatically remove all downloaded packs that are not in the config to free up unneeded space")
.name(PACKS)
.beginArray();
for (PackMetaUnloaded pack : packs) {
GC_PackMetaUnloaded.serialize(pack, writer);
}
writer.endArray()
.comment("Automatically remove all downloaded packs that are not in the config to free up unneeded space")
.name(PRUNE_UNUSED)
.value(pruneUnused)
.comment("Whether to filter packs to remove files unchanged from vanilla and empty directories")
@ -133,7 +143,7 @@ public class RescloneConfig implements JfCustomConfig {
Resclone.LOGGER.error("Could not write config", e);
}
}).setPath(path)
.<Set<PackMetaUnloaded>>value(PACKS, new HashSet<>(), Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, io.gitlab.jfronny.libjf.config.api.v2.type.Type.ofClass(META_SET), 100, () -> packs, p -> packs = p)
.<Set<PackMetaUnloaded>>value(PACKS, new HashSet<>(), Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, io.gitlab.jfronny.libjf.config.api.v2.type.Type.ofClass(new TypeToken<Set<PackMetaUnloaded>>(){}.getType()), 100, () -> packs, p -> packs = p)
.value(PRUNE_UNUSED, pruneUnused, () -> pruneUnused, p -> pruneUnused = p)
.value(FILTER_PACKS, filterPacks, () -> filterPacks, p -> filterPacks = p)
.value(LOG_PROCESSING, logProcessing, () -> logProcessing, p -> logProcessing = p)

View File

@ -11,16 +11,13 @@ import java.util.List;
public class RescloneResourcePack extends ZipResourcePack implements ModResourcePack {
private static final ModMetadata METADATA = FabricLoader.getInstance().getModContainer(Resclone.MOD_ID).orElseThrow().getMetadata();
private final String name;
private final ResourcePackInfo info;
private final ZipFileWrapper file;
RescloneResourcePack(ZipFileWrapper file, String name, String overlay) {
super(name, file, true, overlay);
this.name = name;
}
@Override
public String getName() {
return name;
RescloneResourcePack(ZipFileWrapper file, ResourcePackInfo info, String overlay) {
super(info, file, overlay);
this.info = info;
this.file = file;
}
@Override
@ -28,29 +25,34 @@ public class RescloneResourcePack extends ZipResourcePack implements ModResource
return METADATA;
}
@Override
public ModResourcePack createOverlay(String overlay) {
return new RescloneResourcePack(this.file, this.info, overlay);
}
public static class Factory implements ResourcePackProfile.PackFactory {
private final File file;
private final String name;
private final ResourcePackInfo info;
public Factory(File file, String name) {
public Factory(File file, ResourcePackInfo info) {
this.file = file;
this.name = name;
this.info = info;
}
@Override
public ResourcePack open(String name) {
public ResourcePack open(ResourcePackInfo info) {
ZipFileWrapper zipFileWrapper = new ZipFileWrapper(this.file);
return new RescloneResourcePack(zipFileWrapper, this.name, "");
return new RescloneResourcePack(zipFileWrapper, this.info, "");
}
@Override
public ResourcePack openWithOverlays(String name, ResourcePackProfile.Metadata metadata) {
public ResourcePack openWithOverlays(ResourcePackInfo info, ResourcePackProfile.Metadata metadata) {
ZipFileWrapper zipFileWrapper = new ZipFileWrapper(this.file);
ZipResourcePack resourcePack = new RescloneResourcePack(zipFileWrapper, this.name, "");
ZipResourcePack resourcePack = new RescloneResourcePack(zipFileWrapper, this.info, "");
List<String> overlays = metadata.overlays();
if (overlays.isEmpty()) return resourcePack;
List<ResourcePack> overlayPacks = new ArrayList<>(overlays.size());
for (String string : overlays) overlayPacks.add(new RescloneResourcePack(zipFileWrapper, this.name, string));
for (String string : overlays) overlayPacks.add(new RescloneResourcePack(zipFileWrapper, this.info, string));
return new OverlayResourcePack(resourcePack, overlayPacks);
}
}

View File

@ -1,20 +1,7 @@
package io.gitlab.jfronny.resclone.data;
// Represents a pack as present in the config
// Can't be a record since it'll need to be parsed by Gson
@SuppressWarnings("ClassCanBeRecord")
public class PackMetaUnloaded {
public final String fetcher;
public final String source;
public final String name;
public final boolean forceDownload;
public final boolean forceEnable;
import io.gitlab.jfronny.commons.serialize.generator.annotations.GSerializable;
public PackMetaUnloaded(String fetcher, String source, String name, boolean forceDownload, boolean forceEnable) {
this.fetcher = fetcher;
this.source = source;
this.name = name;
this.forceDownload = forceDownload;
this.forceEnable = forceEnable;
}
@GSerializable
public record PackMetaUnloaded(String fetcher, String source, String name, boolean forceDownload, boolean forceEnable) {
}

View File

@ -1,11 +1,15 @@
package io.gitlab.jfronny.resclone.data.curseforge;
import io.gitlab.jfronny.commons.serialize.generator.annotations.GSerializable;
import java.util.Date;
import java.util.List;
@GSerializable
public class GetModFilesResponse {
public List<Data> data;
@GSerializable
public static class Data {
public String downloadUrl;
public Date fileDate;

View File

@ -1,8 +1,12 @@
package io.gitlab.jfronny.resclone.data.curseforge;
import io.gitlab.jfronny.commons.serialize.generator.annotations.GSerializable;
@GSerializable
public class GetModResponse {
public Data data;
@GSerializable
public static class Data {
public Boolean allowModDistribution;
}

View File

@ -1,11 +1,15 @@
package io.gitlab.jfronny.resclone.data.github;
import io.gitlab.jfronny.commons.serialize.generator.annotations.GSerializable;
import java.util.List;
@GSerializable
public class Release {
public List<Asset> assets;
public String zipball_url;
@GSerializable
public static class Asset {
public String name;
public String content_type;

View File

@ -1,5 +1,8 @@
package io.gitlab.jfronny.resclone.data.github;
import io.gitlab.jfronny.commons.serialize.generator.annotations.GSerializable;
@GSerializable
public class Repository {
public String default_branch;
}

View File

@ -1,11 +1,13 @@
package io.gitlab.jfronny.resclone.data.modrinth;
import io.gitlab.jfronny.gson.annotations.SerializedName;
import io.gitlab.jfronny.commons.serialize.annotations.SerializedName;
import io.gitlab.jfronny.commons.serialize.generator.annotations.GSerializable;
import org.jetbrains.annotations.Nullable;
import java.util.Date;
import java.util.List;
@GSerializable
public class Version {
public String name;
public String version_number;
@ -24,6 +26,7 @@ public class Version {
public Integer downloads;
public List<File> files;
@GSerializable
public static class Dependency {
@Nullable public String version_id;
@Nullable public String project_id;
@ -43,6 +46,7 @@ public class Version {
listed, archived, draft, unlisted, scheduled, unknown
}
@GSerializable
public static class File {
public Hashes hashes;
public String url;
@ -51,6 +55,7 @@ public class Version {
public Integer size;
@Nullable public Type file_type;
@GSerializable
public static class Hashes {
public String sha512;
public String sha1;

View File

@ -1,13 +1,19 @@
package io.gitlab.jfronny.resclone.fetchers;
import io.gitlab.jfronny.commons.http.client.HttpClient;
import io.gitlab.jfronny.commons.serialize.json.JsonReader;
import io.gitlab.jfronny.commons.throwable.ThrowingFunction;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.resclone.Resclone;
import io.gitlab.jfronny.resclone.data.curseforge.GC_GetModFilesResponse;
import io.gitlab.jfronny.resclone.data.curseforge.GC_GetModResponse;
import io.gitlab.jfronny.resclone.data.curseforge.GetModFilesResponse;
import io.gitlab.jfronny.resclone.data.curseforge.GetModResponse;
import net.minecraft.MinecraftVersion;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.Random;
@ -33,7 +39,7 @@ public class CurseforgeFetcher extends BasePackFetcher {
@Override
public String getDownloadUrl(String baseUrl) throws Exception {
try {
GetModResponse response = GET(baseUrl, GetModResponse.class);
GetModResponse response = GET(baseUrl, GC_GetModResponse::deserialize);
if (!response.data.allowModDistribution)
throw new Exception("The author of " + baseUrl + " disabled access to this pack outside of the curseforge launcher");
@ -43,7 +49,7 @@ public class CurseforgeFetcher extends BasePackFetcher {
Date latestDate = null;
boolean foundMatchingVersion = false;
for (GetModFilesResponse.Data addon : GET(baseUrl + "/files", GetModFilesResponse.class).data) {
for (GetModFilesResponse.Data addon : GET(baseUrl + "/files", GC_GetModFilesResponse::deserialize).data) {
if (foundMatchingVersion && !addon.gameVersions.contains(version)) continue;
if (!foundMatchingVersion && addon.gameVersions.contains(version)) {
foundMatchingVersion = true;
@ -63,8 +69,11 @@ public class CurseforgeFetcher extends BasePackFetcher {
}
}
private static <T> T GET(String suffix, Class<T> klazz) throws URISyntaxException, IOException {
return HttpClient.get("https://api.curseforge.com/v1/mods/" + suffix).header("x-api-key", API_KEY).sendSerialized(klazz);
private static <T> T GET(String suffix, ThrowingFunction<JsonReader, T, IOException> klazz) throws URISyntaxException, IOException {
try (Reader r = HttpClient.get("https://api.curseforge.com/v1/mods/" + suffix).header("x-api-key", API_KEY).sendReader();
JsonReader jr = LibJf.LENIENT_TRANSPORT.createReader(r)) {
return klazz.apply(jr);
}
}
private static byte[] unsalt(byte[] data, int salt) {

View File

@ -1,12 +1,16 @@
package io.gitlab.jfronny.resclone.fetchers;
import io.gitlab.jfronny.commons.http.client.HttpClient;
import io.gitlab.jfronny.commons.serialize.json.JsonReader;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.resclone.Resclone;
import io.gitlab.jfronny.resclone.data.github.GC_Release;
import io.gitlab.jfronny.resclone.data.github.GC_Repository;
import io.gitlab.jfronny.resclone.data.github.Release;
import io.gitlab.jfronny.resclone.data.github.Repository;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.io.Reader;
import java.net.URISyntaxException;
public class GitHubFetcher extends BasePackFetcher {
@ -39,8 +43,9 @@ public class GitHubFetcher extends BasePackFetcher {
//"user/repo/release" - Gets from the latest release.
else if (parts[2].equalsIgnoreCase("release")) {
try {
Release latestRelease = HttpClient.get("https://api.github.com/repos/" + parts[0] + "/" + parts[1] + "/releases/latest").sendSerialized(Release.class);
try (Reader r = HttpClient.get("https://api.github.com/repos/" + parts[0] + "/" + parts[1] + "/releases/latest").sendReader();
JsonReader jr = LibJf.LENIENT_TRANSPORT.createReader(r)) {
Release latestRelease = GC_Release.deserialize(jr);
String res = null;
for (Release.Asset asset : latestRelease.assets) {
@ -73,8 +78,9 @@ public class GitHubFetcher extends BasePackFetcher {
private String getFromBranch(String repo, @Nullable String branch) {
if (branch == null) {
try {
branch = HttpClient.get("https://api.github.com/repos/" + repo).<Repository>sendSerialized(Repository.class).default_branch;
try (Reader r = HttpClient.get("https://api.github.com/repos/" + repo).sendReader();
JsonReader jr = LibJf.LENIENT_TRANSPORT.createReader(r)) {
branch = GC_Repository.deserialize(jr).default_branch;
} catch (IOException | URISyntaxException e) {
Resclone.LOGGER.error("Failed to fetch branch for " + repo + ". Choosing \"main\"", e);
branch = "main";

View File

@ -1,19 +1,21 @@
package io.gitlab.jfronny.resclone.fetchers;
import com.google.common.reflect.TypeToken;
import io.gitlab.jfronny.commons.http.client.HttpClient;
import io.gitlab.jfronny.commons.serialize.json.JsonReader;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.resclone.Resclone;
import io.gitlab.jfronny.resclone.data.modrinth.GC_Version;
import io.gitlab.jfronny.resclone.data.modrinth.Version;
import io.gitlab.jfronny.resclone.util.ListAdaptation;
import net.minecraft.MinecraftVersion;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Type;
import java.io.Reader;
import java.util.Date;
import java.util.List;
public class ModrinthFetcher extends BasePackFetcher {
private static final Type versionType = new TypeToken<List<Version>>() {}.getType();
@Override
public String getSourceTypeName() {
return "modrinth";
@ -28,9 +30,14 @@ public class ModrinthFetcher extends BasePackFetcher {
Date latestDate = null;
boolean foundMatchingVersion = false;
for (Version ver : HttpClient.get("https://api.modrinth.com/v2/project/" + baseUrl + "/version")
List<Version> versions;
try (Reader r = HttpClient.get("https://api.modrinth.com/v2/project/" + baseUrl + "/version")
.userAgent(Resclone.USER_AGENT)
.<List<Version>>sendSerialized(versionType)) {
.sendReader();
JsonReader jr = LibJf.LENIENT_TRANSPORT.createReader(r)) {
versions = ListAdaptation.deserializeList(jr, GC_Version::deserialize);
}
for (Version ver : versions) {
if (foundMatchingVersion && !ver.game_versions.contains(version)) continue;
if (ver.files.isEmpty()) continue;
if (!foundMatchingVersion && ver.game_versions.contains(version)) {
@ -54,7 +61,7 @@ public class ModrinthFetcher extends BasePackFetcher {
if (file.file_type == Version.File.Type.REQUIRED_RESOURCE_PACK) return file.url;
}
Resclone.LOGGER.error("Identification failed, using first file of " + baseUrl);
return latest.files.get(0).url;
return latest.files.getFirst().url;
} catch (Throwable e) {
throw new IOException("Could not get Modrinth download for " + baseUrl, e);
}

View File

@ -10,6 +10,7 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.Optional;
import java.util.function.Consumer;
@Mixin(FileResourcePackProvider.class)
@ -20,14 +21,21 @@ public class FileResourcePackProviderMixin {
@Inject(at = @At("TAIL"), method = "register(Ljava/util/function/Consumer;)V")
public void registerExtra(Consumer<ResourcePackProfile> consumer, CallbackInfo info) {
for (PackMetaLoaded meta : Resclone.DOWNLOADED_PACKS) {
ResourcePackInfo ifo = new ResourcePackInfo(
"resclone/" + meta.name(),
Text.literal(meta.name()),
source,
Optional.empty()
);
ResourcePackProfile resourcePackProfile = ResourcePackProfile.create(
"resclone/" + meta.name(),
Text.literal(meta.name()),
meta.forceEnable(),
new RescloneResourcePack.Factory(meta.zipPath().toFile(), meta.name()),
ifo,
new RescloneResourcePack.Factory(meta.zipPath().toFile(), ifo),
type,
ResourcePackProfile.InsertionPosition.TOP,
source
new ResourcePackPosition(
meta.forceEnable(),
ResourcePackProfile.InsertionPosition.TOP,
false
)
);
if (resourcePackProfile != null) {
consumer.accept(resourcePackProfile);

View File

@ -0,0 +1,31 @@
package io.gitlab.jfronny.resclone.util;
import io.gitlab.jfronny.commons.serialize.SerializeReader;
import io.gitlab.jfronny.commons.throwable.ThrowingFunction;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class ListAdaptation {
public static <T, TEx extends Exception, Reader extends SerializeReader<TEx, Reader>> List<T> deserializeList(Reader reader, ThrowingFunction<Reader, T, TEx> deserializeOne) throws TEx {
List<T> result = new ArrayList<>();
reader.beginArray();
while (reader.hasNext()) {
result.add(deserializeOne.apply(reader));
}
reader.endArray();
return result;
}
public static <T, TEx extends Exception, Reader extends SerializeReader<TEx, Reader>> Set<T> deserializeSet(Reader reader, ThrowingFunction<Reader, T, TEx> serializeOne) throws TEx {
Set<T> result = new LinkedHashSet<>();
reader.beginArray();
while (reader.hasNext()) {
result.add(serializeOne.apply(reader));
}
reader.endArray();
return result;
}
}

View File

@ -1,5 +1,5 @@
accessWidener v2 named
accessible class net/minecraft/resource/ZipResourcePack$ZipFileWrapper
extendable method net/minecraft/resource/ZipResourcePack <init> (Ljava/lang/String;Lnet/minecraft/resource/ZipResourcePack$ZipFileWrapper;ZLjava/lang/String;)V
accessible method net/minecraft/resource/ZipResourcePack <init> (Lnet/minecraft/resource/ResourcePackInfo;Lnet/minecraft/resource/ZipResourcePack$ZipFileWrapper;Ljava/lang/String;)V
accessible method net/minecraft/resource/ZipResourcePack$ZipFileWrapper <init> (Ljava/io/File;)V

View File

@ -2,7 +2,6 @@
"required": true,
"minVersion": "0.8",
"package": "io.gitlab.jfronny.resclone.mixin",
"compatibilityLevel": "JAVA_8",
"mixins": [
"FileResourcePackProviderMixin"
],