Allow JSON mod content types from other class loaders
This commit is contained in:
@@ -72,9 +72,8 @@ public class AndroidLauncher extends AndroidApplication{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class<?> loadJar(Fi jar, String mainClass) throws Exception{
|
public ClassLoader loadJar(Fi jar, String mainClass) throws Exception{
|
||||||
DexClassLoader loader = new DexClassLoader(jar.file().getPath(), getFilesDir().getPath(), null, getClassLoader());
|
return new DexClassLoader(jar.file().getPath(), getFilesDir().getPath(), null, getClassLoader());
|
||||||
return Class.forName(mainClass, true, loader);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -20,10 +20,9 @@ import static mindustry.Vars.*;
|
|||||||
|
|
||||||
public interface Platform{
|
public interface Platform{
|
||||||
|
|
||||||
/** Dynamically loads a jar file. */
|
/** Dynamically creates a class loader for a jar file. */
|
||||||
default Class<?> loadJar(Fi jar, String mainClass) throws Exception{
|
default ClassLoader loadJar(Fi jar, String mainClass) throws Exception{
|
||||||
URLClassLoader classLoader = new URLClassLoader(new URL[]{jar.file().toURI().toURL()}, getClass().getClassLoader());
|
return new URLClassLoader(new URL[]{jar.file().toURI().toURL()}, getClass().getClassLoader());
|
||||||
return Class.forName(mainClass, true, classLoader);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Steam: Update lobby visibility.*/
|
/** Steam: Update lobby visibility.*/
|
||||||
|
|||||||
@@ -705,6 +705,14 @@ public class ContentParser{
|
|||||||
try{
|
try{
|
||||||
return (Class<T>)Class.forName(base);
|
return (Class<T>)Class.forName(base);
|
||||||
}catch(Exception ignored){
|
}catch(Exception ignored){
|
||||||
|
//try to load from a mod's class loader
|
||||||
|
for(LoadedMod mod : mods.mods){
|
||||||
|
if(mod.loader != null){
|
||||||
|
try{
|
||||||
|
return (Class<T>)Class.forName(base, true, mod.loader);
|
||||||
|
}catch(Exception ignore){}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -92,6 +92,8 @@ public class Mods implements Loadable{
|
|||||||
var loaded = loadMod(dest, true);
|
var loaded = loadMod(dest, true);
|
||||||
mods.add(loaded);
|
mods.add(loaded);
|
||||||
requiresReload = true;
|
requiresReload = true;
|
||||||
|
//enable the mod on import
|
||||||
|
Core.settings.put("mod-" + loaded.name + "-enabled", true);
|
||||||
sortMods();
|
sortMods();
|
||||||
//try to load the mod's icon so it displays on import
|
//try to load the mod's icon so it displays on import
|
||||||
Core.app.post(() -> {
|
Core.app.post(() -> {
|
||||||
@@ -664,9 +666,10 @@ public class Mods implements Loadable{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ClassLoader loader = null;
|
||||||
Mod mainMod;
|
Mod mainMod;
|
||||||
|
|
||||||
Fi mainFile = zip;
|
Fi mainFile = zip;
|
||||||
|
|
||||||
if(android){
|
if(android){
|
||||||
mainFile = mainFile.child("classes.dex");
|
mainFile = mainFile.child("classes.dex");
|
||||||
}else{
|
}else{
|
||||||
@@ -687,7 +690,8 @@ public class Mods implements Loadable{
|
|||||||
throw new IllegalArgumentException("Java class mods are not supported on iOS.");
|
throw new IllegalArgumentException("Java class mods are not supported on iOS.");
|
||||||
}
|
}
|
||||||
|
|
||||||
Class<?> main = platform.loadJar(sourceFile, mainClass);
|
loader = platform.loadJar(sourceFile, mainClass);
|
||||||
|
Class<?> main = Class.forName(mainClass, true, loader);
|
||||||
metas.put(main, meta);
|
metas.put(main, meta);
|
||||||
mainMod = (Mod)main.getDeclaredConstructor().newInstance();
|
mainMod = (Mod)main.getDeclaredConstructor().newInstance();
|
||||||
}else{
|
}else{
|
||||||
@@ -710,7 +714,7 @@ public class Mods implements Loadable{
|
|||||||
if(!headless){
|
if(!headless){
|
||||||
Log.info("Loaded mod '@' in @ms", meta.name, Time.elapsed());
|
Log.info("Loaded mod '@' in @ms", meta.name, Time.elapsed());
|
||||||
}
|
}
|
||||||
return new LoadedMod(sourceFile, zip, mainMod, meta);
|
return new LoadedMod(sourceFile, zip, mainMod, loader, meta);
|
||||||
|
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
//delete root zip file so it can be closed on windows
|
//delete root zip file so it can be closed on windows
|
||||||
@@ -743,10 +747,13 @@ public class Mods implements Loadable{
|
|||||||
public ModState state = ModState.enabled;
|
public ModState state = ModState.enabled;
|
||||||
/** Icon texture. Should be disposed. */
|
/** Icon texture. Should be disposed. */
|
||||||
public @Nullable Texture iconTexture;
|
public @Nullable Texture iconTexture;
|
||||||
|
/** Class loader for JAR mods. Null if the mod isn't loaded or this isn't a jar mod. */
|
||||||
|
public @Nullable ClassLoader loader;
|
||||||
|
|
||||||
public LoadedMod(Fi file, Fi root, Mod main, ModMeta meta){
|
public LoadedMod(Fi file, Fi root, Mod main, ClassLoader loader, ModMeta meta){
|
||||||
this.root = root;
|
this.root = root;
|
||||||
this.file = file;
|
this.file = file;
|
||||||
|
this.loader = loader;
|
||||||
this.main = main;
|
this.main = main;
|
||||||
this.meta = meta;
|
this.meta = meta;
|
||||||
this.name = meta.name.toLowerCase().replace(" ", "-");
|
this.name = meta.name.toLowerCase().replace(" ", "-");
|
||||||
|
|||||||
Reference in New Issue
Block a user