Initial Commit

This commit is contained in:
Chicken Bones committed 2013-07-16 17:58:02 +10:00
1 parent 397135f687
commit 0f32dd8620
82 files changed
+10693

No files matched your search

+153
View File
@@ -0,0 +1,153 @@
package codechicken.lib.config;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class ConfigFile extends ConfigTagParent
{
public ConfigFile(File file)
{
if(!file.exists())
{
try
{
file.createNewFile();
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
this.file = file;
newlinemode = 2;
loadConfig();
}
private void loadConfig()
{
loading = true;
BufferedReader reader;
try
{
reader = new BufferedReader(new FileReader(file));
while(true)
{
reader.mark(2000);
String line = reader.readLine();
if(line != null && line.startsWith("#"))
{
if(comment == null || comment.equals(""))
comment = line.substring(1);
else
comment = comment+"\n"+line.substring(1);
}
else
{
reader.reset();
break;
}
}
loadChildren(reader);
reader.close();
}
catch(IOException e)
{
throw new RuntimeException(e);
}
loading = false;
}
@Override
public ConfigFile setComment(String header)
{
super.setComment(header);
return this;
}
@Override
public ConfigFile setSortMode(int mode)
{
super.setSortMode(mode);
return this;
}
@Override
public String getNameQualifier()
{
return "";
}
public static String readLine(BufferedReader reader) throws IOException
{
String line = reader.readLine();
if(line != null)
return line.replace("\t", "");
return line;
}
public static String formatLine(String line)
{
line = line.replace("\t", "");
if(line.startsWith("#"))
{
return line;
}
else if(line.contains("="))
{
line = line.substring(0, line.indexOf("=")).replace(" ", "")+line.substring(line.indexOf("="));
return line;
}
else
{
line = line.replace(" ", "");
return line;
}
}
public static void writeLine(PrintWriter writer, String line, int tabs)
{
for(int i = 0; i < tabs; i++)
writer.print('\t');
writer.println(line);
}
public void saveConfig()
{
if(loading)
return;
PrintWriter writer;
try
{
writer = new PrintWriter(file);
}
catch(FileNotFoundException e)
{
throw new RuntimeException(e);
}
writeComment(writer, 0);
ConfigFile.writeLine(writer, "", 0);
saveTagTree(writer, 0, "");
writer.flush();
writer.close();
}
public boolean isLoading()
{
return loading;
}
public File file;
private boolean loading;
public static final byte[] lineend = new byte[]{0xD, 0xA};
}
+307
View File
@@ -0,0 +1,307 @@
package codechicken.lib.config;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import codechicken.lib.colour.Colour;
import codechicken.lib.colour.ColourRGBA;
public class ConfigTag extends ConfigTagParent
{
public ConfigTag(ConfigTagParent parent, String name)
{
this.parent = parent;
this.name = name;
qualifiedname = parent.getNameQualifier()+name;
newline = parent.newlinemode == 2;
parent.addChild(this);
}
@Override
public String getNameQualifier()
{
return qualifiedname+".";
}
@Override
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()
{
return this;
}
public void setValue(String value)
{
this.value = value;
saveConfig();
}
public void setDefaultValue(String defaultvalue)
{
if(value == null)
{
value = defaultvalue;
saveConfig();
}
}
public void setIntValue(int i)
{
setValue(Integer.toString(i));
}
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 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 String getValue()
{
return value;
}
public String getValue(String defaultvalue)
{
setDefaultValue(defaultvalue);
return value;
}
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 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();
}
}
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 void save(PrintWriter writer, int tabs, String bracequalifier, boolean first)
{
String vname;
if(qualifiedname.contains(".") && bracequalifier.length() > 0)
vname = qualifiedname.substring(bracequalifier.length() + 1).replace(' ', '_');
else
vname = qualifiedname;
vname.replace(' ', '_');
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)
ConfigFile.writeLine(writer, vname, tabs);
ConfigFile.writeLine(writer, "{", tabs);
saveTagTree(writer, tabs+1, qualifiedname.replace(' ', '_'));
ConfigFile.writeLine(writer, "}", tabs);
}
else
{
saveTagTree(writer, tabs, bracequalifier);
}
}
@Override
public ConfigTag setComment(String comment)
{
super.setComment(comment);
return this;
}
@Override
public ConfigTag setSortMode(int mode)
{
super.setSortMode(mode);
return this;
}
public ConfigTag setNewLine(boolean b)
{
newline = b;
saveConfig();
return this;
}
public ConfigTag useBraces()
{
brace = true;
if(parent.newlinemode == 1)
newline = true;
saveConfig();
return this;
}
public ConfigTag setPosition(int pos)
{
position = pos;
saveConfig();
return this;
}
public boolean containsTag(String tagname)
{
return getTag(tagname, false) != null;
}
public int getId(String name, int defaultValue)
{
return getTag(name).getIntValue(defaultValue);
}
public int getId(String name)
{
int ret = getId(name, IDBase);
IDBase = ret+1;
return ret;
}
public int getAcheivementId(String name, int defaultValue)
{
return getTag(name).getIntValue(defaultValue);
}
public ConfigTag setBaseID(int i)
{
IDBase = i;
return this;
}
public ConfigTagParent parent;
public String name;
public String qualifiedname;
public String value;
public boolean brace;
public boolean newline;
public int position = Integer.MAX_VALUE;
private int IDBase;
}
+241
View File
@@ -0,0 +1,241 @@
package codechicken.lib.config;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
public abstract class ConfigTagParent
{
public static class TagOrderComparator implements Comparator<ConfigTag>
{
int sortMode;
public TagOrderComparator(int sortMode)
{
this.sortMode = sortMode;
}
public int compare(ConfigTag o1, ConfigTag o2)
{
if(o1.position != o2.position)
return compareInt(o1.position, o2.position);
if(o1.brace != o2.brace)
return o1.brace ? 1 : -1;//braced one goes after
switch(sortMode)
{
case 1:
if(o1.value == o2.value)
return 0;
if(o1.value == null)
return 1;
if(o2.value == null)
return -1;
return o1.value.compareTo(o2.value);
default:
return o1.name.compareTo(o2.name);
}
}
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;
/**
* 0 = name, 1 = value
*/
public int sortMode = 0;
/**
* The mode for determining when child tags should leave a blank line between them and the one above
* 0 = never, 1 = when braced, 2 = always
*/
public int newlinemode = 1;
public abstract void saveConfig();
public abstract String getNameQualifier();
public ConfigTagParent setComment(String comment)
{
this.comment = comment;
saveConfig();
return this;
}
public ConfigTagParent setSortMode(int mode)
{
sortMode = mode;
saveConfig();
return this;
}
public ConfigTagParent setNewLineMode(int mode)
{
newlinemode = mode;
for(Entry<String, ConfigTag> entry : childtags.entrySet())
{
ConfigTag tag = entry.getValue();
if(newlinemode == 0)
tag.newline = false;
else if(newlinemode == 1)
tag.newline = tag.brace;
else if(newlinemode == 2)
tag.newline = true;
}
saveConfig();
return this;
}
public Map<String, ConfigTag> childTagMap()
{
return childtags;
}
public boolean hasChildTags()
{
return !childtags.isEmpty();
}
public boolean containsTag(String tagname)
{
return getTag(tagname, false) != null;
}
public ConfigTag getNewTag(String tagname)
{
return new ConfigTag(this, tagname);
}
public ConfigTag getTag(String tagname, boolean create)
{
tagname = tagname.replace('_', ' ');
int dotpos = tagname.indexOf(".");
String basetagname = dotpos == -1 ? tagname : tagname.substring(0, dotpos);
ConfigTag basetag = childtags.get(basetagname);
if(basetag == null)
{
if(!create)
return null;
basetag = getNewTag(basetagname);
saveConfig();
}
if(dotpos == -1)
return basetag;
return basetag.getTag(tagname.substring(dotpos+1), create);
}
public ConfigTag getTag(String tagname)
{
return getTag(tagname, true);
}
public boolean removeTag(String tagname)
{
ConfigTag tag = getTag(tagname, false);
if(tag == null)
return false;
int dotpos = tagname.lastIndexOf(".");
String lastpart = dotpos == -1 ? tagname : tagname.substring(dotpos+1, tagname.length());
if(tag.parent != null)
return tag.parent.childtags.remove(lastpart) != null;
return false;
}
public void addChild(ConfigTag tag)
{
childtags.put(tag.name, tag);
}
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());
Collections.sort(taglist, new TagOrderComparator(sortMode));
return taglist;
}
public void loadChildren(BufferedReader reader)
{
String comment = "";
String bracequalifier="";
try
{
while(true)
{
String line = ConfigFile.readLine(reader);
if(line == null)
break;
if(line.startsWith("#"))
{
if(comment == null || comment.equals(""))
comment = line.substring(1);
else
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));
comment = "";
bracequalifier = qualifiedname;
}
else if(line.equals("{"))
{
getTag(bracequalifier).setComment(comment).useBraces().loadChildren(reader);
comment = "";
bracequalifier = "";
}
else if(line.equals("}"))
{
break;
}
else
{
bracequalifier = line;
}
}
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
public void saveTagTree(PrintWriter writer, int tabs, String bracequalifier)
{
boolean first = true;
for(ConfigTag tag : getSortedTagList())
{
tag.save(writer, tabs, bracequalifier, first);
first = false;
}
}
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);
}
}
}
@@ -0,0 +1,150 @@
package codechicken.lib.config;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map.Entry;
public class SimpleProperties
{
public HashMap<String, String> propertyMap = new HashMap<String, String>();
public File propertyFile;
public boolean saveOnChange = false;
public String encoding;
private boolean loading = false;
public SimpleProperties(File file, boolean saveOnChange, String encoding)
{
propertyFile = file;
this.saveOnChange = saveOnChange;
this.encoding = encoding;
}
public SimpleProperties(File file, boolean saveOnChange)
{
this(file, saveOnChange, Charset.defaultCharset().name());
}
public SimpleProperties(File file)
{
this(file, true);
}
public void load()
{
clear();
loading = true;
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(propertyFile), encoding));
while(true)
{
String read = reader.readLine();
if(read == null)
break;
int equalIndex = read.indexOf('=');
if(equalIndex == -1)
continue;
setProperty(read.substring(0, equalIndex), read.substring(equalIndex+1));
}
reader.close();
}
catch(Exception e)
{
throw new RuntimeException(e);
}
loading = false;
}
public void save()
{
try
{
PrintStream writer = new PrintStream(propertyFile);
for(Entry<String, String> entry : propertyMap.entrySet())
{
writer.println(entry.getKey()+"="+entry.getValue());
}
writer.close();
}
catch(Exception e)
{
throw new RuntimeException(e);
}
}
public void clear()
{
propertyMap.clear();
}
public boolean hasProperty(String key)
{
return propertyMap.containsKey(key);
}
public void removeProperty(String key)
{
if(propertyMap.remove(key) != null && saveOnChange && !loading)
save();
}
public void setProperty(String key, int value)
{
setProperty(key, Integer.toString(value));
}
public void setProperty(String key, boolean value)
{
setProperty(key, Boolean.toString(value));
}
public void setProperty(String key, String value)
{
propertyMap.put(key, value);
if(saveOnChange && !loading)
save();
}
public int getProperty(String property, int defaultvalue)
{
try {
return Integer.parseInt(getProperty(property, Integer.toString(defaultvalue)));
} catch(NumberFormatException nfe)
{return defaultvalue;}
}
public boolean getProperty(String property, boolean defaultvalue)
{
try {
return Boolean.parseBoolean(getProperty(property, Boolean.toString(defaultvalue)));
} catch(NumberFormatException nfe)
{return defaultvalue;}
}
public String getProperty(String property, String defaultvalue)
{
String value = propertyMap.get(property);
if(value == null)
{
setProperty(property, defaultvalue);
return defaultvalue;
}
return value;
}
public String getProperty(String property)
{
return propertyMap.get(property);
}
}