= Iteration on config loading
This commit is contained in:
@@ -1,19 +1,13 @@
|
|||||||
package core;
|
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.Path;
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import dataStructures.TaggedPairStore;
|
import dataStructures.TaggedPairStore;
|
||||||
import utils.GlobalLog;
|
import utils.GlobalLog;
|
||||||
import utils.LogFilter;
|
import utils.LogFilter;
|
||||||
import utils.io.FileMonitor;
|
import utils.io.FileMonitor;
|
||||||
import utils.io.FileUtils;
|
import utils.io.FileUtils;
|
||||||
import utils.io.MonitoredFile;
|
|
||||||
|
|
||||||
// A quick-and-dirty localization tool that scrapes the project for calls to itself, then
|
// 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.
|
// generates/updates a file externally with all the stub values as keys that are localized.
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import utils.LogFilter;
|
|||||||
// commands that are being looked up will behave slightly differently so trimming
|
// 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
|
// rules for this file are different than the localization ones - this is more
|
||||||
// aggresive with whitespace removal.
|
// aggresive with whitespace removal.
|
||||||
public class CommandEnabler extends BaseKeyValueFile
|
public class CommandEnabler extends BaseKeyValueFile implements IConfigSection
|
||||||
{
|
{
|
||||||
// Config/const variables
|
// Config/const variables
|
||||||
public static final String enabled = "1";
|
public static final String enabled = "1";
|
||||||
@@ -106,4 +106,28 @@ public class CommandEnabler extends BaseKeyValueFile
|
|||||||
|
|
||||||
return true;
|
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
@@ -1,14 +1,17 @@
|
|||||||
package core;
|
package core;
|
||||||
import java.nio.file.Path;
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
import utils.GlobalLog;
|
import utils.GlobalLog;
|
||||||
import utils.LogFilter;
|
import utils.LogFilter;
|
||||||
import utils.io.FileMonitor;
|
import utils.io.FileMonitor;
|
||||||
|
import utils.io.FileUtils;
|
||||||
|
|
||||||
public class Config
|
public class Config
|
||||||
{
|
{
|
||||||
private static final String filepath = Constants.AssetDirectory + Constants.ConfigFilename;
|
private static final String filepath = Constants.AssetDirectory + Constants.ConfigFilename;
|
||||||
FileMonitor configFile;
|
|
||||||
|
|
||||||
public static Config instance;
|
public static Config instance;
|
||||||
|
|
||||||
@@ -17,19 +20,27 @@ public class Config
|
|||||||
public LocCommands locCommands;
|
public LocCommands locCommands;
|
||||||
public CommandEnabler commandEnabler;
|
public CommandEnabler commandEnabler;
|
||||||
|
|
||||||
|
// Private
|
||||||
|
private Vector<IConfigSection> sections;
|
||||||
|
private FileMonitor monitoredConfigFile;
|
||||||
|
|
||||||
public Config()
|
public Config()
|
||||||
{
|
{
|
||||||
if(instance == null)
|
if(instance == null)
|
||||||
{
|
{
|
||||||
// Start by reading from things that are external. Because
|
// Create local variables
|
||||||
// we require these things to be resolved before the rest of the application,
|
sections = new Vector<IConfigSection>();
|
||||||
// we place them here.
|
|
||||||
configFile = new FileMonitor(filepath);
|
|
||||||
|
|
||||||
commandEnabler = new CommandEnabler();
|
// Add all sections
|
||||||
locStrings = new LocStrings();
|
sections.add(new CommandEnabler());
|
||||||
locCommands = new LocCommands();
|
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;
|
instance = this;
|
||||||
}
|
}
|
||||||
else
|
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
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package core;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import utils.GlobalLog;
|
import utils.GlobalLog;
|
||||||
import utils.LogFilter;
|
import utils.LogFilter;
|
||||||
import utils.io.FileMonitor;
|
|
||||||
import dataStructures.Pair;
|
import dataStructures.Pair;
|
||||||
|
|
||||||
// Performs the same localization for the strings associated with command names as
|
// Performs the same localization for the strings associated with command names as
|
||||||
|
|||||||
@@ -2,12 +2,11 @@ package core;
|
|||||||
|
|
||||||
import utils.GlobalLog;
|
import utils.GlobalLog;
|
||||||
import utils.LogFilter;
|
import utils.LogFilter;
|
||||||
import utils.io.FileMonitor;
|
|
||||||
|
|
||||||
// A quick-and-dirty localization tool that scrapes the project for calls to itself, then
|
// 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
|
// generates/updates a file externally (phrases.config) with all the stub values as keys that
|
||||||
// can then be localized.
|
// 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 fileName = Constants.AssetDirectory + "locStrings.config";
|
||||||
public static final String function = "LocStrings.stub";
|
public static final String function = "LocStrings.stub";
|
||||||
@@ -40,4 +39,28 @@ public class LocStrings extends BaseLocFile
|
|||||||
{
|
{
|
||||||
return instance.getKey(stubbedPreviously);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -58,7 +58,7 @@ public class ObjectBuilderFactory
|
|||||||
@SuppressWarnings("unused") private static LocCommands locCommands;
|
@SuppressWarnings("unused") private static LocCommands locCommands;
|
||||||
|
|
||||||
// Config
|
// Config
|
||||||
private static Config config;
|
@SuppressWarnings("unused") private static Config config;
|
||||||
|
|
||||||
// Lazy initialization multithreaded mutex stuff to prevent explosions.
|
// Lazy initialization multithreaded mutex stuff to prevent explosions.
|
||||||
// TODO: Investigate using 'synchronized' instead potentially
|
// TODO: Investigate using 'synchronized' instead potentially
|
||||||
@@ -85,7 +85,9 @@ public class ObjectBuilderFactory
|
|||||||
database = null;
|
database = null;
|
||||||
stats = 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();
|
config = new Config();
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
|||||||
@@ -2,9 +2,8 @@ package main;
|
|||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
import core.Config;
|
||||||
import core.DatabaseManager;
|
import core.DatabaseManager;
|
||||||
import core.LocCommands;
|
|
||||||
import core.LocStrings;
|
|
||||||
import core.RPManager;
|
import core.RPManager;
|
||||||
import core.Stats;
|
import core.Stats;
|
||||||
import dataStructures.KittyChannel;
|
import dataStructures.KittyChannel;
|
||||||
@@ -74,9 +73,8 @@ public class Superintendent
|
|||||||
// Happens just before the command / plugin runs.
|
// Happens just before the command / plugin runs.
|
||||||
public static boolean perCommandUpkeepPre()
|
public static boolean perCommandUpkeepPre()
|
||||||
{
|
{
|
||||||
// Upkeep localization system's file monitoring
|
// Upkeep the config file monitoring
|
||||||
LocStrings.upkeep();
|
Config.instance.upkeep();
|
||||||
LocCommands.upkeep();
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user