LiteLoaderEnumerator.java 25 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 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
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.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 net.minecraft.launchwrapper.Launch;
import net.minecraft.launchwrapper.LaunchClassLoader;

import com.google.common.base.Throwables;
import com.mumfrey.liteloader.LiteMod;
import com.mumfrey.liteloader.api.EnumeratorModule;
import com.mumfrey.liteloader.api.LiteAPI;
import com.mumfrey.liteloader.api.manager.APIProvider;
import com.mumfrey.liteloader.core.exceptions.OutdatedLoaderException;
import com.mumfrey.liteloader.interfaces.LoaderEnumerator;
import com.mumfrey.liteloader.interfaces.Loadable;
import com.mumfrey.liteloader.interfaces.LoadableMod;
import com.mumfrey.liteloader.interfaces.TweakContainer;
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;

/**
 * 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;
	
	private final LiteLoaderTweaker tweaker;

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

	/**
	 * Classes to load, mapped by class name 
	 */
	private final Set<ModInfo<LoadableMod<?>>> modsToLoad = new LinkedHashSet<ModInfo<LoadableMod<?>>>();
	
	/**
	 * Mod containers which are disabled 
	 */
	private final Map<String, ModInfo<Loadable<?>>> disabledContainers = new HashMap<String, ModInfo<Loadable<?>>>();

	/**
	 * Mapping of identifiers to mod containers 
	 */
	private final Map<String, LoadableMod<?>> enabledContainers = new HashMap<String, LoadableMod<?>>();
	
	/**
	 * Containers which have already been checked for potential mod candidates 
	 */
	private final Set<LoadableMod<?>> enumeratedContainers = new HashSet<LoadableMod<?>>();
	
	/**
	 * Tweaks to inject 
	 */
	private final List<TweakContainer<File>> tweakContainers = new ArrayList<TweakContainer<File>>();
	
	/**
	 * Other tweak-containing jars which we have injected 
	 */
	private final List<ModInfo<Loadable<?>>> injectedTweaks = new ArrayList<ModInfo<Loadable<?>>>();
	
	/**
	 * 
	 */
	private final List<EnumeratorModule> modules = new ArrayList<EnumeratorModule>();

	private final String[] supportedPrefixes;
	
	protected EnumeratorState state = EnumeratorState.INIT;
	
	/**
	 * @param properties
	 * @param classLoader
	 * @param loadTweaks
	 * @param gameFolder
	 */
	public LiteLoaderEnumerator(LoaderEnvironment environment, LoaderProperties properties, LaunchClassLoader classLoader)
	{
		this.environment       = environment;
		this.properties        = properties;
		this.tweaker           = (LiteLoaderTweaker)environment.getTweaker();
		this.classLoader       = classLoader;
		this.supportedPrefixes = this.getSupportedPrefixes(environment);
		
		// Initialise the shared mod list if we haven't already
		this.getSharedModList();
	}

	/**
	 * @param environment
	 * @return
	 */
	private String[] getSupportedPrefixes(LoaderEnvironment environment)
	{
		List<String> prefixes = new ArrayList<String>();

		for (LiteAPI api : environment.getAPIProvider().getAPIs())
		{
			List<EnumeratorModule> apiModules = api.getEnumeratorModules();
			
			if (apiModules != null)
			{
				for (EnumeratorModule module : apiModules)
				{
					this.registerModule(module);
				}			
			}
			
			String prefix = api.getModClassPrefix();
			if (prefix != null)
			{
				LiteLoaderLogger.info("Adding supported mod class prefix '%s'", prefix);
				prefixes.add(prefix);
			}
		}
		
		return prefixes.toArray(new String[prefixes.size()]);
	}
	
	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;
		}
	}

	/**
	 * @return
	 */
	public LoaderEnvironment getEnvironment()
	{
		return this.environment;
	}

	/**
	 * Initialise the "shared" mod list if it's not already been created
	 * @return 
	 */
	@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);
		}
	}
	
	/**
	 * 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);
	}
	
	/**
	 * @return
	 */
	@Override
	public Collection<? extends ModInfo<Loadable<?>>> getDisabledContainers()
	{
		this.checkState(EnumeratorState.FINALISED, "getDisabledContainers");
		
		return this.disabledContainers.values();
	}
	
	/**
	 * Get the list of injected tweak containers
	 */
	@Override
	public List<? extends ModInfo<Loadable<?>>> getInjectedTweaks()
	{
		this.checkState(EnumeratorState.FINALISED, "getInjectedTweaks");
		
		return this.injectedTweaks;
	}

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

	/**
	 * Get a metadata value for the specified mod
	 * 
	 * @param modClassName
	 * @param metaDataKey
	 * @param defaultValue
	 * @return
	 */
	@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 mod
	 * @return
	 */
	@Override
	public LoadableMod<?> getContainer(String identifier)
	{
		this.checkState(EnumeratorState.FINALISED, "getContainer");
		
		return this.getContainerById(identifier);
	}

	/**
	 * @param identifier
	 * @return
	 */
	private LoadableMod<?> getContainerById(String identifier)
	{
		LoadableMod<?> container = this.enabledContainers.get(identifier);
		return container != null ? container : LoadableMod.NONE;
	}
	
	/**
	 * @param mod
	 * @return
	 */
	@Override
	public LoadableMod<?> getContainer(Class<? extends LiteMod> modClass)
	{
		this.checkState(EnumeratorState.FINALISED, "getContainer");
		
		return this.getContainerForMod(modClass);
	}

	/**
	 * @param modClass
	 * @return
	 */
	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
	 * @return
	 */
	@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());
			}
		}
	}
	
	private void injectDiscoveredTweaks()
	{
		this.gotoState(EnumeratorState.INJECT);
		
		for (TweakContainer<File> tweakContainer : this.tweakContainers)
		{
			this.addTweaksFrom(tweakContainer);
		}
	}
	
	/**
	 * Enumerate class path and discovered mod files to find mod classes
	 */
	@Override
	public void onInit()
	{
		try
		{
			this.gotoState(EnumeratorState.INJECT);
			
			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());
				}
			}

			this.gotoState(EnumeratorState.REGISTER);
			
			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());
				}
			}

			LiteLoaderLogger.info("Mod class discovery completed");

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

	/* (non-Javadoc)
	 * @see com.mumfrey.liteloader.interfaces.PluggableEnumerator#registerModContainer(com.mumfrey.liteloader.interfaces.LoadableMod)
	 */
	@Override
	public final boolean registerModContainer(LoadableMod<?> container)
	{
		this.checkState(EnumeratorState.DISCOVER, "registerModContainer");
		
		if (container != null)
		{
			if (!container.isEnabled(this.environment))
			{
				LiteLoaderLogger.info("Container %s is disabled", container.getLocation());
				this.registerDisabledContainer(container);
				return false;
			}
			
			if (!this.checkDependencies(container))
			{
				LiteLoaderLogger.info("Container %s is missing one or more dependencies", container.getLocation());
				this.registerDisabledContainer(container);
				return false;
			}
				
			if (!this.checkAPIRequirements(container))
			{
				LiteLoaderLogger.info("Container %s is missing one or more required APIs", container.getLocation());
				this.registerDisabledContainer(container);
				return false;
			}

			this.registerEnabledContainer(container);
		}

		return true;
	}

	/**
	 * @param container
	 */
	protected void registerEnabledContainer(LoadableMod<?> container)
	{
		this.checkState(EnumeratorState.DISCOVER, "registerEnabledContainer");
		
		this.disabledContainers.remove(container.getIdentifier());
		this.enabledContainers.put(container.getIdentifier(), container);
	}

	/**
	 * @param container
	 */
	protected void registerDisabledContainer(LoadableMod<?> container)
	{
		this.checkState(EnumeratorState.DISCOVER, "registerDisabledContainer");
		
		this.enabledContainers.remove(container.getIdentifier());
		this.disabledContainers.put(container.getIdentifier(), new NonMod(container, false));
	}
	
	/* (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("Mod %s is disabled for profile %s, not injecting tranformers", container.getIdentifier(), this.environment.getProfile());
			return false;
		}

		this.tweakContainers.add(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, tweakContainer.getClassTransformerClassNames());
			}
		}
	}

	private void addTweakFrom(TweakContainer<File> container)
	{
		try
		{
			String tweakClass = container.getTweakClassName();
			int tweakPriority = container.getTweakPriority();
			LiteLoaderLogger.info("Mod file '%s' provides tweakClass '%s', adding to Launch queue with priority %d", container.getName(), tweakClass, tweakPriority);
			if (this.tweaker.addCascadedTweaker(tweakClass, tweakPriority))
			{
				LiteLoaderLogger.info("tweakClass '%s' was successfully added", tweakClass);
				container.injectIntoClassPath(this.classLoader, true);
				
				if (container.isExternalJar())
				{
					this.injectedTweaks.add(new NonMod(container, true));
				}
				
				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) {}
					}
				}
			}
		}
		catch (MalformedURLException ex)
		{
		}
	}

	private void addClassTransformersFrom(TweakContainer<File> container, List<String> classTransformerClasses)
	{
		try
		{
			for (String classTransformerClass : classTransformerClasses)
			{
				LiteLoaderLogger.info("Mod file '%s' provides classTransformer '%s', adding to class loader", container.getName(), classTransformerClass);
				if (this.tweaker.getTransformerManager().injectTransformer(classTransformerClass))
				{
					LiteLoaderLogger.info("classTransformer '%s' was successfully added", classTransformerClass);
					container.injectIntoClassPath(this.classLoader, true);
				}
			}
		}
		catch (MalformedURLException ex)
		{
		}
	}

	/* (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.disabledContainers.containsValue(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);
		
		LinkedList<Class<? extends LiteMod>> modClasses = LiteLoaderEnumerator.<LiteMod>getSubclassesFor(container, this.classLoader, LiteMod.class, this.supportedPrefixes);
		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.disabledContainers.remove(container.getIdentifier());
			this.enabledContainers.put(container.getIdentifier(), 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);
	}

	/**
	 * Enumerate classes on the classpath which are subclasses of the specified
	 * class
	 * 
	 * @param superClass
	 * @return
	 */
	private static <T> LinkedList<Class<? extends T>> getSubclassesFor(LoadableMod<?> container, ClassLoader classloader, Class<T> superClass, String[] supportedPrefixes)
	{
		LinkedList<Class<? extends T>> classes = new LinkedList<Class<? extends T>>();
		
		if (container != null)
		{
			try
			{
				for (String fullClassName : container.getContainedClassNames())
				{
					boolean isDefaultPackage = fullClassName.lastIndexOf('.') == -1;
					String className = isDefaultPackage ? fullClassName : fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
					if (supportedPrefixes == null || supportedPrefixes.length == 0 || LiteLoaderEnumerator.startsWithAny(className, supportedPrefixes))
					{
						LiteLoaderEnumerator.<T>checkAndAddClass(classloader, superClass, classes, fullClassName);
					}
				}
			}
			catch (OutdatedLoaderException ex)
			{
				classes.clear();
				LiteLoaderLogger.info("Error searching in '%s', missing API component '%s', your loader is probably out of date", container, ex.getMessage());
			}
			catch (Throwable th)
			{
				LiteLoaderLogger.warning(th, "Enumeration error");
			}
		}
		
		return classes;
	}

	/**
	 * @param classloader
	 * @param superClass
	 * @param classes
	 * @param className
	 * @throws OutdatedLoaderException 
	 */
	private static <T> void checkAndAddClass(ClassLoader classloader, Class<T> superClass, LinkedList<Class<? extends T>> classes, String className) throws OutdatedLoaderException
	{
		if (className.indexOf('$') > -1)
			return;
		
		try
		{
			Class<?> subClass = classloader.loadClass(className);
			
			if (subClass != null && !superClass.equals(subClass) && superClass.isAssignableFrom(subClass) && !subClass.isInterface() && !classes.contains(subClass))
			{
				@SuppressWarnings("unchecked")
				Class<? extends T> matchingClass = (Class<? extends T>)subClass;
				classes.add(matchingClass);
			}
		}
		catch (Throwable th)
		{
			if (th.getCause() != null)
			{
				String missingClassName = th.getCause().getMessage();
				if (th.getCause() instanceof NoClassDefFoundError && missingClassName != null)
				{
					if (missingClassName.startsWith("com/mumfrey/liteloader/"))
					{
						throw new OutdatedLoaderException(missingClassName.substring(missingClassName.lastIndexOf('/') + 1));
					}
					
//					String mahClassName = LiteLoaderEnumerator.getMissingAPIHandlerClass(classloader, className);
//					if (mahClassName != null)
//					{
//						LiteLoaderEnumerator.checkAndAddClass(classloader, superClass, classes, mahClassName);
//						return;
//					}
				}
			}
			
			LiteLoaderLogger.warning(th, "checkAndAddClass error while checking '%s'", className);
		}
	}

