2023-02-26 14:00:07 +01:00
|
|
|
package io.gitlab.jfronny.resclone.fetchers;
|
|
|
|
|
|
|
|
import com.google.common.reflect.TypeToken;
|
2023-09-22 20:34:51 +02:00
|
|
|
import io.gitlab.jfronny.commons.http.client.HttpClient;
|
2023-02-26 14:00:07 +01:00
|
|
|
import io.gitlab.jfronny.resclone.Resclone;
|
|
|
|
import io.gitlab.jfronny.resclone.data.modrinth.Version;
|
|
|
|
import net.minecraft.MinecraftVersion;
|
|
|
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDownloadUrl(String baseUrl) throws Exception {
|
|
|
|
try {
|
|
|
|
String version = MinecraftVersion.CURRENT.getName();
|
|
|
|
|
|
|
|
Version latest = null;
|
|
|
|
Date latestDate = null;
|
|
|
|
boolean foundMatchingVersion = false;
|
|
|
|
|
2023-09-22 20:34:51 +02:00
|
|
|
for (Version ver : HttpClient.get("https://api.modrinth.com/v2/project/" + baseUrl + "/version")
|
2023-02-26 14:00:07 +01:00
|
|
|
.userAgent(Resclone.USER_AGENT)
|
|
|
|
.<List<Version>>sendSerialized(versionType)) {
|
|
|
|
if (foundMatchingVersion && !ver.game_versions.contains(version)) continue;
|
|
|
|
if (ver.files.isEmpty()) continue;
|
|
|
|
if (!foundMatchingVersion && ver.game_versions.contains(version)) {
|
|
|
|
foundMatchingVersion = true;
|
|
|
|
latest = null;
|
|
|
|
}
|
|
|
|
if (latest == null || ver.date_published.after(latestDate)) {
|
|
|
|
latest = ver;
|
|
|
|
latestDate = ver.date_published;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (latest == null) throw new FileNotFoundException("Could not identify valid version");
|
|
|
|
if (!foundMatchingVersion) Resclone.LOGGER.error("Could not find matching version of " + baseUrl + ", using latest");
|
|
|
|
|
|
|
|
for (Version.File file : latest.files) {
|
|
|
|
if (file.primary) return file.url;
|
|
|
|
}
|
|
|
|
Resclone.LOGGER.error("Could not identify primary file of " + baseUrl + ", attempting identification by file_type");
|
|
|
|
for (Version.File file : latest.files) {
|
|
|
|
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;
|
|
|
|
} catch (Throwable e) {
|
|
|
|
throw new IOException("Could not get Modrinth download for " + baseUrl, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|