2013-07-16 17:58:02 +10:00
|
|
|
package codechicken.lib.config;
|
|
|
|
|
|
2014-04-09 13:38:05 +10:00
|
|
|
import java.io.*;
|
2013-07-16 17:58:02 +10:00
|
|
|
|
|
|
|
|
public class ConfigFile extends ConfigTagParent
|
2014-04-07 14:02:47 +10:00
|
|
|
{
|
2014-04-28 19:40:13 +10:00
|
|
|
public static final byte[] crlf = new byte[]{0xD, 0xA};
|
|
|
|
|
|
|
|
|
|
public File file;
|
|
|
|
|
private boolean loading;
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public ConfigFile(File file) {
|
2014-04-28 19:40:13 +10:00
|
|
|
newlinemode = 2;
|
|
|
|
|
load(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected ConfigFile() {}
|
|
|
|
|
|
|
|
|
|
protected void load(File file) {
|
2014-04-07 14:02:47 +10:00
|
|
|
try {
|
|
|
|
|
if(!file.getParentFile().exists())
|
|
|
|
|
file.getParentFile().mkdirs();
|
|
|
|
|
if (!file.exists())
|
2013-07-16 17:58:02 +10:00
|
|
|
file.createNewFile();
|
2014-04-07 14:02:47 +10:00
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
|
|
|
|
this.file = file;
|
|
|
|
|
loadConfig();
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
2014-04-28 19:40:13 +10:00
|
|
|
protected void loadConfig() {
|
2013-07-16 17:58:02 +10:00
|
|
|
loading = true;
|
|
|
|
|
BufferedReader reader;
|
2014-04-07 14:02:47 +10:00
|
|
|
try {
|
2013-07-16 17:58:02 +10:00
|
|
|
reader = new BufferedReader(new FileReader(file));
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
while (true) {
|
2013-07-16 17:58:02 +10:00
|
|
|
reader.mark(2000);
|
|
|
|
|
String line = reader.readLine();
|
2014-04-07 14:02:47 +10:00
|
|
|
if (line != null && line.startsWith("#")) {
|
|
|
|
|
if (comment == null || comment.equals(""))
|
2013-07-16 17:58:02 +10:00
|
|
|
comment = line.substring(1);
|
|
|
|
|
else
|
2014-04-07 14:02:47 +10:00
|
|
|
comment = comment + "\n" + line.substring(1);
|
|
|
|
|
} else {
|
2013-07-16 17:58:02 +10:00
|
|
|
reader.reset();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
loadChildren(reader);
|
|
|
|
|
reader.close();
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
2013-07-16 17:58:02 +10:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
2013-07-16 17:58:02 +10:00
|
|
|
loading = false;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
2013-07-16 17:58:02 +10:00
|
|
|
@Override
|
2014-04-07 14:02:47 +10:00
|
|
|
public ConfigFile setComment(String header) {
|
2013-07-16 17:58:02 +10:00
|
|
|
super.setComment(header);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
2013-07-16 17:58:02 +10:00
|
|
|
@Override
|
2014-04-07 14:02:47 +10:00
|
|
|
public ConfigFile setSortMode(int mode) {
|
2013-07-16 17:58:02 +10:00
|
|
|
super.setSortMode(mode);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
2013-07-16 17:58:02 +10:00
|
|
|
@Override
|
2014-04-07 14:02:47 +10:00
|
|
|
public String getNameQualifier() {
|
2013-07-16 17:58:02 +10:00
|
|
|
return "";
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public static String readLine(BufferedReader reader) throws IOException {
|
2013-07-16 17:58:02 +10:00
|
|
|
String line = reader.readLine();
|
2014-04-28 19:40:13 +10:00
|
|
|
return line == null ? null : line.replace("\t", "");
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public static void writeLine(PrintWriter writer, String line, int tabs) {
|
|
|
|
|
for (int i = 0; i < tabs; i++)
|
2013-07-16 17:58:02 +10:00
|
|
|
writer.print('\t');
|
2014-04-07 14:02:47 +10:00
|
|
|
|
2013-07-16 17:58:02 +10:00
|
|
|
writer.println(line);
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public void saveConfig() {
|
|
|
|
|
if (loading)
|
2013-07-16 17:58:02 +10:00
|
|
|
return;
|
2014-04-07 14:02:47 +10:00
|
|
|
|
2013-07-16 17:58:02 +10:00
|
|
|
PrintWriter writer;
|
2014-04-07 14:02:47 +10:00
|
|
|
try {
|
2013-07-16 17:58:02 +10:00
|
|
|
writer = new PrintWriter(file);
|
2014-04-07 14:02:47 +10:00
|
|
|
} catch (FileNotFoundException e) {
|
2013-07-16 17:58:02 +10:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
2013-07-16 17:58:02 +10:00
|
|
|
writeComment(writer, 0);
|
|
|
|
|
ConfigFile.writeLine(writer, "", 0);
|
|
|
|
|
saveTagTree(writer, 0, "");
|
|
|
|
|
writer.flush();
|
|
|
|
|
writer.close();
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public boolean isLoading() {
|
2013-07-16 17:58:02 +10:00
|
|
|
return loading;
|
|
|
|
|
}
|
|
|
|
|
}
|