LiteLoaderEnumerator.java 26.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
/*
 * This file is part of LiteLoader.
 * Copyright (C) 2012-16 Adam Mummery-Smith
 * All Rights Reserved.
 */
package com.mumfrey.liteloader.core;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.Phase;
import org.spongepowered.asm.mixin.Mixins;

import com.google.common.base.Throwables;
import com.mumfrey.liteloader.LiteMod;
import com.mumfrey.liteloader.api.ContainerRegistry;
import com.mumfrey.liteloader.api.ContainerRegistry.DisabledReason;
import com.mumfrey.liteloader.api.EnumerationObserver;
import com.mumfrey.liteloader.api.EnumeratorModule;
import com.mumfrey.liteloader.api.EnumeratorPlugin;
import com.mumfrey.liteloader.api.LiteAPI;
import com.mumfrey.liteloader.api.ModClassValidator;
import com.mumfrey.liteloader.core.api.DefaultClassValidator;
import com.mumfrey.liteloader.core.api.DefaultEnumeratorPlugin;
import com.mumfrey.liteloader.core.event.HandlerList;
import com.mumfrey.liteloader.interfaces.FastIterableDeque;
import com.mumfrey.liteloader.interfaces.Injectable;
import com.mumfrey.liteloader.interfaces.Loadable;
import com.mumfrey.liteloader.interfaces.LoadableMod;
import com.mumfrey.liteloader.interfaces.LoaderEnumerator;
import com.mumfrey.liteloader.interfaces.MixinContainer;
import com.mumfrey.liteloader.interfaces.TweakContainer;
import com.mumfrey.liteloader.launch.ClassTransformerManager;
import com.mumfrey.liteloader.launch.LiteLoaderTweaker;
import com.mumfrey.liteloader.launch.LoaderEnvironment;
import com.mumfrey.liteloader.launch.LoaderProperties;
import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
import com.mumfrey.liteloader.util.log.LiteLoaderLogger.Verbosity;

import net.minecraft.launchwrapper.Launch;
import net.minecraft.launchwrapper.LaunchClassLoader;

/**
 * The enumerator performs all mod discovery functions for LiteLoader, this
 * includes locating mod files to load as well as searching for mod classes
 * within the class path and discovered mod files.
 *
 * @author Adam Mummery-Smith
 */
public class LiteLoaderEnumerator implements LoaderEnumerator
{
    public enum EnumeratorState
    {
        INIT(null),
        DISCOVER(INIT),
        INJECT(DISCOVER),
        REGISTER(INJECT),
        FINALISED(REGISTER);

        private final EnumeratorState previousState;

        private EnumeratorState(EnumeratorState previousState)
        {
            this.previousState = previousState;
        }

        public boolean checkGotoState(EnumeratorState fromState)
        {
            if (fromState != this && fromState != this.previousState)
            {
                throw new IllegalStateException("Attempted to move to an invalid enumerator state " + this + ", expected to be in state "
                        + this.previousState + " but current state is " + fromState);
            }

            return true;
        }
    }

    private final LoaderEnvironment environment;

    private final LoaderProperties properties;

    /**
     * Reference to the launch classloader
     */
    private final LaunchClassLoader classLoader;

    /**
     * 
     */
    private final List<EnumeratorModule> modules = new ArrayList<EnumeratorModule>();

    private final List<EnumeratorPlugin> plugins = new ArrayList<EnumeratorPlugin>();

    private final ContainerRegistry containers = new Containers();

    /**
     * Containers which have already been checked for potential mod candidates 
     */
    private final Set<LoadableMod<?>> enumeratedContainers = new HashSet<LoadableMod<?>>();

    /**
     * Classes to load, mapped by class name 
     */
    private final Set<ModInfo<LoadableMod<?>>> modsToLoad = new LinkedHashSet<ModInfo<LoadableMod<?>>>();

    private final ModClassValidator validator;

    private final FastIterableDeque<EnumerationObserver> observers = new HandlerList<EnumerationObserver>(EnumerationObserver.class);

    protected EnumeratorState state = EnumeratorState.INIT;

