From b43793b3fc74a58b06c164aa97713ba7ec19713d Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Wed, 12 Oct 2016 10:44:31 +0100 Subject: [PATCH] Initial untested work on mod remapping --- build.gradle | 1 + .../net/fabricmc/loom/AbstractPlugin.java | 54 +++++----- .../net/fabricmc/loom/util/BuidRemapper.java | 34 ------ .../net/fabricmc/loom/util/Constants.java | 3 +- .../net/fabricmc/loom/util/ModRemapper.java | 100 ++++++++++++++++++ 5 files changed, 130 insertions(+), 62 deletions(-) delete mode 100644 src/main/java/net/fabricmc/loom/util/BuidRemapper.java create mode 100644 src/main/java/net/fabricmc/loom/util/ModRemapper.java diff --git a/build.gradle b/build.gradle index 3c6d5b1..6cb2e26 100644 --- a/build.gradle +++ b/build.gradle @@ -58,6 +58,7 @@ dependencies { shade 'com.google.guava:guava:19.0' shade 'net.fabricmc:weave:0.1.0.6' shade 'cuchaz:enigma:0.11.0.5:lib' + shade 'net.fabricmc:tiny-remapper:+' compile 'net.fabricmc:fabric-base:16w38a-0.0.4-SNAPSHOT' } diff --git a/src/main/java/net/fabricmc/loom/AbstractPlugin.java b/src/main/java/net/fabricmc/loom/AbstractPlugin.java index 1d6cc1f..410a60f 100644 --- a/src/main/java/net/fabricmc/loom/AbstractPlugin.java +++ b/src/main/java/net/fabricmc/loom/AbstractPlugin.java @@ -30,10 +30,9 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import net.fabricmc.loom.task.DownloadTask; import net.fabricmc.loom.task.GenIdeaProjectTask; -import net.fabricmc.loom.util.BuidRemapper; +import net.fabricmc.loom.util.ModRemapper; import net.fabricmc.loom.util.Constants; import net.fabricmc.loom.util.Version; -import org.gradle.api.Action; import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.Task; @@ -41,7 +40,6 @@ import org.gradle.api.artifacts.repositories.MavenArtifactRepository; import org.gradle.api.plugins.JavaPlugin; import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.SourceSet; -import org.gradle.api.tasks.compile.JavaCompile; import org.gradle.api.tasks.javadoc.Javadoc; import org.gradle.plugins.ide.eclipse.model.EclipseModel; import org.gradle.plugins.ide.idea.model.IdeaModel; @@ -53,8 +51,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; -import java.util.Map; -import java.util.Set; public class AbstractPlugin implements Plugin { protected Project project; @@ -91,27 +87,27 @@ public class AbstractPlugin implements Plugin { configureIDEs(); configureCompile(); - //TODO other languages? - Map> taskMap = project.getAllTasks(true); - for (Map.Entry> entry : taskMap.entrySet()) { - Project project = entry.getKey(); - Set taskSet = entry.getValue(); - for (Task task : taskSet) { - if (task instanceof JavaCompile) { - JavaCompile javaCompileTask = (JavaCompile) task; - javaCompileTask.doFirst(task1 -> { - project.getLogger().lifecycle(":setting java compiler args"); - try { - javaCompileTask.getOptions().getCompilerArgs().add("-AreobfNotchSrgFile=" + Constants.MAPPINGS_SRG.get(extension).getCanonicalPath()); - javaCompileTask.getOptions().getCompilerArgs().add("-AdefaultObfuscationEnv=notch"); - } catch (IOException e) { - e.printStackTrace(); - } - }); - } - } - - } + //TODO wait for AP thing +// Map> taskMap = project.getAllTasks(true); +// for (Map.Entry> entry : taskMap.entrySet()) { +// Project project = entry.getKey(); +// Set taskSet = entry.getValue(); +// for (Task task : taskSet) { +// if (task instanceof JavaCompile) { +// JavaCompile javaCompileTask = (JavaCompile) task; +// javaCompileTask.doFirst(task1 -> { +// project.getLogger().lifecycle(":setting java compiler args"); +// try { +// javaCompileTask.getOptions().getCompilerArgs().add("-AreobfNotchSrgFile=" + Constants.MAPPINGS_SRG.get(extension).getCanonicalPath()); +// javaCompileTask.getOptions().getCompilerArgs().add("-AdefaultObfuscationEnv=notch"); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// }); +// } +// } +// +// } } @@ -246,7 +242,11 @@ public class AbstractPlugin implements Plugin { project.getTasks().getByName("build").doLast(task -> { project.getLogger().lifecycle(":remapping mods"); - BuidRemapper.reamp(project); + try { + ModRemapper.remap(project); + } catch (IOException e) { + e.printStackTrace(); + } }); project.afterEvaluate(project12 -> { diff --git a/src/main/java/net/fabricmc/loom/util/BuidRemapper.java b/src/main/java/net/fabricmc/loom/util/BuidRemapper.java deleted file mode 100644 index 86a8e07..0000000 --- a/src/main/java/net/fabricmc/loom/util/BuidRemapper.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * This file is part of fabric-loom, licensed under the MIT License (MIT). - * - * Copyright (c) 2016 FabricMC - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package net.fabricmc.loom.util; - -import org.gradle.api.Project; - -public class BuidRemapper { - - public static void reamp(Project project){ - - } -} diff --git a/src/main/java/net/fabricmc/loom/util/Constants.java b/src/main/java/net/fabricmc/loom/util/Constants.java index 60f715f..d51d18f 100644 --- a/src/main/java/net/fabricmc/loom/util/Constants.java +++ b/src/main/java/net/fabricmc/loom/util/Constants.java @@ -47,7 +47,8 @@ public class Constants { public static final IDelayed POMF_DIR = new DelayedFile(extension -> new File(extension.getFabricUserCache(), "pomf")); public static final IDelayed MAPPINGS_ZIP = new DelayedFile(extension -> new File(POMF_DIR.get(extension), "pomf-enigma-" + extension.version + "." + extension.pomfVersion + ".zip")); public static final IDelayed MAPPINGS_DIR = new DelayedFile(extension -> new File(POMF_DIR.get(extension), "pomf-enigma-" + extension.version + "." + extension.pomfVersion + "")); - public static final IDelayed MAPPINGS_SRG = new DelayedFile(extension -> new File(WORKING_DIRECTORY, "mappings.srg")); + public static final IDelayed MAPPINGS_TINY_GZ = new DelayedFile(extension -> new File(POMF_DIR.get(extension), "pomf-tiny-" + extension.version + "." + extension.pomfVersion + ".gz")); + public static final IDelayed MAPPINGS_TINY = new DelayedFile(extension -> new File(POMF_DIR.get(extension), "pomf-tiny-" + extension.version + "." + extension.pomfVersion)); public static final IDelayed REF_MAP = new DelayedFile(extension -> new File(WORKING_DIRECTORY, "RefMap.json")); diff --git a/src/main/java/net/fabricmc/loom/util/ModRemapper.java b/src/main/java/net/fabricmc/loom/util/ModRemapper.java new file mode 100644 index 0000000..0bfdabc --- /dev/null +++ b/src/main/java/net/fabricmc/loom/util/ModRemapper.java @@ -0,0 +1,100 @@ +/* + * This file is part of fabric-loom, licensed under the MIT License (MIT). + * + * Copyright (c) 2016 FabricMC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.fabricmc.loom.util; + +import net.fabricmc.loom.LoomGradleExtension; +import net.fabricmc.tinyremapper.OutputConsumerJar; +import net.fabricmc.tinyremapper.TinyRemapper; +import net.fabricmc.tinyremapper.TinyUtils; +import org.apache.commons.io.FileUtils; +import org.gradle.api.Project; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URL; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.GZIPInputStream; + +public class ModRemapper { + + public static void remap(Project project) throws IOException { + LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); + //TODO whats the proper way of doing this??? + File libsDir = new File(project.getBuildDir(), "libs"); + File modJar = new File(libsDir, project.getName() + "-" + project.getVersion() + ".jar"); + File modOutputJar = new File(libsDir, project.getName() + "-" + project.getVersion() + "-final.jar"); + if (!modJar.exists()) { + project.getLogger().error("Could not find mod jar @" + modJar.getAbsolutePath()); + return; + } + if (!Constants.MAPPINGS_TINY.get(extension).exists()) { + if (!Constants.MAPPINGS_TINY_GZ.get(extension).exists()) { + project.getLogger().lifecycle(":downloading tiny mappings"); + FileUtils.copyURLToFile(new URL("http://asie.pl:8080/job/pomf/" + extension.pomfVersion + "/artifact/build/libs/pomf-tiny-" + extension.version + "." + extension.pomfVersion + ".gz"), Constants.MAPPINGS_TINY_GZ.get(extension)); + } + GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(Constants.MAPPINGS_TINY_GZ.get(extension))); + FileOutputStream fileOutputStream = new FileOutputStream(Constants.MAPPINGS_TINY.get(extension)); + int length; + byte[] buffer = new byte[1024]; + while ((length = gzipInputStream.read(buffer)) > 0) { + fileOutputStream.write(buffer, 0, length); + } + gzipInputStream.close(); + fileOutputStream.close(); + } + + Path mappings = Constants.MAPPINGS_TINY.get(extension).toPath(); + + String fromM = "pomf"; + String toM = "mojang"; + + List classpathFiles = new ArrayList<>(); + classpathFiles.addAll(project.getConfigurations().getByName("compile").getFiles()); + classpathFiles.addAll(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES_CLIENT).getFiles()); + classpathFiles.addAll(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES).getFiles()); + classpathFiles.add(Constants.MINECRAFT_FINAL_JAR.get(extension)); + + Path[] classpath = new Path[classpathFiles.size()]; + for (int i = 0; i < classpathFiles.size(); i++) { + classpath[i] = classpathFiles.get(i).toPath(); + } + + TinyRemapper remapper = TinyRemapper.newRemapper() + .withMappings(TinyUtils.createTinyMappingProvider(mappings, fromM, toM)) + .build(); + + OutputConsumerJar outputConsumer = new OutputConsumerJar(modOutputJar); + outputConsumer.addNonClassFiles(modJar); + remapper.read(modJar.toPath()); + remapper.read(classpath); + remapper.apply(modJar.toPath(), outputConsumer); + outputConsumer.finish(); + remapper.finish(); + } +}