Keep class references of config package within config so it can be used at the ASM level

Add simple extensible config serialisation
This commit is contained in:
Chicken-Bomes committed 2014-08-05 18:00:41 +10:00
1 parent 76a6c9e7d5
commit 3b081ed3cd
3 files changed
+244 -320

No files matched your search

+30
View File
@@ -1,13 +1,43 @@
package codechicken.lib.colour;
import codechicken.lib.config.ConfigTag.IConfigType;
import codechicken.lib.math.MathHelper;
import codechicken.lib.util.Copyable;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import org.lwjgl.opengl.GL11;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class Colour implements Copyable<Colour>
{
public static IConfigType<Colour> configRGB = new IConfigType<Colour>()
{
@Override
public String configValue(Colour entry) {
String s = Long.toString(((long) entry.rgb()) << 32 >>> 32, 16);
while (s.length() < 6)
s = "0" + s;
return "0x" + s.toUpperCase();
}
private final Pattern patternRGB = Pattern.compile("(\\d+),(\\d+),(\\d+)");
@Override
public Colour valueOf(String text) throws Exception {
Matcher matcherRGB = patternRGB.matcher(text.replaceAll("\\s", ""));
if(matcherRGB.matches())
return new ColourRGBA(
Integer.parseInt(matcherRGB.group(1)),
Integer.parseInt(matcherRGB.group(2)),
Integer.parseInt(matcherRGB.group(3)),
0xFF);
int hex = (int) Long.parseLong(text.replace("0x", ""), 16);
return new ColourRGBA(hex<<8|0xFF);
}
};
public byte r;
public byte g;
public byte b;
+135 -204
View File
@@ -1,299 +1,230 @@
package codechicken.lib.config;
import codechicken.lib.colour.Colour;
import codechicken.lib.colour.ColourRGBA;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ConfigTag extends ConfigTagParent
{
public ConfigTag(ConfigTagParent parent, String name)
{
public interface IConfigType<T>
{
public String configValue(T entry);
public T valueOf(String text) throws Exception;
}
public ConfigTag(ConfigTagParent parent, String name) {
this.parent = parent;
this.name = name;
qualifiedname = parent.getNameQualifier()+name;
qualifiedname = parent.getNameQualifier() + name;
newline = parent.newlinemode == 2;
parent.addChild(this);
}
@Override
public String getNameQualifier()
{
return qualifiedname+".";
public String getNameQualifier() {
return qualifiedname + ".";
}
@Override
public void saveConfig()
{
public void saveConfig() {
parent.saveConfig();
}
/**
* Called when the tag is loaded from a config file as opposed to constructed by a mod
*
* @return this
*/
public ConfigTag onLoaded()
{
public ConfigTag onLoaded() {
return this;
}
public void setValue(String value)
{
public void setValue(String value) {
this.value = value;
saveConfig();
}
public void setDefaultValue(String defaultvalue)
{
if(value == null)
{
value = defaultvalue;
public void setDefaultValue(String defaultValue) {
if (value == null) {
value = defaultValue;
saveConfig();
}
}
public void setIntValue(int i)
{
public void setIntValue(int i) {
setValue(Integer.toString(i));
}
public void setBooleanValue(boolean b)
{
public void setBooleanValue(boolean b) {
setValue(Boolean.toString(b));
}
public void setHexValue(int i)
{
setValue("0x"+Long.toString(((long)i) << 32 >>> 32, 16));
public void setHexValue(int i) {
setValue("0x" + Long.toString(((long) i) << 32 >>> 32, 16));
}
public void setColourRGB(Colour c)
{
String s = Long.toString(((long)c.rgb()) << 32 >>> 32, 16);
while(s.length() < 6)
s = "0"+s;
setValue("0x"+s.toUpperCase());
public <T> void set(IConfigType<T> type, T entry) {
setValue(type.configValue(entry));
}
public String getValue()
{
public String getValue() {
return value;
}
public String getValue(String defaultvalue)
{
setDefaultValue(defaultvalue);
public String getValue(String defaultValue) {
setDefaultValue(defaultValue);
return value;
}
public int getIntValue()
{
public int getIntValue() {
return Integer.parseInt(getValue());
}
public int getIntValue(int defaultvalue)
{
if(value == null)
{
setIntValue(defaultvalue);
}
try
{
return getIntValue();
}
catch(NumberFormatException nfe)
{
setIntValue(defaultvalue);
return getIntValue();
}
}
public boolean getBooleanValue()
{
String value = getValue();
if(value != null && (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")))
{
return true;
}
else if(value != null && (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no")))
{
return false;
}
throw new NumberFormatException(qualifiedname+".value="+value);
}
public boolean getBooleanValue(boolean defaultvalue)
{
if(value == null)
{
setBooleanValue(defaultvalue);
}
try
{
return getBooleanValue();
}
catch(NumberFormatException nfe)
{
setBooleanValue(defaultvalue);
return getBooleanValue();
}
public int getIntValue(int defaultValue) {
try {
if (value != null)
return getIntValue();
} catch (NumberFormatException ignored) {}
setIntValue(defaultValue);
return defaultValue;
}
public int getHexValue()
{
public boolean getBooleanValue() {
String value = getValue();
if (value != null && (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")))
return true;
else if (value != null && (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no")))
return false;
throw new NumberFormatException(qualifiedname + ".value=" + value);
}
public boolean getBooleanValue(boolean defaultValue) {
try {
if (value != null)
return getBooleanValue();
} catch (NumberFormatException ignored) {}
setBooleanValue(defaultValue);
return defaultValue;
}
public int getHexValue() {
return (int) Long.parseLong(getValue().replace("0x", ""), 16);
}
public int getHexValue(int defaultvalue)
{
if(value == null)
{
setHexValue(defaultvalue);
}
try
{
return getHexValue();
}
catch(NumberFormatException nfe)
{
setHexValue(defaultvalue);
return getHexValue();
}
public int getHexValue(int defaultValue) {
try {
if (value != null)
return getHexValue();
} catch (NumberFormatException ignored) {}
setHexValue(defaultValue);
return defaultValue;
}
private static final Pattern patternRGB = Pattern.compile("(\\d+),(\\d+),(\\d+)");
public Colour getColourRGB()
{
Matcher matcherRGB = patternRGB.matcher(getValue().replaceAll("\\s", ""));
if(matcherRGB.matches())
{
return new ColourRGBA(
Integer.parseInt(matcherRGB.group(1)),
Integer.parseInt(matcherRGB.group(2)),
Integer.parseInt(matcherRGB.group(3)),
0xFF);
}
return new ColourRGBA(getHexValue()<<8|0xFF);
}
public Colour getColourRGB(Colour defaultvalue)
{
if(value == null)
{
setColourRGB(defaultvalue);
}
try
{
return getColourRGB();
}
catch(NumberFormatException nfe)
{
setColourRGB(defaultvalue);
return getColourRGB();
public <T> T get(IConfigType<T> type) {
try {
return type.valueOf(getValue());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void save(PrintWriter writer, int tabs, String bracequalifier, boolean first)
{
public <T> T get(IConfigType<T> type, T defaultValue) {
try {
if (value != null)
return get(type);
} catch (Exception ignored) {}
set(type, defaultValue);
return defaultValue;
}
public void save(PrintWriter writer, int tabs, String bracequalifier, boolean first) {
String vname;
if(qualifiedname.contains(".") && bracequalifier.length() > 0)
if (qualifiedname.contains(".") && bracequalifier.length() > 0)
vname = qualifiedname.substring(bracequalifier.length() + 1);
else
vname = qualifiedname;
if(newline && !first)
ConfigFile.writeLine(writer, "", tabs);
writeComment(writer, tabs);
if(value != null)
ConfigFile.writeLine(writer, vname+"="+value, tabs);
if(!hasChildTags())
if (newline && !first)
ConfigFile.writeLine(writer, "", tabs);
writeComment(writer, tabs);
if (value != null)
ConfigFile.writeLine(writer, vname + "=" + value, tabs);
if (!hasChildTags())
return;
if(brace)
{
if(value == null)
if (brace) {
if (value == null)
ConfigFile.writeLine(writer, vname, tabs);
ConfigFile.writeLine(writer, "{", tabs);
saveTagTree(writer, tabs+1, qualifiedname);
saveTagTree(writer, tabs + 1, qualifiedname);
ConfigFile.writeLine(writer, "}", tabs);
}
else
{
} else {
saveTagTree(writer, tabs, bracequalifier);
}
}
}
@Override
public ConfigTag setComment(String comment)
{
public ConfigTag setComment(String comment) {
super.setComment(comment);
return this;
}
@Override
public ConfigTag setSortMode(int mode)
{
public ConfigTag setSortMode(int mode) {
super.setSortMode(mode);
return this;
}
public ConfigTag setNewLine(boolean b)
{
public ConfigTag setNewLine(boolean b) {
newline = b;
saveConfig();
return this;
}
public ConfigTag useBraces()
{
public ConfigTag useBraces() {
brace = true;
if(parent.newlinemode == 1)
if (parent.newlinemode == 1)
newline = true;
saveConfig();
return this;
}
public ConfigTag setPosition(int pos)
{
public ConfigTag setPosition(int pos) {
position = pos;
saveConfig();
return this;
}
public boolean containsTag(String tagname)
{
public boolean containsTag(String tagname) {
return getTag(tagname, false) != null;
}
public int getId(String name, int defaultValue)
{
public int getId(String name, int defaultValue) {
return getTag(name).getIntValue(defaultValue);
}
public int getId(String name)
{
public int getId(String name) {
int ret = getId(name, IDBase);
IDBase = ret+1;
IDBase = ret + 1;
return ret;
}
public int getAcheivementId(String name, int defaultValue)
{
public int getAcheivementId(String name, int defaultValue) {
return getTag(name).getIntValue(defaultValue);
}
public ConfigTag setBaseID(int i)
{
public ConfigTag setBaseID(int i) {
IDBase = i;
return this;
}
public ConfigTagParent parent;
public String name;
public String qualifiedname;
@@ -301,6 +232,6 @@ public class ConfigTag extends ConfigTagParent
public boolean brace;
public boolean newline;
public int position = Integer.MAX_VALUE;
private int IDBase;
}
+79 -116
View File
@@ -11,26 +11,23 @@ public abstract class ConfigTagParent
public static class TagOrderComparator implements Comparator<ConfigTag>
{
int sortMode;
public TagOrderComparator(int sortMode)
{
public TagOrderComparator(int sortMode) {
this.sortMode = sortMode;
}
public int compare(ConfigTag o1, ConfigTag o2)
{
if(o1.position != o2.position)
public int compare(ConfigTag o1, ConfigTag o2) {
if (o1.position != o2.position)
return compareInt(o1.position, o2.position);
if(o1.brace != o2.brace)
if (o1.brace != o2.brace)
return o1.brace ? 1 : -1;//braced one goes after
switch(sortMode)
{
switch (sortMode) {
case 1:
if(o1.value == o2.value)
if (o1.value == o2.value)
return 0;
if(o1.value == null)
if (o1.value == null)
return 1;
if(o2.value == null)
if (o2.value == null)
return -1;
return o1.value.compareTo(o2.value);
default:
@@ -38,12 +35,11 @@ public abstract class ConfigTagParent
}
}
private int compareInt(int a, int b)
{
private int compareInt(int a, int b) {
return a == b ? 0 : a < b ? -1 : 1;
}
}
private TreeMap<String, ConfigTag> childtags = new TreeMap<String, ConfigTag>();
public String comment;
/**
@@ -55,187 +51,154 @@ public abstract class ConfigTagParent
* 0 = never, 1 = when braced, 2 = always
*/
public int newlinemode = 1;
public abstract void saveConfig();
public abstract String getNameQualifier();
public ConfigTagParent setComment(String comment)
{
public ConfigTagParent setComment(String comment) {
this.comment = comment;
saveConfig();
return this;
}
public ConfigTagParent setSortMode(int mode)
{
public ConfigTagParent setSortMode(int mode) {
sortMode = mode;
saveConfig();
return this;
}
public ConfigTagParent setNewLineMode(int mode)
{
public ConfigTagParent setNewLineMode(int mode) {
newlinemode = mode;
for(Entry<String, ConfigTag> entry : childtags.entrySet())
{
for (Entry<String, ConfigTag> entry : childtags.entrySet()) {
ConfigTag tag = entry.getValue();
if(newlinemode == 0)
if (newlinemode == 0)
tag.newline = false;
else if(newlinemode == 1)
else if (newlinemode == 1)
tag.newline = tag.brace;
else if(newlinemode == 2)
else if (newlinemode == 2)
tag.newline = true;
}
saveConfig();
return this;
}
public Map<String, ConfigTag> childTagMap()
{
public Map<String, ConfigTag> childTagMap() {
return childtags;
}
public boolean hasChildTags()
{
public boolean hasChildTags() {
return !childtags.isEmpty();
}
public boolean containsTag(String tagname)
{
public boolean containsTag(String tagname) {
return getTag(tagname, false) != null;
}
public ConfigTag getNewTag(String tagname)
{
public ConfigTag getNewTag(String tagname) {
return new ConfigTag(this, tagname);
}
public ConfigTag getTag(String tagname, boolean create)
{
public ConfigTag getTag(String tagname, boolean create) {
int dotpos = tagname.indexOf(".");
String basetagname = dotpos == -1 ? tagname : tagname.substring(0, dotpos);
ConfigTag basetag = childtags.get(basetagname);
if(basetag == null)
{
if(!create)
if (basetag == null) {
if (!create)
return null;
basetag = getNewTag(basetagname);
saveConfig();
}
if(dotpos == -1)
if (dotpos == -1)
return basetag;
return basetag.getTag(tagname.substring(dotpos+1), create);
return basetag.getTag(tagname.substring(dotpos + 1), create);
}
public ConfigTag getTag(String tagname)
{
public ConfigTag getTag(String tagname) {
return getTag(tagname, true);
}
public boolean removeTag(String tagname)
{
public boolean removeTag(String tagname) {
ConfigTag tag = getTag(tagname, false);
if(tag == null)
if (tag == null)
return false;
int dotpos = tagname.lastIndexOf(".");
String lastpart = dotpos == -1 ? tagname : tagname.substring(dotpos+1, tagname.length());
if(tag.parent != null)
{
String lastpart = dotpos == -1 ? tagname : tagname.substring(dotpos + 1, tagname.length());
if (tag.parent != null) {
boolean ret = tag.parent.childtags.remove(lastpart) != null;
if(ret)
if (ret)
saveConfig();
return ret;
}
return false;
}
public void addChild(ConfigTag tag)
{
public void addChild(ConfigTag tag) {
childtags.put(tag.name, tag);
}
public <T extends ConfigTag> ArrayList<T> getSortedTagList()
{
public <T extends ConfigTag> ArrayList<T> getSortedTagList() {
ArrayList<T> taglist = new ArrayList<T>(childtags.size());
for(Entry<String, ConfigTag> tag : childtags.entrySet())
taglist.add((T)tag.getValue());
for (Entry<String, ConfigTag> tag : childtags.entrySet())
taglist.add((T) tag.getValue());
Collections.sort(taglist, new TagOrderComparator(sortMode));
return taglist;
}
public void loadChildren(BufferedReader reader)
{
public void loadChildren(BufferedReader reader) {
String comment = "";
String bracequalifier="";
try
{
while(true)
{
String bracequalifier = "";
try {
while (true) {
String line = ConfigFile.readLine(reader);
if(line == null)
if (line == null)
break;
if(line.startsWith("#"))
{
if(comment.equals(""))
if (line.startsWith("#")) {
if (comment.equals(""))
comment = line.substring(1);
else
comment = comment+"\n"+line.substring(1);
}
else if(line.contains("="))
{
comment = comment + "\n" + line.substring(1);
} else if (line.contains("=")) {
String qualifiedname = line.substring(0, line.indexOf("="));
getTag(qualifiedname)
.onLoaded()
.setComment(comment)
.setValue(line.substring(line.indexOf("=")+1));
.onLoaded()
.setComment(comment)
.setValue(line.substring(line.indexOf("=") + 1));
comment = "";
bracequalifier = qualifiedname;
}
else if(line.equals("{"))
{
} else if (line.equals("{")) {
getTag(bracequalifier).setComment(comment).useBraces().loadChildren(reader);
comment = "";
bracequalifier = "";
}
else if(line.equals("}"))
{
} else if (line.equals("}")) {
break;
}
else
{
} else {
bracequalifier = line;
}
}
}
catch(IOException e)
{
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void saveTagTree(PrintWriter writer, int tabs, String bracequalifier)
{
public void saveTagTree(PrintWriter writer, int tabs, String bracequalifier) {
boolean first = true;
for(ConfigTag tag : getSortedTagList())
{
for (ConfigTag tag : getSortedTagList()) {
tag.save(writer, tabs, bracequalifier, first);
first = false;
}
}
public void writeComment(PrintWriter writer, int tabs)
{
if(comment != null && !comment.equals(""))
{
public void writeComment(PrintWriter writer, int tabs) {
if (comment != null && !comment.equals("")) {
String[] comments = comment.split("\n");
for(int i = 0; i < comments.length; i++)
ConfigFile.writeLine(writer, "#"+comments[i], tabs);
for (int i = 0; i < comments.length; i++)
ConfigFile.writeLine(writer, "#" + comments[i], tabs);
}
}
}