Merge branch 'master' into mod-dependencies
This commit is contained in:
13
core/src/mindustry/mod/ClassLoaderCloser.java
Normal file
13
core/src/mindustry/mod/ClassLoaderCloser.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package mindustry.mod;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
public class ClassLoaderCloser{
|
||||
|
||||
/** Workaround for the close() method not being available on Android. */
|
||||
public static void close(ClassLoader loader) throws Exception{
|
||||
if(loader instanceof URLClassLoader u){
|
||||
u.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import arc.struct.*;
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ClassMap{
|
||||
public static final ObjectMap<String, Class<?>> classes = new ObjectMap<>();
|
||||
|
||||
|
||||
static{
|
||||
classes.put("AssemblerAI", mindustry.ai.types.AssemblerAI.class);
|
||||
classes.put("BoostAI", mindustry.ai.types.BoostAI.class);
|
||||
@@ -104,6 +104,7 @@ public class ClassMap{
|
||||
classes.put("ItemStack", mindustry.type.ItemStack.class);
|
||||
classes.put("Liquid", mindustry.type.Liquid.class);
|
||||
classes.put("LiquidStack", mindustry.type.LiquidStack.class);
|
||||
classes.put("MapLocales", mindustry.type.MapLocales.class);
|
||||
classes.put("PayloadSeq", mindustry.type.PayloadSeq.class);
|
||||
classes.put("PayloadStack", mindustry.type.PayloadStack.class);
|
||||
classes.put("Planet", mindustry.type.Planet.class);
|
||||
@@ -140,7 +141,9 @@ public class ClassMap{
|
||||
classes.put("ConstructBlock", mindustry.world.blocks.ConstructBlock.class);
|
||||
classes.put("ConstructBuild", mindustry.world.blocks.ConstructBlock.ConstructBuild.class);
|
||||
classes.put("ControlBlock", mindustry.world.blocks.ControlBlock.class);
|
||||
classes.put("ExplosionShield", mindustry.world.blocks.ExplosionShield.class);
|
||||
classes.put("ItemSelection", mindustry.world.blocks.ItemSelection.class);
|
||||
classes.put("RotBlock", mindustry.world.blocks.RotBlock.class);
|
||||
classes.put("UnitTetherBlock", mindustry.world.blocks.UnitTetherBlock.class);
|
||||
classes.put("Accelerator", mindustry.world.blocks.campaign.Accelerator.class);
|
||||
classes.put("AcceleratorBuild", mindustry.world.blocks.campaign.Accelerator.AcceleratorBuild.class);
|
||||
@@ -250,6 +253,8 @@ public class ClassMap{
|
||||
classes.put("OreBlock", mindustry.world.blocks.environment.OreBlock.class);
|
||||
classes.put("OverlayFloor", mindustry.world.blocks.environment.OverlayFloor.class);
|
||||
classes.put("Prop", mindustry.world.blocks.environment.Prop.class);
|
||||
classes.put("RemoveOre", mindustry.world.blocks.environment.RemoveOre.class);
|
||||
classes.put("RemoveWall", mindustry.world.blocks.environment.RemoveWall.class);
|
||||
classes.put("SeaBush", mindustry.world.blocks.environment.SeaBush.class);
|
||||
classes.put("Seaweed", mindustry.world.blocks.environment.Seaweed.class);
|
||||
classes.put("ShallowLiquid", mindustry.world.blocks.environment.ShallowLiquid.class);
|
||||
@@ -435,6 +440,7 @@ public class ClassMap{
|
||||
classes.put("ConsumeItemExplosive", mindustry.world.consumers.ConsumeItemExplosive.class);
|
||||
classes.put("ConsumeItemFilter", mindustry.world.consumers.ConsumeItemFilter.class);
|
||||
classes.put("ConsumeItemFlammable", mindustry.world.consumers.ConsumeItemFlammable.class);
|
||||
classes.put("ConsumeItemList", mindustry.world.consumers.ConsumeItemList.class);
|
||||
classes.put("ConsumeItemRadioactive", mindustry.world.consumers.ConsumeItemRadioactive.class);
|
||||
classes.put("ConsumeItems", mindustry.world.consumers.ConsumeItems.class);
|
||||
classes.put("ConsumeLiquid", mindustry.world.consumers.ConsumeLiquid.class);
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package mindustry.mod;
|
||||
|
||||
import arc.*;
|
||||
import arc.assets.*;
|
||||
import arc.assets.loaders.MusicLoader.*;
|
||||
import arc.assets.loaders.SoundLoader.*;
|
||||
import arc.audio.*;
|
||||
import arc.files.*;
|
||||
import arc.func.*;
|
||||
@@ -642,6 +639,19 @@ public class ContentParser{
|
||||
value.remove("sector");
|
||||
value.remove("planet");
|
||||
|
||||
if(value.has("rules")){
|
||||
JsonValue r = value.remove("rules");
|
||||
if(!r.isObject()) throw new RuntimeException("Rules must be an object!");
|
||||
out.rules = rules -> {
|
||||
try{
|
||||
//Use standard JSON, this is not content-parser relevant
|
||||
JsonIO.json.readFields(rules, r);
|
||||
}catch(Throwable e){ //Try not to crash here, as that would be catastrophic and confusing
|
||||
Log.err(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
readFields(out, value);
|
||||
});
|
||||
return out;
|
||||
@@ -698,7 +708,7 @@ public class ContentParser{
|
||||
throw new RuntimeException("Team field missing.");
|
||||
}
|
||||
value.remove("team");
|
||||
|
||||
|
||||
if(locate(ContentType.team, name) != null){
|
||||
entry = locate(ContentType.team, name);
|
||||
readBundle(ContentType.team, name, value);
|
||||
@@ -1097,29 +1107,41 @@ public class ContentParser{
|
||||
}
|
||||
Field field = metadata.field;
|
||||
try{
|
||||
boolean isMap = ObjectMap.class.isAssignableFrom(field.getType()) || ObjectIntMap.class.isAssignableFrom(field.getType()) || ObjectFloatMap.class.isAssignableFrom(field.getType());
|
||||
boolean mergeMap = isMap && child.has("add") && child.get("add").isBoolean() && child.getBoolean("add", false);
|
||||
if(child.isObject() && child.has("add") && (Seq.class.isAssignableFrom(field.getType()) || ObjectSet.class.isAssignableFrom(field.getType()))){
|
||||
Object readField = parser.readValue(field.getType(), metadata.elementType, child.get("add"), metadata.keyType);
|
||||
Object fieldObj = field.get(object);
|
||||
|
||||
if(mergeMap){
|
||||
child.remove("add");
|
||||
}
|
||||
|
||||
Object readField = parser.readValue(field.getType(), metadata.elementType, child, metadata.keyType);
|
||||
Object fieldObj = field.get(object);
|
||||
|
||||
//if a map has add: true, add its contents to the map instead
|
||||
if(mergeMap && (fieldObj instanceof ObjectMap<?,?> || fieldObj instanceof ObjectIntMap<?> || fieldObj instanceof ObjectFloatMap<?>)){
|
||||
if(field.get(object) instanceof ObjectMap<?,?> baseMap){
|
||||
baseMap.putAll((ObjectMap)readField);
|
||||
}else if(field.get(object) instanceof ObjectIntMap<?> baseMap){
|
||||
baseMap.putAll((ObjectIntMap)readField);
|
||||
}else if(field.get(object) instanceof ObjectFloatMap<?> baseMap){
|
||||
baseMap.putAll((ObjectFloatMap)readField);
|
||||
if(fieldObj instanceof ObjectSet set){
|
||||
set.addAll((ObjectSet)readField);
|
||||
}else if(fieldObj instanceof Seq seq){
|
||||
seq.addAll((Seq)readField);
|
||||
}else{
|
||||
throw new SerializationException("This should be impossible");
|
||||
}
|
||||
}else{
|
||||
field.set(object, readField);
|
||||
}
|
||||
boolean isMap = ObjectMap.class.isAssignableFrom(field.getType()) || ObjectIntMap.class.isAssignableFrom(field.getType()) || ObjectFloatMap.class.isAssignableFrom(field.getType());
|
||||
boolean mergeMap = isMap && child.has("add") && child.get("add").isBoolean() && child.getBoolean("add", false);
|
||||
|
||||
if(mergeMap){
|
||||
child.remove("add");
|
||||
}
|
||||
|
||||
Object readField = parser.readValue(field.getType(), metadata.elementType, child, metadata.keyType);
|
||||
Object fieldObj = field.get(object);
|
||||
|
||||
//if a map has add: true, add its contents to the map instead
|
||||
if(mergeMap && (fieldObj instanceof ObjectMap<?,?> || fieldObj instanceof ObjectIntMap<?> || fieldObj instanceof ObjectFloatMap<?>)){
|
||||
if(field.get(object) instanceof ObjectMap<?,?> baseMap){
|
||||
baseMap.putAll((ObjectMap)readField);
|
||||
}else if(field.get(object) instanceof ObjectIntMap<?> baseMap){
|
||||
baseMap.putAll((ObjectIntMap)readField);
|
||||
}else if(field.get(object) instanceof ObjectFloatMap<?> baseMap){
|
||||
baseMap.putAll((ObjectFloatMap)readField);
|
||||
}
|
||||
}else{
|
||||
field.set(object, readField);
|
||||
}
|
||||
}
|
||||
}catch(IllegalAccessException ex){
|
||||
throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
|
||||
}catch(SerializationException ex){
|
||||
@@ -1202,6 +1224,7 @@ public class ContentParser{
|
||||
}
|
||||
//reparent the node
|
||||
node.parent = parent;
|
||||
node.planet = parent.planet;
|
||||
}
|
||||
}else{
|
||||
Log.warn(unlock.name + " is not a root node, and does not have a `parent: ` property. Ignoring.");
|
||||
|
||||
@@ -63,12 +63,13 @@ public class Mods implements Loadable{
|
||||
return mainLoader;
|
||||
}
|
||||
|
||||
/** @return the folder where configuration files for this mod should go. The folder may not exist yet; call mkdirs() before writing to it.
|
||||
* Call this in init(). */
|
||||
/** @return the folder where configuration files for this mod should go. Call this in init(). */
|
||||
public Fi getConfigFolder(Mod mod){
|
||||
ModMeta load = metas.get(mod.getClass());
|
||||
if(load == null) throw new IllegalArgumentException("Mod is not loaded yet (or missing)!");
|
||||
return modDirectory.child(load.name);
|
||||
Fi result = modDirectory.child(load.name);
|
||||
result.mkdirs();
|
||||
return result;
|
||||
}
|
||||
|
||||
/** @return a file named 'config.json' in the config folder for the specified mod.
|
||||
@@ -413,6 +414,14 @@ public class Mods implements Loadable{
|
||||
|
||||
/** Removes a mod file and marks it for requiring a restart. */
|
||||
public void removeMod(LoadedMod mod){
|
||||
if(!android && mod.loader != null){
|
||||
try{
|
||||
ClassLoaderCloser.close(mod.loader);
|
||||
}catch(Exception e){
|
||||
Log.err(e);
|
||||
}
|
||||
}
|
||||
|
||||
if(mod.root instanceof ZipFi){
|
||||
mod.root.delete();
|
||||
}
|
||||
@@ -426,7 +435,9 @@ public class Mods implements Loadable{
|
||||
mods.remove(mod);
|
||||
newImports.remove(mod);
|
||||
mod.dispose();
|
||||
requiresReload = true;
|
||||
if(mod.state != ModState.disabled){
|
||||
requiresReload = true;
|
||||
}
|
||||
}
|
||||
|
||||
public Scripts getScripts(){
|
||||
@@ -468,13 +479,7 @@ public class Mods implements Loadable{
|
||||
ModMeta meta = null;
|
||||
|
||||
try{
|
||||
Fi zip = file.isDirectory() ? file : new ZipFi(file);
|
||||
|
||||
if(zip.list().length == 1 && zip.list()[0].isDirectory()){
|
||||
zip = zip.list()[0];
|
||||
}
|
||||
|
||||
meta = findMeta(zip);
|
||||
meta = findMeta(resolveRoot(file.isDirectory() ? file : new ZipFi(file)));
|
||||
}catch(Throwable ignored){
|
||||
}
|
||||
|
||||
@@ -499,7 +504,7 @@ public class Mods implements Loadable{
|
||||
if(steam) mod.addSteamID(file.name());
|
||||
}catch(Throwable e){
|
||||
if(e instanceof ClassNotFoundException && e.getMessage().contains("mindustry.plugin.Plugin")){
|
||||
Log.info("Plugin '@' is outdated and needs to be ported to 6.0! Update its main class to inherit from 'mindustry.mod.Plugin'. See https://mindustrygame.github.io/wiki/modding/6-migrationv6/", file.name());
|
||||
Log.warn("Plugin '@' is outdated and needs to be ported to v7! Update its main class to inherit from 'mindustry.mod.Plugin'.", file.name());
|
||||
}else if(steam){
|
||||
Log.err("Failed to load mod workshop file @. Skipping.", file);
|
||||
Log.err(e);
|
||||
@@ -929,6 +934,8 @@ public class Mods implements Loadable{
|
||||
|
||||
//this finishes parsing content fields
|
||||
parser.finishParsing();
|
||||
|
||||
Events.fire(new ModContentLoadEvent());
|
||||
}
|
||||
|
||||
public void handleContentError(Content content, Throwable error){
|
||||
@@ -1065,10 +1072,10 @@ public class Mods implements Loadable{
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Loads a mod file+meta, but does not add it to the list.
|
||||
* Note that directories can be loaded as mods. */
|
||||
private LoadedMod loadMod(Fi sourceFile) throws Exception{
|
||||
return loadMod(sourceFile, false, true);
|
||||
private Fi resolveRoot(Fi fi){
|
||||
if(OS.isMac && (!(fi instanceof ZipFi))) fi.child(".DS_Store").delete();
|
||||
Fi[] files = fi.list();
|
||||
return files.length == 1 && files[0].isDirectory() ? files[0] : fi;
|
||||
}
|
||||
|
||||
/** Loads a mod file+meta, but does not add it to the list.
|
||||
@@ -1079,10 +1086,7 @@ public class Mods implements Loadable{
|
||||
ZipFi rootZip = null;
|
||||
|
||||
try{
|
||||
Fi zip = sourceFile.isDirectory() ? sourceFile : (rootZip = new ZipFi(sourceFile));
|
||||
if(zip.list().length == 1 && zip.list()[0].isDirectory()){
|
||||
zip = zip.list()[0];
|
||||
}
|
||||
Fi zip = resolveRoot(sourceFile.isDirectory() ? sourceFile : (rootZip = new ZipFi(sourceFile)));
|
||||
|
||||
ModMeta meta = findMeta(zip);
|
||||
|
||||
@@ -1100,6 +1104,12 @@ public class Mods implements Loadable{
|
||||
if(other != null){
|
||||
//steam mods can't really be deleted, they need to be unsubscribed
|
||||
if(overwrite && !other.hasSteamID()){
|
||||
|
||||
//close the classloader for jar mods
|
||||
if(!android){
|
||||
ClassLoaderCloser.close(other.loader);
|
||||
}
|
||||
|
||||
//close zip file
|
||||
if(other.root instanceof ZipFi){
|
||||
other.root.delete();
|
||||
@@ -1398,6 +1408,7 @@ public class Mods implements Loadable{
|
||||
if(name != null) name = Strings.stripColors(name);
|
||||
if(displayName != null) displayName = Strings.stripColors(displayName);
|
||||
if(displayName == null) displayName = name;
|
||||
if(version == null) version = "0";
|
||||
if(author != null) author = Strings.stripColors(author);
|
||||
if(description != null) description = Strings.stripColors(description);
|
||||
if(subtitle != null) subtitle = Strings.stripColors(subtitle).replace("\n", "");
|
||||
|
||||
@@ -10,8 +10,8 @@ import rhino.*;
|
||||
import rhino.module.*;
|
||||
import rhino.module.provider.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Scripts implements Disposable{
|
||||
@@ -46,7 +46,18 @@ public class Scripts implements Disposable{
|
||||
Object o = context.evaluateString(scope, text, "console.js", 1);
|
||||
if(o instanceof NativeJavaObject n) o = n.unwrap();
|
||||
if(o == null) o = "null";
|
||||
|
||||
else if(o instanceof Undefined) o = "undefined";
|
||||
|
||||
else if(o instanceof Object[] arr) o = Arrays.toString(arr);
|
||||
else if(o instanceof int[] arr) o = Arrays.toString(arr);
|
||||
else if(o instanceof float[] arr) o = Arrays.toString(arr);
|
||||
else if(o instanceof byte[] arr) o = Arrays.toString(arr);
|
||||
else if(o instanceof double[] arr) o = Arrays.toString(arr);
|
||||
else if(o instanceof long[] arr) o = Arrays.toString(arr);
|
||||
else if(o instanceof char[] arr) o = Arrays.toString(arr);
|
||||
else if(o instanceof boolean[] arr) o = Arrays.toString(arr);
|
||||
|
||||
var out = o.toString();
|
||||
return out == null ? "null" : out;
|
||||
}catch(Throwable t){
|
||||
@@ -73,7 +84,7 @@ public class Scripts implements Disposable{
|
||||
|
||||
public void run(LoadedMod mod, Fi file){
|
||||
currentMod = mod;
|
||||
run(file.readString(), file.name(), true);
|
||||
run(file.readString(), mod.name + "/" + file.name(), true);
|
||||
currentMod = null;
|
||||
}
|
||||
|
||||
@@ -83,15 +94,10 @@ public class Scripts implements Disposable{
|
||||
//inject script info into file
|
||||
context.evaluateString(scope, "modName = \"" + currentMod.name + "\"\nscriptName = \"" + file + "\"", "initscript.js", 1);
|
||||
}
|
||||
context.evaluateString(scope,
|
||||
wrap ? "(function(){'use strict';\n" + script + "\n})();" : script,
|
||||
file, 0);
|
||||
context.evaluateString(scope, wrap ? "(function(){'use strict';\n" + script + "\n})();" : script, file, 0);
|
||||
return true;
|
||||
}catch(Throwable t){
|
||||
if(currentMod != null){
|
||||
file = currentMod.name + "/" + file;
|
||||
}
|
||||
log(LogLevel.err, file, "" + getError(t, true));
|
||||
log(LogLevel.err, file, getError(t, true));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -111,10 +117,10 @@ public class Scripts implements Disposable{
|
||||
@Override
|
||||
public ModuleSource loadSource(String moduleId, Scriptable paths, Object validator) throws URISyntaxException{
|
||||
if(currentMod == null) return null;
|
||||
return loadSource(moduleId, currentMod.root.child("scripts"), validator);
|
||||
return loadSource(currentMod, moduleId, currentMod.root.child("scripts"), validator);
|
||||
}
|
||||
|
||||
private ModuleSource loadSource(String moduleId, Fi root, Object validator) throws URISyntaxException{
|
||||
private ModuleSource loadSource(LoadedMod sourceMod, String moduleId, Fi root, Object validator) throws URISyntaxException{
|
||||
Matcher matched = directory.matcher(moduleId);
|
||||
if(matched.find()){
|
||||
LoadedMod required = Vars.mods.locateMod(matched.group(1));
|
||||
@@ -122,18 +128,16 @@ public class Scripts implements Disposable{
|
||||
if(required == null){ // Mod not found, treat it as a folder
|
||||
Fi dir = root.child(matched.group(1));
|
||||
if(!dir.exists()) return null; // Mod and folder not found
|
||||
return loadSource(script, dir, validator);
|
||||
return loadSource(sourceMod, script, dir, validator);
|
||||
}
|
||||
|
||||
currentMod = required;
|
||||
return loadSource(script, required.root.child("scripts"), validator);
|
||||
return loadSource(sourceMod, 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())),
|
||||
new URI(moduleId), root.file().toURI(), validator);
|
||||
return new ModuleSource(module.reader(Vars.bufferSize), new URI(sourceMod.name + "/" + moduleId + ".js"), root.file().toURI(), validator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user