Inceptum/src/main/java/io/gitlab/jfronny/inceptum/util/api/McApi.java

68 lines
2.8 KiB
Java

package io.gitlab.jfronny.inceptum.util.api;
import io.gitlab.jfronny.inceptum.Inceptum;
import io.gitlab.jfronny.inceptum.model.mojang.*;
import io.gitlab.jfronny.inceptum.util.OSCheck;
import io.gitlab.jfronny.inceptum.util.Utils;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import static io.gitlab.jfronny.inceptum.util.Utils.downloadObject;
public class McApi {
public static VersionsList getVersions() {
try {
return downloadObject("https://launchermeta.mojang.com/mc/game/version_manifest.json", VersionsList.class);
} catch (IOException e) {
throw new RuntimeException("Could not load version manifest", e);
}
}
public static VersionInfo getVersionInfo(VersionsListInfo listInfo) throws IOException {
return downloadObject(listInfo.url, VersionInfo.class);
}
public static AssetIndex getAssetIndex(VersionInfo info) throws IOException {
Path file = Inceptum.ASSETS_DIR.resolve("indexes");
if (!Files.exists(file)) Files.createDirectories(file);
file = file.resolve(info.assetIndex.url.substring(info.assetIndex.url.lastIndexOf("/") + 1));
try {
Utils.downloadFile(info.assetIndex.url, info.assetIndex.sha1, file);
}
catch (IOException e) {
if (!Files.exists(file)) throw e;
}
return Utils.loadObject(file, AssetIndex.class);
}
public static Map<String, JvmFileInfo.File> getJvm(String component, int majorVersion) throws IOException {
// https://github.com/ATLauncher/ATLauncher/blob/master/src/main/java/com/atlauncher/constants/Constants.java#L116
JvmInfo info = Utils.downloadObject("https://launchermeta.mojang.com/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json", JvmInfo.class);
JvmInfo.Jvms vms = switch (OSCheck.OS) {
case WINDOWS -> info.windowsX64;
case LINUX -> info.linux;
case MAC_OS -> info.macOs;
};
List<JvmInfo.Jvms.Jvm> vmList = switch (component) {
case "java-runtime-alpha" -> vms.javaRuntimeAlpha;
case "jre-legacy" -> vms.jreLegacy;
default -> throw new IOException("Invalid JVM component: " + component);
};
for (JvmInfo.Jvms.Jvm jvm : vmList) {
if (jvm.version.name.startsWith(Integer.toString(majorVersion))) {
return downloadObject(jvm.manifest.url, jvm.manifest.sha1, JvmFileInfo.class).files;
}
}
throw new IOException("JVM not found");
}
public static void downloadAsset(AssetIndex.Asset asset, Path path) throws IOException {
String url = "http://resources.download.minecraft.net/" + asset.hash.substring(0, 2) + "/" + asset.hash;
Utils.downloadFile(url, asset.hash, path);
}
}