Implemented extremely simple (and incomplete) YAML parser to no longer

depend on external libraries like SnakeYAML.
This commit is contained in:
Evenprime 2011-05-14 17:54:11 +02:00
parent 29361d0867
commit c6aa6b213b
4 changed files with 191 additions and 44 deletions

3
manifest Normal file
View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: cc.co.evenprime.bukkit.nocheat.Main
Class-Path: snakeyaml.jar

View File

@ -8,17 +8,17 @@ import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.logging.FileHandler; import java.util.logging.FileHandler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.util.config.Configuration;
import cc.co.evenprime.bukkit.nocheat.ConfigurationException; import cc.co.evenprime.bukkit.nocheat.ConfigurationException;
import cc.co.evenprime.bukkit.nocheat.actions.Action; import cc.co.evenprime.bukkit.nocheat.actions.Action;
import cc.co.evenprime.bukkit.nocheat.actions.CancelAction; import cc.co.evenprime.bukkit.nocheat.actions.CancelAction;
import cc.co.evenprime.bukkit.nocheat.actions.LogAction; import cc.co.evenprime.bukkit.nocheat.actions.LogAction;
import cc.co.evenprime.bukkit.nocheat.config.LevelOption.LogLevel; import cc.co.evenprime.bukkit.nocheat.config.LevelOption.LogLevel;
import cc.co.evenprime.bukkit.nocheat.yaml.SimpleYaml;
/** /**
* Central location for everything that's described in the configuration file * Central location for everything that's described in the configuration file
@ -35,6 +35,8 @@ public class NoCheatConfiguration {
private Map<String, Action> actionMap = new HashMap<String,Action>(); private Map<String, Action> actionMap = new HashMap<String,Action>();
private Map<String, Object> yamlContent = new HashMap<String, Object>();
// Our personal logger // Our personal logger
private final static String loggerName = "cc.co.evenprime.nocheat"; private final static String loggerName = "cc.co.evenprime.nocheat";
public final Logger logger = Logger.getLogger(loggerName); public final Logger logger = Logger.getLogger(loggerName);
@ -54,8 +56,13 @@ public class NoCheatConfiguration {
*/ */
public void config(File configurationFile) { public void config(File configurationFile) {
Configuration CONFIG = new Configuration(configurationFile);
CONFIG.load(); try {
yamlContent = (Map<String, Object>) SimpleYaml.read(configurationFile);
} catch (Exception e) {
yamlContent = new HashMap<String, Object>();
}
root = new ParentOption(""); root = new ParentOption("");
@ -66,19 +73,19 @@ public class NoCheatConfiguration {
root.add(loggingNode); root.add(loggingNode);
loggingNode.add(new MediumStringOption("filename", loggingNode.add(new MediumStringOption("filename",
CONFIG.getString("logging.filename", "plugins/NoCheat/nocheat.log"))); SimpleYaml.getString("logging.filename", "plugins/NoCheat/nocheat.log", yamlContent)));
loggingNode.add(new LevelOption("logtofile", loggingNode.add(new LevelOption("logtofile",
LogLevel.getLogLevelFromString(CONFIG.getString("logging.logtofile", LogLevel.LOW.asString())))); LogLevel.getLogLevelFromString(SimpleYaml.getString("logging.logtofile", LogLevel.LOW.asString(), yamlContent))));
loggingNode.add(new LevelOption("logtoconsole", loggingNode.add(new LevelOption("logtoconsole",
LogLevel.getLogLevelFromString(CONFIG.getString("logging.logtoconsole", LogLevel.HIGH.asString())))); LogLevel.getLogLevelFromString(SimpleYaml.getString("logging.logtoconsole", LogLevel.HIGH.asString(), yamlContent))));
loggingNode.add(new LevelOption("logtochat", loggingNode.add(new LevelOption("logtochat",
LogLevel.getLogLevelFromString(CONFIG.getString("logging.logtochat", LogLevel.MED.asString())))); LogLevel.getLogLevelFromString(SimpleYaml.getString("logging.logtochat", LogLevel.MED.asString(), yamlContent))));
loggingNode.add(new LevelOption("logtoirc", loggingNode.add(new LevelOption("logtoirc",
LogLevel.getLogLevelFromString(CONFIG.getString("logging.logtoirc", LogLevel.MED.asString())))); LogLevel.getLogLevelFromString(SimpleYaml.getString("logging.logtoirc", LogLevel.MED.asString(), yamlContent))));
loggingNode.add(new ShortStringOption("logtoirctag", loggingNode.add(new ShortStringOption("logtoirctag",
CONFIG.getString("logging.logtoirctag", "nocheat"))); SimpleYaml.getString("logging.logtoirctag", "nocheat", yamlContent)));
} }
/*** ACTIVE section ***/ /*** ACTIVE section ***/
@ -87,17 +94,17 @@ public class NoCheatConfiguration {
root.add(activeNode); root.add(activeNode);
activeNode.add(new BooleanOption("speedhack", activeNode.add(new BooleanOption("speedhack",
CONFIG.getBoolean("active.speedhack", true))); SimpleYaml.getBoolean("active.speedhack", true, yamlContent)));
activeNode.add(new BooleanOption("moving", activeNode.add(new BooleanOption("moving",
CONFIG.getBoolean("active.moving", true))); SimpleYaml.getBoolean("active.moving", true, yamlContent)));
activeNode.add(new BooleanOption("airbuild", activeNode.add(new BooleanOption("airbuild",
CONFIG.getBoolean("active.airbuild", false))); SimpleYaml.getBoolean("active.airbuild", false, yamlContent)));
activeNode.add(new BooleanOption("bedteleport", activeNode.add(new BooleanOption("bedteleport",
CONFIG.getBoolean("active.bedteleport", true))); SimpleYaml.getBoolean("active.bedteleport", true, yamlContent)));
activeNode.add(new BooleanOption("itemdupe", activeNode.add(new BooleanOption("itemdupe",
CONFIG.getBoolean("active.itemdupe", true))); SimpleYaml.getBoolean("active.itemdupe", true, yamlContent)));
activeNode.add(new BooleanOption("bogusitems", activeNode.add(new BooleanOption("bogusitems",
CONFIG.getBoolean("active.bogusitems", false))); SimpleYaml.getBoolean("active.bogusitems", false, yamlContent)));
} }
/*** SPEEDHACK section ***/ /*** SPEEDHACK section ***/
@ -106,7 +113,7 @@ public class NoCheatConfiguration {
root.add(speedhackNode); root.add(speedhackNode);
speedhackNode.add(new LongStringOption("logmessage", speedhackNode.add(new LongStringOption("logmessage",
CONFIG.getString("logging.filename", "%1$s sent %2$d move events, but only %3$d were allowed. Speedhack?"))); SimpleYaml.getString("logging.filename", "%1$s sent %2$d move events, but only %3$d were allowed. Speedhack?", yamlContent)));
/*** SPEEDHACK LIMITS section ***/ /*** SPEEDHACK LIMITS section ***/
{ {
@ -114,11 +121,11 @@ public class NoCheatConfiguration {
speedhackNode.add(speedhackLimitsNode); speedhackNode.add(speedhackLimitsNode);
speedhackLimitsNode.add(new IntegerOption("low", speedhackLimitsNode.add(new IntegerOption("low",
CONFIG.getInt("speedhack.limits.low", 30))); SimpleYaml.getInt("speedhack.limits.low", 30, yamlContent)));
speedhackLimitsNode.add(new IntegerOption("med", speedhackLimitsNode.add(new IntegerOption("med",
CONFIG.getInt("speedhack.limits.med", 45))); SimpleYaml.getInt("speedhack.limits.med", 45, yamlContent)));
speedhackLimitsNode.add(new IntegerOption("high", speedhackLimitsNode.add(new IntegerOption("high",
CONFIG.getInt("speedhack.limits.high", 60))); SimpleYaml.getInt("speedhack.limits.high", 60, yamlContent)));
} }
/*** SPEEDHACK ACTIONS section ***/ /*** SPEEDHACK ACTIONS section ***/
@ -127,11 +134,11 @@ public class NoCheatConfiguration {
speedhackNode.add(speedhackActionNode); speedhackNode.add(speedhackActionNode);
speedhackActionNode.add(new MediumStringOption("low", speedhackActionNode.add(new MediumStringOption("low",
CONFIG.getString("speedhack.action.low", "loglow cancel"))); SimpleYaml.getString("speedhack.action.low", "loglow cancel", yamlContent)));
speedhackActionNode.add(new MediumStringOption("med", speedhackActionNode.add(new MediumStringOption("med",
CONFIG.getString("speedhack.action.med", "logmed cancel"))); SimpleYaml.getString("speedhack.action.med", "logmed cancel", yamlContent)));
speedhackActionNode.add(new MediumStringOption("high", speedhackActionNode.add(new MediumStringOption("high",
CONFIG.getString("speedhack.action.high", "loghigh cancel"))); SimpleYaml.getString("speedhack.action.high", "loghigh cancel", yamlContent)));
} }
} }
@ -141,13 +148,13 @@ public class NoCheatConfiguration {
root.add(movingNode); root.add(movingNode);
movingNode.add(new LongStringOption("logmessage", movingNode.add(new LongStringOption("logmessage",
CONFIG.getString("moving.logmessage", "Moving violation: %1$s from %2$s (%4$.1f, %5$.1f, %6$.1f) to %3$s (%7$.1f, %8$.1f, %9$.1f)"))); SimpleYaml.getString("moving.logmessage", "Moving violation: %1$s from %2$s (%4$.1f, %5$.1f, %6$.1f) to %3$s (%7$.1f, %8$.1f, %9$.1f)", yamlContent)));
movingNode.add(new LongStringOption("summarymessage", movingNode.add(new LongStringOption("summarymessage",
CONFIG.getString("moving.summarymessage", "Moving summary of last ~%2$d seconds: %1$s total Violations: (%3$d,%4$d,%5$d)"))); SimpleYaml.getString("moving.summarymessage", "Moving summary of last ~%2$d seconds: %1$s total Violations: (%3$d,%4$d,%5$d)", yamlContent)));
movingNode.add(new BooleanOption("allowflying", movingNode.add(new BooleanOption("allowflying",
CONFIG.getBoolean("moving.allowflying", false))); SimpleYaml.getBoolean("moving.allowflying", false, yamlContent)));
movingNode.add(new BooleanOption("allowfakesneak", movingNode.add(new BooleanOption("allowfakesneak",
CONFIG.getBoolean("moving.allowfakesneak", true))); SimpleYaml.getBoolean("moving.allowfakesneak", true, yamlContent)));
/*** MOVING ACTION section ***/ /*** MOVING ACTION section ***/
{ {
@ -155,11 +162,11 @@ public class NoCheatConfiguration {
movingNode.add(movingActionNode); movingNode.add(movingActionNode);
movingActionNode.add(new MediumStringOption("low", movingActionNode.add(new MediumStringOption("low",
CONFIG.getString("moving.action.low", "loglow cancel"))); SimpleYaml.getString("moving.action.low", "loglow cancel", yamlContent)));
movingActionNode.add(new MediumStringOption("med", movingActionNode.add(new MediumStringOption("med",
CONFIG.getString("moving.action.med", "logmed cancel"))); SimpleYaml.getString("moving.action.med", "logmed cancel", yamlContent)));
movingActionNode.add(new MediumStringOption("high", movingActionNode.add(new MediumStringOption("high",
CONFIG.getString("moving.action.high", "loghigh cancel"))); SimpleYaml.getString("moving.action.high", "loghigh cancel", yamlContent)));
} }
} }
@ -174,11 +181,11 @@ public class NoCheatConfiguration {
airbuildNode.add(airbuildLimitsNode); airbuildNode.add(airbuildLimitsNode);
airbuildLimitsNode.add(new IntegerOption("low", airbuildLimitsNode.add(new IntegerOption("low",
CONFIG.getInt("airbuild.limits.low", 1))); SimpleYaml.getInt("airbuild.limits.low", 1, yamlContent)));
airbuildLimitsNode.add(new IntegerOption("med", airbuildLimitsNode.add(new IntegerOption("med",
CONFIG.getInt("airbuild.limits.med", 3))); SimpleYaml.getInt("airbuild.limits.med", 3, yamlContent)));
airbuildLimitsNode.add(new IntegerOption("high", airbuildLimitsNode.add(new IntegerOption("high",
CONFIG.getInt("airbuild.limits.high", 10))); SimpleYaml.getInt("airbuild.limits.high", 10, yamlContent)));
} }
/*** AIRBUILD ACTION section ***/ /*** AIRBUILD ACTION section ***/
@ -187,11 +194,11 @@ public class NoCheatConfiguration {
airbuildNode.add(airbuildActionNode); airbuildNode.add(airbuildActionNode);
airbuildActionNode.add(new MediumStringOption("low", airbuildActionNode.add(new MediumStringOption("low",
CONFIG.getString("airbuild.action.low", "loglow cancel"))); SimpleYaml.getString("airbuild.action.low", "loglow cancel", yamlContent)));
airbuildActionNode.add(new MediumStringOption("med", airbuildActionNode.add(new MediumStringOption("med",
CONFIG.getString("airbuild.action.med", "logmed cancel"))); SimpleYaml.getString("airbuild.action.med", "logmed cancel", yamlContent)));
airbuildActionNode.add(new MediumStringOption("high", airbuildActionNode.add(new MediumStringOption("high",
CONFIG.getString("airbuild.action.high", "loghigh cancel"))); SimpleYaml.getString("airbuild.action.high", "loghigh cancel", yamlContent)));
} }
} }
@ -218,17 +225,16 @@ public class NoCheatConfiguration {
ParentOption customActionsNode = new ParentOption("customactions"); ParentOption customActionsNode = new ParentOption("customactions");
root.add(customActionsNode); root.add(customActionsNode);
List<String> customs = CONFIG.getKeys("customactions"); Set<String> customs = SimpleYaml.getKeys("customactions", yamlContent);
if(customs != null) {
for(String s : customs) { for(String s : customs) {
CustomActionOption o = new CustomActionOption(s, CONFIG.getString("customactions."+s, "unknown")); CustomActionOption o = new CustomActionOption(s, SimpleYaml.getString("customactions."+s, "unknown", yamlContent));
customActionsNode.add(o); customActionsNode.add(o);
actionMap.put(s, o.getCustomActionValue()); actionMap.put(s, o.getCustomActionValue());
} }
} }
}
if(!configurationFile.exists()) { if(!configurationFile.exists()) {
writeConfigFile(configurationFile, this); writeConfigFile(configurationFile, this);

View File

@ -35,7 +35,7 @@ public class Wizard extends JFrame {
inside.setLayout(new BoxLayout(inside,BoxLayout.Y_AXIS)); inside.setLayout(new BoxLayout(inside,BoxLayout.Y_AXIS));
final NoCheatConfiguration config = new NoCheatConfiguration(new File("config.yml")); final NoCheatConfiguration config = new NoCheatConfiguration(new File("NoCheat/nocheat.yml"));
ParentOptionGui root2 = new ParentOptionGui(config.getRoot()); ParentOptionGui root2 = new ParentOptionGui(config.getRoot());

View File

@ -0,0 +1,138 @@
package cc.co.evenprime.bukkit.nocheat.yaml;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
/**
* An extremely simple YAML parser, not feature complete, but good enough for me
*
* @author Evenprime
*
*/
public class SimpleYaml {
private static final String prefix = " ";
private SimpleYaml() {}
public static Map<String, Object> read(File file) throws IOException {
Map<String, Object> root = new HashMap<String, Object>();
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
LinkedList<String> lines = new LinkedList<String>();
String line = null;
while(( line = r.readLine()) != null) {
lines.add(line);
}
r.close();
return parse(root, lines, "");
}
private static Map<String, Object> parse(Map<String, Object> root, LinkedList<String> lines, String prefix) throws IOException {
String line = null;
while(!lines.isEmpty()) {
line = lines.getFirst();
if(line.trim().startsWith("#")) { lines.removeFirst(); }
else if(line.trim().isEmpty()) { lines.removeFirst(); }
else if(line.startsWith(prefix)) {
lines.removeFirst();
if(line.contains(":")) {
String pair[] = line.split(":", 2);
if(pair[1].trim().isEmpty()) {
Map<String, Object> m = new HashMap<String, Object>();
root.put(pair[0].trim(), parse(m, lines, prefix + SimpleYaml.prefix));
}
else
{
root.put(pair[0].trim(), removeQuotationMarks(pair[1].trim()));
}
}
}
else break;
}
return root;
}
private static String removeQuotationMarks(String s) {
if(s.startsWith("\"") && s.endsWith("\"")) {
return s.substring(1, s.length() -1);
}
else if(s.startsWith("\'") && s.endsWith("\'")) {
return s.substring(1, s.length() -1);
}
return s;
}
/* Convenience methods for retrieving values start here */
@SuppressWarnings("unchecked")
public static Object getProperty(String path, Map<String, Object> node) {
if (!path.contains(".")) {
return node.get(path);
}
else
{
String[] parts = path.split("\\.", 2);
return getProperty(parts[1], (Map<String, Object>) node.get(parts[0]));
}
}
@SuppressWarnings("unchecked")
public static Set<String> getKeys(String path, Map<String, Object> node) {
try {
return ((Map<String, Object>)getProperty(path, node)).keySet();
}
catch(Exception e) {
e.printStackTrace();
return new HashSet<String>();
}
}
public static int getInt(String path, int defaultValue, Map<String, Object> node) {
try {
return (Integer) getProperty(path, node);
}
catch(Exception e) {
return defaultValue;
}
}
public static boolean getBoolean(String path, boolean defaultValue, Map<String, Object> node) {
try {
return (Boolean) getProperty(path, node);
}
catch(Exception e) {
return defaultValue;
}
}
public static String getString(String path, String defaultValue, Map<String, Object> node) {
try {
return (String) getProperty(path, node);
}
catch(Exception e) {
return defaultValue;
}
}
}