Commit cf4e662ff064db44d1acd9f50cf1d1b3b63f63cb

Authored by Mumfrey
1 parent 62a9bc25

LiteLoader 1.6.4_02 - experimental - make enable/disable mods public, minor refactoring

java/com/mumfrey/liteloader/core/LiteLoader.java
... ... @@ -133,7 +133,7 @@ public final class LiteLoader
133 133 private String loadedModsList = "none";
134 134  
135 135 /**
136   - * Global list of mods which we can loaded
  136 + * Global list of mods which we can load
137 137 */
138 138 private final LinkedList<LiteMod> mods = new LinkedList<LiteMod>();
139 139  
... ... @@ -354,7 +354,7 @@ public final class LiteLoader
354 354 this.events.initHooks();
355 355 this.startupComplete = true;
356 356  
357   - this.enabledModsList.save();
  357 + this.enabledModsList.saveTo(this.enabledModsFile);
358 358 this.bootstrap.writeProperties();
359 359 }
360 360  
... ... @@ -572,23 +572,6 @@ public final class LiteLoader
572 572 }
573 573  
574 574 /**
575   - * Used by the version upgrade code, gets a version of the mod name suitable
576   - * for inclusion in the properties file
577   - *
578   - * @param modName
579   - * @return
580   - */
581   - private String getModNameForConfig(Class<? extends LiteMod> modClass, String modName)
582   - {
583   - if (modName == null || modName.isEmpty())
584   - {
585   - modName = modClass.getSimpleName().toLowerCase();
586   - }
587   -
588   - return String.format("version.%s", modName.toLowerCase().replaceAll("[^a-z0-9_\\-\\.]", ""));
589   - }
590   -
591   - /**
592 575 * Get a reference to a loaded mod, if the mod exists
593 576 *
594 577 * @param modName Mod's name, meta name or class name
... ... @@ -713,8 +696,83 @@ public final class LiteLoader
713 696 {
714 697 return this.enumerator.getModMetaName(modClass);
715 698 }
  699 +
  700 + /**
  701 + * Get the mod "name" metadata key, this is used for versioning, exclusivity, and enablement checks
  702 + *
  703 + * @param modClass
  704 + * @return
  705 + */
  706 + public Class<? extends LiteMod> getModFromMetaName(String modName)
  707 + {
  708 + if (modName == null) return null;
  709 +
  710 + for (LiteMod mod : this.mods)
  711 + {
  712 + if (modName.equalsIgnoreCase(this.enumerator.getModMetaName(mod.getClass())))
  713 + {
  714 + return mod.getClass();
  715 + }
  716 + }
  717 +
  718 + return null;
  719 + }
  720 +
  721 + /**
  722 + * @param modMetaName Mod meta name to enable
  723 + */
  724 + public void enableMod(String modMetaName)
  725 + {
  726 + this.setModEnabled(modMetaName, true);
  727 + }
  728 +
  729 + /**
  730 + * @param modMetaName Mod meta name to disable
  731 + */
  732 + public void disableMod(String modMetaName)
  733 + {
  734 + this.setModEnabled(modMetaName, false);
  735 + }
  736 +
  737 + /**
  738 + * @param modMetaName Mod meta name to enable/disable
  739 + * @param enabled
  740 + */
  741 + public void setModEnabled(String modMetaName, boolean enabled)
  742 + {
  743 + this.enabledModsList.setEnabled(this.bootstrap.getProfile(), modMetaName, enabled);
  744 + this.enabledModsList.saveTo(this.enabledModsFile);
  745 + }
