mirror of
https://github.com/SKCraft/Launcher.git
synced 2024-11-27 12:46:22 +01:00
Improve fabric loader processor by better utilizing fabric meta API
Removes a bunch of hardcoded workarounds :)
This commit is contained in:
parent
7707e5e45e
commit
3a7c250736
@ -174,6 +174,11 @@ public class PackageBuilder {
|
||||
if (processor != null) {
|
||||
LoaderResult result = processor.process(file, manifest, mapper, baseDir);
|
||||
|
||||
if (result == null) {
|
||||
log.warning("Loader " + file.getName() + " failed to process.");
|
||||
return;
|
||||
}
|
||||
|
||||
loaderLibraries.addAll(result.getLoaderLibraries());
|
||||
installerLibraries.addAll(result.getProcessorLibraries());
|
||||
jarMavens.addAll(result.getJarMavens());
|
||||
|
@ -1,23 +1,19 @@
|
||||
package com.skcraft.launcher.builder.loaders;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.io.Closer;
|
||||
import com.skcraft.launcher.builder.BuilderUtils;
|
||||
import com.skcraft.launcher.model.loader.MavenUrl;
|
||||
import com.skcraft.launcher.model.loader.profiles.FabricInstallProfile;
|
||||
import com.skcraft.launcher.model.loader.FabricMod;
|
||||
import com.skcraft.launcher.model.minecraft.Library;
|
||||
import com.skcraft.launcher.model.minecraft.VersionManifest;
|
||||
import com.skcraft.launcher.model.modpack.Manifest;
|
||||
import com.skcraft.launcher.util.HttpRequest;
|
||||
import lombok.extern.java.Log;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.zip.ZipEntry;
|
||||
|
||||
@ -30,77 +26,40 @@ public class FabricLoaderProcessor implements ILoaderProcessor {
|
||||
Closer closer = Closer.create();
|
||||
|
||||
try {
|
||||
ZipEntry installerEntry = BuilderUtils.getZipEntry(jarFile, "fabric-installer.json");
|
||||
String loaderVersion;
|
||||
|
||||
if (installerEntry != null) {
|
||||
InputStreamReader reader = new InputStreamReader(jarFile.getInputStream(installerEntry));
|
||||
FabricInstallProfile profile = mapper.readValue(
|
||||
BuilderUtils.readStringFromStream(closer.register(reader)), FabricInstallProfile.class);
|
||||
ZipEntry modEntry = BuilderUtils.getZipEntry(jarFile, "fabric.mod.json");
|
||||
if (modEntry != null) {
|
||||
InputStreamReader reader = new InputStreamReader(jarFile.getInputStream(modEntry));
|
||||
FabricMod loaderMod = mapper.readValue(
|
||||
BuilderUtils.readStringFromStream(closer.register(reader)), FabricMod.class);
|
||||
|
||||
// Check version
|
||||
if (profile.getVersion() != 1) {
|
||||
log.warning(String.format("Fabric installer metadata version is %d - we expect version 1.",
|
||||
profile.getVersion()));
|
||||
loaderVersion = loaderMod.getVersion();
|
||||
} else {
|
||||
log.warning("Fabric loader has no 'fabric.mod.json' file, is it really a Fabric Loader jar?");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Add libraries (TODO: Add server-only libraries to somewhere)
|
||||
Iterable<Library> libraries = Iterables.concat(profile.getLibraries().getClient(),
|
||||
profile.getLibraries().getCommon());
|
||||
for (Library library : libraries) {
|
||||
log.info("Downloading fabric metadata...");
|
||||
URL metaUrl = HttpRequest.url(
|
||||
String.format("https://meta.fabricmc.net/v2/versions/loader/%s/%s/profile/json",
|
||||
manifest.getGameVersion(), loaderVersion));
|
||||
VersionManifest fabricManifest = HttpRequest.get(metaUrl)
|
||||
.execute()
|
||||
.expectResponseCode(200)
|
||||
.returnContent()
|
||||
.asJson(VersionManifest.class);
|
||||
|
||||
for (Library library : fabricManifest.getLibraries()) {
|
||||
result.getLoaderLibraries().add(library);
|
||||
log.info("Adding loader library " + library.getName());
|
||||
}
|
||||
|
||||
// Add actual loader jar into library path
|
||||
if (profile.getLoader() != null) {
|
||||
result.getLoaderLibraries().add(profile.getLoader());
|
||||
log.info(String.format("Adding Fabric Loader '%s'", profile.getLoader().getName()));
|
||||
} else {
|
||||
log.warning("Fabric loader metadata is missing a `loader` section, making up a fake library");
|
||||
Library loader = new Library();
|
||||
loader.setName("faked:loader:" + FilenameUtils.getBaseName(loaderJar.getName()));
|
||||
|
||||
Library.Downloads downloads = new Library.Downloads();
|
||||
downloads.setArtifact(new Library.Artifact());
|
||||
downloads.getArtifact().setPath(loaderJar.getName());
|
||||
downloads.getArtifact().setUrl("");
|
||||
loader.setDownloads(downloads);
|
||||
|
||||
result.getLoaderLibraries().add(loader);
|
||||
// Little bit of a hack here, pretending the filesystem is a maven
|
||||
result.getJarMavens().add(new URL("file:" + loaderJar.getParentFile().getAbsolutePath() + "/"));
|
||||
}
|
||||
|
||||
// Set main class
|
||||
String mainClass = profile.getMainClass().getClient();
|
||||
String mainClass = fabricManifest.getMainClass();
|
||||
if (mainClass != null) {
|
||||
manifest.getVersionManifest().setMainClass(mainClass);
|
||||
log.info("Using main class " + mainClass);
|
||||
}
|
||||
|
||||
// Add intermediary library
|
||||
log.info("Downloading fabric metadata...");
|
||||
URL url = HttpRequest.url("https://meta.fabricmc.net/v2/versions/intermediary/"
|
||||
+ manifest.getVersionManifest().getId());
|
||||
List<MavenUrl> versions = HttpRequest.get(url)
|
||||
.execute()
|
||||
.expectResponseCode(200)
|
||||
.returnContent()
|
||||
.asJson(new TypeReference<List<MavenUrl>>() {});
|
||||
|
||||
if (versions != null && versions.size() > 0) {
|
||||
MavenUrl intermediaryLib = versions.get(0);
|
||||
|
||||
if (intermediaryLib.getUrl() == null) {
|
||||
// FIXME temporary hack since maven URL is missing, hopefully can go away soon
|
||||
// waiting on PR FabricMC/fabric-meta#9
|
||||
intermediaryLib.setUrl("https://maven.fabricmc.net/");
|
||||
}
|
||||
|
||||
result.getLoaderLibraries().add(intermediaryLib.toLibrary());
|
||||
log.info("Added intermediary " + intermediaryLib.getName());
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
log.warning("HTTP request to fabric metadata API was interrupted, this will probably not work!");
|
||||
} finally {
|
||||
|
@ -0,0 +1,12 @@
|
||||
package com.skcraft.launcher.model.loader;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class FabricMod {
|
||||
private String id;
|
||||
private String name;
|
||||
private String version;
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package com.skcraft.launcher.model.loader.profiles;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.skcraft.launcher.model.loader.ExtendedSidedData;
|
||||
import com.skcraft.launcher.model.loader.SidedData;
|
||||
import com.skcraft.launcher.model.minecraft.Library;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class FabricInstallProfile {
|
||||
private int version;
|
||||
private Library loader;
|
||||
private ExtendedSidedData<List<Library>> libraries;
|
||||
private SidedData<String> mainClass;
|
||||
}
|
Loading…
Reference in New Issue
Block a user