Desktop-specific scripting
This commit is contained in:
@@ -154,6 +154,7 @@ project(":desktop"){
|
|||||||
compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
|
compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
|
||||||
|
|
||||||
compile "com.github.Anuken:steamworks4j:$steamworksVersion"
|
compile "com.github.Anuken:steamworks4j:$steamworksVersion"
|
||||||
|
compile "org.graalvm.js:js:19.3.0"
|
||||||
|
|
||||||
compile arcModule("backends:backend-sdl")
|
compile arcModule("backends:backend-sdl")
|
||||||
compile 'com.github.MinnDevelopment:java-discord-rpc:v2.0.1'
|
compile 'com.github.MinnDevelopment:java-discord-rpc:v2.0.1'
|
||||||
@@ -254,7 +255,6 @@ project(":core"){
|
|||||||
compileJava.dependsOn(preGen)
|
compileJava.dependsOn(preGen)
|
||||||
|
|
||||||
compile "org.lz4:lz4-java:1.4.1"
|
compile "org.lz4:lz4-java:1.4.1"
|
||||||
compile "org.graalvm.js:js:19.3.0"
|
|
||||||
compile arcModule("arc-core")
|
compile arcModule("arc-core")
|
||||||
compile arcModule("extensions:freetype")
|
compile arcModule("extensions:freetype")
|
||||||
compile arcModule("extensions:arcnet")
|
compile arcModule("extensions:arcnet")
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import io.anuke.arc.func.*;
|
|||||||
import io.anuke.arc.math.*;
|
import io.anuke.arc.math.*;
|
||||||
import io.anuke.arc.scene.ui.*;
|
import io.anuke.arc.scene.ui.*;
|
||||||
import io.anuke.arc.util.serialization.*;
|
import io.anuke.arc.util.serialization.*;
|
||||||
|
import io.anuke.mindustry.mod.*;
|
||||||
import io.anuke.mindustry.net.*;
|
import io.anuke.mindustry.net.*;
|
||||||
import io.anuke.mindustry.net.Net.*;
|
import io.anuke.mindustry.net.Net.*;
|
||||||
import io.anuke.mindustry.type.*;
|
import io.anuke.mindustry.type.*;
|
||||||
@@ -45,6 +46,11 @@ public interface Platform{
|
|||||||
return new ArcNetImpl();
|
return new ArcNetImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Gets the scripting implementation. */
|
||||||
|
default Scripts createScripts(){
|
||||||
|
return new Scripts();
|
||||||
|
}
|
||||||
|
|
||||||
/** Add a text input dialog that should show up after the field is tapped. */
|
/** Add a text input dialog that should show up after the field is tapped. */
|
||||||
default void addDialog(TextField field){
|
default void addDialog(TextField field){
|
||||||
addDialog(field, 16);
|
addDialog(field, 16);
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -374,7 +374,7 @@ public class Mods implements Loadable{
|
|||||||
for(FileHandle file : mod.scripts){
|
for(FileHandle file : mod.scripts){
|
||||||
try{
|
try{
|
||||||
if(scripts == null){
|
if(scripts == null){
|
||||||
scripts = new Scripts();
|
scripts = platform.createScripts();
|
||||||
}
|
}
|
||||||
scripts.run(mod, file);
|
scripts.run(mod, file);
|
||||||
}catch(Throwable e){
|
}catch(Throwable e){
|
||||||
|
|||||||
@@ -1,41 +1,12 @@
|
|||||||
package io.anuke.mindustry.mod;
|
package io.anuke.mindustry.mod;
|
||||||
|
|
||||||
import io.anuke.arc.*;
|
|
||||||
import io.anuke.arc.files.*;
|
import io.anuke.arc.files.*;
|
||||||
import io.anuke.arc.util.*;
|
import io.anuke.arc.util.*;
|
||||||
import io.anuke.mindustry.mod.Mods.*;
|
import io.anuke.mindustry.mod.Mods.*;
|
||||||
import org.graalvm.polyglot.*;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
|
|
||||||
public class Scripts{
|
public class Scripts{
|
||||||
private static final Class[] denied = {FileHandle.class, InputStream.class, File.class, Scripts.class, Files.class};
|
|
||||||
private final Context context;
|
|
||||||
private final String wrapper;
|
|
||||||
|
|
||||||
public Scripts(){
|
|
||||||
Time.mark();
|
|
||||||
Context.Builder builder = Context.newBuilder("js").allowHostClassLookup(ClassAccess.allowedClassNames::contains);
|
|
||||||
|
|
||||||
HostAccess.Builder hb = HostAccess.newBuilder();
|
|
||||||
hb.allowPublicAccess(true);
|
|
||||||
for(Class c : denied){
|
|
||||||
hb.denyAccess(c);
|
|
||||||
}
|
|
||||||
builder.allowHostAccess(hb.build());
|
|
||||||
|
|
||||||
context = builder.build();
|
|
||||||
wrapper = Core.files.internal("scripts/wrapper.js").readString();
|
|
||||||
|
|
||||||
run(Core.files.internal("scripts/global.js").readString());
|
|
||||||
Log.info("Time to load script engine: {0}", Time.elapsed());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void run(LoadedMod mod, FileHandle file){
|
public void run(LoadedMod mod, FileHandle file){
|
||||||
run(wrapper.replace("$SCRIPT_NAME$", mod.name + "_" +file.nameWithoutExtension().replace("-", "_").replace(" ", "_")).replace("$CODE$", file.readString()));
|
Log.info("Skipping {0} (no scripting implenmentation)", file);
|
||||||
}
|
|
||||||
|
|
||||||
private void run(String script){
|
|
||||||
context.eval("js", script);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import io.anuke.mindustry.core.GameState.*;
|
|||||||
import io.anuke.mindustry.core.Version;
|
import io.anuke.mindustry.core.Version;
|
||||||
import io.anuke.mindustry.desktop.steam.*;
|
import io.anuke.mindustry.desktop.steam.*;
|
||||||
import io.anuke.mindustry.game.EventType.*;
|
import io.anuke.mindustry.game.EventType.*;
|
||||||
|
import io.anuke.mindustry.mod.*;
|
||||||
import io.anuke.mindustry.mod.Mods.*;
|
import io.anuke.mindustry.mod.Mods.*;
|
||||||
import io.anuke.mindustry.net.*;
|
import io.anuke.mindustry.net.*;
|
||||||
import io.anuke.mindustry.net.Net.*;
|
import io.anuke.mindustry.net.Net.*;
|
||||||
@@ -264,6 +265,11 @@ public class DesktopLauncher extends ClientLauncher{
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Scripts createScripts(){
|
||||||
|
return new GraalScripts();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Array<FileHandle> getWorkshopContent(Class<? extends Publishable> type){
|
public Array<FileHandle> getWorkshopContent(Class<? extends Publishable> type){
|
||||||
return !steam ? super.getWorkshopContent(type) : SVars.workshop.getWorkshopFiles(type);
|
return !steam ? super.getWorkshopContent(type) : SVars.workshop.getWorkshopFiles(type);
|
||||||
|
|||||||
42
desktop/src/io/anuke/mindustry/desktop/GraalScripts.java
Normal file
42
desktop/src/io/anuke/mindustry/desktop/GraalScripts.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package io.anuke.mindustry.desktop;
|
||||||
|
|
||||||
|
import io.anuke.arc.*;
|
||||||
|
import io.anuke.arc.files.*;
|
||||||
|
import io.anuke.arc.util.*;
|
||||||
|
import io.anuke.mindustry.mod.*;
|
||||||
|
import io.anuke.mindustry.mod.Mods.*;
|
||||||
|
import org.graalvm.polyglot.*;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class GraalScripts extends Scripts{
|
||||||
|
private static final Class[] denied = {FileHandle.class, InputStream.class, File.class, Scripts.class, Files.class, ClassAccess.class};
|
||||||
|
private final Context context;
|
||||||
|
private final String wrapper;
|
||||||
|
|
||||||
|
public GraalScripts(){
|
||||||
|
Time.mark();
|
||||||
|
Context.Builder builder = Context.newBuilder("js").allowHostClassLookup(ClassAccess.allowedClassNames::contains);
|
||||||
|
|
||||||
|
HostAccess.Builder hb = HostAccess.newBuilder();
|
||||||
|
hb.allowPublicAccess(true);
|
||||||
|
for(Class c : denied){
|
||||||
|
hb.denyAccess(c);
|
||||||
|
}
|
||||||
|
builder.allowHostAccess(hb.build());
|
||||||
|
|
||||||
|
context = builder.build();
|
||||||
|
wrapper = Core.files.internal("scripts/wrapper.js").readString();
|
||||||
|
|
||||||
|
run(Core.files.internal("scripts/global.js").readString());
|
||||||
|
Log.info("Time to load script engine: {0}", Time.elapsed());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run(LoadedMod mod, FileHandle file){
|
||||||
|
run(wrapper.replace("$SCRIPT_NAME$", mod.name + "_" +file.nameWithoutExtension().replace("-", "_").replace(" ", "_")).replace("$CODE$", file.readString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void run(String script){
|
||||||
|
context.eval("js", script);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user