Search in sources :

Example 1 with PluginInformationImplementation

use of io.xol.chunkstories.plugin.PluginInformationImplementation 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

Asset (io.xol.chunkstories.api.content.Asset)1 PluginLoadException (io.xol.chunkstories.api.exceptions.plugins.PluginLoadException)1 ForeignCodeClassLoader (io.xol.chunkstories.content.sandbox.ForeignCodeClassLoader)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