use of io.xol.chunkstories.api.exceptions.content.mods.ModNotFoundException 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);
}
Aggregations