-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
182 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
plugins { | ||
id 'java-library' | ||
} | ||
|
||
repositories { | ||
mavenLocal() | ||
maven { url = 'https://maven.neoforged.net/releases' } | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(project.java_version)) | ||
} | ||
} | ||
|
||
dependencies { | ||
compileOnly "org.jetbrains:annotations:${project.jetbrains_annotations_version}" | ||
implementation "net.neoforged.fancymodloader:spi:${project.fancy_mod_loader_version}" | ||
implementation "cpw.mods:modlauncher:${project.modlauncher_version}" | ||
} |
121 changes: 121 additions & 0 deletions
121
coremods/src/main/java/net/neoforged/neoforge/coremods/FieldToMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package net.neoforged.neoforge.coremods; | ||
|
||
import cpw.mods.modlauncher.api.ITransformer; | ||
import cpw.mods.modlauncher.api.ITransformerVotingContext; | ||
import cpw.mods.modlauncher.api.TransformerVoteResult; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.tree.AbstractInsnNode; | ||
import org.objectweb.asm.tree.ClassNode; | ||
import org.objectweb.asm.tree.FieldInsnNode; | ||
import org.objectweb.asm.tree.FieldNode; | ||
import org.objectweb.asm.tree.MethodInsnNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
|
||
import java.lang.reflect.Modifier; | ||
import java.util.ListIterator; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
public class FieldToMethod implements ITransformer<ClassNode> { | ||
private final Map<String, String> fieldToMethod; | ||
private final Set<Target> targets; | ||
|
||
public FieldToMethod(String className, Map<String, String> fieldToMethod) { | ||
this.targets = Set.of(Target.targetClass(className)); | ||
this.fieldToMethod = fieldToMethod; | ||
} | ||
|
||
@Override | ||
public @NotNull ClassNode transform(ClassNode input, ITransformerVotingContext context) { | ||
for (var entry : fieldToMethod.entrySet()) { | ||
redirectFieldToMethod(input, entry.getKey(), entry.getValue()); | ||
} | ||
|
||
return input; | ||
} | ||
|
||
@Override | ||
public @NotNull TransformerVoteResult castVote(ITransformerVotingContext context) { | ||
return TransformerVoteResult.YES; | ||
} | ||
|
||
@Override | ||
public @NotNull Set<Target> targets() { | ||
return targets; | ||
} | ||
|
||
/** | ||
* Rewrites accesses to a specific field in the given class to a method-call. | ||
* <p> | ||
* The field specified by fieldName must be private and non-static. | ||
* The method-call the field-access is redirected to does not take any parameters and returns an object of the | ||
* same type as the field. | ||
* If no methodName is passed, any method matching the described signature will be used as callable method. | ||
* | ||
* @param classNode the class to rewrite the accesses in | ||
* @param fieldName the field accesses should be redirected to | ||
* @param methodName the name of the method to redirect accesses through, | ||
* or null if any method with matching signature should be applicable | ||
*/ | ||
private static void redirectFieldToMethod(final ClassNode classNode, final String fieldName, @Nullable final String methodName) { | ||
MethodNode foundMethod = null; | ||
FieldNode foundField = null; | ||
for (FieldNode fieldNode : classNode.fields) { | ||
if (Objects.equals(fieldNode.name, fieldName)) { | ||
if (foundField == null) { | ||
foundField = fieldNode; | ||
} else { | ||
throw new IllegalStateException("Found multiple fields with name " + fieldName); | ||
} | ||
} | ||
} | ||
|
||
if (foundField == null) { | ||
throw new IllegalStateException("No field with name " + fieldName + " found"); | ||
} | ||
if (!Modifier.isPrivate(foundField.access) || Modifier.isStatic(foundField.access)) { | ||
throw new IllegalStateException("Field " + fieldName + " is not private and an instance field"); | ||
} | ||
|
||
final String methodSignature = "()" + foundField.desc; | ||
|
||
for (MethodNode methodNode : classNode.methods) { | ||
if (Objects.equals(methodNode.desc, methodSignature)) { | ||
if (foundMethod == null && Objects.equals(methodNode.name, methodName)) { | ||
foundMethod = methodNode; | ||
} else if (foundMethod == null && methodName == null) { | ||
foundMethod = methodNode; | ||
} else if (foundMethod != null && (methodName == null || Objects.equals(methodNode.name, methodName))) { | ||
throw new IllegalStateException("Found duplicate method with signature " + methodSignature); | ||
} | ||
} | ||
} | ||
|
||
if (foundMethod == null) { | ||
throw new IllegalStateException("Unable to find method " + methodSignature); | ||
} | ||
|
||
for (MethodNode methodNode : classNode.methods) { | ||
// skip the found getter method | ||
if (methodNode == foundMethod) continue; | ||
if (!Objects.equals(methodNode.desc, methodSignature)) { | ||
final ListIterator<AbstractInsnNode> iterator = methodNode.instructions.iterator(); | ||
while (iterator.hasNext()) { | ||
AbstractInsnNode insnNode = iterator.next(); | ||
if (insnNode.getOpcode() == Opcodes.GETFIELD) { | ||
FieldInsnNode fieldInsnNode = (FieldInsnNode) insnNode; | ||
if (Objects.equals(fieldInsnNode.name, fieldName)) { | ||
iterator.remove(); | ||
MethodInsnNode replace = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name, foundMethod.name, foundMethod.desc, false); | ||
iterator.add(replace); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
coremods/src/main/java/net/neoforged/neoforge/coremods/NeoForgeCoreMod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package net.neoforged.neoforge.coremods; | ||
|
||
import cpw.mods.modlauncher.api.ITransformer; | ||
import net.neoforged.neoforgespi.coremod.ICoreMod; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class NeoForgeCoreMod implements ICoreMod { | ||
@Override | ||
public Iterable<? extends ITransformer<?>> getTransformers() { | ||
return List.of( | ||
new FieldToMethod("net.minecraft.world.level.biome.Biome", Map.of( | ||
"climateSettings", "getModifiedClimateSettings", | ||
"specialEffects", "getModifiedSpecialEffects" | ||
)), | ||
new FieldToMethod("net.minecraft.world.level.levelgen.structure.Structure", Map.of( | ||
"settings", "getModifiedStructureSettings" | ||
)), | ||
new FieldToMethod("net.minecraft.world.level.block.LiquidBlock", Map.of( | ||
"fluid", "getFluid" | ||
)), | ||
new FieldToMethod("net.minecraft.world.item.BucketItem", Map.of( | ||
"content", "getFluid" | ||
)), | ||
new FieldToMethod("net.minecraft.world.level.block.StairBlock", Map.of( | ||
"base", "getModelBlock" | ||
)), | ||
new FieldToMethod("net.minecraft.world.level.block.FlowerPotBlock", Map.of( | ||
"potted", "getPotted" | ||
)) | ||
); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions
1
coremods/src/main/resources/META-INF/services/net.neoforged.neoforgespi.coremod.ICoreMod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
net.neoforged.neoforge.coremods.NeoForgeCoreMod |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ include ':tests' | |
project(":tests").projectDir = file("tests") | ||
|
||
include ':testframework' | ||
include ':coremods' |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.