Skip to content

Commit 84a8e0c

Browse files
committed
properly inject package
1 parent d94e3f4 commit 84a8e0c

File tree

3 files changed

+63
-18
lines changed

3 files changed

+63
-18
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

src/main/java/com/cleanroommc/groovyscript/sandbox/transformer/GroovyScriptEarlyCompiler.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package com.cleanroommc.groovyscript.sandbox.transformer;
22

3-
import com.cleanroommc.groovyscript.GroovyScript;
4-
import com.cleanroommc.groovyscript.api.GroovyLog;
53
import com.cleanroommc.groovyscript.compat.mods.GroovyContainer;
64
import com.cleanroommc.groovyscript.compat.mods.ModSupport;
75
import com.cleanroommc.groovyscript.core.mixin.groovy.ModuleNodeAccessor;
8-
import com.cleanroommc.groovyscript.sandbox.FileUtil;
96
import org.codehaus.groovy.ast.ClassHelper;
107
import org.codehaus.groovy.ast.ClassNode;
118
import org.codehaus.groovy.ast.MethodNode;
@@ -24,7 +21,6 @@
2421
import org.codehaus.groovy.syntax.Token;
2522
import org.codehaus.groovy.syntax.Types;
2623

27-
import java.io.File;
2824
import java.util.List;
2925

3026
public class GroovyScriptEarlyCompiler extends CompilationCustomizer {
@@ -35,19 +31,7 @@ public GroovyScriptEarlyCompiler() {
3531

3632
@Override
3733
public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
38-
String root = GroovyScript.getScriptPath();
39-
String script = source.getName();
4034
ModuleNode module = classNode.getModule();
41-
String rel = FileUtil.relativize(root, script);
42-
int i = rel.lastIndexOf(File.separatorChar);
43-
if (i >= 0) {
44-
// inject correct package declaration into script
45-
String packageName = rel.substring(0, i).replace(File.separatorChar, '.') + '.';
46-
if (module.getPackage() != null && !module.getPackage().getName().equals(packageName)) {
47-
GroovyLog.get().error("Expected package {} but got {} in script {}", packageName, module.getPackage().getName(), rel);
48-
}
49-
module.setPackageName(packageName);
50-
}
5135
List<MethodNode> methods = module.getClasses().get(0).getMethods("run");
5236
if (methods.isEmpty()) return; // class scripts don't have a run method
5337
BlockStatement scriptStatement = (BlockStatement) methods.get(0).getCode();

src/main/resources/mixin.groovyscript.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@
2929
"groovy.Java8Mixin",
3030
"groovy.MetaClassImplMixin",
3131
"groovy.ModuleNodeAccessor",
32+
"groovy.ModuleNodeMixin",
3233
"loot.LoadTableEventMixin",
3334
"loot.LootPoolAccessor",
3435
"loot.LootTableAccessor",
3536
"loot.LootTableMixin"
3637
],
3738
"client": [
3839
"DefaultResourcePackAccessor",
39-
"GuiCreateWorldMixin",
40+
"GuiCreateWorldMixin"
4041
]
41-
}
42+
}

0 commit comments

Comments
 (0)