= Base commands config class, and command standardization
This commit is contained in:
@@ -1,6 +1,90 @@
|
||||
package core;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import dataStructures.Pair;
|
||||
import utils.GlobalLog;
|
||||
import utils.LogFilter;
|
||||
import utils.io.FileUtils;
|
||||
|
||||
public class BaseKeyValueFile
|
||||
{
|
||||
|
||||
// Variables
|
||||
public static final String headerStart = "[";
|
||||
public static final String headerEnd = "]";
|
||||
public static final String pairSplit = "=";
|
||||
public static final char pairSeparator = '\n';
|
||||
|
||||
protected final String filename;
|
||||
protected final String header;
|
||||
|
||||
|
||||
// Constructor
|
||||
public BaseKeyValueFile(String filename)
|
||||
{
|
||||
this.filename = filename;
|
||||
this.header = headerStart + this.getClass().getSimpleName() + headerEnd;
|
||||
}
|
||||
|
||||
// Reads in and calls the specifid function for each keyvalue pair we find
|
||||
protected void Parse(Consumer<? super Pair<String, String>> keyValueCallback)
|
||||
{
|
||||
File f = new File(filename);
|
||||
if(f.isFile() && f.canRead())
|
||||
{
|
||||
String content = FileUtils.ReadContent(f).trim();
|
||||
String[] lines = content.split("" + pairSeparator);
|
||||
|
||||
for(int i = 0; i < lines.length; ++i)
|
||||
{
|
||||
if(lines[i].contains(header))
|
||||
continue;
|
||||
|
||||
String[] pair = lines[i].split(pairSplit);
|
||||
|
||||
if(pair.length < 2)
|
||||
continue;
|
||||
|
||||
String key = pair[0].trim().toLowerCase();
|
||||
String value = pair[1].trim().toLowerCase();
|
||||
|
||||
keyValueCallback.accept(new Pair<String, String>(key, value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Writes out a set of keyvalue pairs
|
||||
protected void Write(List<Pair<String, String>> toWrite)
|
||||
{
|
||||
try
|
||||
{
|
||||
ListIterator<Pair<String, String>> iter = toWrite.listIterator();
|
||||
|
||||
String outString = "";
|
||||
outString += header + pairSeparator;
|
||||
|
||||
while(iter.hasNext())
|
||||
{
|
||||
Pair<String, String> pair = iter.next();
|
||||
String key = pair.First.toLowerCase();
|
||||
String value = pair.Second.toLowerCase();
|
||||
|
||||
outString += key + pairSplit + value + pairSeparator;
|
||||
}
|
||||
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
|
||||
writer.write(outString);
|
||||
writer.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
GlobalLog.Error(LogFilter.Core, "Issue writing file " + filename + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import dataStructures.TaggedPairStore;
|
||||
import utils.FileUtils;
|
||||
import utils.GlobalLog;
|
||||
import utils.LogFilter;
|
||||
import utils.io.FileMonitor;
|
||||
import utils.io.FileUtils;
|
||||
import utils.io.MonitoredFile;
|
||||
|
||||
// A quick-and-dirty localization tool that scrapes the project for calls to itself, then
|
||||
@@ -37,7 +37,7 @@ public abstract class BaseLocFile
|
||||
// File monitoring
|
||||
protected FileMonitor fileMonitor;
|
||||
|
||||
// Ok... so this is an array because if it's not an array, the parser will parse the string
|
||||
// Constructor
|
||||
public BaseLocFile(String filename, String functionName)
|
||||
{
|
||||
this.filename = filename;
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
package core;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
|
||||
import utils.FileUtils;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
import dataStructures.Pair;
|
||||
import utils.GlobalLog;
|
||||
import utils.LogFilter;
|
||||
|
||||
@@ -18,11 +16,6 @@ import utils.LogFilter;
|
||||
public class CommandEnabler extends BaseKeyValueFile
|
||||
{
|
||||
// Config/const variables
|
||||
public static final String headerStart = "[";
|
||||
public static final String headerEnd = "]";
|
||||
public static final String filename = "commands.config";
|
||||
public static final String pairSplit = "=";
|
||||
public static final char pairSeparator = '\n';
|
||||
public static final String enabled = "1";
|
||||
public static final String disabled = "0";
|
||||
public static final boolean defaultEnabledState = true;
|
||||
@@ -30,15 +23,16 @@ public class CommandEnabler extends BaseKeyValueFile
|
||||
// Local variables
|
||||
private HashMap<String, Boolean> enabledMap; // Quick lookup
|
||||
private ArrayList<String> keyList; // Tracking ordering for later
|
||||
private final String header;
|
||||
private final static String name = "commands.config";
|
||||
|
||||
public CommandEnabler()
|
||||
{
|
||||
super(name);
|
||||
|
||||
// Create/Init variables
|
||||
GlobalLog.Log(LogFilter.Core, "Initializing " + this.getClass().getSimpleName());
|
||||
enabledMap = new HashMap<>();
|
||||
keyList = new ArrayList<>();
|
||||
header = headerStart + this.getClass().getSimpleName() + headerEnd;
|
||||
|
||||
// Startup
|
||||
ReadIn();
|
||||
@@ -49,33 +43,17 @@ public class CommandEnabler extends BaseKeyValueFile
|
||||
// Reads in the config file and parses it, keeping tabs on the order it read things
|
||||
private void ReadIn()
|
||||
{
|
||||
File f = new File(filename);
|
||||
if(f.isFile() && f.canRead())
|
||||
{
|
||||
String content = FileUtils.ReadContent(f).trim();
|
||||
String[] lines = content.split("" + pairSeparator);
|
||||
Parse((pair) ->{
|
||||
String key = pair.First;
|
||||
String value = pair.Second;
|
||||
|
||||
for(int i = 0; i < lines.length; ++i)
|
||||
{
|
||||
if(lines[i].contains(header))
|
||||
continue;
|
||||
|
||||
String[] pair = lines[i].split(pairSplit);
|
||||
|
||||
if(pair.length < 2)
|
||||
continue;
|
||||
|
||||
String key = pair[0].trim();
|
||||
String value = pair[1].trim().toLowerCase();
|
||||
|
||||
keyList.add(key);
|
||||
keyList.add(key);
|
||||
|
||||
if(value.equalsIgnoreCase(enabled))
|
||||
enabledMap.putIfAbsent(key, true);
|
||||
else
|
||||
enabledMap.putIfAbsent(key, false);
|
||||
}
|
||||
}
|
||||
if(value.equalsIgnoreCase(enabled))
|
||||
enabledMap.putIfAbsent(key, true);
|
||||
else
|
||||
enabledMap.putIfAbsent(key, false);
|
||||
});
|
||||
}
|
||||
|
||||
// Look up the already scraped values from the localizer and store them if they
|
||||
@@ -98,36 +76,31 @@ public class CommandEnabler extends BaseKeyValueFile
|
||||
// Write out enabled/disabled file info.
|
||||
private void WriteOut()
|
||||
{
|
||||
try
|
||||
List<Pair<String, String>> list = new Vector<Pair<String, String>>();
|
||||
|
||||
for(int i = 0; i < keyList.size(); ++i)
|
||||
{
|
||||
String outString = "";
|
||||
outString += header + pairSeparator;
|
||||
for(int i = 0; i < keyList.size(); ++i)
|
||||
{
|
||||
String key = keyList.get(i);
|
||||
String value = enabled;
|
||||
|
||||
if(enabledMap.get(key) == false)
|
||||
value = disabled;
|
||||
|
||||
outString += key + pairSplit + value + pairSeparator;
|
||||
}
|
||||
String key = keyList.get(i).toLowerCase();
|
||||
String value = enabled.toLowerCase();
|
||||
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
|
||||
writer.write(outString);
|
||||
writer.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
GlobalLog.Error(LogFilter.Core, "Command enabler issue writing file! " + e.getMessage());
|
||||
if(enabledMap.get(key) == false)
|
||||
value = disabled.toLowerCase();
|
||||
|
||||
list.add(new Pair<String, String>(key, value));
|
||||
}
|
||||
|
||||
Collections.sort(list, (c1, c2) -> { return c1.First.compareTo(c2.First); });
|
||||
|
||||
Write(list);
|
||||
}
|
||||
|
||||
// Looks up a key to see if it's enabled or not
|
||||
public boolean IsEnabled(String key)
|
||||
{
|
||||
if(enabledMap.containsKey(key))
|
||||
return enabledMap.get(key);
|
||||
String toCheck = key.toLowerCase();
|
||||
|
||||
if(enabledMap.containsKey(toCheck))
|
||||
return enabledMap.get(toCheck);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import dataStructures.Pair;
|
||||
import utils.FileUtils;
|
||||
import utils.io.DirectoryMonitor;
|
||||
import utils.io.FileUtils;
|
||||
import utils.io.MonitoredFile;
|
||||
|
||||
// All things considered, this doesn't need to be particularly efficient since anything
|
||||
|
||||
Reference in New Issue
Block a user