//	private static String getMissingAPIHandlerClass(ClassLoader classloader, String className)
//	{
//		String mahTypeDescriptor = Type.getDescriptor(MissingAPIHandlerClass.class);
//		
//		if (classloader instanceof LaunchClassLoader)
//		{
//			try
//			{
//				byte[] basicClass = ((LaunchClassLoader)classloader).getClassBytes(className);
//				ClassNode classNode = LiteLoaderEnumerator.readClass(basicClass);
//				
//				if (classNode.invisibleAnnotations != null)
//				{
//					for (AnnotationNode annotation : classNode.invisibleAnnotations)
//					{
//						if (mahTypeDescriptor.equals(annotation.desc))
//						{
//							return LiteLoaderEnumerator.<String>getAnnotationValue(annotation, "value");
//						}
//					}
//				}
//			}
//			catch (IOException ex)
//			{
//				ex.printStackTrace();
//			}
//		}
//		
//		return null;
//	}
//
//	/**
//	 * @param basicClass
//	 * @return
//	 */
//	private static ClassNode readClass(byte[] basicClass)
//	{
//		ClassReader classReader = new ClassReader(basicClass);
//		ClassNode classNode = new ClassNode();
//		classReader.accept(classNode, ClassReader.EXPAND_FRAMES);
//		return classNode;
//	}
//
//	/**
//	 * @param annotation
//	 * @param key
//	 * @return
//	 */
//	@SuppressWarnings("unchecked")
//	private static <T> T getAnnotationValue(AnnotationNode annotation, String key)
//	{
//		boolean getNextValue = false;
//		for (Object value : annotation.values)
//		{
//			if (getNextValue) return (T)value;
//			if (value.equals(key)) getNextValue = true;
//		}
//		return null;
//	}
	
	@Override
	public boolean checkAPIRequirements(LoadableMod<?> container)
	{
		boolean result = true;
		APIProvider apiProvider = this.environment.getAPIProvider();
		
		for (String identifier : container.getRequiredAPIs())
		{
			if (!apiProvider.isAPIAvailable(identifier))
			{
				container.registerMissingAPI(identifier);
				result = false;
			}
		}
		
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public boolean checkDependencies(TweakContainer<File> tweakContainer)
	{
		if (tweakContainer instanceof LoadableMod)
		{
			return this.checkDependencies((LoadableMod<File>)tweakContainer);
		}
		
		return true;
	}

	@Override
	public boolean checkDependencies(LoadableMod<?> base)
	{
		if (base == null || !base.hasDependencies()) return true;
		
		HashSet<String> circularDependencySet = new HashSet<String>();
		circularDependencySet.add(base.getIdentifier());
		
		boolean result = this.checkDependencies(base, base, circularDependencySet);
		LiteLoaderLogger.info("Dependency check for %s %s", base.getIdentifier(), result ? "passed" : "failed");
		
		return result;
	}

	private boolean checkDependencies(LoadableMod<?> base, LoadableMod<?> container, Set<String> circularDependencySet)
	{
		if (container.getDependencies().size() == 0)
			return true;
		
		boolean result = true;
		
		for (String dependency : container.getDependencies())
		{
			if (!circularDependencySet.contains(dependency))
			{
				circularDependencySet.add(dependency);
				
				LoadableMod<?> dependencyContainer = this.getContainerById(dependency);
				if (dependencyContainer != LoadableMod.NONE)
				{
					if (this.environment.getEnabledModsList().isEnabled(this.environment.getProfile(), dependency))
					{
						result &= this.checkDependencies(base, dependencyContainer, circularDependencySet);
					}
					else
					{
//						LiteLoaderLogger.warning("Dependency %s required by %s is currently disabled", dependency, base.getIdentifier());
						base.registerMissingDependency(dependency);
						result = false;
					}
				}
				else
				{
//					LiteLoaderLogger.info("Dependency %s for %s is was not located, no container ", dependency, base.getIdentifier());
					base.registerMissingDependency(dependency);
					result = false;
				}
			}
		}
		
		return result;
	}

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

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

	private static boolean startsWithAny(String string, String[] candidates)
	{
		for (String candidate : candidates)
			if (string.startsWith(candidate)) return true;
		
		return false;
	}
}