parse fabric-installer.json for mod devenvs, add loaderLaunchMethod param for that purpose, tweak LineNumberAdjustmentVisitor

dev/0.11
Adrian Siekierka 2018-12-22 10:31:10 +01:00
parent 1445b8240a
commit 79ec748a12
9 changed files with 155 additions and 64 deletions

View File

@ -24,6 +24,7 @@
package net.fabricmc.loom;
import com.google.gson.JsonObject;
import net.fabricmc.loom.providers.MappingsProvider;
import net.fabricmc.loom.providers.MinecraftMappedProvider;
import net.fabricmc.loom.providers.MinecraftProvider;
@ -41,6 +42,7 @@ import java.util.Objects;
public class LoomGradleExtension {
public String runDir = "run";
public String refmapName;
public String loaderLaunchMethod;
public boolean remapMod = true;
public boolean autoGenIDERuns = true;
@ -49,6 +51,8 @@ public class LoomGradleExtension {
//Not to be set in the build.gradle
private Project project;
private LoomDependencyManager dependencyManager;
private JsonObject installerJson;
private int installerJsonPriority = Integer.MAX_VALUE; // 0+, higher = less prioritized
public LoomGradleExtension(Project project) {
this.project = project;
@ -62,6 +66,17 @@ public class LoomGradleExtension {
return Collections.unmodifiableList(unmappedModsBuilt);
}
public void setInstallerJson(JsonObject object, int priority) {
if (installerJson == null || priority <= installerJsonPriority) {
this.installerJson = object;
this.installerJsonPriority = priority;
}
}
public JsonObject getInstallerJson() {
return installerJson;
}
public File getUserCache() {
File userCache = new File(project.getGradle().getGradleUserHomeDir(), "caches" + File.separator + "fabric-loom");
if (!userCache.exists()) {
@ -96,6 +111,10 @@ public class LoomGradleExtension {
return null;
}
public String getLoaderLaunchMethod() {
return loaderLaunchMethod != null ? loaderLaunchMethod : "";
}
public LoomDependencyManager getDependencyManager() {
return dependencyManager;
}

View File

@ -58,13 +58,11 @@ public class MappingsProvider extends DependencyProvider {
project.getLogger().lifecycle(":setting up mappings (" + dependency.getDependency().getName() + " " + dependency.getResolvedVersion() + ")");
String version = dependency.getResolvedVersion();
String[] split = version.split("\\.");
File mappingsJar = dependency.resolveFile();
this.mappingsName = dependency.getDependency().getName();
this.minecraftVersion = split[0];
this.mappingsVersion = split[1];
this.minecraftVersion = version.substring(0, version.lastIndexOf('.'));
this.mappingsVersion = version.substring(version.lastIndexOf('.') + 1);
initFiles(project);

View File

@ -40,20 +40,19 @@ public class LineNumberAdjustmentVisitor extends ClassVisitor {
@Override
public void visitLineNumber(final int line, final Label start) {
int tLine = line;
/* while (tLine >= 1 && !lineNumberMap.containsKey(tLine)) {
tLine--;
} */
while (tLine <= maxLine && !lineNumberMap.containsKey(tLine)) {
tLine++;
}
if (tLine <= 0) {
tLine = 1;
super.visitLineNumber(1, start);
} else if (tLine >= maxLine) {
tLine = maxLineDst;
super.visitLineNumber(maxLineDst, start);
} else {
tLine = lineNumberMap.get(tLine);
Integer matchedLine = null;
while (tLine <= maxLine && ((matchedLine = lineNumberMap.get(tLine)) == null)) {
tLine++;
}
super.visitLineNumber(matchedLine != null ? matchedLine : maxLineDst, start);
}
super.visitLineNumber(tLine, start);
}
}

View File

@ -54,7 +54,7 @@ public class RunClientTask extends JavaExec {
}
classpath(libs);
args("--tweakClass", Constants.FABRIC_CLIENT_TWEAKER, "--assetIndex", minecraftVersionInfo.assetIndex.getFabricId(extension.getMinecraftProvider().minecraftVersion), "--assetsDir", new File(extension.getUserCache(), "assets").getAbsolutePath());
args("--tweakClass", Constants.DEFAULT_FABRIC_CLIENT_TWEAKER, "--assetIndex", minecraftVersionInfo.assetIndex.getFabricId(extension.getMinecraftProvider().minecraftVersion), "--assetsDir", new File(extension.getUserCache(), "assets").getAbsolutePath());
setWorkingDir(new File(getProject().getRootDir(), "run"));

View File

@ -49,7 +49,7 @@ public class RunServerTask extends JavaExec {
}
classpath(libs);
args("--tweakClass", Constants.FABRIC_SERVER_TWEAKER);
args("--tweakClass", Constants.DEFAULT_FABRIC_SERVER_TWEAKER);
setWorkingDir(new File(getProject().getRootDir(), "run"));

View File

@ -27,8 +27,8 @@ package net.fabricmc.loom.util;
public class Constants {
public static final String FABRIC_CLIENT_TWEAKER = "net.fabricmc.loader.launch.FabricClientTweaker";
public static final String FABRIC_SERVER_TWEAKER = "net.fabricmc.loader.launch.FabricServerTweaker";
public static final String DEFAULT_FABRIC_CLIENT_TWEAKER = "net.fabricmc.loader.launch.FabricClientTweaker";
public static final String DEFAULT_FABRIC_SERVER_TWEAKER = "net.fabricmc.loader.launch.FabricServerTweaker";
public static final String LIBRARIES_BASE = "https://libraries.minecraft.net/";
public static final String RESOURCES_BASE = "http://resources.download.minecraft.net/";

View File

@ -24,9 +24,12 @@
package net.fabricmc.loom.util;
import com.google.gson.JsonObject;
import net.fabricmc.loom.LoomGradleExtension;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ExternalModuleDependency;
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
import java.util.ArrayList;
import java.util.LinkedHashSet;
@ -78,7 +81,36 @@ public class LoomDependencyManager {
}
}
});
}
if (extension.getInstallerJson() != null) {
handleInstallerJson(extension.getInstallerJson(), project);
} else {
project.getLogger().warn("fabric-installer.json not found in classpath!");
}
}
private static void handleInstallerJson(JsonObject jsonObject, Project project){
JsonObject libraries = jsonObject.get("libraries").getAsJsonObject();
libraries.get("common").getAsJsonArray().forEach(jsonElement -> {
String name = jsonElement.getAsJsonObject().get("name").getAsString();
Configuration configuration = project.getConfigurations().getByName("compile");
ExternalModuleDependency modDep = (ExternalModuleDependency) project.getDependencies().create(name);
modDep.setTransitive(false);
configuration.getDependencies().add(modDep);
if(jsonElement.getAsJsonObject().has("url")){
String url = jsonElement.getAsJsonObject().get("url").getAsString();
long count = project.getRepositories().stream()
.filter(artifactRepository -> artifactRepository instanceof MavenArtifactRepository)
.map(artifactRepository -> (MavenArtifactRepository) artifactRepository)
.filter(mavenArtifactRepository -> mavenArtifactRepository.getUrl().toString().equalsIgnoreCase(url)).count();
if(count == 0){
project.getRepositories().maven(mavenArtifactRepository -> mavenArtifactRepository.setUrl(jsonElement.getAsJsonObject().get("url").getAsString()));
}
}
});
}
}

View File

@ -50,17 +50,14 @@ import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
public class ModProcessor {
private static final Gson GSON = new Gson();
public static void handleMod(File input, File output, Project project){
if(output.exists()){
output.delete();
}
remapJar(input, output, project);
JsonObject jsonObject = readInstallerJson(input);
if(jsonObject != null){
handleInstallerJson(jsonObject, project);
}
readInstallerJson(input, project);
}
private static void remapJar(File input, File output, Project project){
@ -120,48 +117,39 @@ public class ModProcessor {
}
}
private static void handleInstallerJson(JsonObject jsonObject, Project project){
JsonObject libraries = jsonObject.get("libraries").getAsJsonObject();
libraries.get("common").getAsJsonArray().forEach(jsonElement -> {
String name = jsonElement.getAsJsonObject().get("name").getAsString();
Configuration configuration = project.getConfigurations().getByName("compile");
ExternalModuleDependency modDep = (ExternalModuleDependency) project.getDependencies().create(name);
modDep.setTransitive(false);
configuration.getDependencies().add(modDep);
if(jsonElement.getAsJsonObject().has("url")){
String url = jsonElement.getAsJsonObject().get("url").getAsString();
long count = project.getRepositories().stream()
.filter(artifactRepository -> artifactRepository instanceof MavenArtifactRepository)
.map(artifactRepository -> (MavenArtifactRepository) artifactRepository)
.filter(mavenArtifactRepository -> mavenArtifactRepository.getUrl().toString().equalsIgnoreCase(url)).count();
if(count == 0){
project.getRepositories().maven(mavenArtifactRepository -> mavenArtifactRepository.setUrl(jsonElement.getAsJsonObject().get("url").getAsString()));
}
}
});
}
private static JsonObject readInstallerJson(File file){
private static void readInstallerJson(File file, Project project){
try {
JarFile jarFile = new JarFile(file);
ZipEntry entry = jarFile.getEntry("fabric-installer.json");
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
String launchMethod = extension.getLoaderLaunchMethod();
int priority = 0;
ZipEntry entry = null;
if (!launchMethod.isEmpty()) {
entry = jarFile.getEntry("fabric-installer." + launchMethod + ".json");
if (entry == null) {
project.getLogger().warn("Could not find loader launch method '" + launchMethod + "', falling back");
}
}
if(entry == null){
return null;
entry = jarFile.getEntry("fabric-installer.json");
priority = 1;
if (entry == null) {
return;
}
}
InputStream inputstream = jarFile.getInputStream(entry);
String jsonStr = IOUtils.toString(inputstream, StandardCharsets.UTF_8);
inputstream.close();
jarFile.close();
JsonObject jsonObject = new Gson().fromJson(jsonStr, JsonObject.class);
return jsonObject;
JsonObject jsonObject = GSON.fromJson(jsonStr, JsonObject.class);
extension.setInstallerJson(jsonObject, priority);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}

View File

@ -26,6 +26,9 @@ package net.fabricmc.loom.util;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.strobel.collections.ImmutableList;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.providers.MinecraftProvider;
import org.apache.commons.io.IOUtils;
@ -40,6 +43,8 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class RunConfig {
@ -82,18 +87,72 @@ public class RunConfig {
return e;
}
private static void populate(Project project, LoomGradleExtension extension, RunConfig runConfig, String mode) {
runConfig.projectName = project.getName();
runConfig.runDir = "file://$PROJECT_DIR$/" + extension.runDir;
runConfig.vmArgs = "-Dfabric.development=true";
switch (extension.getLoaderLaunchMethod()) {
case "launchwrapper":
runConfig.mainClass = "net.minecraft.launchwrapper.Launch";
runConfig.programArgs = "--tweakClass " + ("client".equals(mode) ? Constants.DEFAULT_FABRIC_CLIENT_TWEAKER : Constants.DEFAULT_FABRIC_SERVER_TWEAKER);
break;
default:
runConfig.mainClass = "net.fabricmc.loader.launch.knot.Knot" + mode.substring(0, 1).toUpperCase(Locale.ROOT) + mode.substring(1).toLowerCase(Locale.ROOT);
runConfig.programArgs = "";
break;
}
// if installer.json found...
JsonObject installerJson = extension.getInstallerJson();
if (installerJson != null) {
List<String> sideKeys = ImmutableList.of(mode, "common");
// copy main class
if (installerJson.has("mainClass")) {
JsonElement mainClassJson = installerJson.get("mainClass");
if (mainClassJson.isJsonObject()) {
JsonObject mainClassesJson = mainClassJson.getAsJsonObject();
for (String s : sideKeys) {
if (mainClassesJson.has(s)) {
runConfig.mainClass = mainClassesJson.get(s).getAsString();
break;
}
}
} else {
runConfig.mainClass = mainClassJson.getAsString();
}
}
// copy launchwrapper tweakers
if (installerJson.has("launchwrapper")) {
JsonObject launchwrapperJson = installerJson.getAsJsonObject("launchwrapper");
if (launchwrapperJson.has("tweakers")) {
JsonObject tweakersJson = launchwrapperJson.getAsJsonObject("tweakers");
StringBuilder builder = new StringBuilder();
for (String s : sideKeys) {
if (tweakersJson.has(s)) {
for (JsonElement element : tweakersJson.getAsJsonArray(s)) {
builder.append(" --tweakClass ").append(element.getAsString());
}
}
}
runConfig.programArgs += builder.toString();
}
}
}
}
public static RunConfig clientRunConfig(Project project){
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
MinecraftProvider minecraftProvider = extension.getMinecraftProvider();
MinecraftVersionInfo minecraftVersionInfo = minecraftProvider.versionInfo;
RunConfig ideaClient = new RunConfig();
ideaClient.mainClass = "net.minecraft.launchwrapper.Launch";
ideaClient.projectName = project.getName();
populate(project, extension, ideaClient, "client");
ideaClient.configName = "Minecraft Client";
ideaClient.runDir = "file://$PROJECT_DIR$/" + extension.runDir;
ideaClient.vmArgs = "-Dfabric.development=true" + getOSClientJVMArgs();
ideaClient.programArgs = "--tweakClass " + Constants.FABRIC_CLIENT_TWEAKER + " --assetIndex " + minecraftVersionInfo.assetIndex.getFabricId(extension.getMinecraftProvider().minecraftVersion) + " --assetsDir \"" + new File(extension.getUserCache(), "assets").getAbsolutePath() + "\"";
ideaClient.programArgs += " --assetIndex " + minecraftVersionInfo.assetIndex.getFabricId(extension.getMinecraftProvider().minecraftVersion) + " --assetsDir \"" + new File(extension.getUserCache(), "assets").getAbsolutePath() + "\"";
ideaClient.vmArgs += getOSClientJVMArgs();
return ideaClient;
}
@ -102,12 +161,8 @@ public class RunConfig {
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
RunConfig ideaServer = new RunConfig();
ideaServer.mainClass = "net.minecraft.launchwrapper.Launch";
ideaServer.projectName = project.getName();
populate(project, extension, ideaServer, "server");
ideaServer.configName = "Minecraft Server";
ideaServer.runDir = "file://$PROJECT_DIR$/" + extension.runDir;
ideaServer.vmArgs = "-Dfabric.development=true";
ideaServer.programArgs = "--tweakClass " + Constants.FABRIC_SERVER_TWEAKER;
return ideaServer;
}