From 497f3190594454415fd2d6813d4e4cea5a701a0b Mon Sep 17 00:00:00 2001 From: hypherionmc Date: Thu, 25 Apr 2024 19:10:24 +0200 Subject: [PATCH 1/3] [CHORE] Update gradle and dependencies --- build.gradle | 16 ++++++++-------- gradle/wrapper/gradle-wrapper.properties | 2 +- .../java/io/github/pacifistmc/forgix/Forgix.java | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/build.gradle b/build.gradle index 76b0a6e..b32f9f8 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,13 @@ plugins { id 'java' id 'java-gradle-plugin' - id 'com.github.johnrengelman.shadow' version '7.0.0' + id 'com.github.johnrengelman.shadow' version '8.0.0' id 'maven-publish' - id 'com.gradle.plugin-publish' version '0.16.0' + id 'com.gradle.plugin-publish' version '0.21.0' } group 'io.github.pacifistmc.forgix' -version '1.2.6' // Make sure to change the version number in io.github.pacifistmc.forgix.Forgix +version '1.2.7' // Make sure to change the version number in io.github.pacifistmc.forgix.Forgix configurations { shadowMe @@ -21,15 +21,15 @@ repositories { dependencies { implementation gradleApi() - shadowMe 'commons-io:commons-io:2.11.0' + shadowMe 'commons-io:commons-io:2.16.1' // Magic - shadowMe('me.lucko:jar-relocator:1.5') { + shadowMe('me.lucko:jar-relocator:1.7') { exclude group: 'org.ow2.asm' } - shadowMe 'org.ow2.asm:asm:9.3' - shadowMe 'org.ow2.asm:asm-commons:9.3' - shadowMe 'net.lingala.zip4j:zip4j:2.11.1' + shadowMe 'org.ow2.asm:asm:9.7' + shadowMe 'org.ow2.asm:asm-commons:9.7' + shadowMe 'net.lingala.zip4j:zip4j:2.11.5' shadowMe fileTree(dir: 'libs', include: ['*.jar']) // https://bitbucket.org/ReaperSoon/maven-repo/src/367c0bcb2749/fr/stevecohen/jarmanager } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 41dfb87..e411586 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/io/github/pacifistmc/forgix/Forgix.java b/src/main/java/io/github/pacifistmc/forgix/Forgix.java index c8b71a3..176946a 100644 --- a/src/main/java/io/github/pacifistmc/forgix/Forgix.java +++ b/src/main/java/io/github/pacifistmc/forgix/Forgix.java @@ -34,7 +34,7 @@ public class Forgix { public static final String manifestVersionKey = "Forgix-Version"; public static class Merge { - private final String version = "1.2.6"; + private final String version = "1.2.7"; public static Set perms; static { From 8710fd75f320070a0f2b5427236736a96f882f01 Mon Sep 17 00:00:00 2001 From: hypherionmc Date: Thu, 25 Apr 2024 19:42:17 +0200 Subject: [PATCH 2/3] [FEAT] Initial NeoForge support --- README.md | 23 +++ .../io/github/pacifistmc/forgix/Forgix.java | 157 +++++++++++++++++- .../forgix/plugin/ForgixMergeExtension.java | 59 +++++++ .../forgix/plugin/ForgixPlugin.java | 2 +- .../forgix/plugin/MergeJarsTask.java | 26 ++- 5 files changed, 259 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d95309f..9effb0d 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,18 @@ subprojects { - This exists because Forge can be a real pain at times, and Forge sometimes does something strange where we can’t actually identify mixins the normal way. However, if we don’t automatically detect the mixins, then only this should be used to specify the mixins explicitly. - This can be used more than once to specify multiple mixins. +##### NeoForge sub-container (“neoforge”) +- `projectName` (String) + - This is the name of the Forge project. This is set to “neoforge” by default. +- `jarLocation` (String) + - This is the location of the built NeoForge jar **from the project that’s specified in `projectName`**. By default, this retrieves the jar with the shortest name, which is quite scuffed but I don’t know how to retrieve the built jar without relying on loom or something similar, hopefully it’ll be better in the future though! +- `additionalRelocate` (String, String) + - Simply put, this allows you to define more `group`s, which is useful for relocating libraries. + - This can be used numerous times to specify multiple relocations. +- `mixin` (String) + - This exists because NeoForge can be a real pain at times, and NeoForge sometimes does something strange where we can’t actually identify mixins the normal way. However, if we don’t automatically detect the mixins, then only this should be used to specify the mixins explicitly. + - This can be used more than once to specify multiple mixins. + ##### Quilt sub-container (“quilt”) - `projectName` (String) - This is the name of the Quilt project. This is set to “quilt” by default. @@ -199,6 +211,17 @@ forgix { mixin "forge.mixins.json" mixin "forge.mixins.another.json" } + + neoforge { + projectName = "neoforge" + jarLocation = "build/libs/example-mod.jar" + + additionalRelocate "org.my.lib" "neoforge.org.my.lib" + additionalRelocate "org.my.lib.another" "neoforge.org.my.lib.another" + + mixin "neoforge.mixins.json" + mixin "neoforge.mixins.another.json" + } fabric { projectName = "fabric" diff --git a/src/main/java/io/github/pacifistmc/forgix/Forgix.java b/src/main/java/io/github/pacifistmc/forgix/Forgix.java index 176946a..9e54966 100644 --- a/src/main/java/io/github/pacifistmc/forgix/Forgix.java +++ b/src/main/java/io/github/pacifistmc/forgix/Forgix.java @@ -53,6 +53,9 @@ public static class Merge { private File forgeJar; private Map forgeRelocations; private List forgeMixins; + private File neoforgeJar; + private Map neoforgeRelocations; + private List neoforgeMixins; private File fabricJar; private Map fabricRelocations; private File quiltJar; @@ -67,10 +70,13 @@ public static class Merge { private final Logger logger; private final Map removeDuplicateRelocations = new HashMap<>(); - public Merge(@Nullable File forgeJar, Map forgeRelocations, List forgeMixins, @Nullable File fabricJar, Map fabricRelocations, @Nullable File quiltJar, Map quiltRelocations, Map customContainerMap, String group, File tempDir, String mergedJarName, List removeDuplicates, Logger logger) { + public Merge(@Nullable File forgeJar, Map forgeRelocations, List forgeMixins, @Nullable File neoforgeJar, Map neoforgeRelocations, List neoforgeMixins, @Nullable File fabricJar, Map fabricRelocations, @Nullable File quiltJar, Map quiltRelocations, Map customContainerMap, String group, File tempDir, String mergedJarName, List removeDuplicates, Logger logger) { this.forgeJar = forgeJar; this.forgeRelocations = forgeRelocations; this.forgeMixins = forgeMixins; + this.neoforgeJar = neoforgeJar; + this.neoforgeRelocations = neoforgeRelocations; + this.neoforgeMixins = neoforgeMixins; this.fabricJar = fabricJar; this.fabricRelocations = fabricRelocations; this.quiltJar = quiltJar; @@ -97,7 +103,7 @@ public File merge(boolean returnIfExists) throws IOException { } tempDir.mkdirs(); - if (forgeJar == null && fabricJar == null && quiltJar == null && customContainerMap.isEmpty()) { + if (forgeJar == null && neoforgeJar == null && fabricJar == null && quiltJar == null && customContainerMap.isEmpty()) { throw new IllegalArgumentException("No jars were provided."); } @@ -105,6 +111,10 @@ public File merge(boolean returnIfExists) throws IOException { logger.warn("Forge jar does not exist! You can ignore this if you are not using forge.\nYou might want to change Forgix settings if something is wrong."); } + if (neoforgeJar != null && !neoforgeJar.exists()) { + logger.warn("NeoForge jar does not exist! You can ignore this if you are not using neoforge.\nYou might want to change Forgix settings if something is wrong."); + } + if (fabricJar != null && !fabricJar.exists()) { logger.warn("Fabric jar does not exist! You can ignore this if you are not using fabric.\nYou might want to change Forgix settings if something is wrong."); } @@ -123,6 +133,7 @@ public File merge(boolean returnIfExists) throws IOException { logger.info("\nSettings:\n" + "Forge: " + (forgeJar == null || !forgeJar.exists() ? "No\n" : "Yes\n") + + "NeoForge: " + (neoforgeJar == null || !neoforgeJar.exists() ? "No\n" : "Yes\n") + "Fabric: " + (fabricJar == null || !fabricJar.exists() ? "No\n" : "Yes\n") + "Quilt: " + (quiltJar == null || !quiltJar.exists() ? "No\n" : "Yes\n") + "Custom Containers: " + (customContainerMap.isEmpty() ? "No\n" : "Yes\n") + @@ -134,6 +145,7 @@ public File merge(boolean returnIfExists) throws IOException { File fabricTemps = new File(tempDir, "fabric-temps"); File forgeTemps = new File(tempDir, "forge-temps"); + File neoforgeTemps = new File(tempDir, "neoforge-temps"); File quiltTemps = new File(tempDir, "quilt-temps"); customContainerTemps = new HashMap<>(); @@ -150,6 +162,9 @@ public File merge(boolean returnIfExists) throws IOException { if (forgeTemps.exists()) FileUtils.deleteQuietly(forgeTemps); forgeTemps.mkdirs(); + if (neoforgeTemps.exists()) FileUtils.deleteQuietly(neoforgeTemps); + neoforgeTemps.mkdirs(); + if (quiltTemps.exists()) FileUtils.deleteQuietly(quiltTemps); quiltTemps.mkdirs(); @@ -162,6 +177,7 @@ public File merge(boolean returnIfExists) throws IOException { JarUnpacker jarUnpacker = new JarUnpacker(); if (forgeJar != null && forgeJar.exists()) jarUnpacker.unpack(forgeJar.getAbsolutePath(), forgeTemps.getAbsolutePath()); + if (neoforgeJar != null && neoforgeJar.exists()) jarUnpacker.unpack(neoforgeJar.getAbsolutePath(), neoforgeTemps.getAbsolutePath()); if (fabricJar != null && fabricJar.exists()) jarUnpacker.unpack(fabricJar.getAbsolutePath(), fabricTemps.getAbsolutePath()); if (quiltJar != null && quiltJar.exists()) jarUnpacker.unpack(quiltJar.getAbsolutePath(), quiltTemps.getAbsolutePath()); @@ -177,6 +193,7 @@ public File merge(boolean returnIfExists) throws IOException { Manifest mergedManifest = new Manifest(); Manifest forgeManifest = new Manifest(); + Manifest neoforgeManifest = new Manifest(); Manifest fabricManifest = new Manifest(); Manifest quiltManifest = new Manifest(); List customContainerManifests = new ArrayList<>(); @@ -184,6 +201,8 @@ public File merge(boolean returnIfExists) throws IOException { FileInputStream fileInputStream = null; if (forgeJar != null && forgeJar.exists()) forgeManifest.read(fileInputStream = new FileInputStream(new File(forgeTemps, "META-INF/MANIFEST.MF"))); if (fileInputStream != null) fileInputStream.close(); + if (neoforgeJar != null && neoforgeJar.exists()) neoforgeManifest.read(fileInputStream = new FileInputStream(new File(neoforgeTemps, "META-INF/MANIFEST.MF"))); + if (fileInputStream != null) fileInputStream.close(); if (fabricJar != null && fabricJar.exists()) fabricManifest.read(fileInputStream = new FileInputStream(new File(fabricTemps, "META-INF/MANIFEST.MF"))); if (fileInputStream != null) fileInputStream.close(); if (quiltJar != null && quiltJar.exists()) quiltManifest.read(fileInputStream = new FileInputStream(new File(quiltTemps, "META-INF/MANIFEST.MF"))); @@ -201,6 +220,7 @@ public File merge(boolean returnIfExists) throws IOException { } forgeManifest.getMainAttributes().forEach((key, value) -> mergedManifest.getMainAttributes().putValue(key.toString(), value.toString())); + neoforgeManifest.getMainAttributes().forEach((key, value) -> mergedManifest.getMainAttributes().putValue(key.toString(), value.toString())); fabricManifest.getMainAttributes().forEach((key, value) -> mergedManifest.getMainAttributes().putValue(key.toString(), value.toString())); quiltManifest.getMainAttributes().forEach((key, value) -> mergedManifest.getMainAttributes().putValue(key.toString(), value.toString())); @@ -220,7 +240,11 @@ public File merge(boolean returnIfExists) throws IOException { } for (String mixin : mixins) { - remappedMixin.add("forge-" + mixin); + if (mixin.contains("neoforge") || mixin.contains("neo")) { + remappedMixin.add("neoforge-" + mixin); + } else { + remappedMixin.add("forge-" + mixin); + } } mergedManifest.getMainAttributes().putValue("MixinConfigs", String.join(",", remappedMixin)); @@ -235,7 +259,16 @@ public File merge(boolean returnIfExists) throws IOException { if (!forgeMixins.isEmpty()) mergedManifest.getMainAttributes().putValue("MixinConfigs", String.join(",", this.forgeMixins)); } - remapResources(forgeTemps, fabricTemps, quiltTemps); + if (this.neoforgeMixins != null) { + List newNeoForgeMixins = new ArrayList<>(); + for (String mixin : this.neoforgeMixins) { + newNeoForgeMixins.add("neoforge-" + mixin); + } + this.neoforgeMixins = newNeoForgeMixins; + if (!neoforgeMixins.isEmpty()) mergedManifest.getMainAttributes().putValue("MixinConfigs", String.join(",", this.neoforgeMixins)); + } + + remapResources(forgeTemps, neoforgeTemps, fabricTemps, quiltTemps); if (this.forgeMixins != null && mergedManifest.getMainAttributes().getValue("MixinConfigs") == null) { logger.debug("Couldn't detect forge mixins. You can ignore this if you are not using mixins with forge.\n" + @@ -247,9 +280,20 @@ public File merge(boolean returnIfExists) throws IOException { } } + if (this.neoforgeMixins != null && mergedManifest.getMainAttributes().getValue("MixinConfigs") == null) { + logger.debug("Couldn't detect neoforge mixins. You can ignore this if you are not using mixins with neoforge.\n" + + "If this is an issue then you can configure mixins manually\n" + + "Though we'll try to detect them automatically.\n"); + if (!neoforgeMixins.isEmpty()) { + logger.debug("Detected neoforge mixins: " + String.join(",", this.neoforgeMixins) + "\n"); + mergedManifest.getMainAttributes().putValue("MixinConfigs", String.join(",", this.neoforgeMixins)); + } + } + mergedManifest.getMainAttributes().putValue(manifestVersionKey, version); if (forgeJar != null && forgeJar.exists()) new File(forgeTemps, "META-INF/MANIFEST.MF").delete(); + if (neoforgeJar != null && neoforgeJar.exists()) new File(neoforgeTemps, "META-INF/MANIFEST.MF").delete(); if (fabricJar != null && fabricJar.exists()) new File(fabricTemps, "META-INF/MANIFEST.MF").delete(); if (quiltJar != null && quiltJar.exists()) new File(quiltTemps, "META-INF/MANIFEST.MF").delete(); @@ -265,6 +309,7 @@ public File merge(boolean returnIfExists) throws IOException { outputStream.close(); if (forgeJar != null && forgeJar.exists()) FileUtils.copyDirectory(forgeTemps, mergedTemps); + if (neoforgeJar != null && neoforgeJar.exists()) FileUtils.copyDirectory(neoforgeTemps, mergedTemps); if (fabricJar != null && fabricJar.exists()) FileUtils.copyDirectory(fabricTemps, mergedTemps); if (quiltJar != null && quiltJar.exists()) FileUtils.copyDirectory(quiltTemps, mergedTemps); @@ -302,6 +347,10 @@ public File merge(boolean returnIfExists) throws IOException { FileUtils.deleteQuietly(forgeTemps); forgeJar.delete(); } + if (neoforgeJar != null && neoforgeJar.exists()) { + FileUtils.deleteQuietly(neoforgeTemps); + neoforgeJar.delete(); + } if (fabricJar != null && fabricJar.exists()) { FileUtils.deleteQuietly(fabricTemps); fabricJar.delete(); @@ -368,6 +417,42 @@ private void remap() throws IOException { forgeJar = remappedForgeJar; } + if (neoforgeJar != null && neoforgeJar.exists()) { + File remappedNeoForgeJar = new File(tempDir, "tempNeoForgeInMerging.jar"); + if (remappedNeoForgeJar.exists()) remappedNeoForgeJar.delete(); + remappedNeoForgeJar.createNewFile(); + + List neoforgeRelocation = new ArrayList<>(); + neoforgeRelocation.add(new Relocation(group, "neoforge." + group)); + if (neoforgeRelocations != null) + neoforgeRelocation.addAll(neoforgeRelocations.entrySet().stream().map(entry -> new Relocation(entry.getKey(), entry.getValue())).collect(ArrayList::new, ArrayList::add, ArrayList::addAll)); + + AtomicReference architectury = new AtomicReference<>(); + architectury.set(null); + + JarFile jarFile = new JarFile(neoforgeJar); + jarFile.stream().forEach(jarEntry -> { + if (jarEntry.isDirectory()) { + if (jarEntry.getName().startsWith("architectury_inject")) { + architectury.set(jarEntry.getName()); + } + } else { + String firstDirectory = getFirstDirectory(jarEntry.getName()); + if (firstDirectory.startsWith("architectury_inject")) { + architectury.set(firstDirectory); + } + } + }); + jarFile.close(); + + if (architectury.get() != null) neoforgeRelocation.add(new Relocation(architectury.get(), "neoforge." + architectury.get())); + + JarRelocator neoforgeJarRelocator = new JarRelocator(neoforgeJar, remappedNeoForgeJar, neoforgeRelocation); + neoforgeJarRelocator.run(); + + neoforgeJar = remappedNeoForgeJar; + } + if (fabricJar != null && fabricJar.exists()) { File remappedFabricJar = new File(tempDir, "tempFabricInMerging.jar"); if (remappedFabricJar.exists()) remappedFabricJar.delete(); @@ -481,15 +566,18 @@ private void remap() throws IOException { /** * This is the second remapping method * This basically remaps all resources such as mixins, manifestJars, etc. - * This method also finds all the forge mixins for you if not detected + * This method also finds all the forge/neoforge mixins for you if not detected * * @param forgeTemps The extracted forge jar directory + * @param neoforgeTemps The extracted neoforge jar directory * @param fabricTemps The extracted fabric jar directory * @param quiltTemps The extracted quilt jar directory * @throws IOException If something went wrong */ - private void remapResources(File forgeTemps, File fabricTemps, File quiltTemps) throws IOException { + private void remapResources(File forgeTemps, File neoforgeTemps, File fabricTemps, File quiltTemps) throws IOException { if (forgeRelocations == null) forgeRelocations = new HashMap<>(); + if (neoforgeRelocations == null) neoforgeRelocations = new HashMap<>(); + if (forgeJar != null && forgeJar.exists()) { for (File file : manifestJars(forgeTemps)) { File remappedFile = new File(file.getParentFile(), "forge-" + file.getName()); @@ -542,6 +630,58 @@ private void remapResources(File forgeTemps, File fabricTemps, File quiltTemps) } } + if (neoforgeJar != null && neoforgeJar.exists()) { + for (File file : manifestJars(neoforgeTemps)) { + File remappedFile = new File(file.getParentFile(), "neoforge-" + file.getName()); + neoforgeRelocations.put(file.getName(), remappedFile.getName()); + file.renameTo(remappedFile); + } + + for (File file : listAllPlatformServices(neoforgeTemps, group)) { + File remappedFile = new File(file.getParentFile(), "neoforge." + file.getName()); + neoforgeRelocations.put(file.getName(), remappedFile.getName()); + file.renameTo(remappedFile); + } + + neoforgeMixins = new ArrayList<>(); + for (File file : listAllMixins(neoforgeTemps, false)) { + File remappedFile = new File(file.getParentFile(), "neoforge-" + file.getName()); + neoforgeRelocations.put(file.getName(), remappedFile.getName()); + file.renameTo(remappedFile); + + neoforgeMixins.add(remappedFile.getName()); + } + + for (File file : listAllRefmaps(neoforgeTemps)) { + File remappedFile = new File(file.getParentFile(), "neoforge-" + file.getName()); + neoforgeRelocations.put(file.getName(), remappedFile.getName()); + file.renameTo(remappedFile); + } + + neoforgeRelocations.put(group, "neoforge." + group); + neoforgeRelocations.put(group.replace(".", "/"), "neoforge/" + group.replace(".", "/")); + for (File file : listAllTextFiles(neoforgeTemps)) { + FileInputStream fis = new FileInputStream(file); + Scanner scanner = new Scanner(fis); + StringBuilder sb = new StringBuilder(); + + while (scanner.hasNext()) { + String line = scanner.nextLine(); + for (Map.Entry entry : neoforgeRelocations.entrySet()) { + line = line.replace(entry.getKey(), entry.getValue()); + } + sb.append(line).append("\n"); + } + + scanner.close(); + fis.close(); + FileOutputStream fos = new FileOutputStream(file); + fos.write(sb.toString().getBytes()); + fos.flush(); + fos.close(); + } + } + if (fabricRelocations == null) fabricRelocations = new HashMap<>(); if (fabricJar != null && fabricJar.exists()) { for (File file : manifestJars(fabricTemps)) { @@ -713,6 +853,11 @@ private void setupDuplicates() { removeDuplicateRelocationResources.put("forge/" + duplicatePath, duplicatePath); } + if (neoforgeJar != null && neoforgeJar.exists()) { + removeDuplicateRelocations.put("neoforge." + duplicate, duplicate); + removeDuplicateRelocationResources.put("neoforge/" + duplicatePath, duplicatePath); + } + if (fabricJar != null && fabricJar.exists()) { removeDuplicateRelocations.put("fabric." + duplicate, duplicate); removeDuplicateRelocationResources.put("fabric/" + duplicatePath, duplicatePath); diff --git a/src/main/java/io/github/pacifistmc/forgix/plugin/ForgixMergeExtension.java b/src/main/java/io/github/pacifistmc/forgix/plugin/ForgixMergeExtension.java index 2d7edd4..d99da6d 100644 --- a/src/main/java/io/github/pacifistmc/forgix/plugin/ForgixMergeExtension.java +++ b/src/main/java/io/github/pacifistmc/forgix/plugin/ForgixMergeExtension.java @@ -83,6 +83,23 @@ public ForgeContainer getForgeContainer() { return forgeContainer; } + NeoForgeContainer neoForgeContainer; + + public NeoForgeContainer neoforge(Closure closure) { + neoForgeContainer = new NeoForgeContainer(); + ForgixPlugin.rootProject.configure(neoForgeContainer, closure); + return neoForgeContainer; + } + + public void setNeoForgeContainer(NeoForgeContainer neoForgeContainer) { + this.neoForgeContainer = neoForgeContainer; + } + + public NeoForgeContainer getNeoForgeContainer() { + if (neoForgeContainer == null) neoForgeContainer = new NeoForgeContainer(); + return neoForgeContainer; + } + FabricContainer fabricContainer; public FabricContainer fabric(Closure closure) { @@ -190,6 +207,48 @@ public void mixin(String mixin) { } } + @SuppressWarnings("InnerClassMayBeStatic") + public class NeoForgeContainer { + String projectName = "neoforge"; + String jarLocation; + Map additionalRelocates; + List mixins; + + public String getProjectName() { + return projectName; + } + + public void setProjectName(String projectName) { + this.projectName = projectName; + } + + public String getJarLocation() { + return jarLocation; + } + + public void setJarLocation(String jarLocation) { + this.jarLocation = jarLocation; + } + + public Map getAdditionalRelocates() { + return additionalRelocates; + } + + public void additionalRelocate(String from, String to) { + if (this.additionalRelocates == null) this.additionalRelocates = new java.util.HashMap<>(); + this.additionalRelocates.put(from, to); + } + + public List getMixins() { + return mixins; + } + + public void mixin(String mixin) { + if (this.mixins == null) this.mixins = new java.util.ArrayList<>(); + this.mixins.add(mixin); + } + } + @SuppressWarnings("InnerClassMayBeStatic") public class FabricContainer { String projectName = "fabric"; diff --git a/src/main/java/io/github/pacifistmc/forgix/plugin/ForgixPlugin.java b/src/main/java/io/github/pacifistmc/forgix/plugin/ForgixPlugin.java index 2b50e38..af465c2 100644 --- a/src/main/java/io/github/pacifistmc/forgix/plugin/ForgixPlugin.java +++ b/src/main/java/io/github/pacifistmc/forgix/plugin/ForgixPlugin.java @@ -15,7 +15,7 @@ public void apply(Project project) { settings = rootProject.getExtensions().create("forgix", ForgixMergeExtension.class); rootProject.getTasks().register("mergeJars", MergeJarsTask.class).configure(forgix -> { forgix.setGroup("forgix"); - forgix.setDescription("Merges Fabric (also Quilt) and Forge jars into a single jar!"); + forgix.setDescription("Merges Fabric (also Quilt), Forge and NeoForge jars into a single jar!"); }); } } diff --git a/src/main/java/io/github/pacifistmc/forgix/plugin/MergeJarsTask.java b/src/main/java/io/github/pacifistmc/forgix/plugin/MergeJarsTask.java index e36baff..e77b812 100644 --- a/src/main/java/io/github/pacifistmc/forgix/plugin/MergeJarsTask.java +++ b/src/main/java/io/github/pacifistmc/forgix/plugin/MergeJarsTask.java @@ -24,12 +24,14 @@ void mergeJars() throws IOException { return; } ForgixMergeExtension.ForgeContainer forgeSettings = ForgixPlugin.settings.getForgeContainer(); + ForgixMergeExtension.NeoForgeContainer neoforgeSettings = ForgixPlugin.settings.getNeoForgeContainer(); ForgixMergeExtension.FabricContainer fabricSettings = ForgixPlugin.settings.getFabricContainer(); ForgixMergeExtension.QuiltContainer quiltSettings = ForgixPlugin.settings.getQuiltContainer(); List customSettingsList = ForgixPlugin.settings.getCustomContainers(); Project forgeProject = null; + Project neoforgeProject = null; Project fabricProject = null; Project quiltProject = null; @@ -40,6 +42,10 @@ void mergeJars() throws IOException { forgeProject = ForgixPlugin.rootProject.getAllprojects().stream().filter(p -> !p.getName().equals(ForgixPlugin.rootProject.getName())).filter(p -> p.getName().equalsIgnoreCase(forgeSettings.getProjectName())).findFirst().get(); validation.add(true); } catch (NoSuchElementException ignored) { } + try { + neoforgeProject = ForgixPlugin.rootProject.getAllprojects().stream().filter(p -> !p.getName().equals(ForgixPlugin.rootProject.getName())).filter(p -> p.getName().equalsIgnoreCase(neoforgeSettings.getProjectName())).findFirst().get(); + validation.add(true); + } catch (NoSuchElementException ignored) {} try { fabricProject = ForgixPlugin.rootProject.getAllprojects().stream().filter(p -> !p.getName().equals(ForgixPlugin.rootProject.getName())).filter(p -> p.getName().equalsIgnoreCase(fabricSettings.getProjectName())).findFirst().get(); validation.add(true); @@ -64,6 +70,7 @@ void mergeJars() throws IOException { validation.clear(); File forgeJar = null; + File neoforgeJar = null; File fabricJar = null; File quiltJar = null; @@ -86,6 +93,23 @@ void mergeJars() throws IOException { } } + if (neoforgeProject != null) { + if (neoforgeSettings.getJarLocation() != null) { + neoforgeJar = new File(neoforgeProject.getProjectDir(), neoforgeSettings.getJarLocation()); + } else { + int i = 0; + for (File file : new File(neoforgeProject.getBuildDir(), "libs").listFiles()) { + if (file.isDirectory()) continue; + if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) { + if (file.getName().length() < i || i == 0) { + i = file.getName().length(); + neoforgeJar = file; + } + } + } + } + } + if (fabricProject != null) { if (fabricSettings.getJarLocation() != null) { fabricJar = new File(fabricProject.getProjectDir(), fabricSettings.getJarLocation()); @@ -141,7 +165,7 @@ void mergeJars() throws IOException { if (mergedJar.exists()) FileUtils.forceDelete(mergedJar); if (!mergedJar.getParentFile().exists()) mergedJar.getParentFile().mkdirs(); - Path tempMergedJarPath = new Forgix.Merge(forgeJar, forgeSettings.getAdditionalRelocates(), forgeSettings.getMixins(), fabricJar, fabricSettings.getAdditionalRelocates(), quiltJar, quiltSettings.getAdditionalRelocates(), customJars, ForgixPlugin.settings.getGroup(), new File(ForgixPlugin.rootProject.getRootDir(), ".gradle" + File.separator + "forgix"), ForgixPlugin.settings.getMergedJarName(), ForgixPlugin.settings.getRemoveDuplicates(), ForgixPlugin.rootProject.getLogger()).merge(false).toPath(); + Path tempMergedJarPath = new Forgix.Merge(forgeJar, forgeSettings.getAdditionalRelocates(), forgeSettings.getMixins(), neoforgeJar, neoforgeSettings.getAdditionalRelocates(), neoforgeSettings.getMixins(), fabricJar, fabricSettings.getAdditionalRelocates(), quiltJar, quiltSettings.getAdditionalRelocates(), customJars, ForgixPlugin.settings.getGroup(), new File(ForgixPlugin.rootProject.getRootDir(), ".gradle" + File.separator + "forgix"), ForgixPlugin.settings.getMergedJarName(), ForgixPlugin.settings.getRemoveDuplicates(), ForgixPlugin.rootProject.getLogger()).merge(false).toPath(); Files.move(tempMergedJarPath, mergedJar.toPath(), StandardCopyOption.REPLACE_EXISTING); try { Files.setPosixFilePermissions(mergedJar.toPath(), Forgix.Merge.perms); From ab3351a5d68bc5cf5e0aed3e221b014dad142289 Mon Sep 17 00:00:00 2001 From: hypherionmc Date: Thu, 25 Apr 2024 20:21:57 +0200 Subject: [PATCH 3/3] [BUG] Detect newest file with the shortest name --- .../forgix/plugin/MergeJarsTask.java | 55 ++----------------- .../pacifistmc/forgix/utils/FileUtils.java | 40 ++++++++++++++ 2 files changed, 45 insertions(+), 50 deletions(-) diff --git a/src/main/java/io/github/pacifistmc/forgix/plugin/MergeJarsTask.java b/src/main/java/io/github/pacifistmc/forgix/plugin/MergeJarsTask.java index e77b812..523ec0d 100644 --- a/src/main/java/io/github/pacifistmc/forgix/plugin/MergeJarsTask.java +++ b/src/main/java/io/github/pacifistmc/forgix/plugin/MergeJarsTask.java @@ -80,16 +80,7 @@ void mergeJars() throws IOException { if (forgeSettings.getJarLocation() != null) { forgeJar = new File(forgeProject.getProjectDir(), forgeSettings.getJarLocation()); } else { - int i = 0; - for (File file : new File(forgeProject.getBuildDir(), "libs").listFiles()) { - if (file.isDirectory()) continue; - if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) { - if (file.getName().length() < i || i == 0) { - i = file.getName().length(); - forgeJar = file; - } - } - } + forgeJar = io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(forgeProject.getBuildDir(), "libs")); } } @@ -97,16 +88,7 @@ void mergeJars() throws IOException { if (neoforgeSettings.getJarLocation() != null) { neoforgeJar = new File(neoforgeProject.getProjectDir(), neoforgeSettings.getJarLocation()); } else { - int i = 0; - for (File file : new File(neoforgeProject.getBuildDir(), "libs").listFiles()) { - if (file.isDirectory()) continue; - if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) { - if (file.getName().length() < i || i == 0) { - i = file.getName().length(); - neoforgeJar = file; - } - } - } + neoforgeJar = io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(neoforgeProject.getBuildDir(), "libs")); } } @@ -114,16 +96,7 @@ void mergeJars() throws IOException { if (fabricSettings.getJarLocation() != null) { fabricJar = new File(fabricProject.getProjectDir(), fabricSettings.getJarLocation()); } else { - int i = 0; - for (File file : new File(fabricProject.getBuildDir(), "libs").listFiles()) { - if (file.isDirectory()) continue; - if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) { - if (file.getName().length() < i || i == 0) { - i = file.getName().length(); - fabricJar = file; - } - } - } + fabricJar = io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(fabricProject.getBuildDir(), "libs")); } } @@ -131,16 +104,7 @@ void mergeJars() throws IOException { if (quiltSettings.getJarLocation() != null) { quiltJar = new File(quiltProject.getProjectDir(), quiltSettings.getJarLocation()); } else { - int i = 0; - for (File file : new File(quiltProject.getBuildDir(), "libs").listFiles()) { - if (file.isDirectory()) continue; - if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) { - if (file.getName().length() < i || i == 0) { - i = file.getName().length(); - quiltJar = file; - } - } - } + quiltJar = io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(quiltProject.getBuildDir(), "libs")); } } @@ -148,16 +112,7 @@ void mergeJars() throws IOException { if (entry.getValue().getJarLocation() != null) { customJars.put(entry.getValue(), new File(entry.getKey().getProjectDir(), entry.getValue().getJarLocation())); } else { - int i = 0; - for (File file : new File(entry.getKey().getBuildDir(), "libs").listFiles()) { - if (file.isDirectory()) continue; - if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) { - if (file.getName().length() < i || i == 0) { - i = file.getName().length(); - customJars.put(entry.getValue(), file); - } - } - } + customJars.put(entry.getValue(), io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(entry.getKey().getBuildDir(), "libs"))); } } diff --git a/src/main/java/io/github/pacifistmc/forgix/utils/FileUtils.java b/src/main/java/io/github/pacifistmc/forgix/utils/FileUtils.java index f53969a..65f8470 100644 --- a/src/main/java/io/github/pacifistmc/forgix/utils/FileUtils.java +++ b/src/main/java/io/github/pacifistmc/forgix/utils/FileUtils.java @@ -1,6 +1,7 @@ package io.github.pacifistmc.forgix.utils; import org.apache.commons.io.FilenameUtils; +import org.jetbrains.annotations.Nullable; import java.io.*; import java.nio.charset.Charset; @@ -229,6 +230,45 @@ private static boolean isBinary(File file) { } } + /** + * Try to find the latest file with the shortest name in a directory + * @param directory The directory to find the file in + * @return The newest file or null if no files were found + */ + @Nullable + public static File findLatestFile(File directory) { + File latestFile = null; + long lastModifiedTime = Long.MIN_VALUE; + int i = 0; + List processedFiles = new ArrayList<>(); + + File[] files = directory.listFiles(); + if (files == null) + return null; + + for (File f : files) { + if (f.isDirectory() || !isZipFile(f)) + continue; + + if (f.getName().contains("-shadow") || f.getName().contains("-sources") || f.getName().contains("-dev")) + continue; + + if (f.lastModified() > lastModifiedTime) { + lastModifiedTime = f.lastModified(); + processedFiles.add(f); + } + } + + for (File f : processedFiles) { + if (f.getName().length() < i || i == 0) { + latestFile = f; + i = f.getName().length(); + } + } + + return latestFile; + } + /** * This method returns true if the character is a magic character that's used in binary files * @return If it's a magic character