716 746  
717 747 /**
  748 + * @param modName
  749 + * @return
  750 + */
  751 + public boolean isModEnabled(String modName)
  752 + {
  753 + return this.enabledModsList.isEnabled(LiteLoader.getProfile(), modName);
  754 + }
  755 +
  756 + /**
  757 + * @param modName
  758 + * @return
  759 + */
  760 + public boolean isModActive(String modName)
  761 + {
  762 + if (modName == null) return false;
  763 +
  764 + for (LiteMod mod : this.loadedMods)
  765 + {
  766 + if (modName.equalsIgnoreCase(this.enumerator.getModMetaName(mod.getClass())))
  767 + {
  768 + return true;
  769 + }
  770 + }
  771 +
  772 + return false;
  773 + }
  774 +
  775 + /**
718 776 * Create mod instances from the enumerated classes
719 777 *
720 778 * @param modsToLoad List of mods to load
... ... @@ -740,32 +798,7 @@ public final class LiteLoader
740 798 String metaName = this.getModMetaName(mod);
741 799 if (metaName == null || this.enabledModsList.isEnabled(this.bootstrap.getProfile(), metaName))
742 800 {
743   - LiteLoader.logInfo("Loading mod from %s", mod.getName());
744   -
745   - LiteMod newMod = mod.newInstance();
746   -
747   - this.mods.add(newMod);
748   - String modName = newMod.getName();
749   - if (modName == null && metaName != null) modName = metaName;
750   - LiteLoader.logInfo("Successfully added mod %s version %s", modName, newMod.getVersion());
751   -
752   - // Get the mod file and register it as a resource pack if it exists
753   - ModFile modFile = this.enumerator.getModFile(mod);
754   - if (modFile != null)
755   - {
756   - this.disabledMods.remove(modFile);
757   -
758   - LiteLoader.logInfo("Adding \"%s\" to active resource pack set", modFile.getAbsolutePath());
759   - if (modName != null)
760   - {
761   - modFile.initResourcePack(modName);
762   -
763   - if (modFile.hasResourcePack() && this.registerModResourcePack((ResourcePack)modFile.getResourcePack()))
764   - {
765   - LiteLoader.logInfo("Successfully added \"%s\" to active resource pack set", modFile.getAbsolutePath());
766   - }
767   - }
768   - }
  801 + this.loadMod(metaName, mod);
769 802 }
770 803 else
771 804 {
... ... @@ -782,6 +815,42 @@ public final class LiteLoader
782 815 }
783 816  
784 817 /**
  818 + * @param metaName
  819 + * @param mod
  820 + * @throws InstantiationException
  821 + * @throws IllegalAccessException
  822 + */
  823 + protected void loadMod(String metaName, Class<? extends LiteMod> mod) throws InstantiationException, IllegalAccessException
  824 + {
  825 + LiteLoader.logInfo("Loading mod from %s", mod.getName());
  826 +
  827 + LiteMod newMod = mod.newInstance();
  828 +
  829 + this.mods.add(newMod);
  830 + String modName = newMod.getName();
  831 + if (modName == null && metaName != null) modName = metaName;
  832 + LiteLoader.logInfo("Successfully added mod %s version %s", modName, newMod.getVersion());
  833 +
  834 + // Get the mod file and register it as a resource pack if it exists
  835 + ModFile modFile = this.enumerator.getModFile(mod);
  836 + if (modFile != null)
  837 + {
  838 + this.disabledMods.remove(modFile);
  839 +
  840 + LiteLoader.logInfo("Adding \"%s\" to active resource pack set", modFile.getAbsolutePath());
  841 + if (modName != null)
  842 + {
  843 + modFile.initResourcePack(modName);
  844 +
  845 + if (modFile.hasResourcePack() && this.registerModResourcePack((ResourcePack)modFile.getResourcePack()))
  846 + {
  847 + LiteLoader.logInfo("Successfully added \"%s\" to active resource pack set", modFile.getAbsolutePath());
  848 + }
  849 + }
  850 + }
  851 + }
  852 +
  853 + /**
785 854 * Initialise the mods which were loaded
786 855 */
787 856 private void initMods()
... ... @@ -792,54 +861,87 @@ public final class LiteLoader
792 861 for (Iterator<LiteMod> iter = this.mods.iterator(); iter.hasNext();)
793 862 {
794 863 LiteMod mod = iter.next();
795   - String modName = mod.getName();
796 864  
797 865 try
798 866 {
799   - LiteLoader.logInfo("Initialising mod %s version %s", modName, mod.getVersion());
800   -
801   - try
802   - {
803   - String modKey = this.getModNameForConfig(mod.getClass(), modName);
804   - LiteLoaderVersion lastModVersion = LiteLoaderVersion.getVersionFromRevision(this.bootstrap.getLastKnownModRevision(modKey));
805   -
806   - if (LiteLoaderBootstrap.VERSION.getLoaderRevision() > lastModVersion.getLoaderRevision())
807   - {
808   - LiteLoader.logInfo("Performing config upgrade for mod %s. Upgrading %s to %s...", modName, lastModVersion, LiteLoaderBootstrap.VERSION);
809   - mod.upgradeSettings(LiteLoaderBootstrap.VERSION.getMinecraftVersion(), this.versionConfigFolder, this.inflectVersionedConfigPath(lastModVersion));
810   -
811   - this.bootstrap.storeLastKnownModRevision(modKey);
812   - LiteLoader.logInfo("Config upgrade succeeded for mod %s", modName);
813   - }
814   - }
815   - catch (Throwable th)
816   - {
817   - LiteLoader.logWarning("Error performing settings upgrade for %s. Settings may not be properly migrated", modName);
818   - }
819   -
820   - // pre-1.6.4_01 this was being called with the wrong path, I hope this doesn't break anything
821   - mod.init(this.commonConfigFolder);
822   -
823   - this.events.addListener(mod);
824   -
825   - if (mod instanceof Permissible)
826   - {
827   - this.permissionsManager.registerPermissible((Permissible)mod);
828   - }
829   -
830   - this.loadedMods.add(mod);
831   - this.loadedModsList += String.format("\n - %s version %s", modName, mod.getVersion());
  867 + this.initMod(mod);
832 868 loadedModsCount++;
833 869 }
834 870 catch (Throwable th)
835 871 {
836   - LiteLoader.getLogger().log(Level.WARNING, "Error initialising mod '" + modName, th);
  872 + LiteLoader.getLogger().log(Level.WARNING, "Error initialising mod '" + mod.getName() + "'", th);
837 873 iter.remove();
838 874 }
839 875 }
840 876  
841 877 this.loadedModsList = String.format("%s loaded mod(s)%s", loadedModsCount, this.loadedModsList);
842 878 }
  879 +
  880 + /**
  881 + * @param mod
  882 + */
  883 + protected void initMod(LiteMod mod)
  884 + {
  885 + LiteLoader.logInfo("Initialising mod %s version %s", mod.getName(), mod.getVersion());
  886 +
  887 + try
  888 + {
  889 + this.handleModVersionUpgrade(mod);
  890 + }
  891 + catch (Throwable th)
  892 + {
  893 + LiteLoader.logWarning("Error performing settings upgrade for %s. Settings may not be properly migrated", mod.getName());
  894 + }
  895 +
  896 + // initialise the mod
  897 + mod.init(this.commonConfigFolder);
  898 +
  899 + // add the mod to all relevant listener queues
  900 + this.events.addListener(mod);
  901 +
  902 + if (mod instanceof Permissible)
  903 + {
  904 + this.permissionsManager.registerPermissible((Permissible)mod);
  905 + }
  906 +
  907 + this.loadedMods.add(mod);
  908 + this.loadedModsList += String.format("\n - %s version %s", mod.getName(), mod.getVersion());
  909 + }
  910 +
  911 + /**
  912 + * @param mod
  913 + */
  914 + protected void handleModVersionUpgrade(LiteMod mod)
  915 + {
  916 + String modKey = this.getModNameForConfig(mod.getClass(), mod.getName());
  917 + LiteLoaderVersion lastModVersion = LiteLoaderVersion.getVersionFromRevision(this.bootstrap.getLastKnownModRevision(modKey));
  918 +
  919 + if (LiteLoaderBootstrap.VERSION.getLoaderRevision() > lastModVersion.getLoaderRevision())
  920 + {
  921 + LiteLoader.logInfo("Performing config upgrade for mod %s. Upgrading %s to %s...", mod.getName(), lastModVersion, LiteLoaderBootstrap.VERSION);
  922 + mod.upgradeSettings(LiteLoaderBootstrap.VERSION.getMinecraftVersion(), this.versionConfigFolder, this.inflectVersionedConfigPath(lastModVersion));
  923 +
  924 + this.bootstrap.storeLastKnownModRevision(modKey);
  925 + LiteLoader.logInfo("Config upgrade succeeded for mod %s", mod.getName());
  926 + }
  927 + }
  928 +
  929 + /**
  930 + * Used by the version upgrade code, gets a version of the mod name suitable
  931 + * for inclusion in the properties file
  932 + *
  933 + * @param modName
  934 + * @return
  935 + */
  936 + private String getModNameForConfig(Class<? extends LiteMod> modClass, String modName)
  937 + {
  938 + if (modName == null || modName.isEmpty())
  939 + {
  940 + modName = modClass.getSimpleName().toLowerCase();
  941 + }
  942 +
  943 + return String.format("version.%s", modName.toLowerCase().replaceAll("[^a-z0-9_\\-\\.]", ""));
  944 + }
