Skip to content

Commit 2811a38

Browse files
committed
custom groovy class loader
1 parent 5d1d583 commit 2811a38

File tree

13 files changed

+611
-120
lines changed

13 files changed

+611
-120
lines changed

examples/runConfig.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
{
2-
"packName": "PlaceHolder name",
3-
"packId": "placeholdername",
2+
"packName": "Technological Journey CEu",
3+
"packId": "tjceu",
44
"version": "1.0.0",
55
"debug": true,
6-
"classes": [
7-
"classes/"
8-
],
96
"loaders": {
107
"preInit": [
8+
"classes/",
119
"preInit/"
1210
],
13-
"init": [],
11+
"init": [
12+
"init/"
13+
],
1414
"postInit": [
15-
"postInit/"
15+
"postInit/",
16+
"recipes/"
1617
]
1718
},
1819
"packmode": {

src/main/java/com/cleanroommc/groovyscript/GroovyScript.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ private static RunConfig createRunConfig(JsonObject json) {
275275
if (!Files.exists(main.toPath())) {
276276
try {
277277
main.getParentFile().mkdirs();
278-
Files.write(main.toPath(), "\nprintln('Hello World!')\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
278+
Files.write(main.toPath(), "\nlog.info('Hello World!')\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
279279
} catch (IOException e) {
280280
throw new RuntimeException(e);
281281
}

src/main/java/com/cleanroommc/groovyscript/helper/GroovyHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public static LoadStage getLoadStage() {
2424
}
2525

2626
public static boolean isReloading() {
27-
return getLoadStage().isReloadable() && !ReloadableRegistryManager.isFirstLoad();
27+
LoadStage loadStage = getLoadStage();
28+
return loadStage != null && loadStage.isReloadable() && !ReloadableRegistryManager.isFirstLoad();
2829
}
2930

3031
public static String getMinecraftVersion() {

src/main/java/com/cleanroommc/groovyscript/sandbox/CompiledClass.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import com.cleanroommc.groovyscript.api.GroovyLog;
44
import groovy.lang.GroovyClassLoader;
55
import org.apache.commons.lang3.builder.ToStringBuilder;
6+
import org.codehaus.groovy.runtime.InvokerHelper;
67

78
import java.io.File;
89
import java.io.FileOutputStream;
910
import java.io.IOException;
1011
import java.nio.file.Files;
12+
import java.util.Map;
1113

1214
class CompiledClass {
1315

@@ -30,7 +32,8 @@ public void onCompile(byte[] data, Class<?> clazz, String basePath) {
3032

3133
public void onCompile(Class<?> clazz, String basePath) {
3234
this.clazz = clazz;
33-
this.name = clazz.getName();
35+
if (!this.name.equals(clazz.getName())) throw new IllegalArgumentException();
36+
//this.name = clazz.getName();
3437
if (this.data == null) {
3538
GroovyLog.get().errorMC("The class doesnt seem to be compiled yet. (" + name + ")");
3639
return;
@@ -55,9 +58,10 @@ protected void ensureLoaded(CachedClassLoader classLoader, String basePath) {
5558
}
5659
}
5760

58-
protected void ensureLoaded(GroovyClassLoader classLoader, String basePath) {
61+
protected void ensureLoaded(GroovyClassLoader classLoader, Map<String, CompiledClass> cache, String basePath) {
5962
if (this.clazz == null) {
6063
this.clazz = classLoader.defineClass(this.name, this.data);
64+
cache.put(this.name, this);
6165
}
6266
}
6367

@@ -79,6 +83,11 @@ public void deleteCache(String cachePath) {
7983
} catch (IOException e) {
8084
throw new RuntimeException(e);
8185
}
86+
if (this.clazz != null) {
87+
InvokerHelper.removeClass(this.clazz);
88+
this.clazz = null;
89+
}
90+
this.data = null;
8291
}
8392

8493
protected File getDataFile(String basePath) {

src/main/java/com/cleanroommc/groovyscript/sandbox/CompiledScript.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,24 @@
1212
import java.io.File;
1313
import java.util.ArrayList;
1414
import java.util.List;
15+
import java.util.Map;
1516

1617
class CompiledScript extends CompiledClass {
1718

19+
public static String classNameFromPath(String path) {
20+
int i = path.lastIndexOf('.');
21+
path = path.substring(0, i);
22+
return path.replace('/', '.');
23+
}
24+
1825
final List<CompiledClass> innerClasses = new ArrayList<>();
1926
long lastEdited;
2027
List<String> preprocessors;
2128
boolean preprocessorCheckFailed;
2229
boolean requiresReload;
2330

2431
public CompiledScript(String path, long lastEdited) {
25-
this(path, null, lastEdited);
32+
this(path, classNameFromPath(path), lastEdited);
2633
}
2734

2835
public CompiledScript(String path, String name, long lastEdited) {
@@ -65,17 +72,17 @@ public void ensureLoaded(CachedClassLoader classLoader, String basePath) {
6572
super.ensureLoaded(classLoader, basePath);
6673
}
6774

68-
public void ensureLoaded(GroovyClassLoader classLoader, String basePath) {
75+
public void ensureLoaded(GroovyClassLoader classLoader, Map<String, CompiledClass> cache, String basePath) {
6976
for (CompiledClass comp : this.innerClasses) {
7077
if (comp.clazz == null) {
7178
if (comp.readData(basePath)) {
72-
comp.ensureLoaded(classLoader, basePath);
79+
comp.ensureLoaded(classLoader, cache, basePath);
7380
} else {
7481
GroovyLog.get().error("Error loading inner class {} for class {}", comp.name, this.name);
7582
}
7683
}
7784
}
78-
super.ensureLoaded(classLoader, basePath);
85+
super.ensureLoaded(classLoader, cache, basePath);
7986
}
8087

8188
public @NotNull JsonObject toJson() {

0 commit comments

Comments
 (0)