package io.gitlab.jfronny.inceptum.util.api; import com.google.gson.reflect.TypeToken; import io.gitlab.jfronny.inceptum.model.fabric.FabricVersionLoaderInfo; import io.gitlab.jfronny.inceptum.model.inceptum.InstanceMeta; import io.gitlab.jfronny.inceptum.model.mojang.Rules; import io.gitlab.jfronny.inceptum.model.mojang.VersionInfo; import io.gitlab.jfronny.inceptum.model.mojang.VersionsListInfo; import io.gitlab.jfronny.inceptum.util.Utils; import java.io.IOException; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class FabricMetaApi { private static final Type fabricLoaderVersionListType = new TypeToken>() {}.getType(); private static final String META_URL = "https://meta.fabricmc.net/"; public static List getLoaderVersions(VersionsListInfo version) { try { return Utils.downloadObject(META_URL + "v2/versions/loader/" + version.id, fabricLoaderVersionListType); } catch (IOException e) { throw new RuntimeException("Could not get fabric loader versions", e); } } public static FabricVersionLoaderInfo getLoaderVersion(String gameVersion, String fabricVersion) throws IOException { return Utils.downloadObject(META_URL + "v2/versions/loader/" + gameVersion + "/" + fabricVersion, FabricVersionLoaderInfo.WithMeta.class); } //TODO don't mutate the object to allow cache, then re-enable cache in McApi public static void addFabric(VersionInfo version, String fabricVersion, FabricVersionInfoType type) throws IOException { FabricVersionLoaderInfo ver = getLoaderVersion(version.id, fabricVersion); if (!(ver instanceof FabricVersionLoaderInfo.WithMeta verWithMeta)) throw new IOException("Doesn't hold metadata"); FabricVersionLoaderInfo.WithMeta.LauncherMeta meta = verWithMeta.launcherMeta; if (meta.version != 1) throw new IOException("Unsupported fabric launcherMeta version: " + meta.version); version.mainClass = type == FabricVersionInfoType.Server ? meta.mainClass.server : meta.mainClass.client; List libs = new ArrayList<>(version.libraries); for (FabricVersionLoaderInfo.WithMeta.LauncherMeta.Libraries.Library library : meta.libraries.common) libs.add(convertLib(library)); if (type == FabricVersionInfoType.Client || type == FabricVersionInfoType.Both) { for (FabricVersionLoaderInfo.WithMeta.LauncherMeta.Libraries.Library library : meta.libraries.client) libs.add(convertLib(library)); } if (type == FabricVersionInfoType.Server || type == FabricVersionInfoType.Both) { for (FabricVersionLoaderInfo.WithMeta.LauncherMeta.Libraries.Library library : meta.libraries.server) libs.add(convertLib(library)); } var floader = new FabricVersionLoaderInfo.WithMeta.LauncherMeta.Libraries.Library(); floader.name = "net.fabricmc:fabric-loader:" + fabricVersion; floader.url = "https://maven.fabricmc.net/"; libs.add(convertLib(floader)); floader.name = ver.intermediary.maven; libs.add(convertLib(floader)); version.libraries = List.copyOf(libs); version.id = InstanceMeta.floaderPrefix + fabricVersion + "-" + version.id; } private static VersionInfo.Library convertLib(FabricVersionLoaderInfo.WithMeta.LauncherMeta.Libraries.Library library) { VersionInfo.Library res = new VersionInfo.Library(); res.name = library.name; res.rules = new Rules(true); res.natives = new HashMap<>(); res.downloads = new VersionInfo.Library.Downloads(); res.downloads.classifiers = null; res.downloads.artifact = new VersionInfo.Library.Downloads.Artifact(); String[] lib = library.name.split(":"); assert lib.length == 3; res.downloads.artifact.path = lib[0].replace('.', '/') + '/' + lib[1] + '/' + lib[2] + '/' + lib[1] + '-' + lib[2] + ".jar"; res.downloads.artifact.size = -1; res.downloads.artifact.sha1 = null; res.downloads.artifact.url = library.url + res.downloads.artifact.path; return res; } public enum FabricVersionInfoType { Client, Server, Both } }