feat: WIP: Fabric API install

This commit is contained in:
huanghongxun
2021-09-02 02:04:19 +08:00
parent 57bb719cb8
commit 96987763a5
13 changed files with 262 additions and 44 deletions

View File

@@ -50,6 +50,7 @@ public class InstallerItem extends Control {
private final String imageUrl;
public final StringProperty libraryVersion = new SimpleStringProperty();
public final StringProperty incompatibleLibraryName = new SimpleStringProperty();
public final StringProperty dependencyName = new SimpleStringProperty();
public final BooleanProperty incompatibleWithGame = new SimpleBooleanProperty();
public final BooleanProperty removable = new SimpleBooleanProperty();
public final BooleanProperty upgradable = new SimpleBooleanProperty(false);
@@ -69,6 +70,7 @@ public class InstallerItem extends Control {
imageUrl = "/assets/img/grass.png";
break;
case "fabric":
case "fabric-api":
imageUrl = "/assets/img/fabric.png";
break;
case "forge":
@@ -104,6 +106,7 @@ public class InstallerItem extends Control {
public static class InstallerItemGroup {
public final InstallerItem game = new InstallerItem(MINECRAFT);
public final InstallerItem fabric = new InstallerItem(FABRIC);
public final InstallerItem fabricApi = new InstallerItem(FABRIC_API);
public final InstallerItem forge = new InstallerItem(FORGE);
public final InstallerItem liteLoader = new InstallerItem(LITELOADER);
public final InstallerItem optiFine = new InstallerItem(OPTIFINE);
@@ -124,16 +127,23 @@ public class InstallerItem extends Control {
return null;
}, fabric.libraryVersion));
fabric.incompatibleLibraryName.bind(Bindings.createStringBinding(() -> {
if (liteLoader.libraryVersion.get() != null) return LITELOADER.getPatchId();
if (optiFine.libraryVersion.get() != null) return OPTIFINE.getPatchId();
if (forge.libraryVersion.get() != null) return FORGE.getPatchId();
return null;
}, optiFine.libraryVersion, forge.libraryVersion));
for (InstallerItem fabric : new InstallerItem[]{fabric, fabricApi}) {
fabric.incompatibleLibraryName.bind(Bindings.createStringBinding(() -> {
if (liteLoader.libraryVersion.get() != null) return LITELOADER.getPatchId();
if (optiFine.libraryVersion.get() != null) return OPTIFINE.getPatchId();
if (forge.libraryVersion.get() != null) return FORGE.getPatchId();
return null;
}, optiFine.libraryVersion, forge.libraryVersion));
}
fabricApi.dependencyName.bind(Bindings.createStringBinding(() -> {
if (fabric.libraryVersion.get() == null) return FABRIC.getPatchId();
else return null;
}, fabric.libraryVersion));
}
public InstallerItem[] getLibraries() {
return new InstallerItem[]{game, fabric, forge, liteLoader, optiFine};
return new InstallerItem[]{game, forge, liteLoader, optiFine, fabric, fabricApi};
}
}

View File