843 945  
844 946 /**
845 947 * Called before mod late initialisation, refresh the resources that have been added so that mods can use them
... ...
java/com/mumfrey/liteloader/core/LiteLoaderEnumerator.java
... ... @@ -186,7 +186,7 @@ class LiteLoaderEnumerator implements FilenameFilter
186 186 if (!this.modFiles.containsKey(modClassName)) return null;
187 187 return this.modFiles.get(modClassName).getModName().toLowerCase();
188 188 }
189   -
  189 +
190 190 /**
191 191 * Get the discovery settings from the properties file
192 192 */
... ... @@ -238,15 +238,15 @@ class LiteLoaderEnumerator implements FilenameFilter
238 238 {
239 239 LiteLoaderEnumerator.logInfo("Mods folder found, searching %s", modsFolder.getPath());
240 240 this.findModFiles(modsFolder, false);
241   - LiteLoaderEnumerator.logInfo("Found %d mod file(s)", this.allModFiles.size());
242 241  
243 242 File versionedModsFolder = new File(modsFolder, LiteLoaderBootstrap.VERSION.getMinecraftVersion());
244 243 if (versionedModsFolder.exists() && versionedModsFolder.isDirectory())
245 244 {
246 245 LiteLoaderEnumerator.logInfo("Versioned mods folder found, searching %s", versionedModsFolder.getPath());
247 246 this.findModFiles(versionedModsFolder, true);
248   - LiteLoaderEnumerator.logInfo("Found %d mod file(s)", this.allModFiles.size());
249 247 }
  248 +
  249 + LiteLoaderEnumerator.logInfo("Found %d mod file(s)", this.allModFiles.size());
250 250 }
251 251 }
252 252 }
... ...
java/com/mumfrey/liteloader/gui/GuiModListEntry.java
... ... @@ -21,11 +21,6 @@ public class GuiModListEntry extends Gui
21 21 private static final int PANEL_SPACING = 4;
22 22  
23 23 /**
24   - * Enabled mods list, keep a reference so that we can toggle mod enablement when required
25   - */
26   - private EnabledModsList enabledModsList;
27   -
28   - /**
29 24 * For text display
30 25 */
31 26 private FontRenderer fontRenderer;
... ... @@ -91,7 +86,6 @@ public class GuiModListEntry extends Gui
91 86 */
92 87 GuiModListEntry(LiteLoader loader, EnabledModsList enabledMods, FontRenderer fontRenderer, LiteMod mod)
93 88 {
94   - this.enabledModsList = enabledMods;
95 89 this.fontRenderer = fontRenderer;
96 90 this.metaName = loader.getModMetaName(mod.getClass());
97 91 this.name = mod.getName();
... ... @@ -100,7 +94,7 @@ public class GuiModListEntry extends Gui
100 94 this.url = loader.getModMetaData(mod.getClass(), "url", null);
101 95 this.description = loader.getModMetaData(mod.getClass(), "description", "");
102 96 this.enabled = true;
103   - this.canBeToggled = this.metaName != null && this.enabledModsList.saveAllowed();
  97 + this.canBeToggled = this.metaName != null && enabledMods.saveAllowed();
104 98 this.willBeEnabled = true;
105 99 }
106 100  
... ... @@ -114,7 +108,6 @@ public class GuiModListEntry extends Gui
114 108 */
115 109 GuiModListEntry(LiteLoader loader, EnabledModsList enabledMods, FontRenderer fontRenderer, ModFile file)
116 110 {
117   - this.enabledModsList = enabledMods;
118 111 this.fontRenderer = fontRenderer;
119 112 this.metaName = file.getModName().toLowerCase();
120 113 this.name = file instanceof ClassPathMod ? file.getModName() : file.getName();
... ... @@ -123,7 +116,7 @@ public class GuiModListEntry extends Gui
123 116 this.url = file.getMetaValue("url", null);
124 117 this.description = file.getMetaValue("description", "");
125 118 this.enabled = false;
126   - this.canBeToggled = this.enabledModsList.saveAllowed();
  119 + this.canBeToggled = enabledMods.saveAllowed();
127 120 this.willBeEnabled = enabledMods.isEnabled(loader.getProfile(), this.metaName);
128 121 }
129 122  
... ... @@ -202,8 +195,7 @@ public class GuiModListEntry extends Gui
202 195 if (this.canBeToggled)
203 196 {
204 197 this.willBeEnabled = !this.willBeEnabled;
205   - this.enabledModsList.setEnabled(LiteLoader.getProfile(), this.metaName, this.willBeEnabled);
206   - this.enabledModsList.save();
  198 + LiteLoader.getInstance().setModEnabled(this.metaName, this.willBeEnabled);
207 199 }
208 200 }
209 201  
... ...