    /**
     * @param environment
     * @param properties
     * @param classLoader
     */
    public LiteLoaderEnumerator(LoaderEnvironment environment, LoaderProperties properties, LaunchClassLoader classLoader)
    {
        this.environment       = environment;
        this.properties        = properties;
        this.classLoader       = classLoader;
        this.validator         = this.getValidator(environment);

        this.initModules(environment);
        this.registerPlugin(new DefaultEnumeratorPlugin());

        // Initialise observers
        this.observers.addAll(environment.getAPIAdapter().getPreInitObservers(EnumerationObserver.class));

        // Initialise the shared mod list if we haven't already
        this.getSharedModList();
    }

    /**
     * @param environment
     */
    private ModClassValidator getValidator(LoaderEnvironment environment)
    {
        List<String> prefixes = new ArrayList<String>();

        for (LiteAPI api : environment.getAPIProvider().getAPIs())
        {
            String prefix = api.getModClassPrefix();
            if (prefix != null)
            {
                LiteLoaderLogger.info("Adding supported mod class prefix '%s'", prefix);
                prefixes.add(prefix);
            }
        }

        return new DefaultClassValidator<LiteMod>(LiteMod.class, prefixes);
    }

    /**
     * @param environment
     */
    private void initModules(LoaderEnvironment environment)
    {
        for (LiteAPI api : environment.getAPIProvider().getAPIs())
        {
            List<EnumeratorModule> apiModules = api.getEnumeratorModules();

            if (apiModules != null)
            {
                for (EnumeratorModule module : apiModules)
                {
                    this.registerModule(module);
                }
            }
        }
    }

    private void checkState(EnumeratorState state, String action)
    {
        if (this.state != state)
        {
            throw new IllegalStateException("Illegal enumerator state whilst performing " + action + ", expecting " + state + " but current state is "
                    + this.state);
        }
    }

    private void gotoState(EnumeratorState state)
    {
        if (state.checkGotoState(this.state))
        {
            this.state = state;
        }
    }

    /**
     * Get the loader environment
     */
    public LoaderEnvironment getEnvironment()
    {
        return this.environment;
    }

    /**
     * Initialise the "shared" mod list if it's not already been created
     */
    @Override
    public Map<String, Map<String, String>> getSharedModList()
    {
        try
        {
            @SuppressWarnings("unchecked")
            Map<String, Map<String,String>> sharedModList = (Map<String, Map<String, String>>) Launch.blackboard.get("modList");

            if (sharedModList == null)
            {
                sharedModList = new HashMap<String, Map<String,String>>();
                Launch.blackboard.put("modList", sharedModList);
            }

            return sharedModList;
        }
        catch (Exception ex)
        {
            LiteLoaderLogger.warning("Shared mod list was invalid or not accessible, this isn't especially bad but something isn't quite right");
            return null;
        }
    }

    /* (non-Javadoc)
     * @see com.mumfrey.liteloader.core.PluggableEnumerator
     *      #registerModule(com.mumfrey.liteloader.core.EnumeratorModule)
     */
    @Override
    public void registerModule(EnumeratorModule module)
    {
        this.checkState(EnumeratorState.INIT, "registerModule");

        if (module != null && !this.modules.contains(module))
        {
            LiteLoaderLogger.info("Registering discovery module %s: [%s]", module.getClass().getSimpleName(), module);
            this.modules.add(module);
            module.init(this.environment, this.properties);
        }
    }

    @Override
    public void registerPlugin(EnumeratorPlugin plugin)
    {
        this.checkState(EnumeratorState.INIT, "registerPlugin");

        if (plugin != null && !this.plugins.contains(plugin))
        {
            LiteLoaderLogger.info("Registering enumerator plugin %s: [%s]", plugin.getClass().getSimpleName(), plugin);
            this.plugins.add(plugin);
            plugin.init(this.environment, this.properties);
        }
    }

    /**
     * Get the list of all enumerated mod classes to load
     */
    @Override
    public Collection<? extends ModInfo<LoadableMod<?>>> getModsToLoad()
    {
        this.checkState(EnumeratorState.FINALISED, "getModsToLoad");
        return Collections.unmodifiableSet(this.modsToLoad);
    }

