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

@@ -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]+)");
}