Skip to content

Commit

Permalink
Make mixins apply to forge mods classes correctly
Browse files Browse the repository at this point in the history
Fixes for CodeChickenCore and NEI
NEI Plugins are not working currently
  • Loading branch information
thecatcore committed Aug 12, 2023
1 parent 5c753cc commit 5f8b556
Show file tree
Hide file tree
Showing 27 changed files with 516 additions and 168 deletions.
149 changes: 89 additions & 60 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,104 +1,133 @@
import com.diffplug.gradle.spotless.JavaExtension

plugins {
id 'java-library'
id 'com.diffplug.spotless' version "6.12.0"
id 'fabric-loom' version "1.3-SNAPSHOT" // To use chocoloom, change the version to a commit hash
id 'legacy-looming' version "1.3-SNAPSHOT" // Version must be the same as fabric-loom's
id 'fabric-loom' version "1.3-SNAPSHOT" apply false // To use chocoloom, change the version to a commit hash
id 'legacy-looming' version "1.3-SNAPSHOT" apply false // Version must be the same as fabric-loom's
id 'maven-publish'
id "com.modrinth.minotaur" version "2.+"
}

version = project.mod_version
def displayVersion = version + "+" + project.minecraft_version
group = project.maven_group

def debug = true
def branch = System.getenv("GITHUB_REF_NAME") == null ? "-forge" : System.getenv("GITHUB_REF_NAME")
if (!branch.endsWith("-forge")) {
debug = false
}

