Search in sources :

Example 1 with NotAllModsLoadedException

use of io.xol.chunkstories.api.exceptions.content.mods.NotAllModsLoadedException in project chunkstories by Hugobros3.

the class ModsManagerImplementation method loadEnabledMods.

@Override
public void loadEnabledMods() throws NotAllModsLoadedException {
    enabledMods.clear();
    List<ModLoadFailureException> modLoadExceptions = new ArrayList<ModLoadFailureException>();
    // Creates mods dir if it needs to
    File modsDir = new File(GameDirectory.getGameFolderPath() + "/mods");
    if (!modsDir.exists())
        modsDir.mkdirs();
    // Server mods
    File serverMods = new File(GameDirectory.getGameFolderPath() + "/servermods");
    if (!serverMods.exists())
        serverMods.mkdirs();
    for (String name : modsEnabled) {
        try {
            ModImplementation mod = null;
            // Servers give a md5 hash for their required mods
            if (name.startsWith("md5:")) {
                // Look for a mod with that md5 hash
                String hash = name.substring(4, name.length());
                String url = null;
                // If the hash is bundled with an url, split'em
                if (hash.contains(":")) {
                    int i = hash.indexOf(":");
                    url = hash.substring(i + 1);
                    hash = hash.substring(0, i);
                }
                System.out.println("Looking for hashed mod " + hash + " (url = " + url + ")");
                // Look for the mod zip in local fs first.
                File zippedMod = new File(serverMods.getAbsolutePath() + "/" + hash + ".zip");
                if (zippedMod.exists()) {
                    // Awesome we found it !
                    mod = new ModZip(zippedMod);
                } else if (url != null) {
                // TODO download and hanle files from server
                } else {
                // We failed. Mod won't be loaded
                }
            } else {
                System.out.println("Looking for mod " + name + " on the local filesystem");
                // First look for it in the directory section
                File modDirectory = new File(modsDir.getAbsolutePath() + "/" + name);
                if (modDirectory.exists()) {
                    mod = new ModFolder(modDirectory);
                    System.out.println("Found mod in directory : " + modDirectory);
                } else {
                    // Then look for a .zip file in the same directory
                    File zippedMod = new File(modsDir.getAbsolutePath() + "/" + name + ".zip");
                    if (zippedMod.exists()) {
                        mod = new ModZip(zippedMod);
                        System.out.println("Found mod in zipfile : " + zippedMod);
                    } else {
                        // Finally just look for it in the global os path
                        if (name.endsWith(".zip")) {
                            zippedMod = new File(name);
                            if (zippedMod.exists()) {
                                mod = new ModZip(zippedMod);
                                System.out.println("Found mod in global zipfile : " + zippedMod);
                            }
                        } else {
                            modDirectory = new File(name);
                            if (modDirectory.exists()) {
                                mod = new ModFolder(modDirectory);
                                System.out.println("Found mod in global directory : " + modDirectory);
                            }
                        }
                    }
                }
            }
            // Did we manage it ?
            if (mod != null) {
                if (!enabledMods.add(mod)) {
                    // Somehow we added a mod twice and it's now conflicting.
                    throw new ModLoadFailureException(mod, "Conflicting mod, another mod with the same name or hash is already loaded.");
                }
            } else
                throw new ModNotFoundException(name);
        } catch (ModLoadFailureException exception) {
            modLoadExceptions.add(exception);
        }
    }
    buildModsFileSystem();
    // Return an exception if some mods failed to load.
    if (modLoadExceptions.size() > 0)
        throw new NotAllModsLoadedException(modLoadExceptions);
}
Also used : ModLoadFailureException(io.xol.chunkstories.api.exceptions.content.mods.ModLoadFailureException) ArrayList(java.util.ArrayList) File(java.io.File) ModNotFoundException(io.xol.chunkstories.api.exceptions.content.mods.ModNotFoundException) NotAllModsLoadedException(io.xol.chunkstories.api.exceptions.content.mods.NotAllModsLoadedException)

Example 2 with NotAllModsLoadedException

use of io.xol.chunkstories.api.exceptions.content.mods.NotAllModsLoadedException in project chunkstories by Hugobros3.

the class ModsManagerTest method testModsManager.

@Test
public void testModsManager() {
    String coreContentLocation = System.getProperty("coreContentLocation", "../chunkstories-core/build/distributions/core_content.zip");
    try {
        ModsManager modsManager = new ModsManagerImplementation(new File(coreContentLocation));
        modsManager.loadEnabledMods();
    } catch (NonExistentCoreContent e) {
        e.printStackTrace();
        fail();
    } catch (NotAllModsLoadedException e) {
        e.printStackTrace();
    }
}
Also used : ModsManagerImplementation(io.xol.chunkstories.content.mods.ModsManagerImplementation) ModsManager(io.xol.chunkstories.api.content.mods.ModsManager) File(java.io.File) NotAllModsLoadedException(io.xol.chunkstories.api.exceptions.content.mods.NotAllModsLoadedException) NonExistentCoreContent(io.xol.chunkstories.content.mods.ModsManagerImplementation.NonExistentCoreContent) Test(org.junit.Test)

Aggregations

NotAllModsLoadedException (io.xol.chunkstories.api.exceptions.content.mods.NotAllModsLoadedException)2 File (java.io.File)2 ModsManager (io.xol.chunkstories.api.content.mods.ModsManager)1 ModLoadFailureException (io.xol.chunkstories.api.exceptions.content.mods.ModLoadFailureException)1 ModNotFoundException (io.xol.chunkstories.api.exceptions.content.mods.ModNotFoundException)1 ModsManagerImplementation (io.xol.chunkstories.content.mods.ModsManagerImplementation)1 NonExistentCoreContent (io.xol.chunkstories.content.mods.ModsManagerImplementation.NonExistentCoreContent)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1