Search in sources :

Example 1 with PluginLoadException

use of io.xol.chunkstories.api.exceptions.plugins.PluginLoadException in project chunkstories by Hugobros3.

the class DefaultPluginManager method reloadPlugins.

@Override
public /**
 * Implementation : Checks %gamedir%/plugins/ folder, loads what it can within, checks enabled mods and their plugins/ folder, enables what it can
 */
void reloadPlugins() {
    // First disable any leftover plugins
    disablePlugins();
    // We make a list
    LinkedList<PluginInformationImplementation> pluginsToLoad = new LinkedList<PluginInformationImplementation>();
    // Creates plugins folder if it isn't present.
    File pluginsFolder = new File(GameDirectory.getGameFolderPath() + "/plugins/");
    pluginsFolder.mkdirs();
    // Iterates over files of the folder
    for (File file : pluginsFolder.listFiles()) {
        if (file.getName().endsWith(".jar")) {
            try {
                PluginInformationImplementation pluginInformation = new PluginInformationImplementation(file, ((ModsManagerImplementation) pluginExecutionContext.getContent().modsManager()).getFinalClassLoader());
                // Client only plugins require actually being a client
                if (pluginInformation.getPluginType() == PluginType.CLIENT_ONLY && !(this instanceof ClientPluginManager))
                    continue;
                else // Server only plugins require the same
                if (pluginInformation.getPluginType() == PluginType.MASTER && !(this instanceof ServerPluginManager))
                    continue;
                pluginsToLoad.add(pluginInformation);
            } catch (PluginLoadException | IOException e) {
                logger().error("Failed to load plugin file " + file + e.getMessage());
                e.printStackTrace();
            }
        }
    }
    // Mods too can bundle plugins
    for (PluginInformationImplementation pluginInformation : ((ModsManagerImplementation) this.pluginExecutionContext.getContent().modsManager()).getAllModsPlugins()) {
        // Client only plugins require actually being a client
        if (pluginInformation.getPluginType() == PluginType.CLIENT_ONLY && !(this instanceof ClientPluginManager))
            continue;
        else // Server only plugins require the same
        if (pluginInformation.getPluginType() == PluginType.MASTER && !(this instanceof ServerPluginManager))
            continue;
        pluginsToLoad.add(pluginInformation);
    }
    // Enables the found plugins
    enablePlugins(pluginsToLoad);
}
Also used : ModsManagerImplementation(io.xol.chunkstories.content.mods.ModsManagerImplementation) ClientPluginManager(io.xol.chunkstories.api.plugin.ClientPluginManager) PluginLoadException(io.xol.chunkstories.api.exceptions.plugins.PluginLoadException) ServerPluginManager(io.xol.chunkstories.api.plugin.ServerPluginManager) IOException(java.io.IOException) File(java.io.File) LinkedList(java.util.LinkedList)

Example 2 with PluginLoadException

use of io.xol.chunkstories.api.exceptions.plugins.PluginLoadException 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

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