More logging
This commit is contained in:
parent
f37c8ae4d7
commit
44779a56b1
3 changed files with 51 additions and 17 deletions
|
@ -69,6 +69,8 @@ public class Loader {
|
||||||
try {
|
try {
|
||||||
fileHandler = new FileHandler("ForgeModLoader-%g.log", 0, 3);
|
fileHandler = new FileHandler("ForgeModLoader-%g.log", 0, 3);
|
||||||
fileHandler.setLevel(Level.ALL);
|
fileHandler.setLevel(Level.ALL);
|
||||||
|
// We're stealing minecraft's log formatter
|
||||||
|
fileHandler.setFormatter(FMLHandler.getMinecraftLogger().getHandlers()[0].getFormatter());
|
||||||
Loader.log.addHandler(fileHandler);
|
Loader.log.addHandler(fileHandler);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// Whatever - give up
|
// Whatever - give up
|
||||||
|
@ -82,31 +84,37 @@ public class Loader {
|
||||||
|
|
||||||
private void preModInit() {
|
private void preModInit() {
|
||||||
state = State.PREINIT;
|
state = State.PREINIT;
|
||||||
|
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.getName()));
|
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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void modInit() {
|
private void modInit() {
|
||||||
state = State.INIT;
|
state = State.INIT;
|
||||||
|
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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void postModInit() {
|
private void postModInit() {
|
||||||
state = State.POSTINIT;
|
state = State.POSTINIT;
|
||||||
|
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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void load() {
|
private void load() {
|
||||||
|
@ -115,12 +123,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) {
|
||||||
|
@ -129,11 +137,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));
|
||||||
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);
|
||||||
|
@ -141,27 +150,37 @@ 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.info(String.format("Found directory %s. Attempting load.", modFile.getName()));
|
||||||
attemptDirLoad(modFile);
|
boolean modFound=attemptDirLoad(modFile);
|
||||||
log.info(String.format("Directory %s loaded successfully", modFile.getName()));
|
if (modFound) {
|
||||||
|
log.info(String.format("Directory %s loaded successfully.", modFile.getName()));
|
||||||
|
} else {
|
||||||
|
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.info(String.format("Found zip or jar file %s. Attempting load.", matcher.group(0)));
|
||||||
attemptFileLoad(modFile);
|
boolean modFound=attemptFileLoad(modFile);
|
||||||
|
if (modFound) {
|
||||||
log.info(String.format("File %s loaded successfully.", matcher.group(0)));
|
log.info(String.format("File %s loaded successfully.", matcher.group(0)));
|
||||||
|
} else {
|
||||||
|
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()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void attemptDirLoad(File modDir) {
|
private boolean attemptDirLoad(File modDir) {
|
||||||
extendClassLoader(modDir);
|
extendClassLoader(modDir);
|
||||||
|
|
||||||
|
boolean foundAModClass=false;
|
||||||
File[] content = modDir.listFiles(new FilenameFilter() {
|
File[] content = modDir.listFiles(new FilenameFilter() {
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(File dir, String name) {
|
public boolean accept(File dir, String name) {
|
||||||
|
@ -173,7 +192,10 @@ public class Loader {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return foundAModClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadModClass(File classSource, String classFileName, String clazzName) {
|
private void loadModClass(File classSource, String classFileName, String clazzName) {
|
||||||
|
@ -188,7 +210,7 @@ public class Loader {
|
||||||
log.fine(String.format("ModLoader BaseMod class found: %s. Loading", clazzName));
|
log.fine(String.format("ModLoader BaseMod class found: %s. 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);
|
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 loaded: %s.", clazzName));
|
||||||
} else {
|
} else {
|
||||||
|
@ -212,9 +234,10 @@ public class Loader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void attemptFileLoad(File modFile) {
|
private boolean attemptFileLoad(File modFile) {
|
||||||
extendClassLoader(modFile);
|
extendClassLoader(modFile);
|
||||||
|
|
||||||
|
boolean foundAModClass=false;
|
||||||
try {
|
try {
|
||||||
ZipFile jar = new ZipFile(modFile);
|
ZipFile jar = new ZipFile(modFile);
|
||||||
for (ZipEntry ze : Collections.list(jar.entries())) {
|
for (ZipEntry ze : Collections.list(jar.entries())) {
|
||||||
|
@ -225,6 +248,7 @@ public class Loader {
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -232,6 +256,7 @@ public class Loader {
|
||||||
log.throwing("fml.server.Loader", "attemptFileLoad", e);
|
log.throwing("fml.server.Loader", "attemptFileLoad", e);
|
||||||
state = State.ERRORED;
|
state = State.ERRORED;
|
||||||
}
|
}
|
||||||
|
return foundAModClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<ModContainer> getModList() {
|
public static List<ModContainer> getModList() {
|
||||||
|
@ -252,8 +277,9 @@ public class Loader {
|
||||||
modInit();
|
modInit();
|
||||||
postModInit();
|
postModInit();
|
||||||
state = State.UP;
|
state = State.UP;
|
||||||
log.info("Forge Mod Loader load complete");
|
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) {
|
||||||
return instance().namedMods.containsKey(modname);
|
return instance().namedMods.containsKey(modname);
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,4 +23,5 @@ public interface ModContainer {
|
||||||
void tickStart();
|
void tickStart();
|
||||||
void tickEnd();
|
void tickEnd();
|
||||||
boolean matches(Object mod);
|
boolean matches(Object mod);
|
||||||
|
String getSource();
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,8 +26,10 @@ public class ModLoaderModContainer implements ModContainer {
|
||||||
private Class<? extends BaseMod> modClazz;
|
private Class<? extends BaseMod> modClazz;
|
||||||
private BaseMod mod;
|
private BaseMod mod;
|
||||||
private boolean isTicking;
|
private boolean isTicking;
|
||||||
public ModLoaderModContainer(Class<? extends BaseMod> modClazz) {
|
private String modSource ;
|
||||||
|
public ModLoaderModContainer(Class<? extends BaseMod> modClazz, String modSource) {
|
||||||
this.modClazz=modClazz;
|
this.modClazz=modClazz;
|
||||||
|
this.modSource =modSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean wantsPreInit() {
|
public boolean wantsPreInit() {
|
||||||
|
@ -100,4 +102,9 @@ public class ModLoaderModContainer implements ModContainer {
|
||||||
}
|
}
|
||||||
return modList;
|
return modList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSource() {
|
||||||
|
return modSource;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue