fix: fabric support from BMCLAPI and mcbbs

This commit is contained in:
huanghongxun
2020-02-13 14:34:48 +08:00
parent aa2838d219
commit 37d62b665e
3 changed files with 117 additions and 52 deletions

View File

@@ -85,7 +85,7 @@ public class BMCLAPIDownloadProvider implements DownloadProvider {
.replaceFirst("https?://files\\.minecraftforge\\.net/maven", apiRoot + "/maven")
.replace("http://dl.liteloader.com/versions/versions.json", apiRoot + "/maven/com/mumfrey/liteloader/versions.json")
.replace("http://dl.liteloader.com/versions", apiRoot + "/maven")
.replace("https://meta.fabricmc.net/v2/versions/game", apiRoot + "/fabric-meta/v2/versions")
.replace("https://meta.fabricmc.net", apiRoot + "/fabric-meta")
.replace("https://maven.fabricmc.net", apiRoot + "/maven")
.replace("https://authlib-injector.yushi.moe", apiRoot + "/mirrors/authlib-injector");
}

View File

@@ -20,6 +20,7 @@ package org.jackhuang.hmcl.download.fabric;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.download.LibraryAnalyzer;
import org.jackhuang.hmcl.game.Arguments;
import org.jackhuang.hmcl.game.Artifact;
import org.jackhuang.hmcl.game.Library;
@@ -29,13 +30,7 @@ import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.NetworkUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.*;
/**
* <b>Note</b>: Fabric should be installed first.
@@ -55,7 +50,7 @@ public final class FabricInstallTask extends Task<Version> {
this.version = version;
this.remote = remoteVersion;
launchMetaTask = new GetTask(NetworkUtils.toURL(getLaunchMetaUrl(remote.getSelfVersion())))
launchMetaTask = new GetTask(NetworkUtils.toURL(dependencyManager.getDownloadProvider().injectURL(getLaunchMetaUrl(remote.getGameVersion(), remote.getSelfVersion()))))
.setCacheRepository(dependencyManager.getCacheRepository());
}
@@ -86,32 +81,33 @@ public final class FabricInstallTask extends Task<Version> {
}
@Override
public void execute() throws IOException {
setResult(getPatch(JsonUtils.GSON.fromJson(launchMetaTask.getResult(), JsonObject.class), remote.getGameVersion(), remote.getSelfVersion()));
public void execute() {
setResult(getPatch(JsonUtils.GSON.fromJson(launchMetaTask.getResult(), FabricInfo.class), remote.getGameVersion(), remote.getSelfVersion()));
dependencies.add(dependencyManager.checkLibraryCompletionAsync(getResult()));
}
private static String getLaunchMetaUrl(String loaderVersion) {
return String.format("%s/%s/%s/%s/%3$s-%4$s.json", "https://maven.fabricmc.net/", "net/fabricmc", "fabric-loader", loaderVersion);
private static String getLaunchMetaUrl(String gameVersion, String loaderVersion) {
return String.format("https://meta.fabricmc.net/v2/versions/loader/%s/%s", gameVersion, loaderVersion);
}
private Version getPatch(JsonObject jsonObject, String gameVersion, String loaderVersion) {
private Version getPatch(FabricInfo fabricInfo, String gameVersion, String loaderVersion) {
JsonObject launcherMeta = fabricInfo.launcherMeta;
Arguments arguments = new Arguments();
String mainClass;
if (!jsonObject.get("mainClass").isJsonObject()) {
mainClass = jsonObject.get("mainClass").getAsString();
if (!launcherMeta.get("mainClass").isJsonObject()) {
mainClass = launcherMeta.get("mainClass").getAsString();
} else {
mainClass = jsonObject.get("mainClass").getAsJsonObject().get("client").getAsString();
mainClass = launcherMeta.get("mainClass").getAsJsonObject().get("client").getAsString();
}
if (jsonObject.has("launchwrapper")) {
String clientTweaker = jsonObject.get("launchwrapper").getAsJsonObject().get("tweakers").getAsJsonObject().get("client").getAsJsonArray().get(0).getAsString();
if (launcherMeta.has("launchwrapper")) {
String clientTweaker = launcherMeta.get("launchwrapper").getAsJsonObject().get("tweakers").getAsJsonObject().get("client").getAsJsonArray().get(0).getAsString();
arguments = arguments.addGameArguments("--tweakClass", clientTweaker);
}
JsonObject librariesObject = jsonObject.getAsJsonObject("libraries");
JsonObject librariesObject = launcherMeta.getAsJsonObject("libraries");
List<Library> libraries = new ArrayList<>();
// "common, server" is hard coded in fabric installer.
@@ -122,10 +118,94 @@ public final class FabricInstallTask extends Task<Version> {
}
}
libraries.add(new Library(new Artifact("net.fabricmc", "intermediary", gameVersion), "https://maven.fabricmc.net/", null));
libraries.add(new Library(new Artifact("net.fabricmc", "fabric-loader", loaderVersion), "https://maven.fabricmc.net/", null));
libraries.add(new Library(Artifact.fromDescriptor(fabricInfo.intermediary.maven), "https://maven.fabricmc.net/", null));
libraries.add(new Library(Artifact.fromDescriptor(fabricInfo.loader.maven), "https://maven.fabricmc.net/", null));
return new Version("net.fabricmc", loaderVersion, 30000, arguments, mainClass, libraries);
return new Version(LibraryAnalyzer.LibraryType.FABRIC.getPatchId(), loaderVersion, 30000, arguments, mainClass, libraries);
}
public static class FabricInfo {
private final LoaderInfo loader;
private final IntermediaryInfo intermediary;
private final JsonObject launcherMeta;
public FabricInfo(LoaderInfo loader, IntermediaryInfo intermediary, JsonObject launcherMeta) {
this.loader = loader;
this.intermediary = intermediary;
this.launcherMeta = launcherMeta;
}
public LoaderInfo getLoader() {
return loader;
}
public IntermediaryInfo getIntermediary() {
return intermediary;
}
public JsonObject getLauncherMeta() {
return launcherMeta;
}
}
public static class LoaderInfo {
private final String separator;
private final int build;
private final String maven;
private final String version;
private final boolean stable;
public LoaderInfo(String separator, int build, String maven, String version, boolean stable) {
this.separator = separator;
this.build = build;
this.maven = maven;
this.version = version;
this.stable = stable;
}
public String getSeparator() {
return separator;
}
public int getBuild() {
return build;
}
public String getMaven() {
return maven;
}
public String getVersion() {
return version;
}
public boolean isStable() {
return stable;
}
}
public static class IntermediaryInfo {
private final String maven;
private final String version;
private final boolean stable;
public IntermediaryInfo(String maven, String version, boolean stable) {
this.maven = maven;
this.version = version;
this.stable = stable;
}
public String getMaven() {
return maven;
}
public String getVersion() {
return version;
}
public boolean isStable() {
return stable;
}
}
public static class UnsupportedFabricInstallationException extends Exception {

View File

@@ -23,14 +23,11 @@ import org.jackhuang.hmcl.download.VersionList;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jetbrains.annotations.Nullable;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@@ -51,8 +48,8 @@ public final class FabricVersionList extends VersionList<FabricRemoteVersion> {
return new Task<Void>() {
@Override
public void execute() throws IOException, XMLStreamException {
List<String> gameVersions = getGameVersions(META_URL);
List<String> loaderVersions = getVersions(FABRIC_MAVEN_URL, FABRIC_PACKAGE_NAME, FABRIC_JAR_NAME);
List<String> gameVersions = getGameVersions(GAME_META_URL);
List<String> loaderVersions = getGameVersions(LOADER_META_URL);
lock.writeLock().lock();
@@ -67,27 +64,8 @@ public final class FabricVersionList extends VersionList<FabricRemoteVersion> {
};
}
private static final String META_URL = "https://meta.fabricmc.net/v2/versions/game";
private static final String FABRIC_MAVEN_URL = "https://maven.fabricmc.net/";
private static final String FABRIC_PACKAGE_NAME = "net/fabricmc";
private static final String FABRIC_JAR_NAME = "fabric-loader";
private List<String> getVersions(String mavenServerURL, String packageName, String jarName) throws IOException, XMLStreamException {
List<String> versions = new ArrayList<>();
URL url = new URL(downloadProvider.injectURL(mavenServerURL + packageName + "/" + jarName + "/maven-metadata.xml"));
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(url.openStream());
while(reader.hasNext()) {
if (reader.next() == 1 && reader.getLocalName().equals("version")) {
String text = reader.getElementText();
versions.add(text);
}
}
reader.close();
Collections.reverse(versions);
return versions;
}
private static final String LOADER_META_URL = "https://meta.fabricmc.net/v2/versions/loader";
private static final String GAME_META_URL = "https://meta.fabricmc.net/v2/versions/game";
private List<String> getGameVersions(String metaUrl) throws IOException {
String json = NetworkUtils.doGet(NetworkUtils.toURL(downloadProvider.injectURL(metaUrl)));
@@ -97,14 +75,16 @@ public final class FabricVersionList extends VersionList<FabricRemoteVersion> {
private static class GameVersion {
private final String version;
private final String maven;
private final boolean stable;
public GameVersion() {
this("", false);
this("", null, false);
}
public GameVersion(String version, boolean stable) {
public GameVersion(String version, String maven, boolean stable) {
this.version = version;
this.maven = maven;
this.stable = stable;
}
@@ -112,6 +92,11 @@ public final class FabricVersionList extends VersionList<FabricRemoteVersion> {
return version;
}
@Nullable
public String getMaven() {
return maven;
}
public boolean isStable() {
return stable;
}