From 79ec748a1288fb508f4b07b7d375d2dc119f9469 Mon Sep 17 00:00:00 2001 From: Adrian Siekierka Date: Sat, 22 Dec 2018 10:31:10 +0100 Subject: [PATCH] parse fabric-installer.json for mod devenvs, add loaderLaunchMethod param for that purpose, tweak LineNumberAdjustmentVisitor --- .../fabricmc/loom/LoomGradleExtension.java | 19 +++++ .../loom/providers/MappingsProvider.java | 6 +- .../task/LineNumberAdjustmentVisitor.java | 19 +++-- .../net/fabricmc/loom/task/RunClientTask.java | 2 +- .../net/fabricmc/loom/task/RunServerTask.java | 2 +- .../net/fabricmc/loom/util/Constants.java | 4 +- .../loom/util/LoomDependencyManager.java | 32 ++++++++ .../net/fabricmc/loom/util/ModProcessor.java | 60 ++++++--------- .../net/fabricmc/loom/util/RunConfig.java | 75 ++++++++++++++++--- 9 files changed, 155 insertions(+), 64 deletions(-) diff --git a/src/main/java/net/fabricmc/loom/LoomGradleExtension.java b/src/main/java/net/fabricmc/loom/LoomGradleExtension.java index 2262de7..216f543 100644 --- a/src/main/java/net/fabricmc/loom/LoomGradleExtension.java +++ b/src/main/java/net/fabricmc/loom/LoomGradleExtension.java @@ -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; } diff --git a/src/main/java/net/fabricmc/loom/providers/MappingsProvider.java b/src/main/java/net/fabricmc/loom/providers/MappingsProvider.java index 0c42d67..989ee34 100644 --- a/src/main/java/net/fabricmc/loom/providers/MappingsProvider.java +++ b/src/main/java/net/fabricmc/loom/providers/MappingsProvider.java @@ -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); diff --git a/src/main/java/net/fabricmc/loom/task/LineNumberAdjustmentVisitor.java b/src/main/java/net/fabricmc/loom/task/LineNumberAdjustmentVisitor.java index 2b06e36..6a3ad39 100644 --- a/src/main/java/net/fabricmc/loom/task/LineNumberAdjustmentVisitor.java +++ b/src/main/java/net/fabricmc/loom/task/LineNumberAdjustmentVisitor.java @@ -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); } } diff --git a/src/main/java/net/fabricmc/loom/task/RunClientTask.java b/src/main/java/net/fabricmc/loom/task/RunClientTask.java index 9a29f9d..2b3cec2 100644 --- a/src/main/java/net/fabricmc/loom/task/RunClientTask.java +++ b/src/main/java/net/fabricmc/loom/task/RunClientTask.java @@ -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")); diff --git a/src/main/java/net/fabricmc/loom/task/RunServerTask.java b/src/main/java/net/fabricmc/loom/task/RunServerTask.java index 38c5607..67b3299 100644 --- a/src/main/java/net/fabricmc/loom/task/RunServerTask.java +++ b/src/main/java/net/fabricmc/loom/task/RunServerTask.java @@ -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")); diff --git a/src/main/java/net/fabricmc/loom/util/Constants.java b/src/main/java/net/fabricmc/loom/util/Constants.java index 40025f4..f64494b 100644 --- a/src/main/java/net/fabricmc/loom/util/Constants.java +++ b/src/main/java/net/fabricmc/loom/util/Constants.java @@ -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/"; diff --git a/src/main/java/net/fabricmc/loom/util/LoomDependencyManager.java b/src/main/java/net/fabricmc/loom/util/LoomDependencyManager.java index edd0036..7893edf 100644 --- a/src/main/java/net/fabricmc/loom/util/LoomDependencyManager.java +++ b/src/main/java/net/fabricmc/loom/util/LoomDependencyManager.java @@ -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())); + } + + } + }); + } } diff --git a/src/main/java/net/fabricmc/loom/util/ModProcessor.java b/src/main/java/net/fabricmc/loom/util/ModProcessor.java index 447abb2..aa85a3e 100644 --- a/src/main/java/net/fabricmc/loom/util/ModProcessor.java +++ b/src/main/java/net/fabricmc/loom/util/ModProcessor.java @@ -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; } } - } diff --git a/src/main/java/net/fabricmc/loom/util/RunConfig.java b/src/main/java/net/fabricmc/loom/util/RunConfig.java index 7906ba1..6c0209e 100644 --- a/src/main/java/net/fabricmc/loom/util/RunConfig.java +++ b/src/main/java/net/fabricmc/loom/util/RunConfig.java @@ -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 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; }