Merge
This commit is contained in:
@@ -317,7 +317,7 @@ public class Mods implements Loadable{
|
||||
return result;
|
||||
}
|
||||
|
||||
private LoadedMod locateMod(String name){
|
||||
public LoadedMod locateMod(String name){
|
||||
return mods.find(mod -> mod.enabled() && mod.name.equals(name));
|
||||
}
|
||||
|
||||
@@ -460,22 +460,25 @@ public class Mods implements Loadable{
|
||||
eachEnabled(mod -> {
|
||||
if(mod.root.child("scripts").exists()){
|
||||
content.setCurrentMod(mod);
|
||||
mod.scripts = mod.root.child("scripts").findAll(f -> f.extension().equals("js"));
|
||||
Log.debug("[{0}] Found {1} scripts.", mod.meta.name, mod.scripts.size);
|
||||
|
||||
for(Fi file : mod.scripts){
|
||||
//if there's only one script file, use it (for backwards compatibility); if there isn't, use "main.js"
|
||||
Array<Fi> allScripts = mod.root.child("scripts").findAll(f -> f.extEquals("js"));
|
||||
Fi main = allScripts.size == 1 ? allScripts.first() : mod.root.child("scripts").child("main.js");
|
||||
if(main.exists() && !main.isDirectory()){
|
||||
try{
|
||||
if(scripts == null){
|
||||
scripts = platform.createScripts();
|
||||
}
|
||||
scripts.run(mod, file);
|
||||
scripts.run(mod, main);
|
||||
}catch(Throwable e){
|
||||
Core.app.post(() -> {
|
||||
Log.err("Error loading script {0} for mod {1}.", file.name(), mod.meta.name);
|
||||
Log.err("Error loading main script {0} for mod {1}.", main.name(), mod.meta.name);
|
||||
e.printStackTrace();
|
||||
});
|
||||
break;
|
||||
}
|
||||
}else{
|
||||
Core.app.post(() -> {
|
||||
Log.err("No main.js found for mod {0}.", mod.meta.name);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -8,6 +8,12 @@ import arc.util.Log.*;
|
||||
import mindustry.*;
|
||||
import mindustry.mod.Mods.*;
|
||||
import org.mozilla.javascript.*;
|
||||
import org.mozilla.javascript.commonjs.module.*;
|
||||
import org.mozilla.javascript.commonjs.module.provider.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Scripts implements Disposable{
|
||||
private final Array<String> blacklist = Array.with("net", "files", "reflect", "javax", "rhino", "file", "channels", "jdk",
|
||||
@@ -15,9 +21,9 @@ public class Scripts implements Disposable{
|
||||
".awt", "socket", "classloader", "oracle", "invoke");
|
||||
private final Array<String> whitelist = Array.with("mindustry.net");
|
||||
private final Context context;
|
||||
private final String wrapper;
|
||||
private Scriptable scope;
|
||||
private boolean errored;
|
||||
private LoadedMod currentMod = null;
|
||||
|
||||
public Scripts(){
|
||||
Time.mark();
|
||||
@@ -25,9 +31,12 @@ public class Scripts implements Disposable{
|
||||
context = Vars.platform.getScriptContext();
|
||||
context.setClassShutter(type -> !blacklist.contains(type.toLowerCase()::contains) || whitelist.contains(type.toLowerCase()::contains));
|
||||
context.getWrapFactory().setJavaPrimitiveWrap(false);
|
||||
|
||||
|
||||
scope = new ImporterTopLevel(context);
|
||||
wrapper = Core.files.internal("scripts/wrapper.js").readString();
|
||||
|
||||
new RequireBuilder()
|
||||
.setModuleScriptProvider(new SoftCachingModuleScriptProvider(new ScriptModuleProvider()))
|
||||
.setSandboxed(true).createRequire(context, scope).install(scope);
|
||||
|
||||
if(!run(Core.files.internal("scripts/global.js").readString(), "global.js")){
|
||||
errored = true;
|
||||
@@ -68,11 +77,17 @@ public class Scripts implements Disposable{
|
||||
}
|
||||
|
||||
public void run(LoadedMod mod, Fi file){
|
||||
run(wrapper.replace("$SCRIPT_NAME$", mod.name + "/" + file.nameWithoutExtension()).replace("$CODE$", file.readString()).replace("$MOD_NAME$", mod.name), file.name());
|
||||
currentMod = mod;
|
||||
run(file.readString(), file.name());
|
||||
currentMod = null;
|
||||
}
|
||||
|
||||
private boolean run(String script, String file){
|
||||
try{
|
||||
if(currentMod != null){
|
||||
//inject script info into file (TODO maybe rhino handles this?)
|
||||
context.evaluateString(scope, "modName = \"" + currentMod.name + "\"\nscriptName = \"" + file + "\"", "initscript.js", 1, null);
|
||||
}
|
||||
context.evaluateString(scope, script, file, 1, null);
|
||||
return true;
|
||||
}catch(Throwable t){
|
||||
@@ -85,4 +100,38 @@ public class Scripts implements Disposable{
|
||||
public void dispose(){
|
||||
Context.exit();
|
||||
}
|
||||
|
||||
private class ScriptModuleProvider extends UrlModuleSourceProvider{
|
||||
private Pattern directory = Pattern.compile("^(.+?)/(.+)");
|
||||
|
||||
public ScriptModuleProvider(){
|
||||
super(null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModuleSource loadSource(String moduleId, Scriptable paths, Object validator) throws IOException, URISyntaxException{
|
||||
if(currentMod == null) return null;
|
||||
return loadSource(moduleId, currentMod.root.child("scripts"), validator);
|
||||
}
|
||||
|
||||
private ModuleSource loadSource(String moduleId, Fi root, Object validator) throws URISyntaxException{
|
||||
Matcher matched = directory.matcher(moduleId);
|
||||
if(matched.find()){
|
||||
LoadedMod required = Vars.mods.locateMod(matched.group(1));
|
||||
String script = matched.group(2);
|
||||
if(required == null || root.equals(required.root.child("scripts"))){ // Mod not found, or already using a mod
|
||||
Fi dir = root.child(matched.group(1));
|
||||
if(!dir.exists()) return null; // Mod and folder not found
|
||||
return loadSource(script, dir, validator);
|
||||
}
|
||||
return loadSource(script, required.root.child("scripts"), validator);
|
||||
}
|
||||
|
||||
Fi module = root.child(moduleId + ".js");
|
||||
if(!module.exists() || module.isDirectory()) return null;
|
||||
return new ModuleSource(
|
||||
new InputStreamReader(new ByteArrayInputStream((module.readString()).getBytes())),
|
||||
null, new URI(moduleId), root.file().toURI(), validator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user