Fixed scripts not working on older Android phones

This commit is contained in:
Anuken
2019-12-09 12:48:15 -05:00
parent 177cea5c5d
commit 0078a8cb8e
10 changed files with 349 additions and 6 deletions

View File

@@ -381,6 +381,10 @@ public class Mods implements Loadable{
requiresReload = false;
Events.fire(new ContentReloadEvent());
if(scripts != null && scripts.hasErrored()){
Core.app.post(() -> Core.settings.getBoolOnce("scripts-errored", () -> ui.showErrorMessage("$mod.scripts.unsupported")));
}
}
/** This must be run on the main thread! */

View File

@@ -12,20 +12,29 @@ public class Scripts implements Disposable{
private final Context context;
private final String wrapper;
private Scriptable scope;
private boolean errored;
public Scripts(){
Time.mark();
context = Vars.platform.getScriptContext();
context.setClassShutter(type -> (ClassAccess.allowedClassNames.contains(type) || type.startsWith("adapter") || type.contains("PrintStream") || type.startsWith("io.anuke.mindustry")) && !type.equals("io.anuke.mindustry.mod.ClassAccess"));
context.setClassShutter(type -> (ClassAccess.allowedClassNames.contains(type) || type.startsWith("$Proxy") ||
type.startsWith("adapter") || type.contains("PrintStream") ||
type.startsWith("io.anuke.mindustry")) && !type.equals("io.anuke.mindustry.mod.ClassAccess"));
scope = new ImporterTopLevel(context);
wrapper = Core.files.internal("scripts/wrapper.js").readString();
run(Core.files.internal("scripts/global.js").readString(), "global.js");
if(!run(Core.files.internal("scripts/global.js").readString(), "global.js")){
errored = true;
}
Log.debug("Time to load script engine: {0}", Time.elapsed());
}
public boolean hasErrored(){
return errored;
}
public String runConsole(String text){
try{
Object o = context.evaluateString(scope, text, "console.js", 1, null);
@@ -58,11 +67,13 @@ public class Scripts implements Disposable{
run(wrapper.replace("$SCRIPT_NAME$", mod.name + "/" + file.nameWithoutExtension()).replace("$CODE$", file.readString()).replace("$MOD_NAME$", mod.name), file.name());
}
private void run(String script, String file){
private boolean run(String script, String file){
try{
context.evaluateString(scope, script, file, 1, null);
return true;
}catch(Throwable t){
log(LogLevel.err, file, "" + getError(t));
return false;
}
}