= Iterative step - config reading but improper writing
This commit is contained in:
@@ -1,17 +1,10 @@
|
||||
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
|
||||
{
|
||||
@@ -21,70 +14,55 @@ public class BaseKeyValueFile
|
||||
public static final String pairSplit = "=";
|
||||
public static final char pairSeparator = '\n';
|
||||
|
||||
protected final String filename;
|
||||
protected final String header;
|
||||
|
||||
|
||||
// Constructor
|
||||
public BaseKeyValueFile(String filename)
|
||||
public BaseKeyValueFile()
|
||||
{
|
||||
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)
|
||||
protected void parse(String content, Consumer<? super Pair<String, String>> keyValueCallback)
|
||||
{
|
||||
File f = new File(filename);
|
||||
if(f.isFile() && f.canRead())
|
||||
content = content.trim();
|
||||
String[] lines = content.split("" + pairSeparator);
|
||||
|
||||
for(int i = 0; i < lines.length; ++i)
|
||||
{
|
||||
String content = FileUtils.readContent(f).trim();
|
||||
String[] lines = content.split("" + pairSeparator);
|
||||
if(lines[i].contains(header))
|
||||
continue;
|
||||
|
||||
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));
|
||||
}
|
||||
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)
|
||||
|
||||
// Writes the set of keyvalue pairs to a string
|
||||
protected String write(List<Pair<String, String>> toWrite)
|
||||
{
|
||||
try
|
||||
ListIterator<Pair<String, String>> iter = toWrite.listIterator();
|
||||
|
||||
String outString = "";
|
||||
outString += header + pairSeparator;
|
||||
|
||||
while(iter.hasNext())
|
||||
{
|
||||
ListIterator<Pair<String, String>> iter = toWrite.listIterator();
|
||||
Pair<String, String> pair = iter.next();
|
||||
String key = pair.First.toLowerCase();
|
||||
String value = pair.Second.toLowerCase();
|
||||
|
||||
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());
|
||||
outString += key + pairSplit + value + pairSeparator;
|
||||
}
|
||||
|
||||
return outString;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,6 @@ import utils.io.FileUtils;
|
||||
// generates/updates a file externally with all the stub values as keys that are localized.
|
||||
public abstract class BaseLocFile
|
||||
{
|
||||
// Pre-defined values
|
||||
public static final String KittySourceDirectory = "./src";
|
||||
|
||||
// Filename
|
||||
public final String functionName; // Example: "Localizer.Stub";
|
||||
|
||||
@@ -130,7 +127,7 @@ public abstract class BaseLocFile
|
||||
public void scrapeAll()
|
||||
{
|
||||
ArrayList<LocInfo> localizeList = new ArrayList<LocInfo>();
|
||||
FileUtils.acquireAllFiles(KittySourceDirectory).forEach((path) -> tryStripSpecified(path, localizeList));
|
||||
FileUtils.acquireAllFiles(Constants.SourceDirectory).forEach((path) -> tryStripSpecified(path, localizeList));
|
||||
|
||||
for(LocInfo toStub : localizeList)
|
||||
stringStore.addKeyValue(toStub.file, toStub.phrase, toStub.phrase);
|
||||
|
||||
@@ -19,32 +19,38 @@ public class CommandEnabler extends BaseKeyValueFile implements IConfigSection
|
||||
public static final String enabled = "1";
|
||||
public static final String disabled = "0";
|
||||
public static final boolean defaultEnabledState = true;
|
||||
public static final String HeaderName = "Command Management";
|
||||
|
||||
// Local variables
|
||||
private HashMap<String, Boolean> enabledMap; // Quick lookup
|
||||
private ArrayList<String> keyList; // Tracking ordering for later
|
||||
private final static String name = Constants.AssetDirectory + "commands.config";
|
||||
|
||||
public static CommandEnabler instance;
|
||||
|
||||
// Constructor
|
||||
public CommandEnabler()
|
||||
{
|
||||
super(name);
|
||||
super();
|
||||
|
||||
// Create/Init variables
|
||||
GlobalLog.log(LogFilter.Core, "Initializing " + this.getClass().getSimpleName());
|
||||
enabledMap = new HashMap<>();
|
||||
keyList = new ArrayList<>();
|
||||
|
||||
// Startup
|
||||
readIn();
|
||||
getTrackedCommands();
|
||||
writeOut();
|
||||
if(instance == null)
|
||||
{
|
||||
// Create/Init variables
|
||||
GlobalLog.log(LogFilter.Core, "Initializing " + this.getClass().getSimpleName());
|
||||
enabledMap = new HashMap<>();
|
||||
keyList = new ArrayList<>();
|
||||
|
||||
instance = this;
|
||||
}
|
||||
else
|
||||
{
|
||||
GlobalLog.error(LogFilter.Core, "You can't have two of the following: " + this.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
// Reads in the config file and parses it, keeping tabs on the order it read things
|
||||
private void readIn()
|
||||
private void readIn(String contents)
|
||||
{
|
||||
parse((pair) ->{
|
||||
parse(contents, (pair) ->{
|
||||
String key = pair.First;
|
||||
String value = pair.Second;
|
||||
|
||||
@@ -76,7 +82,7 @@ public class CommandEnabler extends BaseKeyValueFile implements IConfigSection
|
||||
}
|
||||
|
||||
// Write out enabled/disabled file info.
|
||||
private void writeOut()
|
||||
private String writeOut()
|
||||
{
|
||||
List<Pair<String, String>> list = new Vector<Pair<String, String>>();
|
||||
|
||||
@@ -93,7 +99,7 @@ public class CommandEnabler extends BaseKeyValueFile implements IConfigSection
|
||||
|
||||
Collections.sort(list, (c1, c2) -> { return c1.First.compareTo(c2.First); });
|
||||
|
||||
write(list);
|
||||
return write(list);
|
||||
}
|
||||
|
||||
// Looks up a key to see if it's enabled or not
|
||||
@@ -109,25 +115,17 @@ public class CommandEnabler extends BaseKeyValueFile implements IConfigSection
|
||||
|
||||
@Override
|
||||
public String getHeader() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
return HeaderName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preUpdate() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
public void read(String contents) {
|
||||
getTrackedCommands();
|
||||
readIn(contents);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContent() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
public String write() {
|
||||
return writeOut();
|
||||
}
|
||||
}
|
||||
|
||||
+97
-16
@@ -1,9 +1,10 @@
|
||||
package core;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Vector;
|
||||
|
||||
import utils.GlobalLog;
|
||||
import utils.LogFilter;
|
||||
import utils.io.FileMonitor;
|
||||
@@ -14,12 +15,7 @@ public class Config
|
||||
private static final String filepath = Constants.AssetDirectory + Constants.ConfigFilename;
|
||||
|
||||
public static Config instance;
|
||||
|
||||
// Accessable config related things
|
||||
public LocStrings locStrings;
|
||||
public LocCommands locCommands;
|
||||
public CommandEnabler commandEnabler;
|
||||
|
||||
|
||||
// Private
|
||||
private Vector<IConfigSection> sections;
|
||||
private FileMonitor monitoredConfigFile;
|
||||
@@ -32,9 +28,9 @@ public class Config
|
||||
sections = new Vector<IConfigSection>();
|
||||
|
||||
// Add all sections
|
||||
sections.add(new CommandEnabler());
|
||||
sections.add(new LocStrings());
|
||||
sections.add(new LocCommands());
|
||||
sections.add(new LocStrings());
|
||||
sections.add(new CommandEnabler());
|
||||
|
||||
// Read file in and parse everything out
|
||||
performStartup();
|
||||
@@ -50,13 +46,14 @@ public class Config
|
||||
}
|
||||
}
|
||||
|
||||
// Perform startup
|
||||
private void performStartup()
|
||||
{
|
||||
File configFile = new File(filepath);
|
||||
|
||||
if(configFile.exists())
|
||||
{
|
||||
String configContents = FileUtils.readContent(configFile);
|
||||
reformConfig(configContents);
|
||||
reformConfig(configFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -72,20 +69,104 @@ public class Config
|
||||
}
|
||||
}
|
||||
|
||||
private void reformConfig(String fullContents)
|
||||
public static final String sectionStart = "[[";
|
||||
public static final String sectionEnd = "]]";
|
||||
public static final String headerStart = "[";
|
||||
public static final String headerEnd = "]";
|
||||
public static final String pairSplit = "=";
|
||||
public static final String pairSeparator = "\n";
|
||||
|
||||
private void reformConfig(File configFile)
|
||||
{
|
||||
for(IConfigSection section : sections)
|
||||
// Read and update
|
||||
String configContents = FileUtils.readContent(configFile);
|
||||
readConfigs(configContents);
|
||||
|
||||
// Form updated data as necessary for autogeneration
|
||||
String output = combineConfigs();
|
||||
String path = configFile.getPath();
|
||||
|
||||
// Write to file.
|
||||
FileWriter fileWriter;
|
||||
|
||||
try
|
||||
{
|
||||
// Parse stuff and sections here based on interface
|
||||
fileWriter = new FileWriter(path);
|
||||
fileWriter.write(output);
|
||||
fileWriter.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
GlobalLog.error(LogFilter.Core, "Config writing failure.");
|
||||
GlobalLog.error(LogFilter.Core, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void readConfigs(String fullContents)
|
||||
{
|
||||
String[] str = fullContents.split(pairSeparator);
|
||||
HashMap<String, String> parsedSections = new HashMap<String, String>();
|
||||
|
||||
String currentHeader = "";
|
||||
for(int i = 0; i < str.length; ++i)
|
||||
{
|
||||
String line = str[i].trim();
|
||||
if(line.startsWith(sectionStart) && line.endsWith(sectionEnd))
|
||||
{
|
||||
currentHeader = line.substring(sectionStart.length(), line.length() - sectionEnd.length());
|
||||
}
|
||||
else
|
||||
{
|
||||
String sectionContents = parsedSections.getOrDefault(currentHeader, "");
|
||||
parsedSections.put(currentHeader, sectionContents);
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < sections.size(); ++i)
|
||||
{
|
||||
IConfigSection section = sections.get(i);
|
||||
String header = section.getHeader();
|
||||
String content = parsedSections.getOrDefault(header, null);
|
||||
|
||||
if(content != null)
|
||||
{
|
||||
section.read(content);
|
||||
}
|
||||
else
|
||||
{
|
||||
GlobalLog.warn(LogFilter.Core, "Mismatch during config parsing - expected but did not find " + header);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String combineConfigs()
|
||||
{
|
||||
String output = "";
|
||||
|
||||
for(int i = 0; i < sections.size(); ++i)
|
||||
{
|
||||
// If not the first section, add spacing!
|
||||
if(i != 0)
|
||||
{
|
||||
for(int spacing = 0; spacing < 3; ++spacing)
|
||||
{
|
||||
output += pairSeparator;
|
||||
}
|
||||
}
|
||||
|
||||
IConfigSection section = sections.get(i);
|
||||
output += sectionStart + section.getHeader() + sectionEnd + pairSeparator;
|
||||
output += section.write() + pairSeparator;
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
public void upkeep()
|
||||
{
|
||||
monitoredConfigFile.update((monitoredFile) -> {
|
||||
File configFile = new File(filepath);
|
||||
String configContents = FileUtils.readContent(configFile);
|
||||
reformConfig(configContents);
|
||||
reformConfig(configFile);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -8,4 +8,5 @@ public final class Constants
|
||||
public static final Color ColorDefault = new Color(7*16, 8*16, 9*16); // A slate-grey
|
||||
public static final String AssetDirectory = "./assets/";
|
||||
public static final String ConfigFilename = "config.config";
|
||||
public static final String SourceDirectory = "./src";
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package core;
|
||||
public abstract interface IConfigSection
|
||||
{
|
||||
public abstract String getHeader();
|
||||
public abstract void preUpdate();
|
||||
public abstract void init();
|
||||
public abstract String getContent();
|
||||
public abstract void read(String contents);
|
||||
public abstract String write();
|
||||
}
|
||||
|
||||
@@ -46,25 +46,17 @@ public class LocCommands extends BaseLocFile implements IConfigSection
|
||||
|
||||
@Override
|
||||
public String getHeader() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
return HeaderName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preUpdate() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
public void read(String contents) {
|
||||
updateLocFromString(contents);
|
||||
scrapeAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContent() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
public String write() {
|
||||
return super.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import utils.LogFilter;
|
||||
// can then be localized.
|
||||
public class LocStrings extends BaseLocFile implements IConfigSection
|
||||
{
|
||||
public static final String fileName = Constants.AssetDirectory + "locStrings.config";
|
||||
public static final String HeaderName = "Localized Strings";
|
||||
public static final String function = "LocStrings.stub";
|
||||
|
||||
private static LocStrings instance;
|
||||
@@ -42,25 +42,17 @@ public class LocStrings extends BaseLocFile implements IConfigSection
|
||||
|
||||
@Override
|
||||
public String getHeader() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
return HeaderName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preUpdate() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
public void read(String contents) {
|
||||
updateLocFromString(contents);
|
||||
scrapeAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContent() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
public String write() {
|
||||
return super.toString();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -49,7 +49,7 @@ public class Main extends ListenerAdapter
|
||||
// Factory startup. The ordering is intentional.
|
||||
GlobalLog.initialize();
|
||||
databaseManager = ObjectBuilderFactory.constructDatabaseManager();
|
||||
commandManager = ObjectBuilderFactory.constructCommandManager(Config.instance.commandEnabler);
|
||||
commandManager = ObjectBuilderFactory.constructCommandManager(CommandEnabler.instance); // TODO: Untangle this singleton
|
||||
stats = ObjectBuilderFactory.constructStats(commandManager);
|
||||
charManager = new CharacterManager();
|
||||
rpManager = ObjectBuilderFactory.constructRPManager();
|
||||
|
||||
Reference in New Issue
Block a user