More logging and maybe fix default package mods

This commit is contained in:
Christian Weeks 2012-03-31 00:59:43 -04:00
parent 8091d8bdaf
commit e27f1c3a70
1 changed files with 29 additions and 31 deletions

View File

@ -44,7 +44,7 @@ public class Loader {
private static Logger log = Logger.getLogger("ForgeModLoader"); private static Logger log = Logger.getLogger("ForgeModLoader");
private static Pattern zipJar = Pattern.compile("([^\\s]+).(zip|jar)$"); private static Pattern zipJar = Pattern.compile("([^\\s]+).(zip|jar)$");
private static Pattern modClass = Pattern.compile("(.*/)(mod\\_[^\\s]+).class$"); private static Pattern modClass = Pattern.compile("(.*/?)(mod\\_[^\\s]+).class$");
private static int major=Integer.parseInt("@MAJOR@"); private static int major=Integer.parseInt("@MAJOR@");
private static int minor=Integer.parseInt("@MINOR@"); private static int minor=Integer.parseInt("@MINOR@");
@ -76,7 +76,7 @@ public class Loader {
} catch (Exception e) { } catch (Exception e) {
// Whatever - give up // Whatever - give up
} }
log.info(String.format("Forge Mod Loader version %d.%d.%d.%d for Minecraft %s loading.",major,minor,rev,build,mcversion)); log.info(String.format("Forge Mod Loader version %d.%d.%d.%d for Minecraft %s loading",major,minor,rev,build,mcversion));
} }
private void sortModList() { private void sortModList() {
@ -85,37 +85,37 @@ public class Loader {
private void preModInit() { private void preModInit() {
state = State.PREINIT; state = State.PREINIT;
log.fine("Beginning mod pre-initialization."); log.fine("Beginning mod pre-initialization");
for (ModContainer mod : mods) { for (ModContainer mod : mods) {
if (mod.wantsPreInit()) { if (mod.wantsPreInit()) {
log.finer(String.format("Pre-initializing %s.", mod.getSource())); log.finer(String.format("Pre-initializing %s", mod.getSource()));
mod.preInit(); mod.preInit();
namedMods.put(mod.getName(), mod); namedMods.put(mod.getName(), mod);
} }
} }
log.fine("Mod pre-initialization complete."); log.fine("Mod pre-initialization complete");
} }
private void modInit() { private void modInit() {
state = State.INIT; state = State.INIT;
log.fine("Beginning mod initialization."); log.fine("Beginning mod initialization");
for (ModContainer mod : mods) { for (ModContainer mod : mods) {
log.finer(String.format("Initializing %s.", mod.getName())); log.finer(String.format("Initializing %s", mod.getName()));
mod.init(); mod.init();
} }
log.fine("Mod initialization complete."); log.fine("Mod initialization complete");
} }
private void postModInit() { private void postModInit() {
state = State.POSTINIT; state = State.POSTINIT;
log.fine("Beginning mod post-initialization."); log.fine("Beginning mod post-initialization");
for (ModContainer mod : mods) { for (ModContainer mod : mods) {
if (mod.wantsPostInit()) { if (mod.wantsPostInit()) {
log.finer(String.format("Post-initializing %s.", mod.getName())); log.finer(String.format("Post-initializing %s", mod.getName()));
mod.postInit(); mod.postInit();
} }
} }
log.fine("Mod post-initialization complete."); log.fine("Mod post-initialization complete");
} }
private void load() { private void load() {
@ -124,12 +124,12 @@ public class Loader {
try { try {
canonicalModsPath = modsDir.getCanonicalPath(); canonicalModsPath = modsDir.getCanonicalPath();
} catch (IOException ioe) { } catch (IOException ioe) {
log.severe(String.format("Failed to resolve mods directory mods %s.", modsDir.getAbsolutePath())); log.severe(String.format("Failed to resolve mods directory mods %s", modsDir.getAbsolutePath()));
log.throwing("fml.server.Loader", "initialize", ioe); log.throwing("fml.server.Loader", "initialize", ioe);
throw new LoaderException(ioe); throw new LoaderException(ioe);
} }
if (!modsDir.exists()) { if (!modsDir.exists()) {
log.fine(String.format("No mod directory found, creating one: %s.", canonicalModsPath)); log.fine(String.format("No mod directory found, creating one: %s", canonicalModsPath));
try { try {
modsDir.mkdir(); modsDir.mkdir();
} catch (Exception e) { } catch (Exception e) {
@ -138,12 +138,12 @@ public class Loader {
} }
} }
if (!modsDir.isDirectory()) { if (!modsDir.isDirectory()) {
log.severe(String.format("Attempting to load mods from %s, which is not a directory.", canonicalModsPath)); log.severe(String.format("Attempting to load mods from %s, which is not a directory", canonicalModsPath));
LoaderException loaderException = new LoaderException(); LoaderException loaderException = new LoaderException();
log.throwing("fml.server.Loader", "initialize", loaderException); log.throwing("fml.server.Loader", "initialize", loaderException);
throw loaderException; throw loaderException;
} }
log.info(String.format("Loading mods from %s.",canonicalModsPath)); log.info(String.format("Loading mods from %s",canonicalModsPath));
File[] modList = modsDir.listFiles(); File[] modList = modsDir.listFiles();
// Sort the files into alphabetical order first // Sort the files into alphabetical order first
Arrays.sort(modList); Arrays.sort(modList);
@ -151,31 +151,31 @@ public class Loader {
state = State.LOADING; state = State.LOADING;
for (File modFile : modList) { for (File modFile : modList) {
if (modFile.isDirectory()) { if (modFile.isDirectory()) {
log.info(String.format("Found directory %s. Attempting load.", modFile.getName())); log.fine(String.format("Found a directory %s, attempting to load it", modFile.getName()));
boolean modFound=attemptDirLoad(modFile); boolean modFound=attemptDirLoad(modFile);
if (modFound) { if (modFound) {
log.info(String.format("Directory %s loaded successfully.", modFile.getName())); log.fine(String.format("Directory %s loaded successfully", modFile.getName()));
} else { } else {
log.info(String.format("Directory %s contained no mods.", modFile.getName())); log.info(String.format("Directory %s contained no mods", modFile.getName()));
} }
} else { } else {
Matcher matcher = zipJar.matcher(modFile.getName()); Matcher matcher = zipJar.matcher(modFile.getName());
if (matcher.matches()) { if (matcher.matches()) {
log.info(String.format("Found zip or jar file %s. Attempting load.", matcher.group(0))); log.fine(String.format("Found a zip or jar file %s, attempting to load it", matcher.group(0)));
boolean modFound=attemptFileLoad(modFile); boolean modFound=attemptFileLoad(modFile);
if (modFound) { if (modFound) {
log.info(String.format("File %s loaded successfully.", matcher.group(0))); log.fine(String.format("File %s loaded successfully", matcher.group(0)));
} else { } else {
log.info(String.format("File %s contained no mods.", matcher.group(0))); log.info(String.format("File %s contained no mods", matcher.group(0)));
} }
} }
} }
} }
if (state == State.ERRORED) { if (state == State.ERRORED) {
log.severe("A problem has occured during mod loading. Giving up now."); log.severe("A problem has occured during mod loading, giving up now");
throw new RuntimeException("Giving up please"); throw new RuntimeException("Giving up please");
} }
log.fine(String.format("Forge Mod Loader has loaded %d mods.",mods.size())); log.info(String.format("Forge Mod Loader has loaded %d mods",mods.size()));
} }
private boolean attemptDirLoad(File modDir) { private boolean attemptDirLoad(File modDir) {
@ -190,7 +190,7 @@ public class Loader {
}); });
for (File modClassFile : content) { for (File modClassFile : content) {
String clazzName = modClass.matcher(modClassFile.getName()).group(2); String clazzName = modClass.matcher(modClassFile.getName()).group(2);
log.fine(String.format("Found a mod class %s in directory %s. Attempting to load it", clazzName, modDir.getName())); log.fine(String.format("Found a mod class %s in directory %s, attempting to load it", clazzName, modDir.getName()));
loadModClass(modDir, modClassFile.getName(), clazzName); loadModClass(modDir, modClassFile.getName(), clazzName);
log.fine(String.format("Successfully loaded mod class %s", modClassFile.getName())); log.fine(String.format("Successfully loaded mod class %s", modClassFile.getName()));
foundAModClass=true; foundAModClass=true;
@ -201,24 +201,22 @@ public class Loader {
private void loadModClass(File classSource, String classFileName, String clazzName) { private void loadModClass(File classSource, String classFileName, String clazzName) {
try { try {
System.out.printf("Loading %s\n", clazzName);
Class<?> clazz = Class.forName(clazzName, false, modClassLoader); Class<?> clazz = Class.forName(clazzName, false, modClassLoader);
System.out.printf("Got %s\n", clazz);
if (clazz.isAnnotationPresent(Mod.class)) { if (clazz.isAnnotationPresent(Mod.class)) {
// an FML mod // an FML mod
mods.add(FMLModContainer.buildFor(clazz)); mods.add(FMLModContainer.buildFor(clazz));
} else if (BaseMod.class.isAssignableFrom(clazz)) { } else if (BaseMod.class.isAssignableFrom(clazz)) {
log.fine(String.format("ModLoader BaseMod class found: %s. Loading", clazzName)); log.fine(String.format("ModLoader BaseMod class %s found, loading", clazzName));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Class<? extends BaseMod> bmClazz = (Class<? extends BaseMod>) clazz; Class<? extends BaseMod> bmClazz = (Class<? extends BaseMod>) clazz;
ModContainer mc=new ModLoaderModContainer(bmClazz,classSource.getCanonicalPath()); ModContainer mc=new ModLoaderModContainer(bmClazz,classSource.getCanonicalPath());
mods.add(mc); mods.add(mc);
log.fine(String.format("ModLoader BaseMod class loaded: %s.", clazzName)); log.fine(String.format("ModLoader BaseMod class %s loaded", clazzName));
} else { } else {
// Unrecognized // Unrecognized
} }
} catch (Exception e) { } catch (Exception e) {
log.warning(String.format("Failed to load mod class %s in %s", classFileName, classSource.getName())); log.warning(String.format("Failed to load mod class %s in %s", classFileName, classSource.getAbsoluteFile()));
log.throwing("fml.server.Loader", "attemptLoad", e); log.throwing("fml.server.Loader", "attemptLoad", e);
state = State.ERRORED; state = State.ERRORED;
} }
@ -246,7 +244,7 @@ public class Loader {
if (match.matches()) { if (match.matches()) {
String pkg = match.group(1).replace('/', '.'); String pkg = match.group(1).replace('/', '.');
String clazzName = pkg + match.group(2); String clazzName = pkg + match.group(2);
log.fine(String.format("Found a mod class %s in file %s. Attempting to load it", clazzName, modFile.getName())); log.fine(String.format("Found a mod class %s in file %s, attempting to load it", clazzName, modFile.getName()));
loadModClass(modFile, ze.getName(), clazzName); loadModClass(modFile, ze.getName(), clazzName);
log.fine(String.format("Mod class %s loaded successfully", clazzName, modFile.getName())); log.fine(String.format("Mod class %s loaded successfully", clazzName, modFile.getName()));
foundAModClass=true; foundAModClass=true;
@ -278,7 +276,7 @@ public class Loader {
modInit(); modInit();
postModInit(); postModInit();
state = State.UP; state = State.UP;
log.info(String.format("Forge Mod Loader load complete. %d mods loaded.",mods.size())); log.info(String.format("Forge Mod Loader load complete, %d mods loaded",mods.size()));
} }
public static boolean isModLoaded(String modname) { public static boolean isModLoaded(String modname) {