= Iteration on config loading

This commit is contained in:
Matthew Cech
2019-06-20 00:24:37 -07:00
parent 99cd76de83
commit 2b781325f5
7 changed files with 115 additions and 27 deletions
+1 -7
View File
@@ -1,19 +1,13 @@
package core;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import dataStructures.TaggedPairStore;
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
// generates/updates a file externally with all the stub values as keys that are localized.
+25 -1
View File
@@ -13,7 +13,7 @@ import utils.LogFilter;
// commands that are being looked up will behave slightly differently so trimming
// rules for this file are different than the localization ones - this is more
// aggresive with whitespace removal.
public class CommandEnabler extends BaseKeyValueFile
public class CommandEnabler extends BaseKeyValueFile implements IConfigSection
{
// Config/const variables
public static final String enabled = "1";
@@ -106,4 +106,28 @@ public class CommandEnabler extends BaseKeyValueFile
return true;
}
@Override
public String getHeader() {
// TODO Auto-generated method stub
return null;
}
@Override
public void preUpdate() {
// TODO Auto-generated method stub
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@Override
public String getContent() {
// TODO Auto-generated method stub
return null;
}
}
+57 -9
View File
@@ -1,14 +1,17 @@
package core;
import java.nio.file.Path;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import utils.GlobalLog;
import utils.LogFilter;
import utils.io.FileMonitor;
import utils.io.FileUtils;
public class Config
{
private static final String filepath = Constants.AssetDirectory + Constants.ConfigFilename;
FileMonitor configFile;
public static Config instance;
@@ -17,19 +20,27 @@ public class Config
public LocCommands locCommands;
public CommandEnabler commandEnabler;
// Private
private Vector<IConfigSection> sections;
private FileMonitor monitoredConfigFile;
public Config()
{
if(instance == null)
{
// Start by reading from things that are external. Because
// we require these things to be resolved before the rest of the application,
// we place them here.
configFile = new FileMonitor(filepath);
// Create local variables
sections = new Vector<IConfigSection>();
commandEnabler = new CommandEnabler();
locStrings = new LocStrings();
locCommands = new LocCommands();
// Add all sections
sections.add(new CommandEnabler());
sections.add(new LocStrings());
sections.add(new LocCommands());
// Read file in and parse everything out
performStartup();
// Being monitoring configs and mark this as the instance now that it's made
monitoredConfigFile = new FileMonitor(filepath);
instance = this;
}
else
@@ -39,4 +50,41 @@ public class Config
}
}
private void performStartup()
{
File configFile = new File(filepath);
if(configFile.exists())
{
String configContents = FileUtils.readContent(new File(filepath));
reformConfig(configContents);
}
else
{
try
{
configFile.createNewFile();
}
catch (IOException e)
{
GlobalLog.error(LogFilter.Core, "Failed to load config or create empty config at " + filepath);
System.exit(-1);
}
}
}
private void reformConfig(String fullContents)
{
for(IConfigSection section : sections)
{
}
}
public void upkeep()
{
configFile.update((monitoredFile) -> {
monitoredFile.path
});
}
}
-1
View File
@@ -3,7 +3,6 @@ package core;
import java.util.ArrayList;
import utils.GlobalLog;
import utils.LogFilter;
import utils.io.FileMonitor;
import dataStructures.Pair;
// Performs the same localization for the strings associated with command names as
+25 -2
View File
@@ -2,12 +2,11 @@ package core;
import utils.GlobalLog;
import utils.LogFilter;
import utils.io.FileMonitor;
// A quick-and-dirty localization tool that scrapes the project for calls to itself, then
// generates/updates a file externally (phrases.config) with all the stub values as keys that
// can then be localized.
public class LocStrings extends BaseLocFile
public class LocStrings extends BaseLocFile implements IConfigSection
{
public static final String fileName = Constants.AssetDirectory + "locStrings.config";
public static final String function = "LocStrings.stub";
@@ -40,4 +39,28 @@ public class LocStrings extends BaseLocFile
{
return instance.getKey(stubbedPreviously);
}
@Override
public String getHeader() {
// TODO Auto-generated method stub
return null;
}
@Override
public void preUpdate() {
// TODO Auto-generated method stub
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@Override
public String getContent() {
// TODO Auto-generated method stub
return null;
}
}
+4 -2
View File
@@ -58,7 +58,7 @@ public class ObjectBuilderFactory
@SuppressWarnings("unused") private static LocCommands locCommands;
// Config
private static Config config;
@SuppressWarnings("unused") private static Config config;
// Lazy initialization multithreaded mutex stuff to prevent explosions.
// TODO: Investigate using 'synchronized' instead potentially
@@ -85,7 +85,9 @@ public class ObjectBuilderFactory
database = null;
stats = null;
// Create the config. We need to do this all first.
// Start by reading from things that are external. Because
// we require these things to be resolved before the rest of the application,
// we place them here.
config = new Config();
}
finally