|
| 1 | +package com.cleanroommc.groovyscript.core.mixin.groovy; |
| 2 | + |
| 3 | +import com.cleanroommc.groovyscript.GroovyScript; |
| 4 | +import com.cleanroommc.groovyscript.api.GroovyLog; |
| 5 | +import com.cleanroommc.groovyscript.sandbox.FileUtil; |
| 6 | +import org.codehaus.groovy.ast.ModuleNode; |
| 7 | +import org.codehaus.groovy.ast.PackageNode; |
| 8 | +import org.codehaus.groovy.control.SourceUnit; |
| 9 | +import org.spongepowered.asm.mixin.Mixin; |
| 10 | +import org.spongepowered.asm.mixin.Shadow; |
| 11 | +import org.spongepowered.asm.mixin.injection.At; |
| 12 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 13 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | + |
| 17 | +@Mixin(value = ModuleNode.class, remap = false) |
| 18 | +public abstract class ModuleNodeMixin { |
| 19 | + |
| 20 | + @Shadow private PackageNode packageNode; |
| 21 | + |
| 22 | + @Shadow private transient SourceUnit context; |
| 23 | + |
| 24 | + @Inject(method = "<init>(Lorg/codehaus/groovy/control/SourceUnit;)V", at = @At("TAIL")) |
| 25 | + public void init(SourceUnit context, CallbackInfo ci) { |
| 26 | + // auto set package name |
| 27 | + String script = context.getName(); |
| 28 | + String rel = FileUtil.relativize(GroovyScript.getScriptPath(), script); |
| 29 | + int i = rel.lastIndexOf(File.separatorChar); |
| 30 | + if (i >= 0) { |
| 31 | + // inject correct package declaration into script |
| 32 | + String packageName = rel.substring(0, i).replace(File.separatorChar, '.') + '.'; |
| 33 | + this.packageNode = new PackageNode(packageName); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Inject(method = "setPackage", at = @At("HEAD"), cancellable = true) |
| 38 | + public void setPackage(PackageNode packageNode, CallbackInfo ci) { |
| 39 | + if (this.packageNode == null || this.context == null) return; |
| 40 | + // package name was already set -> only copy data of new node |
| 41 | + String cur = this.packageNode.getName(); |
| 42 | + String newName = packageNode.getName(); |
| 43 | + if (!cur.equals(newName)) { |
| 44 | + String rel = FileUtil.relativize(GroovyScript.getScriptPath(), this.context.getName()); |
| 45 | + GroovyLog.get().error("Expected package {} but got {} in script {}", cur, newName, rel); |
| 46 | + } |
| 47 | + if (this.packageNode.getAnnotations() != null) { |
| 48 | + this.packageNode.getAnnotations().clear(); |
| 49 | + } |
| 50 | + if (packageNode.getAnnotations() != null) { |
| 51 | + this.packageNode.addAnnotations(packageNode.getAnnotations()); |
| 52 | + } |
| 53 | + this.packageNode.setMetaDataMap(null); |
| 54 | + this.packageNode.copyNodeMetaData(packageNode); |
| 55 | + this.packageNode.setDeclaringClass(packageNode.getDeclaringClass()); |
| 56 | + this.packageNode.setSynthetic(packageNode.isSynthetic()); |
| 57 | + this.packageNode.setSourcePosition(packageNode); |
| 58 | + ci.cancel(); |
| 59 | + } |
| 60 | +} |
0 commit comments