From 2b781325f5c74fe5557918802003bb3bc59a3c60 Mon Sep 17 00:00:00 2001 From: Matthew Cech Date: Thu, 20 Jun 2019 00:24:37 -0700 Subject: [PATCH] = Iteration on config loading --- src/core/BaseLocFile.java | 8 +--- src/core/CommandEnabler.java | 26 +++++++++++- src/core/Config.java | 66 ++++++++++++++++++++++++++---- src/core/LocCommands.java | 1 - src/core/LocStrings.java | 27 +++++++++++- src/core/ObjectBuilderFactory.java | 6 ++- src/main/Superintendent.java | 8 ++-- 7 files changed, 115 insertions(+), 27 deletions(-) diff --git a/src/core/BaseLocFile.java b/src/core/BaseLocFile.java index 95bfdde..5750c5c 100644 --- a/src/core/BaseLocFile.java +++ b/src/core/BaseLocFile.java @@ -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. diff --git a/src/core/CommandEnabler.java b/src/core/CommandEnabler.java index 58878d6..668baae 100644 --- a/src/core/CommandEnabler.java +++ b/src/core/CommandEnabler.java @@ -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; + } } diff --git a/src/core/Config.java b/src/core/Config.java index b7d15d8..84fb30c 100644 --- a/src/core/Config.java +++ b/src/core/Config.java @@ -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 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(); - 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 + }); + } + } diff --git a/src/core/LocCommands.java b/src/core/LocCommands.java index f17e9b5..817b415 100644 --- a/src/core/LocCommands.java +++ b/src/core/LocCommands.java @@ -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 diff --git a/src/core/LocStrings.java b/src/core/LocStrings.java index dafee4b..5f9a3f9 100644 --- a/src/core/LocStrings.java +++ b/src/core/LocStrings.java @@ -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; + } } \ No newline at end of file diff --git a/src/core/ObjectBuilderFactory.java b/src/core/ObjectBuilderFactory.java index c0fb982..a7091f8 100644 --- a/src/core/ObjectBuilderFactory.java +++ b/src/core/ObjectBuilderFactory.java @@ -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 diff --git a/src/main/Superintendent.java b/src/main/Superintendent.java index aae634c..1b49ecf 100644 --- a/src/main/Superintendent.java +++ b/src/main/Superintendent.java @@ -2,9 +2,8 @@ package main; import java.util.concurrent.atomic.AtomicInteger; +import core.Config; import core.DatabaseManager; -import core.LocCommands; -import core.LocStrings; import core.RPManager; import core.Stats; import dataStructures.KittyChannel; @@ -74,9 +73,8 @@ public class Superintendent // Happens just before the command / plugin runs. public static boolean perCommandUpkeepPre() { - // Upkeep localization system's file monitoring - LocStrings.upkeep(); - LocCommands.upkeep(); + // Upkeep the config file monitoring + Config.instance.upkeep(); return true; }