    /**
     * Get the set of disabled containers
     */
    @Override
    public Collection<? extends ModInfo<Loadable<?>>> getDisabledContainers()
    {
        this.checkState(EnumeratorState.FINALISED, "getDisabledContainers");
        return this.containers.getDisabledContainers();
    }

    @Override
    public Collection<? extends ModInfo<Loadable<?>>> getBadContainers()
    {
        this.checkState(EnumeratorState.FINALISED, "getBadContainers");
        return this.containers.getBadContainers();
    }

    /**
     * Get the list of injected tweak containers
     */
    @Override
    public Collection<? extends ModInfo<Loadable<?>>> getInjectedTweaks()
    {
        this.checkState(EnumeratorState.FINALISED, "getInjectedTweaks");
        return this.containers.getInjectedTweaks();
    }

    /**
     * Get the number of mods to load
     */
    @Override
    public int modsToLoadCount()
    {
        return this.modsToLoad.size();
    }

    /**
     * Get a metadata value for the specified mod
     * 
     * @param modClass
     * @param metaDataKey
     * @param defaultValue
     */
    @Override
    public String getModMetaData(Class<? extends LiteMod> modClass, String metaDataKey, String defaultValue)
    {
        this.checkState(EnumeratorState.FINALISED, "getModMetaData");
        return this.getContainerForMod(modClass).getMetaValue(metaDataKey, defaultValue);
    }

    /**
     * @param identifier
     */
    @Override
    public LoadableMod<?> getContainer(String identifier)
    {
        this.checkState(EnumeratorState.FINALISED, "getContainer");
        return this.containers.getEnabledContainer(identifier);
    }

    /**
     * @param modClass
     */
    @Override
    public LoadableMod<?> getContainer(Class<? extends LiteMod> modClass)
    {
        this.checkState(EnumeratorState.FINALISED, "getContainer");
        return this.getContainerForMod(modClass);
    }

    /**
     * @param modClass
     */
    private LoadableMod<?> getContainerForMod(Class<? extends LiteMod> modClass)
    {
        for (ModInfo<LoadableMod<?>> mod : this.modsToLoad)
        {
            if (modClass.equals(mod.getModClass()))
            {
                return mod.getContainer();
            }
        }

        return LoadableMod.NONE;
    }

    /**
     * Get the mod identifier (metadata key), this is used for versioning,
     * exclusivity, and enablement checks.
     * 
     * @param modClass
     */
    @Override
    public String getIdentifier(Class<? extends LiteMod> modClass)
    {
        String modClassName = modClass.getSimpleName();

        for (ModInfo<LoadableMod<?>> mod : this.modsToLoad)
        {
            if (modClassName.equals(mod.getModClassSimpleName()))
            {
                return mod.getIdentifier();
            }
        }

        return LiteLoaderEnumerator.getModClassName(modClass);
    }

    @Override
    public void onPreInit()
    {
        this.discoverContainers();
        this.injectDiscoveredTweaks();
    }

    /**
     * Call enumerator modules in order to find mod containers
     */
    private void discoverContainers()
    {
        this.gotoState(EnumeratorState.DISCOVER);

        for (EnumeratorModule module : this.modules)
        {
            try
            {
                module.enumerate(this, this.environment.getProfile());
            }
            catch (Throwable th)
            {
                LiteLoaderLogger.warning(th, "Enumerator Module %s encountered an error whilst enumerating", module.getClass().getName());
            }
        }

        this.checkDependencies();
    }

    private void injectDiscoveredTweaks()
    {
        this.gotoState(EnumeratorState.INJECT);

        for (TweakContainer<File> tweakContainer : this.containers.getTweakContainers())
        {
            this.addTweaksFrom(tweakContainer);
        }
    }

    /**
     * Enumerate class path and discovered mod files to find mod classes
     */
    @Override
    public void onInit()
    {
        try
        {
            this.gotoState(EnumeratorState.INJECT);
            this.injectIntoClassLoader();

            this.gotoState(EnumeratorState.REGISTER);
            this.registerMods();

            this.gotoState(EnumeratorState.FINALISED);
            LiteLoaderLogger.info("Mod class discovery completed");
        }
        catch (IllegalStateException ex) // wut?
        {
            Throwables.propagate(ex);
        }
        catch (Throwable th)
        {
            LiteLoaderLogger.warning(th, "Mod class discovery failed");
        }
    }

