Search in sources :

Example 1 with ForeignCodeClassLoader

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;
}
Also used : ForeignCodeClassLoader(io.xol.chunkstories.content.sandbox.ForeignCodeClassLoader)

Example 2 with ForeignCodeClassLoader

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;
}
Also used : NotAPluginException(io.xol.chunkstories.plugin.NotAPluginException) PluginLoadException(io.xol.chunkstories.api.exceptions.plugins.PluginLoadException) Asset(io.xol.chunkstories.api.content.Asset) IOException(java.io.IOException) File(java.io.File) ForeignCodeClassLoader(io.xol.chunkstories.content.sandbox.ForeignCodeClassLoader) LinkedList(java.util.LinkedList) PluginInformationImplementation(io.xol.chunkstories.plugin.PluginInformationImplementation)

Aggregations

ForeignCodeClassLoader (io.xol.chunkstories.content.sandbox.ForeignCodeClassLoader)2 Asset (io.xol.chunkstories.api.content.Asset)1 PluginLoadException (io.xol.chunkstories.api.exceptions.plugins.PluginLoadException)1 NotAPluginException (io.xol.chunkstories.plugin.NotAPluginException)1 PluginInformationImplementation (io.xol.chunkstories.plugin.PluginInformationImplementation)1 File (java.io.File)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1