Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,24 @@ repositories {
name 'CurseMaven'
url 'https://curse.cleanroommc.com'
}
maven {
name 'BlameJared Maven'
url 'https://maven.blamejared.com'
}
maven {
name 'GTNH Maven'
url 'https://nexus.gtnewhorizons.com/repository/public/'
}
maven {
name 'GTCEu Maven'
url 'https://maven.gtceu.com'
}
}


dependencies {
implementation 'curse.maven:resource-mod-loader-945917:5768125+5768126'
implementation 'com.cleanroommc:groovyscript:1.2.3'
if (propertyBool('use_asset_mover')) {
implementation "com.cleanroommc:assetmover:${propertyString('asset_mover_version')}"
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mods/Hileb/shotaasm/ScriptLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import java.util.stream.Collectors;

public class ScriptLoader {
private static Collection<IScriptLocator> locators;
private static HashMap<String, IScriptCompiler> compilers = new HashMap<>();
public static Collection<IScriptLocator> locators;
public static HashMap<String, IScriptCompiler> compilers = new HashMap<>();

public static void initialize() {
locators = loadServices(IScriptLocator.class).stream().map( aClass -> {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/mods/Hileb/shotaasm/ShotaASM.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.eventbus.Subscribe;
import mods.Hileb.shotaasm.api.ShotaContext;
import mods.Hileb.shotaasm.impl.ShotaCompiler;
import mods.Hileb.shotaasm.impl.EventHandler;
import net.minecraftforge.fml.common.LoadController;
import net.minecraftforge.fml.common.ModMetadata;
import net.minecraftforge.fml.common.event.FMLStateEvent;
Expand Down Expand Up @@ -71,7 +72,7 @@ public File getSource() {

@Subscribe
public void onFMLState(FMLStateEvent event) {
ShotaCompiler.executeEvent(event.getEventType());
EventHandler.executeEvent(event.getEventType());
}

}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/mods/Hileb/shotaasm/api/ShotaContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ public static void initialize() {}
public static boolean isClassExist(String name) {
return net.minecraft.launchwrapper.Launch.classLoader.isClassExist(name);
}

public static java.util.Set<String> supportedCompilers() {
return mods.Hileb.shotaasm.ScriptLoader.compilers.keySet();
}
}
16 changes: 16 additions & 0 deletions src/main/java/mods/Hileb/shotaasm/impl/EventHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mods.Hileb.shotaasm.impl;

import com.google.common.collect.HashMultimap;

public class EventHandler {
private static final HashMultimap<String, Runnable> eventTasks = HashMultimap.create();

public static void executeEvent(String evt) {
eventTasks.get(evt).forEach(Runnable::run);
eventTasks.removeAll(evt);
}

public static void addEvent(String evt, Runnable r) {
eventTasks.put(evt, r);
}
}
8 changes: 1 addition & 7 deletions src/main/java/mods/Hileb/shotaasm/impl/ShotaCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
import java.lang.reflect.InvocationTargetException;

public class ShotaCompiler implements IScriptCompiler {
private static final HashMultimap<String, Runnable> eventTasks = HashMultimap.create();

public static void executeEvent(String evt) {
eventTasks.get(evt).forEach(Runnable::run);
eventTasks.removeAll(evt);
}

@Override
public String name() {
Expand All @@ -35,7 +29,7 @@ public Runnable compile(final ScriptFile file) {
if (file.property().containsKey("event")) {
String event = Iterables.getFirst(file.property().get("event"), null);
if (event != null) {
eventTasks.put(event, () -> ShotaCompiler.this.compile(file));
EventHandler.addEvent(event, () -> ShotaCompiler.this.compile(file));
return () -> {};
} else return null;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package mods.Hileb.shotaasm.impl.groovy;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables;

import mods.Hileb.shotaasm.api.ShotaContext;
import mods.Hileb.shotaasm.api.IScriptCompiler;
import mods.Hileb.shotaasm.api.ScriptFile;
import mods.Hileb.shotaasm.impl.EventHandler;

public class GroovyShotaCompiler implements IScriptCompiler {

@Override
public String name() {
return "groovyShota";
}

@Override
public Runnable compile(final ScriptFile file) {
if (ShotaContext.isClassExist("groovy.lang.GroovyClassLoader")) {
if (file.property().containsKey("event")) {
String event = Iterables.getFirst(file.property().get("event"), null);
if (event != null) {
EventHandler.addEvent(event, () -> GroovyShotaCompiler.this.compile(file));
return () -> {};
} else throw new RuntimeException("Could not understand the first event property is null. At " + file.name());
} else {
String singleName = file.name().substring(0, file.name().lastIndexOf('.')).replace('.', '_') + file.hashCode();
String name = "mods.Hileb.shotaasm.dynamic.groovy" + singleName;
StringBuilder builder = new StringBuilder();
for (String s : file.property().get("import")) {
builder.append("import ").append(s).append(";\n");
}
builder.append("\n").append(file.text());
try {
return ShotaGroovySandbox.compileScript(name, builder.toString());
} catch (Throwable e) {
throw new RuntimeException("Unable to compile for " + file.name(), e);
}
}
} else {
throw new RuntimeException("Groovy Support not found. But " + file.name() + " required the compiler of groovyShota.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package mods.Hileb.shotaasm.impl.groovy;

import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.codehaus.groovy.control.CompilerConfiguration;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.Script;
import org.codehaus.groovy.runtime.InvokerHelper;
import mods.Hileb.shotaasm.api.ScriptFile;
import net.minecraft.launchwrapper.Launch;

import java.util.*;

public class ShotaGroovySandbox {

ImportCustomizer importCustomizer = new ImportCustomizer();
CompilerConfiguration config = new CompilerConfiguration();
Map<String, Object> bindings = new Object2ObjectOpenHashMap<>();
Binding binding = new Binding(this.bindings);
GroovyClassLoader classLoader;

public ShotaGroovySandbox() {
importCustomizer = new ImportCustomizer();
config = new CompilerConfiguration();

importCustomizer.addImports(
"mods.Hileb.shotaasm.api.TransformerRegistry",
"mods.Hileb.shotaasm.api.ShotaContext"
);
importCustomizer.addStarImports(
"org.objectweb.asm",
"org.objectweb.asm.tree",
"org.objectweb.asm.util",
"org.objectweb.asm.commons",
"org.objectweb.asm.signature"
);
importCustomizer.addStaticStars("org.objectweb.asm.Opcodes");
config.addCompilationCustomizers(importCustomizer);
classLoader = new GroovyClassLoader(Launch.classLoader, config, false);
}

public Class<?> compile(String name, String text) {
return classLoader.parseClass(text, name);
}

public Runnable makeScript(Class<?> cls){
final Script script = InvokerHelper.createScript(cls, binding);
return script::run ;
}

static ShotaGroovySandbox box = null;
public static Runnable compileScript(String name, String file){
if (box == null) box = new ShotaGroovySandbox();
return box.makeScript(box.compile(name, file));
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mods.Hileb.shotaasm.impl.ShotaCompiler
mods.Hileb.shotaasm.impl.ShotaCompiler
mods.Hileb.shotaasm.impl.groovy.GroovyShotaCompiler
Loading