use of io.xol.chunkstories.content.sandbox.ForeignCodeClassLoader in project chunkstories by Hugobros3.
the class ModsManagerImplementation method getClassByName.
@Override
public Class<?> getClassByName(String className) {
// TODO: Make this absolutely unecessary
try {
Class<?> inBaseClasspath = Class.forName(className);
if (inBaseClasspath != null)
return inBaseClasspath;
} catch (ClassNotFoundException e) {
// We don't really care about this
// e.printStackTrace();
}
ForeignCodeClassLoader loader = avaibleForeignClasses.get(className);
if (loader == null) {
logger().warn("Class " + className + " was not found in any loaded mod.");
return null;
}
Class<?> loadedClass = loader.obtainClass(className);
if (loadedClass != null)
return loadedClass;
logger().error("Failed to load class " + className);
// If all fail, return null
return null;
}
use of io.xol.chunkstories.content.sandbox.ForeignCodeClassLoader in project chunkstories by Hugobros3.
the class ModsManagerImplementation method loadModAssets.
private ClassLoader loadModAssets(ModImplementation mod, ClassLoader parentClassLoader) {
List<File> jarFiles = new LinkedList<File>();
// For each asset in the said mod
for (Asset asset : mod.assets()) {
// Skips mod.txt
if (asset.getName().equals("./mod.txt"))
continue;
// Special case for .jar files : we extract them in the cache/ folder and make them avaible through secure ClassLoaders
if (asset.getName().endsWith(".jar")) {
File jarFile = loadJarFile(asset);
if (jarFile != null)
jarFiles.add(jarFile);
continue;
}
// Look for it's entry
ModsAssetHierarchy entry = avaibleAssets.get(asset.getName());
if (entry == null) {
entry = new ModsAssetHierarchy(asset);
avaibleAssets.put(asset.getName(), entry);
} else {
System.out.println("Adding asset " + asset + ", overriding previous stuff (top=" + entry.topInstance() + ")");
entry.addAssetInstance(asset);
}
}
// No jar files ? Just return the parent class loader.
if (jarFiles.size() == 0)
return parentClassLoader;
// Build a specialized class loader based on the parent one and the jars found within the mod
ForeignCodeClassLoader classLoader;
try {
classLoader = new ForeignCodeClassLoader(mod, parentClassLoader, jarFiles);
} catch (IOException e1) {
e1.printStackTrace();
System.out.println("Error whilst creating a ForeignCodeClassLoader for " + mod);
return parentClassLoader;
}
for (String className : classLoader.classes()) {
avaibleForeignClasses.put(className, classLoader);
}
// Load any plugins those jar files might be
for (File jarFile : jarFiles) {
try {
PluginInformationImplementation pluginInformation = new PluginInformationImplementation(jarFile, classLoader);
logger.info("Found plugin " + pluginInformation + " in " + mod);
pluginsWithinEnabledMods.add(pluginInformation);
} catch (NotAPluginException nap) {
// Discard silently
} catch (PluginLoadException | IOException e) {
System.out.println("Something went wrong loading the plugin " + jarFile + " from " + mod);
e.printStackTrace();
}
}
return classLoader;
}
Aggregations