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

70 lines
3.4 KiB
Java
Raw Normal View History

2022-09-04 21:21:24 +02:00
package io.gitlab.jfronny.inceptum.launcher.api;
2021-10-27 22:00:08 +02:00
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;
2022-04-28 23:13:37 +02:00
import io.gitlab.jfronny.commons.OSUtils;
2022-09-04 21:21:24 +02:00
import io.gitlab.jfronny.inceptum.common.MetaHolder;
import io.gitlab.jfronny.inceptum.common.Net;
import io.gitlab.jfronny.inceptum.launcher.model.mojang.*;
2021-10-27 22:00:08 +02:00
import java.io.IOException;
import java.net.URISyntaxException;
2021-10-27 22:00:08 +02:00
import java.nio.file.Files;
import java.nio.file.Path;
2021-10-30 19:26:59 +02:00
import java.util.List;
import java.util.Map;
2021-10-27 22:00:08 +02:00
2022-09-04 21:21:24 +02:00
import static io.gitlab.jfronny.inceptum.common.Net.downloadObject;
2021-10-27 22:00:08 +02:00
public class McApi {
public static VersionsList getVersions() {
try {
2022-11-02 00:38:04 +01:00
return downloadObject("https://launchermeta.mojang.com/mc/game/version_manifest_v2.json", GC_VersionsList::read);
2021-10-27 22:00:08 +02:00
} catch (IOException e) {
throw new RuntimeException("Could not load version manifest", e);
}
}
public static VersionInfo getVersionInfo(VersionsListInfo listInfo) throws IOException {
2022-11-02 00:38:04 +01:00
return downloadObject(listInfo.url, listInfo.sha1, GC_VersionInfo::read);
2021-10-27 22:00:08 +02:00
}
public static AssetIndex getAssetIndex(VersionInfo info) throws IOException, URISyntaxException {
Path file = MetaHolder.ASSETS_DIR.resolve("indexes");
2021-10-27 22:00:08 +02:00
if (!Files.exists(file)) Files.createDirectories(file);
file = file.resolve(info.assetIndex.url.substring(info.assetIndex.url.lastIndexOf("/") + 1));
2021-10-27 22:00:08 +02:00
try {
2022-09-04 21:21:24 +02:00
Net.downloadFile(info.assetIndex.url, info.assetIndex.sha1, file);
} catch (IOException | URISyntaxException e) {
2021-10-27 22:00:08 +02:00
if (!Files.exists(file)) throw e;
}
2022-11-02 00:38:04 +01:00
return GC_AssetIndex.read(file);
2021-10-27 22:00:08 +02:00
}
2021-10-30 19:26:59 +02:00
public static Map<String, JvmFileInfo.File> getJvm(String component, int majorVersion) throws IOException {
2021-12-01 16:21:01 +01:00
// https://github.com/ATLauncher/ATLauncher/blob/master/src/main/java/com/atlauncher/constants/Constants.java#L123
2022-11-02 00:38:04 +01:00
JvmInfo info = Net.downloadObject("https://launchermeta.mojang.com/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json", GC_JvmInfo::read);
2022-04-28 23:13:37 +02:00
Map<String, List<JvmInfo.Jvm>> vms = switch (OSUtils.TYPE) {
case WINDOWS -> info.windowsX64;
case LINUX -> info.linux;
case MAC_OS -> info.macOs;
2021-10-30 19:26:59 +02:00
};
List<JvmInfo.Jvm> vmList = vms[component];
2021-12-01 16:21:01 +01:00
if (vmList == null)
throw new IOException("Invalid component: " + component + " (available: " + String.join(", ", vms.keySet()));
for (JvmInfo.Jvm jvm : vmList) {
2021-10-30 19:26:59 +02:00
if (jvm.version.name.startsWith(Integer.toString(majorVersion))) {
2022-11-02 00:38:04 +01:00
return downloadObject(jvm.manifest.url, jvm.manifest.sha1, GC_JvmFileInfo::read).files;
2021-10-30 19:26:59 +02:00
}
}
throw new IOException("JVM not found");
}
public static void downloadAsset(AssetIndex.Asset asset, Path path) throws IOException, URISyntaxException {
2022-11-02 00:38:04 +01:00
Net.downloadFile("http://resources.download.minecraft.net/" + asset.hash.substring(0, 2) + "/" + asset.hash, asset.hash, path);
2021-10-27 22:00:08 +02:00
}
}