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
+1
View File
@@ -114,6 +114,7 @@ mod.author = [LIGHT_GRAY]Author:[] {0}
mod.missing = This save contains mods that you have recently updated or no longer have installed. Save corruption may occur. Are you sure you want to load it?\n[lightgray]Mods:\n{0}
mod.preview.missing = Before publishing this mod in the workshop, you must add an image preview.\nPlace an image named[accent] preview.png[] into the mod's folder and try again.
mod.folder.missing = Only mods in folder form can be published on the workshop.\nTo convert any mod into a folder, simply unzip its file into a folder and delete the old zip, then restart your game or reload your mods.
mod.scripts.unsupported = Your device does not support mod scripts. Some mods will not function correctly.
about.button = About
name = Name:
@@ -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! */
+14 -3
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;
}
}