@@ -28,6 +28,7 @@ import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import org.jackhuang.hmcl.download.fabric.FabricAPIInstallTask;
import org.jackhuang.hmcl.download.fabric.FabricInstallTask;
import org.jackhuang.hmcl.download.forge.ForgeNewInstallTask;
import org.jackhuang.hmcl.download.forge.ForgeOldInstallTask;
@@ -119,6 +120,8 @@ public final class TaskListPane extends StackPane {
task.setName(i18n("install.installer.install", i18n("install.installer.optifine")));
} else if (task instanceof FabricInstallTask) {
task.setName(i18n("install.installer.install", i18n("install.installer.fabric")));
} else if (task instanceof FabricAPIInstallTask) {
task.setName(i18n("install.installer.install", i18n("install.installer.fabric-api")));
} else if (task instanceof CurseCompletionTask) {
task.setName(i18n("modpack.type.curse.completion"));
} else if (task instanceof ModpackInstallTask) {
@@ -212,6 +215,7 @@ public final class TaskListPane extends StackPane {
case "hmcl.install.liteloader": message = i18n("install.installer.install", i18n("install.installer.liteloader") + " " + stageValue); break;
case "hmcl.install.optifine": message = i18n("install.installer.install", i18n("install.installer.optifine") + " " + stageValue); break;
case "hmcl.install.fabric": message = i18n("install.installer.install", i18n("install.installer.fabric") + " " + stageValue); break;
case "hmcl.install.fabric-api": message = i18n("install.installer.install", i18n("install.installer.fabric-api") + " " + stageValue); break;
default: message = i18n(stageKey); break;
}
// @formatter:on

View File

@@ -86,13 +86,14 @@ class AdditionalInstallersPage extends InstallersPage {
protected void reload() {
LibraryAnalyzer analyzer = LibraryAnalyzer.analyze(version.resolvePreservingPatches(repository));
String game = analyzer.getVersion(MINECRAFT).orElse(null);
String fabric = analyzer.getVersion(FABRIC).orElse(null);
String forge = analyzer.getVersion(FORGE).orElse(null);
String liteLoader = analyzer.getVersion(LITELOADER).orElse(null);
String optiFine = analyzer.getVersion(OPTIFINE).orElse(null);
String fabric = analyzer.getVersion(FABRIC).orElse(null);
String fabricApi = analyzer.getVersion(FABRIC_API).orElse(null);
InstallerItem[] libraries = group.getLibraries();
String[] versions = new String[]{game, fabric, forge, liteLoader, optiFine};
String[] versions = new String[]{game, forge, liteLoader, optiFine, fabric, fabricApi};
String currentGameVersion = Lang.nonNull(getVersion("game"), game);

View File

@@ -32,6 +32,7 @@ import javafx.scene.layout.StackPane;
import org.jackhuang.hmcl.download.DownloadProvider;
import org.jackhuang.hmcl.download.RemoteVersion;
import org.jackhuang.hmcl.download.VersionList;
import org.jackhuang.hmcl.download.fabric.FabricAPIRemoteVersion;
import org.jackhuang.hmcl.download.fabric.FabricRemoteVersion;
import org.jackhuang.hmcl.download.forge.ForgeRemoteVersion;
import org.jackhuang.hmcl.download.game.GameRemoteVersion;
@@ -180,6 +181,13 @@ public final class VersionsPage extends BorderPane implements WizardPage, Refres
} else {
content.setSubtitle(remoteVersion.getGameVersion());
}
} else if (remoteVersion instanceof FabricAPIRemoteVersion) {
content.setImage(new Image("/assets/img/fabric.png", 32, 32, false, true));
if (StringUtils.isNotBlank(content.getSubtitle())) {
content.getTags().setAll(remoteVersion.getGameVersion());
} else {
content.setSubtitle(remoteVersion.getGameVersion());
}
}
}
});

View File

@@ -275,7 +275,9 @@ install.failed.optifine_conflict=Fabric, OptiFine and Forge are installed simult
install.failed.version_mismatch=The library requires the game version %s, but the actual version is %s.
install.installer.change_version=%s, this version is not compatible with current game version. Click here to choose another one.
install.installer.choose=Choose a %s version
install.installer.depend=Depends on %s
install.installer.fabric=Fabric
install.installer.fabric-api=Fabric API
install.installer.forge=Forge
install.installer.game=Minecraft
install.installer.incompatible=Incompatible with %s

View File

@@ -279,7 +279,9 @@ install.failed.optifine_conflict=暂不支持 OptiFine, Fabric 与 Forge 同时
install.failed.version_mismatch=该软件需要的游戏版本为 %s但实际的游戏版本为 %s。
install.installer.change_version=%s该版本与当前游戏不兼容您需要点击此处更换版本或删除
install.installer.choose=选择 %s 版本
install.installer.depend=需要先安装 %s
install.installer.fabric=Fabric
install.installer.fabric-api=Fabric API
install.installer.forge=Forge
install.installer.game=Minecraft
install.installer.incompatible=与 %s 不兼容

View File

