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

70 lines
3.4 KiB
Java

package io.gitlab.jfronny.inceptum.launcher.api;
import gsoncompile.extensions.io.gitlab.jfronny.inceptum.launcher.model.mojang.AssetIndex.GC_AssetIndex;
import gsoncompile.extensions.io.gitlab.jfronny.inceptum.launcher.model.mojang.JvmFileInfo.GC_JvmFileInfo;
import gsoncompile.extensions.io.gitlab.jfronny.inceptum.launcher.model.mojang.JvmInfo.GC_JvmInfo;
import gsoncompile.extensions.io.gitlab.jfronny.inceptum.launcher.model.mojang.VersionInfo.GC_VersionInfo;
import gsoncompile.extensions.io.gitlab.jfronny.inceptum.launcher.model.mojang.VersionsList.GC_VersionsList;
import io.gitlab.jfronny.commons.OSUtils;
import io.gitlab.jfronny.inceptum.common.MetaHolder;
import io.gitlab.jfronny.inceptum.common.Net;
import io.gitlab.jfronny.inceptum.launcher.model.mojang.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import static io.gitlab.jfronny.inceptum.common.Net.downloadObject;
public class McApi {
public static VersionsList getVersions() {
try {
return downloadObject("https://launchermeta.mojang.com/mc/game/version_manifest_v2.json", GC_VersionsList::read);
} catch (IOException e) {
throw new RuntimeException("Could not load version manifest", e);
}
}
public static VersionInfo getVersionInfo(VersionsListInfo listInfo) throws IOException {
return downloadObject(listInfo.url, listInfo.sha1, GC_VersionInfo::read);
}
public static AssetIndex getAssetIndex(VersionInfo info) throws IOException, URISyntaxException {
Path file = MetaHolder.ASSETS_DIR.resolve("indexes");
if (!Files.exists(file)) Files.createDirectories(file);
file = file.resolve(info.assetIndex.url.substring(info.assetIndex.url.lastIndexOf("/") + 1));
try {
Net.downloadFile(info.assetIndex.url, info.assetIndex.sha1, file);
} catch (IOException | URISyntaxException e) {
if (!Files.exists(file)) throw e;
}
return GC_AssetIndex.read(file);
}
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#L123
JvmInfo info = Net.downloadObject("https://launchermeta.mojang.com/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json", GC_JvmInfo::read);
Map<String, List<JvmInfo.Jvm>> vms = switch (OSUtils.TYPE) {
case WINDOWS -> info.windowsX64;
case LINUX -> info.linux;
case MAC_OS -> info.macOs;
};
List<JvmInfo.Jvm> vmList = vms[component];
if (vmList == null)
throw new IOException("Invalid component: " + component + " (available: " + String.join(", ", vms.keySet()));
for (JvmInfo.Jvm jvm : vmList) {
if (jvm.version.name.startsWith(Integer.toString(majorVersion))) {
return downloadObject(jvm.manifest.url, jvm.manifest.sha1, GC_JvmFileInfo::read).files;
}
}
throw new IOException("JVM not found");
}
public static void downloadAsset(AssetIndex.Asset asset, Path path) throws IOException, URISyntaxException {
Net.downloadFile("http://resources.download.minecraft.net/" + asset.hash.substring(0, 2) + "/" + asset.hash, asset.hash, path);
}
}