From 5650ae7ee9eaf0a620dacbe299901c48bd7d4f1c Mon Sep 17 00:00:00 2001 From: LexManos Date: Sun, 28 Oct 2012 04:18:12 -0700 Subject: [PATCH] Added system to place all configs that use Forge's Configuration function into a single file on disc. Optional config to enable this. Blame MattaBase for this idea.. --- .../minecraftforge/common/Configuration.java | 139 +++++++++++++++--- .../common/ForgeDummyContainer.java | 14 ++ 2 files changed, 130 insertions(+), 23 deletions(-) diff --git a/common/net/minecraftforge/common/Configuration.java b/common/net/minecraftforge/common/Configuration.java index 839b0322c4..1bc04ca7c8 100644 --- a/common/net/minecraftforge/common/Configuration.java +++ b/common/net/minecraftforge/common/Configuration.java @@ -13,11 +13,17 @@ import java.util.Date; import java.util.Locale; import java.util.Map; import java.util.TreeMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import com.google.common.base.CharMatcher; import com.google.common.base.Splitter; import com.google.common.collect.Maps; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.Loader; +import cpw.mods.fml.relauncher.FMLInjectionData; + import net.minecraft.src.Block; import net.minecraft.src.Item; import static net.minecraftforge.common.Property.Type.*; @@ -37,28 +43,47 @@ public class Configuration public static final String CATEGORY_ITEM = "item"; public static final String ALLOWED_CHARS = "._-"; public static final String DEFAULT_ENCODING = "UTF-8"; + private static final Pattern CONFIG_START = Pattern.compile("START: \"([^\\\"]+)\""); + private static final Pattern CONFIG_END = Pattern.compile("END: \"([^\\\"]+)\""); private static final CharMatcher allowedProperties = CharMatcher.JAVA_LETTER_OR_DIGIT.or(CharMatcher.anyOf(ALLOWED_CHARS)); + private static Configuration PARENT = null; File file; public Map> categories = new TreeMap>(); + private Map children = new TreeMap(); private Map customCategoryComments = Maps.newHashMap(); private boolean caseSensitiveCustomCategories; public String defaultEncoding = DEFAULT_ENCODING; - + private String fileName = null; + public boolean isChild = false; + static { Arrays.fill(configBlocks, false); Arrays.fill(configItems, false); } + public Configuration(){} + /** * Create a configuration file for the file given in parameter. */ public Configuration(File file) { this.file = file; + String basePath = ((File)(FMLInjectionData.data()[6])).getAbsolutePath().replace(File.separatorChar, '/').replace("/.", ""); + String path = file.getAbsolutePath().replace(File.separatorChar, '/').replace("/./", "/").replace(basePath, ""); + if (PARENT != null) + { + PARENT.setChild(path, this); + isChild = true; + } + else + { + load(); + } } public Configuration(File file, boolean caseSensitiveCustomCategories) @@ -221,6 +246,10 @@ public class Configuration public void load() { + if (PARENT != null && PARENT != this) + { + return; + } BufferedReader buffer = null; try { @@ -252,6 +281,26 @@ public class Configuration break; } + Matcher start = CONFIG_START.matcher(line); + Matcher end = CONFIG_END.matcher(line); + + if (start.matches()) + { + fileName = start.group(1); + categories = new TreeMap>(); + customCategoryComments = Maps.newHashMap(); + continue; + } + else if (end.matches()) + { + fileName = end.group(1); + Configuration child = new Configuration(); + child.categories = categories; + child.customCategoryComments = customCategoryComments; + this.children.put(fileName, child); + continue; + } + int nameStart = -1, nameEnd = -1; boolean skip = false; boolean quoted = false; @@ -352,6 +401,12 @@ public class Configuration public void save() { + if (PARENT != null && PARENT != this) + { + PARENT.save(); + return; + } + try { if (file.getParentFile() != null) @@ -373,31 +428,18 @@ public class Configuration buffer.write("# Generated on " + DateFormat.getInstance().format(new Date()) + "\r\n"); buffer.write("\r\n"); - for(Map.Entry> category : categories.entrySet()) + if (children.isEmpty()) { - buffer.write("####################\r\n"); - buffer.write("# " + category.getKey() + " \r\n"); - if (customCategoryComments.containsKey(category.getKey())) + save(buffer); + } + else + { + for (Map.Entry entry : children.entrySet()) { - buffer.write("#===================\r\n"); - String comment = customCategoryComments.get(category.getKey()); - Splitter splitter = Splitter.onPattern("\r?\n"); - for (String commentLine : splitter.split(comment)) - { - buffer.write("# "); - buffer.write(commentLine+"\r\n"); - } + buffer.write("START: \"" + entry.getKey() + "\"\r\n"); + entry.getValue().save(buffer); + buffer.write("END: \"" + entry.getKey() + "\"\r\n\r\n"); } - buffer.write("####################\r\n\r\n"); - - String catKey = category.getKey(); - if (!allowedProperties.matchesAllOf(catKey)) - { - catKey = '"'+catKey+'"'; - } - buffer.write(catKey + " {\r\n"); - writeProperties(buffer, category.getValue().values()); - buffer.write("}\r\n\r\n"); } buffer.close(); @@ -410,6 +452,36 @@ public class Configuration } } + private void save(BufferedWriter out) throws IOException + { + for(Map.Entry> category : categories.entrySet()) + { + out.write("####################\r\n"); + out.write("# " + category.getKey() + " \r\n"); + if (customCategoryComments.containsKey(category.getKey())) + { + out.write("#===================\r\n"); + String comment = customCategoryComments.get(category.getKey()); + Splitter splitter = Splitter.onPattern("\r?\n"); + for (String commentLine : splitter.split(comment)) + { + out.write("# "); + out.write(commentLine+"\r\n"); + } + } + out.write("####################\r\n\r\n"); + + String catKey = category.getKey(); + if (!allowedProperties.matchesAllOf(catKey)) + { + catKey = '"'+catKey+'"'; + } + out.write(catKey + " {\r\n"); + writeProperties(out, category.getValue().values()); + out.write("}\r\n\r\n"); + } + } + public void addCustomCategoryComment(String category, String comment) { if (!caseSensitiveCustomCategories) @@ -439,6 +511,27 @@ public class Configuration } } + private void setChild(String name, Configuration child) + { + if (!children.containsKey(name)) + { + children.put(name, child); + } + else + { + Configuration old = children.get(name); + child.categories = old.categories; + child.customCategoryComments = old.customCategoryComments; + child.fileName = old.fileName; + } + } + + public static void enableGlobalConfig() + { + PARENT = new Configuration(new File(Loader.instance().getConfigDir(), "global.cfg")); + PARENT.load(); + } + public static class UnicodeInputStreamReader extends Reader { private final InputStreamReader input; diff --git a/common/net/minecraftforge/common/ForgeDummyContainer.java b/common/net/minecraftforge/common/ForgeDummyContainer.java index bb7caaec29..24000efccd 100644 --- a/common/net/minecraftforge/common/ForgeDummyContainer.java +++ b/common/net/minecraftforge/common/ForgeDummyContainer.java @@ -1,5 +1,6 @@ package net.minecraftforge.common; +import java.io.File; import java.util.Arrays; import java.util.Map; @@ -39,6 +40,18 @@ public class ForgeDummyContainer extends DummyModContainer implements WorldAcces meta.updateUrl = "http://MinecraftForge.net/forum/index.php/topic,5.0.html"; meta.screenshots = new String[0]; meta.logoFile = "/forge_logo.png"; + + Configuration config = new Configuration(new File(Loader.instance().getConfigDir(), "forge.cfg")); + if (!config.isChild) + { + config.load(); + Property enableGlobalCfg = config.get(Configuration.CATEGORY_GENERAL, "enableGlobalConfig", false); + if (enableGlobalCfg.getBoolean(false)) + { + Configuration.enableGlobalConfig(); + } + config.save(); + } } @Override @@ -53,6 +66,7 @@ public class ForgeDummyContainer extends DummyModContainer implements WorldAcces { ForgeChunkManager.captureConfig(evt.getModConfigurationDirectory()); } + @Subscribe public void postInit(FMLPostInitializationEvent evt) {