Security / Android compat / Functional extension
This commit is contained in:
@@ -69,6 +69,11 @@ public abstract class ClientLauncher extends ApplicationCore implements Platform
|
||||
Musics.load();
|
||||
Sounds.load();
|
||||
|
||||
assets.loadRun("scriptinit", Scripts.class, () -> {}, () -> {
|
||||
content.createContent(false);
|
||||
mods.loadScripts();
|
||||
});
|
||||
|
||||
assets.loadRun("contentcreate", Content.class, () -> {
|
||||
content.createContent();
|
||||
content.loadColors();
|
||||
@@ -193,7 +198,8 @@ public abstract class ClientLauncher extends ApplicationCore implements Platform
|
||||
|
||||
if(assets.getCurrentLoading() != null){
|
||||
String name = assets.getCurrentLoading().fileName.toLowerCase();
|
||||
String key = name.contains("content") ? "content" : name.contains("mod") ? "mods" : name.contains("msav") || name.contains("maps") ? "map" : name.contains("ogg") || name.contains("mp3") ? "sound" : name.contains("png") ? "image" : "system";
|
||||
String key = name.contains("script") ? "scripts" : name.contains("content") ? "content" : name.contains("mod") ? "mods" : name.contains("msav") ||
|
||||
name.contains("maps") ? "map" : name.contains("ogg") || name.contains("mp3") ? "sound" : name.contains("png") ? "image" : "system";
|
||||
font.draw(bundle.get("load." + key, ""), graphics.getWidth() / 2f, graphics.getHeight() / 2f - height / 2f - Scl.scl(10f), Align.center);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,35 +53,45 @@ public class ContentLoader{
|
||||
|
||||
/** Creates all content types. */
|
||||
public void createContent(){
|
||||
createContent(true);
|
||||
}
|
||||
|
||||
/** Creates all content types. */
|
||||
public void createContent(boolean load){
|
||||
if(loaded){
|
||||
Log.info("Content already loaded, skipping.");
|
||||
return;
|
||||
}
|
||||
|
||||
for(ContentType type : ContentType.values()){
|
||||
contentMap[type.ordinal()] = new Array<>();
|
||||
contentNameMap[type.ordinal()] = new ObjectMap<>();
|
||||
}
|
||||
|
||||
for(ContentList list : content){
|
||||
list.load();
|
||||
}
|
||||
|
||||
if(mods != null){
|
||||
mods.loadContent();
|
||||
}
|
||||
|
||||
//check up ID mapping, make sure it's linear
|
||||
for(Array<Content> arr : contentMap){
|
||||
for(int i = 0; i < arr.size; i++){
|
||||
int id = arr.get(i).id;
|
||||
if(id != i){
|
||||
throw new IllegalArgumentException("Out-of-order IDs for content '" + arr.get(i) + "' (expected " + i + " but got " + id + ")");
|
||||
}
|
||||
if(contentMap[0] == null){
|
||||
for(ContentType type : ContentType.values()){
|
||||
contentMap[type.ordinal()] = new Array<>();
|
||||
contentNameMap[type.ordinal()] = new ObjectMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
loaded = true;
|
||||
if(load){
|
||||
|
||||
for(ContentList list : content){
|
||||
list.load();
|
||||
}
|
||||
|
||||
if(mods != null){
|
||||
mods.loadContent();
|
||||
}
|
||||
|
||||
//check up ID mapping, make sure it's linear
|
||||
for(Array<Content> arr : contentMap){
|
||||
for(int i = 0; i < arr.size; i++){
|
||||
int id = arr.get(i).id;
|
||||
if(id != i){
|
||||
throw new IllegalArgumentException("Out-of-order IDs for content '" + arr.get(i) + "' (expected " + i + " but got " + id + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
loaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
/** Logs content statistics.*/
|
||||
|
||||
@@ -13,6 +13,7 @@ import io.anuke.mindustry.net.*;
|
||||
import io.anuke.mindustry.net.Net.*;
|
||||
import io.anuke.mindustry.type.*;
|
||||
import io.anuke.mindustry.ui.dialogs.*;
|
||||
import org.mozilla.javascript.*;
|
||||
|
||||
import static io.anuke.mindustry.Vars.mobile;
|
||||
|
||||
@@ -51,6 +52,10 @@ public interface Platform{
|
||||
return new Scripts();
|
||||
}
|
||||
|
||||
default Context getScriptContext(){
|
||||
return Context.enter();
|
||||
}
|
||||
|
||||
/** Add a text input dialog that should show up after the field is tapped. */
|
||||
default void addDialog(TextField field){
|
||||
addDialog(field, 16);
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -30,7 +30,7 @@ import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
public class Mods implements Loadable{
|
||||
private Json json = new Json();
|
||||
private Scripts scripts;
|
||||
private @Nullable Scripts scripts;
|
||||
private ContentParser parser = new ContentParser();
|
||||
private ObjectMap<String, Array<FileHandle>> bundles = new ObjectMap<>();
|
||||
private ObjectSet<String> specialFolders = ObjectSet.with("bundles", "sprites");
|
||||
@@ -348,6 +348,12 @@ public class Mods implements Loadable{
|
||||
Sounds.dispose();
|
||||
Sounds.load();
|
||||
Core.assets.finishLoading();
|
||||
if(scripts != null){
|
||||
scripts.dispose();
|
||||
scripts = null;
|
||||
}
|
||||
content.createContent(false);
|
||||
loadScripts();
|
||||
content.clear();
|
||||
content.createContent();
|
||||
loadAsync();
|
||||
@@ -361,9 +367,8 @@ public class Mods implements Loadable{
|
||||
Events.fire(new ContentReloadEvent());
|
||||
}
|
||||
|
||||
/** Creates all the content found in mod files. */
|
||||
public void loadContent(){
|
||||
|
||||
/** This must be run on the main thread! */
|
||||
public void loadScripts(){
|
||||
Time.mark();
|
||||
|
||||
for(LoadedMod mod : loaded){
|
||||
@@ -390,6 +395,10 @@ public class Mods implements Loadable{
|
||||
}
|
||||
|
||||
Log.info("Time to initialize modded scripts: {0}", Time.elapsed());
|
||||
}
|
||||
|
||||
/** Creates all the content found in mod files. */
|
||||
public void loadContent(){
|
||||
|
||||
class LoadRun implements Comparable<LoadRun>{
|
||||
final ContentType type;
|
||||
|
||||
@@ -7,10 +7,7 @@ import io.anuke.mindustry.*;
|
||||
import io.anuke.mindustry.mod.Mods.*;
|
||||
import org.mozilla.javascript.*;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class Scripts{
|
||||
private static final Class[] denied = {FileHandle.class, InputStream.class, File.class, Scripts.class, Files.class, ClassAccess.class};
|
||||
public class Scripts implements Disposable{
|
||||
private final Context context;
|
||||
private final String wrapper;
|
||||
private Scriptable scope;
|
||||
@@ -18,12 +15,26 @@ public class Scripts{
|
||||
public Scripts(){
|
||||
Time.mark();
|
||||
|
||||
context = Context.enter();
|
||||
if(Vars.mobile){
|
||||
context.setOptimizationLevel(-1);
|
||||
}
|
||||
context = Vars.platform.getScriptContext();
|
||||
context.setClassShutter(type -> ClassAccess.allowedClassNames.contains(type) || type.startsWith("adapter") || type.contains("PrintStream"));
|
||||
context.setErrorReporter(new ErrorReporter(){
|
||||
@Override
|
||||
public void warning(String message, String sourceName, int line, String lineSource, int lineOffset){
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String message, String sourceName, int line, String lineSource, int lineOffset){
|
||||
Log.info(message + "@" + sourceName + ":" + line);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EvaluatorException runtimeError(String message, String sourceName, int line, String lineSource, int lineOffset){
|
||||
Log.info(message + "@" + sourceName + ":" + line);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
//context.setClassShutter(ClassAccess.allowedClassNames::contains);
|
||||
scope = context.initStandardObjects();
|
||||
wrapper = Core.files.internal("scripts/wrapper.js").readString();
|
||||
|
||||
@@ -32,10 +43,15 @@ public class Scripts{
|
||||
}
|
||||
|
||||
public void run(LoadedMod mod, FileHandle file){
|
||||
run(wrapper.replace("$SCRIPT_NAME$", mod.name + "_" +file.nameWithoutExtension().replace("-", "_").replace(" ", "_")).replace("$CODE$", file.readString()), file.name());
|
||||
run(wrapper.replace("$SCRIPT_NAME$", mod.name + "_" + file.nameWithoutExtension().replace("-", "_").replace(" ", "_")).replace("$CODE$", file.readString()), file.name());
|
||||
}
|
||||
|
||||
private void run(String script, String file){
|
||||
context.evaluateString(scope, script, file, 1, null);
|
||||
context.evaluateString(scope, script, file, 1, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose(){
|
||||
Context.exit();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user