Merge pull request #1477 from DeltaNedas/patch-5
controlled script loading
This commit is contained in:
@@ -317,7 +317,7 @@ public class Mods implements Loadable{
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private LoadedMod locateMod(String name){
|
public LoadedMod locateMod(String name){
|
||||||
return mods.find(mod -> mod.enabled() && mod.name.equals(name));
|
return mods.find(mod -> mod.enabled() && mod.name.equals(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -460,22 +460,23 @@ public class Mods implements Loadable{
|
|||||||
eachEnabled(mod -> {
|
eachEnabled(mod -> {
|
||||||
if(mod.root.child("scripts").exists()){
|
if(mod.root.child("scripts").exists()){
|
||||||
content.setCurrentMod(mod);
|
content.setCurrentMod(mod);
|
||||||
mod.scripts = mod.root.child("scripts").findAll(f -> f.extension().equals("js"));
|
Fi main = mod.root.child("scripts").child("main.js");
|
||||||
Log.debug("[{0}] Found {1} scripts.", mod.meta.name, mod.scripts.size);
|
if(main.exists() && !main.isDirectory()){
|
||||||
|
|
||||||
for(Fi file : mod.scripts){
|
|
||||||
try{
|
try{
|
||||||
if(scripts == null){
|
if(scripts == null){
|
||||||
scripts = platform.createScripts();
|
scripts = platform.createScripts();
|
||||||
}
|
}
|
||||||
scripts.run(mod, file);
|
scripts.run(mod, main);
|
||||||
}catch(Throwable e){
|
}catch(Throwable e){
|
||||||
Core.app.post(() -> {
|
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();
|
e.printStackTrace();
|
||||||
});
|
});
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
}else{
|
||||||
|
Core.app.post(() -> {
|
||||||
|
Log.err("No main.js found for mod {0}.", mod.meta.name);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,9 +5,16 @@ import arc.files.*;
|
|||||||
import arc.struct.*;
|
import arc.struct.*;
|
||||||
import arc.util.*;
|
import arc.util.*;
|
||||||
import arc.util.Log.*;
|
import arc.util.Log.*;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.regex.*;
|
||||||
import mindustry.*;
|
import mindustry.*;
|
||||||
import mindustry.mod.Mods.*;
|
import mindustry.mod.Mods.*;
|
||||||
import org.mozilla.javascript.*;
|
import org.mozilla.javascript.*;
|
||||||
|
import org.mozilla.javascript.commonjs.module.*;
|
||||||
|
import org.mozilla.javascript.commonjs.module.provider.*;
|
||||||
|
|
||||||
public class Scripts implements Disposable{
|
public class Scripts implements Disposable{
|
||||||
private final Array<String> blacklist = Array.with("net", "files", "reflect", "javax", "rhino", "file", "channels", "jdk",
|
private final Array<String> blacklist = Array.with("net", "files", "reflect", "javax", "rhino", "file", "channels", "jdk",
|
||||||
@@ -18,6 +25,7 @@ public class Scripts implements Disposable{
|
|||||||
private final String wrapper;
|
private final String wrapper;
|
||||||
private Scriptable scope;
|
private Scriptable scope;
|
||||||
private boolean errored;
|
private boolean errored;
|
||||||
|
private LoadedMod loadedMod = null;
|
||||||
|
|
||||||
public Scripts(){
|
public Scripts(){
|
||||||
Time.mark();
|
Time.mark();
|
||||||
@@ -27,6 +35,10 @@ public class Scripts implements Disposable{
|
|||||||
context.getWrapFactory().setJavaPrimitiveWrap(false);
|
context.getWrapFactory().setJavaPrimitiveWrap(false);
|
||||||
|
|
||||||
scope = new ImporterTopLevel(context);
|
scope = new ImporterTopLevel(context);
|
||||||
|
|
||||||
|
new RequireBuilder()
|
||||||
|
.setModuleScriptProvider(new SoftCachingModuleScriptProvider(new ScriptModuleProvider()))
|
||||||
|
.setSandboxed(true).createRequire(context, scope).install(scope);
|
||||||
wrapper = Core.files.internal("scripts/wrapper.js").readString();
|
wrapper = Core.files.internal("scripts/wrapper.js").readString();
|
||||||
|
|
||||||
if(!run(Core.files.internal("scripts/global.js").readString(), "global.js")){
|
if(!run(Core.files.internal("scripts/global.js").readString(), "global.js")){
|
||||||
@@ -68,7 +80,9 @@ public class Scripts implements Disposable{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void run(LoadedMod mod, Fi file){
|
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());
|
loadedMod = mod;
|
||||||
|
run(fillWrapper(file), file.name());
|
||||||
|
loadedMod = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean run(String script, String file){
|
private boolean run(String script, String file){
|
||||||
@@ -81,8 +95,48 @@ public class Scripts implements Disposable{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String fillWrapper(Fi file){
|
||||||
|
return wrapper.replace("$SCRIPT_NAME$", loadedMod.name + "/" + file.nameWithoutExtension())
|
||||||
|
.replace("$CODE$", file.readString())
|
||||||
|
.replace("$MOD_NAME$", loadedMod.name);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dispose(){
|
public void dispose(){
|
||||||
Context.exit();
|
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(loadedMod == null) return null;
|
||||||
|
return loadSource(loadedMod, moduleId, loadedMod.root.child("scripts"), validator);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ModuleSource loadSource(LoadedMod mod, String moduleId, Fi root, Object validator) throws IOException, 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 == 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(mod, script, dir, validator);
|
||||||
|
}
|
||||||
|
return loadSource(required, 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((fillWrapper(module)).getBytes())),
|
||||||
|
null, new URI(moduleId), root.file().toURI(), validator);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user