Skip to content

Commit

Permalink
Merge pull request #24 from PacifistMC/patch/1.20.5
Browse files Browse the repository at this point in the history
Bug fixes and Updates
  • Loading branch information
Ran-Mewo authored Apr 26, 2024
2 parents d3387aa + ab3351a commit 40b980e
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 58 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"
Expand Down
16 changes: 8 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
159 changes: 152 additions & 7 deletions src/main/java/io/github/pacifistmc/forgix/Forgix.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ public ForgeContainer getForgeContainer() {
return forgeContainer;
}

NeoForgeContainer neoForgeContainer;

public NeoForgeContainer neoforge(Closure<NeoForgeContainer> 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<FabricContainer> closure) {
Expand Down Expand Up @@ -190,6 +207,48 @@ public void mixin(String mixin) {
}
}

@SuppressWarnings("InnerClassMayBeStatic")
public class NeoForgeContainer {
String projectName = "neoforge";
String jarLocation;
Map<String, String> additionalRelocates;
List<String> 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<String, String> 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<String> 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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
});
}
}
61 changes: 20 additions & 41 deletions src/main/java/io/github/pacifistmc/forgix/plugin/MergeJarsTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ForgixMergeExtension.CustomContainer> customSettingsList = ForgixPlugin.settings.getCustomContainers();

Project forgeProject = null;
Project neoforgeProject = null;
Project fabricProject = null;
Project quiltProject = null;

Expand All @@ -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);
Expand All @@ -64,6 +70,7 @@ void mergeJars() throws IOException {
validation.clear();

File forgeJar = null;
File neoforgeJar = null;
File fabricJar = null;
File quiltJar = null;

Expand All @@ -73,75 +80,47 @@ 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"));
}
}

if (neoforgeProject != null) {
if (neoforgeSettings.getJarLocation() != null) {
neoforgeJar = new File(neoforgeProject.getProjectDir(), neoforgeSettings.getJarLocation());
} else {
neoforgeJar = io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(neoforgeProject.getBuildDir(), "libs"));
}
}

if (fabricProject != null) {
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"));
}
}

if (quiltProject != null) {
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"));
}
}

for (Map.Entry<Project, ForgixMergeExtension.CustomContainer> entry : customProjects.entrySet()) {
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")));
}
}

File mergedJar = new File(ForgixPlugin.rootProject.getRootDir(), ForgixPlugin.settings.getOutputDir() + File.separator + ForgixPlugin.settings.getMergedJarName());
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);
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/io/github/pacifistmc/forgix/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<File> 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
Expand Down

0 comments on commit 40b980e

Please sign in to comment.