2021-10-30 14:22:07 +02:00
|
|
|
package io.gitlab.jfronny.inceptum.util.api;
|
|
|
|
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.fabric.FabricVersionLoaderInfo;
|
2021-10-30 16:12:39 +02:00
|
|
|
import io.gitlab.jfronny.inceptum.model.inceptum.InstanceMeta;
|
2021-10-30 14:22:07 +02:00
|
|
|
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<List<FabricVersionLoaderInfo>>() {}.getType();
|
|
|
|
private static final String META_URL = "https://meta.fabricmc.net/";
|
|
|
|
|
|
|
|
public static List<FabricVersionLoaderInfo> 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(VersionsListInfo gameVersion, String fabricVersion) throws IOException {
|
|
|
|
return Utils.downloadObject(META_URL + "v2/versions/loader/" + gameVersion.id + "/" + fabricVersion, FabricVersionLoaderInfo.WithMeta.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void addFabric(VersionInfo version, String fabricVersion) throws IOException {
|
|
|
|
FabricVersionLoaderInfo ver = getLoaderVersion(version, 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 = meta.mainClass.client;
|
|
|
|
List<VersionInfo.Library> libs = new ArrayList<>(version.libraries);
|
|
|
|
for (FabricVersionLoaderInfo.WithMeta.LauncherMeta.Libraries.Library library : meta.libraries.common)
|
|
|
|
libs.add(convertLib(library));
|
|
|
|
for (FabricVersionLoaderInfo.WithMeta.LauncherMeta.Libraries.Library library : meta.libraries.client)
|
|
|
|
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));
|
|
|
|
version.libraries = List.copyOf(libs);
|
2021-10-30 16:12:39 +02:00
|
|
|
version.id = InstanceMeta.floaderPrefix + fabricVersion + "-" + version.id;
|
2021-10-30 14:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|