@@ -17,7 +17,9 @@
*/
package org.jackhuang.hmcl.download;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
@@ -29,31 +31,10 @@ import java.util.stream.Collectors;
public class BalancedDownloadProvider implements DownloadProvider {
List<DownloadProvider> candidates;
VersionList<?> game, fabric, forge, liteLoader, optifine;
Map<String, VersionList<?>> versionLists = new HashMap<>();
public BalancedDownloadProvider(List<DownloadProvider> candidates) {
this.candidates = candidates;
this.game = new MultipleSourceVersionList(
candidates.stream()
.map(downloadProvider -> downloadProvider.getVersionListById("game"))
.collect(Collectors.toList()));
this.fabric = new MultipleSourceVersionList(
candidates.stream()
.map(downloadProvider -> downloadProvider.getVersionListById("fabric"))
.collect(Collectors.toList()));
this.forge = new MultipleSourceVersionList(
candidates.stream()
.map(downloadProvider -> downloadProvider.getVersionListById("forge"))
.collect(Collectors.toList()));
this.liteLoader = new MultipleSourceVersionList(
candidates.stream()
.map(downloadProvider -> downloadProvider.getVersionListById("liteloader"))
.collect(Collectors.toList()));
this.optifine = new MultipleSourceVersionList(
candidates.stream()
.map(downloadProvider -> downloadProvider.getVersionListById("optifine"))
.collect(Collectors.toList()));
}
@Override
@@ -73,20 +54,13 @@ public class BalancedDownloadProvider implements DownloadProvider {
@Override
public VersionList<?> getVersionListById(String id) {
switch (id) {
case "game":
return game;
case "fabric":
return fabric;
case "forge":
return forge;
case "liteloader":
return liteLoader;
case "optifine":
return optifine;
default:
throw new IllegalArgumentException("Unrecognized version list id: " + id);
if (!versionLists.containsKey(id)) {
versionLists.put(id, new MultipleSourceVersionList(
candidates.stream()
.map(downloadProvider -> downloadProvider.getVersionListById(id))
.collect(Collectors.toList())));
}
return versionLists.get(id);
}
@Override

View File

@@ -157,6 +157,7 @@ public final class LibraryAnalyzer implements Iterable<LibraryAnalyzer.LibraryMa
public enum LibraryType {
MINECRAFT(true, "game", Pattern.compile("^$"), Pattern.compile("^$")),
FABRIC(true, "fabric", Pattern.compile("net\\.fabricmc"), Pattern.compile("fabric-loader")),
FABRIC_API(true, "fabric-api", Pattern.compile("net\\.fabricmc"), Pattern.compile("fabric-api")),
FORGE(true, "forge", Pattern.compile("net\\.minecraftforge"), Pattern.compile("(forge|fmlloader)")) {
private final Pattern FORGE_VERSION_MATCHER = Pattern.compile("^([0-9.]+)-(?<forge>[0-9.]+)(-([0-9.]+))?$");

View File

@@ -17,6 +17,7 @@
*/
package org.jackhuang.hmcl.download;
import org.jackhuang.hmcl.download.fabric.FabricAPIVersionList;
import org.jackhuang.hmcl.download.fabric.FabricVersionList;
import org.jackhuang.hmcl.download.forge.ForgeBMCLVersionList;
import org.jackhuang.hmcl.download.game.GameVersionList;
@@ -30,6 +31,7 @@ import org.jackhuang.hmcl.download.optifine.OptiFineBMCLVersionList;
public class MojangDownloadProvider implements DownloadProvider {
private final GameVersionList game;
private final FabricVersionList fabric;
private final FabricAPIVersionList fabricApi;
private final ForgeBMCLVersionList forge;
private final LiteLoaderVersionList liteLoader;
private final OptiFineBMCLVersionList optifine;
@@ -39,6 +41,7 @@ public class MojangDownloadProvider implements DownloadProvider {
this.game = new GameVersionList(this);
this.fabric = new FabricVersionList(this);
this.fabricApi = new FabricAPIVersionList(this);
this.forge = new ForgeBMCLVersionList(apiRoot);
this.liteLoader = new LiteLoaderVersionList(this);
this.optifine = new OptiFineBMCLVersionList(apiRoot);
@@ -61,6 +64,8 @@ public class MojangDownloadProvider implements DownloadProvider {
return game;
case "fabric":
return fabric;
case "fabric-api":
return fabricApi;
case "forge":
return forge;
case "liteloader":

View File

@@ -79,6 +79,10 @@ public class RemoteVersion implements Comparable<RemoteVersion> {
return selfVersion;
}
public String getFullVersion() {
return getSelfVersion();
}
public Instant getReleaseDate() {
return releaseDate;
}

View File

@@ -0,0 +1,69 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2021 huangyuhui <huanghongxun2008@126.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.download.fabric;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.download.LibraryAnalyzer;
import org.jackhuang.hmcl.game.*;
import org.jackhuang.hmcl.task.Task;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
/**
* <b>Note</b>: Fabric should be installed first.
*
* @author huangyuhui
*/
public final class FabricAPIInstallTask extends Task<Version> {
private final DefaultDependencyManager dependencyManager;
private final Version version;
private final FabricAPIRemoteVersion remote;
private final List<Task<?>> dependencies = new LinkedList<>();
public FabricAPIInstallTask(DefaultDependencyManager dependencyManager, Version version, FabricAPIRemoteVersion remoteVersion) {
this.dependencyManager = dependencyManager;
this.version = version;
this.remote = remoteVersion;
}
@Override
public Collection<Task<?>> getDependencies() {
return dependencies;
}
@Override
public boolean isRelyingOnDependencies() {
return false;
}
@Override
public void execute() {
List<Library> libraries = new ArrayList<>();
libraries.add(new Library(new Artifact("net", "fabricmc", "fabric-api"), null,
new LibrariesDownloadInfo(new LibraryDownloadInfo(
"net/fabricmc/fabric-api/" + remote.getFullVersion() + "/fabric-api-" + remote.getFullVersion() + ".jar",
remote.getUrls().get(0)))));
setResult(new Version(LibraryAnalyzer.LibraryType.FABRIC_API.getPatchId(), remote.getSelfVersion(), 31000, new Arguments(), null, libraries));
dependencies.add(dependencyManager.checkLibraryCompletionAsync(getResult(), true));
}
}

View File

@@ -0,0 +1,52 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2021 huangyuhui <huanghongxun2008@126.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.download.fabric;
import org.jackhuang.hmcl.download.*;
import org.jackhuang.hmcl.game.Version;
import org.jackhuang.hmcl.task.Task;
import java.util.List;
public class FabricAPIRemoteVersion extends RemoteVersion {
private final String fullVersion;
/**
* Constructor.
*
* @param gameVersion the Minecraft version that this remote version suits.
* @param selfVersion the version string of the remote version.
* @param urls the installer or universal jar original URL.
*/
FabricAPIRemoteVersion(String gameVersion, String selfVersion, String fullVersion, List<String> urls) {
super(LibraryAnalyzer.LibraryType.FABRIC_API.getPatchId(), gameVersion, selfVersion, null, urls);
this.fullVersion = fullVersion;
}
@Override
public String getFullVersion() {
return fullVersion;
}
@Override
public Task<Version> getInstallTask(DefaultDependencyManager dependencyManager, Version baseVersion) {
return new FabricAPIInstallTask(dependencyManager, baseVersion, this);
}
}

View File

@@ -0,0 +1,86 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2021 huangyuhui <huanghongxun2008@126.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.download.fabric;
import org.jackhuang.hmcl.download.DownloadProvider;
import org.jackhuang.hmcl.download.VersionList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.jackhuang.hmcl.util.Lang.wrap;
public class FabricAPIVersionList extends VersionList<FabricAPIRemoteVersion> {
private final DownloadProvider downloadProvider;
public FabricAPIVersionList(DownloadProvider downloadProvider) {
this.downloadProvider = downloadProvider;
}
@Override
public boolean hasType() {
return false;
}
@Override
public CompletableFuture<?> refreshAsync() {
return CompletableFuture.runAsync(wrap(() -> {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(downloadProvider.injectURL("https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api/maven-metadata.xml"));
Element r = doc.getDocumentElement();
NodeList versionElements = r.getElementsByTagName("version");
for (int i = 0; i < versionElements.getLength(); i++) {
String versionName = versionElements.item(i).getTextContent();
Matcher matcher = FABRIC_VERSION_PATTERN.matcher(versionName);
if (matcher.find()) {
String fabricVersion = matcher.group("version");
if (matcher.group("build") != null) {
fabricVersion += "." + matcher.group("build");
}
String gameVersion = matcher.group("mcversion");
versions.put(gameVersion, new FabricAPIRemoteVersion(gameVersion, fabricVersion, versionName,
Collections.singletonList(String.format(
"https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api/%1$s/fabric-api-%1$s.jar", versionName))));
}
}
}));
}
@Override
protected Collection<FabricAPIRemoteVersion> getVersionsImpl(String gameVersion) {
Matcher matcher = GAME_VERSION_PATTERN.matcher(gameVersion);
if (matcher.find()) {
return super.getVersionsImpl(String.format("%s.%s", matcher.group("major"), matcher.group("minor")));
}
return super.getVersionsImpl(gameVersion);
}
private static final Pattern FABRIC_VERSION_PATTERN = Pattern.compile("^(?<version>[0-9.]+)\\+(build\\.(?<build>\\d+)-)?(?<mcversion>[0-9.]+)$");
private static final Pattern GAME_VERSION_PATTERN = Pattern.compile("^(?<major>[0-9]+)\\.(?<minor>[0-9]+)");
}