repositories {
maven {
name = "Modrinth"
url = "https://api.modrinth.com/maven"
content {
includeGroup "maven.modrinth"
allprojects {
apply plugin: "maven-publish"
apply plugin: "java-library"
apply plugin: "fabric-loom"
apply plugin: "legacy-looming"

version = project.mod_version
group = project.maven_group

repositories {
maven {
name = "Modrinth"
url = "https://api.modrinth.com/maven"
content {
includeGroup "maven.modrinth"
}
}
flatDir {
dirs (rootProject.rootDir.toString() + "/lib", rootProject.rootDir.toString() + "/mods")
}
}
flatDir {
dirs ("./lib", "./mods")
}

maven {
name = "Jitpack"
url 'https://jitpack.io/'
maven {
name = "Jitpack"
url 'https://jitpack.io/'
}
}
}

loom {
accessWidenerPath = file("src/main/resources/fabricated-forge.accesswidener")
}
dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings legacy.yarn(project.minecraft_version, project.yarn_mappings)
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings legacy.yarn(project.minecraft_version, project.yarn_mappings)
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
modApi("maven.modrinth:mod-remapping-api:${project.mod_remapping_api}")
// modApi(group: "local", name: "mod-remapping-api", version: "1.8.0")

modApi("maven.modrinth:mod-remapping-api:${project.mod_remapping_api}")
// modApi(group: "local", name: "mod-remapping-api", version: "1.8.0")
modApi("maven.modrinth:spasm:${project.spasm_version}")
modApi(annotationProcessor("com.github.LlamaLad7.MixinExtras:mixinextras-fabric:${project.mixin_extras_version}"))

modApi("maven.modrinth:spasm:${project.spasm_version}")
modApi(annotationProcessor("com.github.LlamaLad7.MixinExtras:mixinextras-fabric:${project.mixin_extras_version}"))
implementation(group: "local", name: "argo", version: "2.25")
implementation(group: "local", name: "guava", version: "12.0.1")

modImplementation(legacy.apiModule("legacy-fabric-logger-api-v1", "1.9.0+1.8.9"))
modImplementation(legacy.apiModule("legacy-fabric-logger-api-v1", "1.9.0+1.8.9"))

implementation(include(group: "local", name: "argo", version: "2.25"))
implementation(include(group: "local", name: "guava", version: "12.0.1"))
// modRuntimeOnly(files("./forge-${project.minecraft_version}-${project.forge_version}-universal-intermediary.jar"))
}

modCompileOnly(group: "local", name: "CodeChickenCore", version: "0.6.5")
modCompileOnly(group: "local", name: "NotEnoughItems", version: "1.4.0.5")
base {
archivesName = project.archives_base_name + "-" + project.minecraft_version
}

include("org.apache.commons:commons-lang3:3.12.0")
processResources {
inputs.property "version", project.version

// modRuntimeOnly(files("./forge-${project.minecraft_version}-${project.forge_version}-universal-intermediary.jar"))
}
filesMatching("fabric.mod.json") {
expand "version": project.version
}
}

base {
archivesName = project.archives_base_name + "-" + project.minecraft_version
}
// ensure that the encoding is set to UTF-8, no matter what the system default is
// this fixes some edge cases with special characters not displaying correctly
// see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html
tasks.withType(JavaCompile).configureEach {
it.options.encoding = "UTF-8"
if (JavaVersion.current().isJava9Compatible()) it.options.release = 8

processResources {
inputs.property "version", project.version
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

filesMatching("fabric.mod.json") {
expand "version": project.version
java {
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
// if it is present.
// If you remove this line, sources will not be generated.
withSourcesJar()
}

jar {
from("LICENSE") {
rename { "${it}_${base.archivesName.get()}" }
}
}
}

// ensure that the encoding is set to UTF-8, no matter what the system default is
// this fixes some edge cases with special characters not displaying correctly
// see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html
tasks.withType(JavaCompile).configureEach {
it.options.encoding = "UTF-8"
if (JavaVersion.current().isJava9Compatible()) it.options.release = 8
loom {
accessWidenerPath = file("src/main/resources/fabricated-forge.accesswidener")
}

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
dependencies {
include(group: "local", name: "argo", version: "2.25")
include(group: "local", name: "guava", version: "12.0.1")

include("org.apache.commons:commons-lang3:3.12.0")
}

subprojects {
dependencies {
api(project.dependencies.project(path: ":", configuration: 'namedElements'))
}
}

java {
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
// if it is present.
// If you remove this line, sources will not be generated.
withSourcesJar()
remapJar {
afterEvaluate {
subprojects.each {
// Include the jar from the sub project.
nestedJars.from project("${it.path}").tasks.getByName("remapJar")
}
}
}

jar {
from("licensing.md")
from("LICENSE") {
rename { "${it}_${base.archivesName.get()}" }
}
from("LICENSE-fml.txt")
from("CREDITS-fml.txt")
from("MinecraftForge-Credits.txt")
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ org.gradle.jvmargs=-Xmx2G
loader_version = 0.14.22

# Legacy Fabric API
mod_remapping_api = 1.14.1
mod_remapping_api = 1.14.2
forge_version = 4.3.5.318
mixin_extras_version=0.2.0-beta.8
spasm_version=0.1.1
spasm_version=0.1.2

# Mod Properties
mod_version = 2.5.0
mod_version = 2.6.0
maven_group = fr.catcore
archives_base_name = fabricated-forge
4 changes: 4 additions & 0 deletions mod-compat/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies {
modCompileOnly(group: "local", name: "CodeChickenCore", version: "0.6.5")
modCompileOnly(group: "local", name: "NotEnoughItems", version: "1.4.0.5")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.catcore.fabricatedforge.compat;

import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint;

public class Dummy implements PreLaunchEntrypoint {
@Override
public void onPreLaunch() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package fr.catcore.fabricatedforge.compat;

import fr.catcore.modremapperapi.api.ModRemapper;
import fr.catcore.modremapperapi.remapping.RemapUtil;
import fr.catcore.modremapperapi.remapping.VisitorInfos;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.Mixins;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExtraRemapper implements ModRemapper {
@Override
public String[] getJarFolders() {
return new String[0];
}

@Override
public Map<String, List<String>> getExclusions() {
return new HashMap<>();
}

@Override
public void getMappingList(RemapUtil.MappingList mappingList) {

}

@Override
public void registerVisitors(VisitorInfos visitorInfos) {

}

@Override
public void afterRemap() {
Mixins.addConfiguration("fabricated-forge.mods.mixins.json");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.catcore.fabricatedforge.mixin;
package fr.catcore.fabricatedforge.compat;

import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
Expand All @@ -10,7 +10,6 @@
public class ModsMixinPlugin implements IMixinConfigPlugin {
@Override
public void onLoad(String mixinPackage) {

}

@Override
Expand All @@ -22,12 +21,13 @@ public String getRefMapperConfig() {
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
if (mixinClassName.contains(".nei.")) {
try {
this.getClass().getClassLoader().loadClass("codechicken.nei.forge.IContainerClientSide");
Class.forName("codechicken.nei.IStackPositioner", false, this.getClass().getClassLoader());
return true;
} catch (ClassNotFoundException e) {
return false;
}
}

return true;
}

Expand All @@ -43,11 +43,9 @@ public List<String> getMixins() {

@Override
public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {

}

@Override
public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package fr.catcore.fabricatedforge.compat.mixin.codechickencore;

import codechicken.core.ClassDiscoverer;
import fr.catcore.fabricatedforge.Constants;
import net.minecraft.ModLoader;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

@Mixin(ClassDiscoverer.class)
public abstract class ClassDiscovererMixin {
@Shadow(remap = false) protected abstract void readFromDirectory(File directory, File basedirectory);

@Shadow(remap = false) protected abstract void readFromZipFile(File file) throws IOException;

@Shadow(remap = false) protected abstract void findModDirMods() throws IOException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException;

@Inject(method = "findModDirMods", cancellable = true, at = @At("HEAD"), remap = false)
private void fixFindModDirMods(CallbackInfo ci) throws IOException {
for(File child : new File[]{Constants.COREMODS_FOLDER, Constants.MODS_FOLDER}) {
if (!child.isFile() || !child.getName().endsWith(".jar") && !child.getName().endsWith(".zip")) {
if (child.isDirectory()) {
this.readFromDirectory(child, child);
}
} else {
this.readFromZipFile(child);
}
}

ci.cancel();
}

@Inject(method = "findClasses", at = @At("RETURN"), remap = false)
private void lookForModsToo(CallbackInfoReturnable<ArrayList> cir) {
try {
this.findModDirMods();
} catch (Exception var2) {
ModLoader.throwException("Code Chicken Core", var2);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.catcore.fabricatedforge.compat.mixin.codechickencore;

import codechicken.core.asm.ClassHeirachyManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(ClassHeirachyManager.class)
public class ClassHeirachyManagerMixin {
@Redirect(method = "classExtends(Ljava/lang/String;Ljava/lang/String;)Z", at = @At(value = "INVOKE", target = "Ljava/lang/Class;forName(Ljava/lang/String;)Ljava/lang/Class;", remap = false), remap = false)
private static Class<?> fixClassExtends(String className) throws ClassNotFoundException {
return Class.forName(className, false, ClassHeirachyManager.class.getClassLoader());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fr.catcore.fabricatedforge.compat.mixin.codechickencore;

import codechicken.core.asm.ObfuscationManager;
import fr.catcore.fabricatedforge.Constants;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ObfuscationManager.ClassMapping.class)
public class ClassMappingMixin {
@Shadow(remap = false) public String classname;

@Inject(method = "<init>", remap = false, at = @At("RETURN"))
private void remapClassName(String classname, CallbackInfo ci) {
if (!this.classname.contains(".")) {
this.classname = Constants.getRemappedClassName(this.classname);
}
}
}
Loading

0 comments on commit 5f8b556

Please sign in to comment.