void updateProperties() { Config config = MCPatcherUtils.config; Element mods = config.getMods(); if (mods == null) { return; } HashMap<String, Element> oldElements = new HashMap<String, Element>(); while (mods.hasChildNodes()) { Node node = mods.getFirstChild(); if (node instanceof Element) { Element element = (Element) node; String name = config.getText(element, Config.TAG_NAME); if (name != null) { oldElements.put(name, element); } } mods.removeChild(node); } for (Mod mod : modsByIndex) { if (mod.internal) { continue; } Element element = oldElements.get(mod.getName()); if (element == null) { defaultModElement(mod); } else { config.setText( element, Config.TAG_ENABLED, Boolean.toString(mod.isEnabled() && mod.okToApply())); updateModElement(mod, element); mods.appendChild(element); oldElements.remove(mod.getName()); } } }
HashMap<String, String> getReverseMap() { HashMap<String, String> map = new HashMap<String, String>(); for (Mod mod : modsByIndex) { for (Map.Entry<String, String> entry : mod.getClassMap().getReverseClassMap().entrySet()) { map.put(entry.getKey(), entry.getValue()); } } return map; }
int addLast(Mod mod) { String name = mod.getName(); Mod oldMod = modsByName.get(name); if (oldMod != null) { remove(oldMod); } modsByIndex.add(mod); modsByName.put(name, mod); mod.setRefs(); return indexOfVisible(mod); }
int replace(Mod oldMod, Mod newMod) { int index = indexOf(oldMod); if (index >= 0 && oldMod.getName().equals(newMod.getName())) { modsByIndex.set(index, newMod); modsByName.put(newMod.getName(), newMod); oldMod.close(); return indexOfVisible(newMod); } else { remove(oldMod); return addFirst(newMod); } }
private boolean addNoReplace(Mod mod) { if (mod == null) { return false; } String name = mod.getName(); if (modsByName.containsKey(name)) { Logger.log(Logger.LOG_MOD, "WARNING: duplicate mod %s ignored", name); return false; } mod.setRefs(); modsByName.put(name, mod); modsByIndex.add(mod); mod.setEnabled(mod.defaultEnabled); mod.loadOptions(); return true; }
private void enableMod(HashMap<Mod, Boolean> inst, Mod mod, boolean recursive) throws ModDependencyException { if (mod == null) { return; } // Logger.log(Logger.LOG_MOD, "%senabling %s", (recursive ? " " : ""), mod.getName()); if (!mod.okToApply()) { throw new ModDependencyException(mod.getName() + " cannot be applied"); } if (inst.containsKey(mod)) { if (!inst.get(mod)) { throw new ModDependencyException(mod.getName() + " is both conflicting and required"); } return; } else { inst.put(mod, true); } for (Mod.Dependency dep : mod.dependencies) { Mod dmod = modsByName.get(dep.name); if (dep.required) { if (dmod == null) { throw new ModDependencyException("dependent mod " + dep.name + " not available"); } else { enableMod(inst, dmod, true); } } else { disableMod(inst, dmod, true); } } for (Mod dmod : modsByIndex) { if (dmod != mod) { for (Mod.Dependency dep : dmod.dependencies) { if (dep.name.equals(mod.getName()) && !dep.required) { disableMod(inst, dmod, true); } } } } }
private void disableMod(HashMap<Mod, Boolean> inst, Mod mod, boolean recursive) throws ModDependencyException { if (mod == null) { return; } // Logger.log(Logger.LOG_MOD, "%sdisabling %s", (recursive ? " " : ""), mod.getName()); if (inst.containsKey(mod)) { if (inst.get(mod)) { throw new ModDependencyException(mod.getName() + " is both conflicting and required"); } return; } else { inst.put(mod, false); } for (Mod dmod : modsByIndex) { if (dmod != mod) { for (Mod.Dependency dep : dmod.dependencies) { if (dep.name.equals(mod.getName()) && dep.required) { disableMod(inst, dmod, true); } } } } }
void loadSavedMods() { Config config = MCPatcherUtils.config; Element mods = config.getMods(); if (mods == null) { return; } NodeList list = mods.getElementsByTagName(Config.TAG_MOD); ArrayList<Element> invalidEntries = new ArrayList<Element>(); for (int i = 0; i < list.getLength(); i++) { Element element = (Element) list.item(i); String name = config.getText(element, Config.TAG_NAME); String type = config.getText(element, Config.TAG_TYPE); String enabled = config.getText(element, Config.TAG_ENABLED); Mod mod = null; if (name == null || type == null) { invalidEntries.add(element); } else if (type.equals(Config.VAL_BUILTIN)) { for (BuiltInMod builtInMod : builtInMods) { if (name.equals(builtInMod.name) && (MCPatcher.experimentalMods || !builtInMod.experimental)) { mod = newModInstance(builtInMod.modClass); } } if (mod == null) { invalidEntries.add(element); } } else if (type.equals(Config.VAL_EXTERNAL_ZIP)) { String path = config.getText(element, Config.TAG_PATH); Element files = config.getElement(element, Config.TAG_FILES); if (path != null && files != null) { File file = new File(path); if (file.exists()) { HashMap<String, String> fileMap = new HashMap<String, String>(); NodeList fileNodes = files.getElementsByTagName(Config.TAG_FILE); for (int j = 0; j < fileNodes.getLength(); j++) { Element fileElem = (Element) fileNodes.item(j); String from = config.getText(fileElem, Config.TAG_FROM); String to = config.getText(fileElem, Config.TAG_TO); if (from != null && to != null) { fileMap.put(to, from); } } try { mod = new ExternalMod(new ZipFile(file), fileMap); } catch (IOException e) { Logger.log(e); } } } else { invalidEntries.add(element); } } else if (type.equals(Config.VAL_EXTERNAL_JAR)) { String path = config.getText(element, Config.TAG_PATH); String className = config.getText(element, Config.TAG_CLASS); if (path != null && className != null) { File file = new File(path); if (file.exists()) { mod = loadCustomMod(file, className); if (mod != null) { mod.customJar = file; } } } else { invalidEntries.add(element); } } else { invalidEntries.add(element); } if (mod != null) { if (addNoReplace(mod)) { if (enabled != null) { mod.setEnabled(Boolean.parseBoolean(enabled)); } } } } for (Element element : invalidEntries) { mods.removeChild(element); } refreshInternalMods(); }