= Iteration on config loading
This commit is contained in:
+57
-9
@@ -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
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user