    /**
     * 
     */
    private void injectIntoClassLoader()
    {
        for (EnumeratorModule module : this.modules)
        {
            try
            {
                module.injectIntoClassLoader(this, this.classLoader);
            }
            catch (Throwable th)
            {
                LiteLoaderLogger.warning(th, "Enumerator Module %s encountered an error whilst injecting", module.getClass().getName());
            }
        }
    }

    /**
     * 
     */
    private void registerMods()
    {
        for (EnumeratorModule module : this.modules)
        {
            try
            {
                module.registerMods(this, this.classLoader);
            }
            catch (Throwable th)
            {
                LiteLoaderLogger.warning(th, "Enumerator Module %s encountered an error whilst registering mods", module.getClass().getName());
            }
        }
    }

    /* (non-Javadoc)
     * @see com.mumfrey.liteloader.interfaces.ModularEnumerator
     *      #registerModContainer(com.mumfrey.liteloader.interfaces.LoadableMod)
     */
    @Override
    public final boolean registerModContainer(LoadableMod<?> container)
    {
        this.checkState(EnumeratorState.DISCOVER, "registerModContainer");

        if (container == null)
        {
            return true;
        }

        if (!this.checkEnabled(container))
        {
            this.registerDisabledContainer(container, DisabledReason.USER_DISABLED);
            return false;
        }

        if (!this.checkAPIRequirements(container))
        {
            this.registerDisabledContainer(container, DisabledReason.MISSING_API);
            return false;
        }

        this.registerEnabledContainer(container);
        return true;
    }

    @Override
    public void registerBadContainer(Loadable<?> container, String reason)
    {
        this.checkState(EnumeratorState.DISCOVER, "registerBadContainer");
        this.containers.registerBadContainer(container, reason);
    }

    /**
     * @param container
     */
    protected void registerEnabledContainer(LoadableMod<?> container)
    {
        this.checkState(EnumeratorState.DISCOVER, "registerEnabledContainer");
        this.containers.registerEnabledContainer(container);
        this.observers.all().onRegisterEnabledContainer(this, container);
    }

    /**
     * @param container
     */
    protected void registerDisabledContainer(LoadableMod<?> container, DisabledReason reason)
    {
        this.checkState(EnumeratorState.DISCOVER, "registerDisabledContainer");

        LiteLoaderLogger.info(Verbosity.REDUCED, reason.getMessage(container));
        this.containers.registerDisabledContainer(container, reason);
        this.observers.all().onRegisterDisabledContainer(this, container, reason);
    }

    /* (non-Javadoc)
     * @see com.mumfrey.liteloader.core.PluggableEnumerator#addTweaksFrom(
     *      com.mumfrey.liteloader.core.TweakContainer)
     */
    @Override
    public boolean registerTweakContainer(TweakContainer<File> container)
    {
        this.checkState(EnumeratorState.DISCOVER, "registerTweakContainer");

        if (!container.isEnabled(this.environment))
        {
            LiteLoaderLogger.info(Verbosity.REDUCED, "Mod %s is disabled for profile %s, not injecting tranformers",
                    container.getIdentifier(), this.environment.getProfile());
            return false;
        }

        this.containers.registerTweakContainer(container);
        this.observers.all().onRegisterTweakContainer(this, container);
        return true;
    }

    /**
     * @param tweakContainer
     */
    private void addTweaksFrom(TweakContainer<File> tweakContainer)
    {
        this.checkState(EnumeratorState.INJECT, "addTweaksFrom");

        if (this.checkDependencies(tweakContainer))
        {
            if (tweakContainer.hasTweakClass())
            {
                this.addTweakFrom(tweakContainer);
            }

            if (tweakContainer.hasClassTransformers())
            {
                this.addClassTransformersFrom(tweakContainer);
            }
            
            if (tweakContainer.hasMixins())
            {
                this.addMixinsFrom(tweakContainer);
            }
        }
    }

