generated from CleanroomMC/TemplateDevEnv
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* start * fix closures * fix issues with inner classes and script deps * fix dependent scripts force compiling * fix not detecting some inner classes * cache preprocessors, delete cache of deleted scripts * comments * clear index on deleteClassCache * file util class & save compiled class in proper dir * fix issues * reviews
- Loading branch information
Showing
19 changed files
with
598 additions
and
96 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
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
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
25 changes: 25 additions & 0 deletions
25
src/main/java/com/cleanroommc/groovyscript/core/mixin/groovy/ClassCollectorMixin.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,25 @@ | ||
package com.cleanroommc.groovyscript.core.mixin.groovy; | ||
|
||
import com.cleanroommc.groovyscript.GroovyScript; | ||
import groovy.lang.GroovyClassLoader; | ||
import org.codehaus.groovy.ast.ClassNode; | ||
import org.codehaus.groovy.control.SourceUnit; | ||
import org.spongepowered.asm.mixin.Final; | ||
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.CallbackInfoReturnable; | ||
|
||
@Mixin(value = GroovyClassLoader.ClassCollector.class, remap = false) | ||
public class ClassCollectorMixin { | ||
|
||
@Shadow | ||
@Final | ||
private SourceUnit su; | ||
|
||
@Inject(method = "createClass", at = @At("RETURN")) | ||
public void onCreateClass(byte[] code, ClassNode classNode, CallbackInfoReturnable<Class<?>> cir) { | ||
GroovyScript.getSandbox().onCompileClass(su, su.getName(), cir.getReturnValue(), code, classNode.getName().contains("$")); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/cleanroommc/groovyscript/core/mixin/groovy/GroovyClassLoaderMixin.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,27 @@ | ||
package com.cleanroommc.groovyscript.core.mixin.groovy; | ||
|
||
import com.cleanroommc.groovyscript.GroovyScript; | ||
import groovy.lang.GroovyClassLoader; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import java.net.URL; | ||
|
||
/** | ||
* If a script depends on another script and the there is a compiled cache for the script, it needs to be loaded manually. | ||
*/ | ||
@Mixin(value = GroovyClassLoader.class, remap = false) | ||
public class GroovyClassLoaderMixin { | ||
|
||
@Inject(method = "recompile", at = @At("HEAD"), cancellable = true) | ||
public void onRecompile(URL source, String className, Class<?> oldClass, CallbackInfoReturnable<Class<?>> cir) { | ||
if (source != null && oldClass == null) { | ||
Class<?> c = GroovyScript.getSandbox().onRecompileClass((GroovyClassLoader) (Object) this, source, className); | ||
if (c != null) { | ||
cir.setReturnValue(c); | ||
} | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/com/cleanroommc/groovyscript/sandbox/CompiledClass.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,88 @@ | ||
package com.cleanroommc.groovyscript.sandbox; | ||
|
||
import com.cleanroommc.groovyscript.api.GroovyLog; | ||
import org.apache.commons.lang3.builder.ToStringBuilder; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
|
||
class CompiledClass { | ||
|
||
public static final String CLASS_SUFFIX = ".clz"; | ||
|
||
final String path; | ||
String name; | ||
byte[] data; | ||
Class<?> clazz; | ||
|
||
public CompiledClass(String path, String name) { | ||
this.path = path; | ||
this.name = name; | ||
} | ||
|
||
public void onCompile(byte[] data, Class<?> clazz, String basePath) { | ||
this.data = data; | ||
onCompile(clazz, basePath); | ||
} | ||
|
||
public void onCompile(Class<?> clazz, String basePath) { | ||
this.clazz = clazz; | ||
this.name = clazz.getName(); | ||
if (this.data == null) { | ||
GroovyLog.get().errorMC("The class doesnt seem to be compiled yet. (" + name + ")"); | ||
return; | ||
} | ||
if (!GroovyScriptSandbox.WRITE_CACHE) return; | ||
try { | ||
File file = getDataFile(basePath); | ||
file.getParentFile().mkdirs(); | ||
try (FileOutputStream stream = new FileOutputStream(file)) { | ||
stream.write(this.data); | ||
stream.flush(); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public boolean readData(String basePath) { | ||
if (this.data != null) return true; | ||
File file = getDataFile(basePath); | ||
if (!file.exists()) return false; | ||
try { | ||
this.data = Files.readAllBytes(file.toPath()); | ||
return true; | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
} | ||
|
||
public void deleteCache(String cachePath) { | ||
try { | ||
Files.deleteIfExists(getDataFile(cachePath).toPath()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
protected File getDataFile(String basePath) { | ||
return FileUtil.makeFile(basePath, FileUtil.getParent(this.path), this.name + CLASS_SUFFIX); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.append("name", name) | ||
.toString(); | ||
} | ||
} |
Oops, something went wrong.