Rework configuration, configs should now use Config.hasChanged to deterne if thehould call save(), also re-worked the saving to not use String.format as much.

This commit is contained in:
LexManos committed 2013-03-04 16:33:52 -08:00
1 parent 1860885fee
commit 16475908be
5 files changed
+259 -115

No files matched your search

@@ -1,5 +1,8 @@
package net.minecraftforge.common;
import static net.minecraftforge.common.Configuration.NEW_LINE;
import static net.minecraftforge.common.Configuration.allowedProperties;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
@@ -7,11 +10,10 @@ import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.base.Splitter;
import static net.minecraftforge.common.Configuration.*;
public class ConfigCategory implements Map<String, Property>
{
private String name;
@@ -19,6 +21,7 @@ public class ConfigCategory implements Map<String, Property>
private ArrayList<ConfigCategory> children = new ArrayList<ConfigCategory>();
private Map<String, Property> properties = new TreeMap<String, Property>();
public final ConfigCategory parent;
private boolean changed = false;
public ConfigCategory(String name)
{
@@ -68,7 +71,7 @@ public class ConfigCategory implements Map<String, Property>
public Map<String, Property> getValues()
{
return properties;
return ImmutableMap.copyOf(properties);
}
public void setComment(String comment)
@@ -86,39 +89,48 @@ public class ConfigCategory implements Map<String, Property>
return properties.get(key);
}
public void set(String key, Property value)
private void write(BufferedWriter out, String... data) throws IOException
{
properties.put(key, value);
write(out, true, data);
}
private void write(BufferedWriter out, boolean new_line, String... data) throws IOException
{
for (int x = 0; x < data.length; x++)
{
out.write(data[x]);
}
if (new_line) out.write(NEW_LINE);
}
public void write(BufferedWriter out, int indent) throws IOException
{
String pad = getIndent(indent);
String pad0 = getIndent(indent);
String pad1 = getIndent(indent + 1);
String pad2 = getIndent(indent + 2);
out.write(pad + "####################" + NEW_LINE);
out.write(pad + "# " + name + NEW_LINE);
write(out, pad0, "####################");
write(out, pad0, "# ", name);
if (comment != null)
{
out.write(pad + "#===================" + NEW_LINE);
write(out, pad0, "#===================");
Splitter splitter = Splitter.onPattern("\r?\n");
for (String line : splitter.split(comment))
{
out.write(pad + "# " + line + NEW_LINE);
write(out, pad0, "# ", line);
}
}
out.write(pad + "####################" + NEW_LINE + NEW_LINE);
write(out, pad0, "####################", NEW_LINE);
if (!allowedProperties.matchesAllOf(name))
{
name = '"' + name + '"';
}
out.write(pad + name + " {" + NEW_LINE);
pad = getIndent(indent + 1);
write(out, pad0, name, " {");
Property[] props = properties.values().toArray(new Property[properties.size()]);
@@ -136,7 +148,7 @@ public class ConfigCategory implements Map<String, Property>
Splitter splitter = Splitter.onPattern("\r?\n");
for (String commentLine : splitter.split(prop.comment))
{
out.write(pad + "# " + commentLine + NEW_LINE);
write(out, pad1, "# ", commentLine);
}
}
@@ -149,23 +161,25 @@ public class ConfigCategory implements Map<String, Property>
if (prop.isList())
{
out.write(String.format(pad + "%s:%s <" + NEW_LINE, prop.getType().getID(), propName));
pad = getIndent(indent + 2);
char type = prop.getType().getID();
write(out, pad1, String.valueOf(type), ":", propName, " <");
for (String line : prop.valueList)
for (String line : prop.getStringList())
{
out.write(pad + line + NEW_LINE);
write(out, pad2, line);
}
out.write(getIndent(indent + 1) + " >" + NEW_LINE);
write(out, pad1, " >");
}
else if (prop.getType() == null)
{
out.write(String.format(pad + "%s=%s" + NEW_LINE, propName, prop.value));
write(out, pad1, propName, "=", prop.getString());
}
else
{
out.write(String.format(pad + "%s:%s=%s" + NEW_LINE, prop.getType().getID(), propName, prop.value));
char type = prop.getType().getID();
write(out, pad1, String.valueOf(type), ":", propName, "=", prop.getString());
}
}
@@ -174,7 +188,7 @@ public class ConfigCategory implements Map<String, Property>
child.write(out, indent + 1);
}
out.write(getIndent(indent) + "}" + NEW_LINE + NEW_LINE);
write(out, pad0, "}", NEW_LINE);
}
private String getIndent(int indent)
@@ -187,6 +201,25 @@ public class ConfigCategory implements Map<String, Property>
return buf.toString();
}
public boolean hasChanged()
{
if (changed) return true;
for (Property prop : properties.values())
{
if (prop.hasChanged()) return true;
}
return false;
}
void resetChangedState()
{
changed = false;
for (Property prop : properties.values())
{
prop.resetChangedState();
}
}
//Map bouncer functions for compatibility with older mods, to be removed once all mods stop using it.
@Override public int size(){ return properties.size(); }
@@ -194,12 +227,33 @@ public class ConfigCategory implements Map<String, Property>
@Override public boolean containsKey(Object key) { return properties.containsKey(key); }
@Override public boolean containsValue(Object value){ return properties.containsValue(value); }
@Override public Property get(Object key) { return properties.get(key); }
@Override public Property put(String key, Property value) { return properties.put(key, value); }
@Override public Property remove(Object key) { return properties.remove(key); }
@Override public void putAll(Map<? extends String, ? extends Property> m) { properties.putAll(m); }
@Override public void clear() { properties.clear(); }
@Override public Property put(String key, Property value)
{
changed = true;
return properties.put(key, value);
}
@Override public Property remove(Object key)
{
changed = true;
return properties.remove(key);
}
@Override public void putAll(Map<? extends String, ? extends Property> m)
{
changed = true;
properties.putAll(m);
}
@Override public void clear()
{
changed = true;
properties.clear();
}
@Override public Set<String> keySet() { return properties.keySet(); }
@Override public Collection<Property> values() { return properties.values(); }
@Override public Set<java.util.Map.Entry<String, Property>> entrySet() { return properties.entrySet(); }
@Override //Immutable copy, changes will NOT be reflected in this category
public Set<java.util.Map.Entry<String, Property>> entrySet()
{
return ImmutableSet.copyOf(properties.entrySet());
}
}
@@ -5,31 +5,41 @@
package net.minecraftforge.common;
import java.io.*;
import java.text.DateFormat;
import static net.minecraftforge.common.Property.Type.BOOLEAN;
import static net.minecraftforge.common.Property.Type.DOUBLE;
import static net.minecraftforge.common.Property.Type.INTEGER;
import static net.minecraftforge.common.Property.Type.STRING;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PushbackInputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
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 net.minecraft.block.Block;
import net.minecraft.item.Item;
import com.google.common.base.CharMatcher;
import com.google.common.collect.ImmutableSet;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.relauncher.FMLInjectionData;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import static net.minecraftforge.common.Property.Type.*;
/**
* This class offers advanced configurations capabilities, allowing to provide
* various categories for configuration variables.
@@ -54,13 +64,14 @@ public class Configuration
File file;
public Map<String, ConfigCategory> categories = new TreeMap<String, ConfigCategory>();
private Map<String, ConfigCategory> categories = new TreeMap<String, ConfigCategory>();
private Map<String, Configuration> children = new TreeMap<String, Configuration>();
private boolean caseSensitiveCustomCategories;
public String defaultEncoding = DEFAULT_ENCODING;
private String fileName = null;
public boolean isChild = false;
private boolean changed = false;
static
{
@@ -148,7 +159,7 @@ public class Configuration
if (Block.blocksList[defaultID] == null && !configMarkers[defaultID])
{
prop.value = Integer.toString(defaultID);
prop.set(defaultID);
configMarkers[defaultID] = true;
return prop;
}
@@ -158,7 +169,7 @@ public class Configuration
{
if (Block.blocksList[j] == null && !configMarkers[j])
{
prop.value = Integer.toString(j);
prop.set(j);
configMarkers[j] = true;
return prop;
}
@@ -196,7 +207,7 @@ public class Configuration
if (Item.itemsList[defaultShift] == null && !configMarkers[defaultShift] && defaultShift >= Block.blocksList.length)
{
prop.value = Integer.toString(defaultID);
prop.set(defaultID);
configMarkers[defaultShift] = true;
return prop;
}
@@ -206,7 +217,7 @@ public class Configuration
{
if (Item.itemsList[x] == null && !configMarkers[x])
{
prop.value = Integer.toString(x - ITEM_SHIFT);
prop.set(x - ITEM_SHIFT);
configMarkers[x] = true;
return prop;
}
@@ -227,7 +238,7 @@ public class Configuration
Property prop = get(category, key, Integer.toString(defaultValue), comment, INTEGER);
if (!prop.isIntValue())
{
prop.value = Integer.toString(defaultValue);
prop.set(defaultValue);
}
return prop;
}
@@ -242,7 +253,7 @@ public class Configuration
Property prop = get(category, key, Boolean.toString(defaultValue), comment, BOOLEAN);
if (!prop.isBooleanValue())
{
prop.value = Boolean.toString(defaultValue);
prop.set(defaultValue);
}
return prop;
}
@@ -257,7 +268,7 @@ public class Configuration
Property prop = get(category, key, Double.toString(defaultValue), comment, DOUBLE);
if (!prop.isDoubleValue())
{
prop.value = Double.toString(defaultValue);
prop.set(defaultValue);
}
return prop;
}
@@ -298,7 +309,7 @@ public class Configuration
Property prop = get(category, key, values, comment, INTEGER);
if (!prop.isIntList())
{
prop.valueList = values;
prop.set(values);
}
return prop;
@@ -321,7 +332,7 @@ public class Configuration
if (!prop.isDoubleList())
{
prop.valueList = values;
prop.set(values);
}
return prop;
@@ -344,7 +355,7 @@ public class Configuration
if (!prop.isBooleanList())
{
prop.valueList = values;
prop.set(values);
}
return prop;
@@ -365,8 +376,8 @@ public class Configuration
if (prop.getType() == null)
{
prop = new Property(prop.getName(), prop.value, type);
cat.set(key, prop);
prop = new Property(prop.getName(), prop.getString(), type);
cat.put(key, prop);
}
prop.comment = comment;
@@ -375,7 +386,8 @@ public class Configuration
else if (defaultValue != null)
{
Property prop = new Property(key, defaultValue, type);
cat.set(key, prop);
prop.set(defaultValue); //Set and mark as dirty to signify it should save
cat.put(key, prop);
prop.comment = comment;
return prop;
}
@@ -400,8 +412,8 @@ public class Configuration
if (prop.getType() == null)
{
prop = new Property(prop.getName(), prop.value, type);
cat.set(key, prop);
prop = new Property(prop.getName(), prop.getString(), type);
cat.put(key, prop);
}
prop.comment = comment;
@@ -412,7 +424,7 @@ public class Configuration
{
Property prop = new Property(key, defaultValue, type);
prop.comment = comment;
cat.set(key, prop);
cat.put(key, prop);
return prop;
}
else
@@ -569,7 +581,7 @@ public class Configuration
Property prop = new Property(name, line.substring(i + 1), type, true);
i = line.length();
currentCat.set(name, prop);
currentCat.put(name, prop);
break;
@@ -603,7 +615,7 @@ public class Configuration
throw new RuntimeException(String.format("Malformed list property \"%s:%d\"", fileName, lineNum));
}
currentCat.set(name, new Property(name, tmpList.toArray(new String[tmpList.size()]), type));
currentCat.put(name, new Property(name, tmpList.toArray(new String[tmpList.size()]), type));
name = null;
tmpList = null;
type = null;
@@ -647,6 +659,8 @@ public class Configuration
} catch (IOException e){}
}
}
resetChangedState();
}
public void save()
@@ -701,29 +715,7 @@ public class Configuration
}
private void save(BufferedWriter out) throws IOException
{
//For compatiblitties sake just in case, Thanks Atomic, to be removed next MC version
//TO-DO: Remove next MC version
Object[] categoryArray = categories.values().toArray();
for (Object o : categoryArray)
{
if (o instanceof TreeMap)
{
TreeMap treeMap = (TreeMap)o;
ConfigCategory converted = new ConfigCategory(file.getName());
FMLLog.warning("Forge found a Treemap saved for Configuration file " + file.getName() + ", this is deprecated behaviour!");
for (Object key : treeMap.keySet())
{
FMLLog.warning("Converting Treemap to ConfigCategory, key: " + key + ", property value: " + ((Property)treeMap.get(key)).value);
converted.set((String)key, (Property)treeMap.get(key));
}
categories.values().remove(o);
categories.put(file.getName(), converted);
}
}
{
for (ConfigCategory cat : categories.values())
{
if (!cat.isChild())
@@ -749,6 +741,7 @@ public class Configuration
{
parent = new ConfigCategory(hierarchy[0]);
categories.put(parent.getQualifiedName(), parent);
changed = true;
}
for (int i = 1; i < hierarchy.length; i++)
@@ -760,6 +753,7 @@ public class Configuration
{
child = new ConfigCategory(hierarchy[i], parent);
categories.put(name, child);
changed = true;
}
ret = child;
@@ -770,6 +764,7 @@ public class Configuration
{
ret = new ConfigCategory(category);
categories.put(category, ret);
changed = true;
}
}
@@ -788,12 +783,14 @@ public class Configuration
if (!children.containsKey(name))
{
children.put(name, child);
changed = true;
}
else
{
Configuration old = children.get(name);
child.categories = old.categories;
child.fileName = old.fileName;
old.changed = true;
}
}
@@ -873,4 +870,40 @@ public class Configuration
input.close();
}
}
public boolean hasChanged()
{
if (changed) return true;
for (ConfigCategory cat : categories.values())
{
if (cat.hasChanged()) return true;
}
for (Configuration child : children.values())
{
if (child.hasChanged()) return true;
}
return false;
}
private void resetChangedState()
{
changed = false;
for (ConfigCategory cat : categories.values())
{
cat.resetChangedState();
}
for (Configuration child : children.values())
{
child.resetChangedState();
}
}
public Set<String> getCategoryNames()
{
return ImmutableSet.copyOf(categories.keySet());
}
}
@@ -775,7 +775,7 @@ public class ForgeChunkManager
static void loadConfiguration()
{
for (String mod : config.categories.keySet())
for (String mod : config.getCategoryNames())
{
if (mod.equals("Forge") || mod.equals("defaults"))
{
@@ -786,7 +786,10 @@ public class ForgeChunkManager
ticketConstraints.put(mod, modTC.getInt(200));
chunkConstraints.put(mod, modCPT.getInt(25));
}
config.save();
if (config.hasChanged())
{
config.save();
}
}
/**
@@ -953,7 +956,7 @@ public class ForgeChunkManager
sampleTC.comment = "Maximum ticket count for the mod. Zero disables chunkloading capabilities.";
sampleTC = config.get("Forge", "maximumChunksPerTicket", 25);
sampleTC.comment = "Maximum chunks per ticket for the mod.";
for (String mod : config.categories.keySet())
for (String mod : config.getCategoryNames())
{
if (mod.equals("Forge") || mod.equals("defaults"))
{
@@ -965,12 +968,12 @@ public class ForgeChunkManager
}
public static Map<String,Property> getConfigMapFor(Object mod)
public static ConfigCategory getConfigFor(Object mod)
{
ModContainer container = getContainer(mod);
if (container != null)
{
return config.getCategory(container.getModId()).getValues();
return config.getCategory(container.getModId());
}
return null;
@@ -981,8 +984,8 @@ public class ForgeChunkManager
ModContainer container = getContainer(mod);
if (container != null)
{
Map<String, Property> props = config.getCategory(container.getModId()).getValues();
props.put(propertyName, new Property(propertyName, value, type));
ConfigCategory cat = config.getCategory(container.getModId());
cat.put(propertyName, new Property(propertyName, value, type));
}
}
}
@@ -44,7 +44,22 @@ public class ForgeDummyContainer extends DummyModContainer implements WorldAcces
meta.screenshots = new String[0];
meta.logoFile = "/forge_logo.png";
Configuration config = new Configuration(new File(Loader.instance().getConfigDir(), "forge.cfg"));
Configuration config = null;
File cfgFile = new File(Loader.instance().getConfigDir(), "forge.cfg");
try
{
config = new Configuration(cfgFile);
}
catch (Exception e)
{
System.out.println("Error loading forge.cfg, deleting file and resetting: ");
e.printStackTrace();
if (cfgFile.exists())
cfgFile.delete();
config = new Configuration(cfgFile);
}
if (!config.isChild)
{
config.load();
@@ -60,9 +75,13 @@ public class ForgeDummyContainer extends DummyModContainer implements WorldAcces
if (clumpingThreshold > 1024 || clumpingThreshold < 64)
{
clumpingThreshold = 64;
clumpingThresholdProperty.value = "64";
clumpingThresholdProperty.set(64);
}
if (config.hasChanged())
{
config.save();
}
config.save();
}
@Override
+57 -22
View File
@@ -40,13 +40,14 @@ public class Property
}
private String name;
public String value;
private String value;
public String comment;
public String[] valueList;
private String[] values;
private final boolean wasRead;
private final boolean isList;
private final Type type;
private boolean changed = false;
public Property()
{
@@ -77,10 +78,20 @@ public class Property
Property(String name, String[] values, Type type, boolean read)
{
setName(name);
this.type = type;
valueList = values;
wasRead = read;
isList = true;
this.type = type;
this.values = values;
wasRead = read;
isList = true;
}
/**
* Returns the value in this property as it's raw string.
*
* @return current value
*/
public String getString()
{
return value;
}
/**
@@ -197,6 +208,11 @@ public class Property
}
}
public String[] getStringList()
{
return values;
}
/**
* Returns the integer value of all values that can
* be parsed in the list.
@@ -207,7 +223,7 @@ public class Property
{
ArrayList<Integer> nums = new ArrayList<Integer>();
for (String value : valueList)
for (String value : values)
{
try
{
@@ -232,7 +248,7 @@ public class Property
*/
public boolean isIntList()
{
for (String value : valueList)
for (String value : values)
{
try
{
@@ -254,21 +270,21 @@ public class Property
*/
public boolean[] getBooleanList()
{
ArrayList<Boolean> values = new ArrayList<Boolean>();
for (String value : valueList)
ArrayList<Boolean> tmp = new ArrayList<Boolean>();
for (String value : values)
{
try
{
values.add(Boolean.parseBoolean(value));
tmp.add(Boolean.parseBoolean(value));
}
catch (NumberFormatException e){}
}
boolean[] primitives = new boolean[values.size()];
boolean[] primitives = new boolean[tmp.size()];
for (int i = 0; i < values.size(); i++)
for (int i = 0; i < tmp.size(); i++)
{
primitives[i] = values.get(i);
primitives[i] = tmp.get(i);
}
return primitives;
@@ -280,7 +296,7 @@ public class Property
*/
public boolean isBooleanList()
{
for (String value : valueList)
for (String value : values)
{
if (!"true".equalsIgnoreCase(value) && !"false".equalsIgnoreCase(value))
{
@@ -299,21 +315,21 @@ public class Property
*/
public double[] getDoubleList()
{
ArrayList<Double> values = new ArrayList<Double>();
for (String value : valueList)
ArrayList<Double> tmp = new ArrayList<Double>();
for (String value : values)
{
try
{
values.add(Double.parseDouble(value));
tmp.add(Double.parseDouble(value));
}
catch (NumberFormatException e) {}
}
double[] primitives = new double[values.size()];
double[] primitives = new double[tmp.size()];
for (int i = 0; i < values.size(); i++)
for (int i = 0; i < tmp.size(); i++)
{
primitives[i] = values.get(i);
primitives[i] = tmp.get(i);
}
return primitives;
@@ -325,7 +341,7 @@ public class Property
*/
public boolean isDoubleList()
{
for (String value : valueList)
for (String value : values)
{
try
{
@@ -371,4 +387,23 @@ public class Property
{
return isList;
}
public boolean hasChanged(){ return changed; }
void resetChangedState(){ changed = false; }
public void set(String value)
{
this.value = value;
changed = true;
}
public void set(String[] values)
{
this.values = values;
changed = true;
}
public void set(int value){ set(Integer.toString(value)); }
public void set(boolean value){ set(Boolean.toString(value)); }
public void set(double value){ set(Double.toString(value)); }
}