    private void addTweakFrom(TweakContainer<File> container)
    {
        try
        {
            String tweakClass = container.getTweakClassName();
            int tweakPriority = container.getTweakPriority();
            LiteLoaderLogger.info(Verbosity.REDUCED, "Mod file '%s' provides tweakClass '%s', adding to Launch queue with priority %d",
                    container.getName(), tweakClass, tweakPriority);
            if (this.environment.addCascadedTweaker(tweakClass, tweakPriority))
            {
                LiteLoaderLogger.info(Verbosity.REDUCED, "tweakClass '%s' was successfully added", tweakClass);
                if (container.isExternalJar())
                {
                    this.containers.registerInjectedTweak(container);
                }

                this.injectTweakContainer(container);
            }
        }
        catch (MalformedURLException ex)
        {
        }
    }

    private void addClassTransformersFrom(TweakContainer<File> container)
    {
        try
        {
            for (String classTransformerClass : container.getClassTransformerClassNames())
            {
                LiteLoaderLogger.info(Verbosity.REDUCED, "Mod file '%s' provides classTransformer '%s', adding to class loader",
                        container.getName(), classTransformerClass);
                ClassTransformerManager transformerManager = this.environment.getTransformerManager();
                if (transformerManager != null && transformerManager.injectTransformer(classTransformerClass))
                {
                    LiteLoaderLogger.info(Verbosity.REDUCED, "classTransformer '%s' was successfully added", classTransformerClass);
                    this.injectContainerRecursive(container);
                }
            }
        }
        catch (MalformedURLException ex)
        {
        }
    }

    @SuppressWarnings("deprecation")
    private void addMixinsFrom(MixinContainer<File> container)
    {
        try
        {
            if (container.hasMixins())
            {
                this.injectContainerRecursive(container);
            }
            
            for (String config : container.getMixinConfigs())
            {
                if (config.endsWith(".json"))
                {
                    LiteLoaderLogger.info(Verbosity.REDUCED, "Registering mixin config %s for %s", config, container.getName());
                    Mixins.addConfiguration(config);
                }
                else if (config.contains(".json@"))
                {
                    int pos = config.indexOf(".json@");
                    String phaseName = config.substring(pos + 6);
                    config = config.substring(0, pos + 5);
                    Phase phase = Phase.forName(phaseName);
                    if (phase != null)
                    {
                        LiteLoaderLogger.info(Verbosity.REDUCED, "Registering mixin config %s for %s", config, container.getName());
                        LiteLoaderLogger.warning("%s specifies mixin environment in metadata which is deprecated, use config instead",
                                container.getName());
                        MixinEnvironment.getEnvironment(phase).addConfiguration(config);
                    }
                }
            }
        }
        catch (MalformedURLException ex)
        {
        }
    }
    
    protected void injectTweakContainer(TweakContainer<File> container) throws MalformedURLException
    {
        if (!container.injectIntoClassPath(this.classLoader, true))
        {
            return;
        }
        
        String[] classPathEntries = container.getClassPathEntries();
        if (classPathEntries != null)
        {
            for (String classPathEntry : classPathEntries)
            {
                try
                {
                    File classPathJar = new File(this.environment.getGameDirectory(), classPathEntry);
                    URL classPathJarUrl = classPathJar.toURI().toURL();

                    LiteLoaderLogger.info("Adding Class-Path entry: %s", classPathEntry); 
                    LiteLoaderTweaker.addURLToParentClassLoader(classPathJarUrl);
                    this.classLoader.addURL(classPathJarUrl);
                }
                catch (MalformedURLException ex) {}
            }
        }
    }

    /**
     * @param container
     */
    private void injectContainerRecursive(Injectable container) throws MalformedURLException
    {
        if (container.injectIntoClassPath(this.classLoader, true) && container instanceof LoadableMod)
        {
            LoadableMod<?> file = (LoadableMod<?>)container;
            for (String dependency : file.getDependencies())
            {
                LoadableMod<?> dependencyContainer = this.containers.getEnabledContainer(dependency);
                if (dependencyContainer != null)
                {
                    this.injectContainerRecursive(dependencyContainer);
                }
            }
        }
    }

    /* (non-Javadoc)
     * @see com.mumfrey.liteloader.core.PluggableEnumerator#registerMods(
     *      com.mumfrey.liteloader.core.LoadableMod, boolean)
     */
    @Override
    public void registerModsFrom(LoadableMod<?> container, boolean registerContainer)
    {
        this.checkState(EnumeratorState.REGISTER, "registerModsFrom");

        if (this.containers.isDisabledContainer(container))
        {
            throw new IllegalArgumentException("Attempted to register mods from a disabled container '" + container.getName() + "'");
        }

        if (this.enumeratedContainers.contains(container))
        {
            // already handled this container
            return;
        }

        this.enumeratedContainers.add(container);

        List<Class<? extends LiteMod>> modClasses = new ArrayList<Class<? extends LiteMod>>();

        for (EnumeratorPlugin plugin : this.plugins)
        {
            List<Class<? extends LiteMod>> classes = plugin.getClasses(container, this.classLoader, this.validator);
            LiteLoaderLogger.debug("Plugin %s returned %d classes for %s", plugin.getClass(), classes.size(), container.getDisplayName());
            modClasses.addAll(classes);
        }

        for (Class<? extends LiteMod> modClass : modClasses)
        {
            Mod mod = new Mod(container, modClass);
            this.registerMod(mod);
        }

        if (modClasses.size() > 0)
        {
            LiteLoaderLogger.info("Found %d potential matches", modClasses.size());
            this.containers.registerEnabledContainer(container);
        }
    }

    /* (non-Javadoc)
     * @see com.mumfrey.liteloader.interfaces.ModularEnumerator#registerMod(
     *      com.mumfrey.liteloader.interfaces.ModInfo)
     */
    @Override
    public void registerMod(ModInfo<LoadableMod<?>> mod)
    {
        this.checkState(EnumeratorState.REGISTER, "registerMod");

        if (this.modsToLoad.contains(mod))
        {
            LiteLoaderLogger.warning("Mod name collision for mod with class '%s', maybe you have more than one copy?", mod.getModClassSimpleName());
        }

        this.modsToLoad.add(mod);

        this.observers.all().onModAdded(this, mod);
    }

    private boolean checkEnabled(LoadableMod<?> container)
    {
        for (EnumeratorPlugin plugin : this.plugins)
        {
            if (!plugin.checkEnabled(this.containers, container)) return false;
        }

        return true;
    }

    @Override
    public boolean checkAPIRequirements(LoadableMod<?> container)
    {
        for (EnumeratorPlugin plugin : this.plugins)
        {
            if (!plugin.checkAPIRequirements(this.containers, container)) return false;
        }

        return true;
    }

    /**
     * Check dependencies of enabled containers
     */
    private void checkDependencies()
    {
        Collection<? extends LoadableMod<?>> enabledContainers = this.containers.getEnabledContainers();
        Deque<LoadableMod<?>> containers = new LinkedList<LoadableMod<?>>(enabledContainers);

        while (containers.size() > 0)
        {
            LoadableMod<?> container = containers.pop();
            if (!this.checkDependencies(container))
            {
                this.registerDisabledContainer(container, DisabledReason.MISSING_DEPENDENCY);

                // Iterate so that a container disabled by a failed dependency check will also
                // disable any containers which depend upon it
                containers.clear();
                containers.addAll(enabledContainers);
            }
        }
    }

    @Override
    public boolean checkDependencies(LoadableMod<?> container)
    {
        for (EnumeratorPlugin plugin : this.plugins)
        {
            if (!plugin.checkDependencies(this.containers, container)) return false;
        }

        return true;
    }

    @SuppressWarnings("unchecked")
    public boolean checkDependencies(TweakContainer<File> tweakContainer)
    {
        if (tweakContainer instanceof LoadableMod)
        {
            return this.checkDependencies((LoadableMod<File>)tweakContainer);
        }

        return true;
    }

    public static String getModClassName(LiteMod mod)
    {
        return LiteLoaderEnumerator.getModClassName(mod.getClass());
    }

    public static String getModClassName(Class<? extends LiteMod> mod)
    {
        return mod.getSimpleName().substring(7);
    }
}