diff --git a/src/commands/CommandDBFlush.java b/src/commands/CommandDBFlush.java index 982c0fa..bfdcaa9 100644 --- a/src/commands/CommandDBFlush.java +++ b/src/commands/CommandDBFlush.java @@ -1,7 +1,7 @@ package commands; import core.Command; -import core.Config; +import core.Constants; import core.DatabaseManager; import core.LocStrings; import dataStructures.KittyChannel; @@ -28,7 +28,7 @@ public class CommandDBFlush extends Command KittyEmbed embed = new KittyEmbed(); embed.title = "Database queue flushed"; embed.descriptionText = "**Dirty:** " + numUpdated; - embed.color = Config.ColorDefault; + embed.color = Constants.ColorDefault; res.send(embed); } diff --git a/src/commands/CommandDBStats.java b/src/commands/CommandDBStats.java index de1fcd5..4e6e4cd 100644 --- a/src/commands/CommandDBStats.java +++ b/src/commands/CommandDBStats.java @@ -4,7 +4,7 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import core.Command; -import core.Config; +import core.Constants; import core.DatabaseManager; import core.LocStrings; import dataStructures.KittyChannel; @@ -36,7 +36,7 @@ public class CommandDBStats extends Command embed.descriptionText = "**Tracked Items:** " + DatabaseManager.instance.getTrackedObjectsSize(); embed.descriptionText += "\n"; embed.descriptionText += "**Last Upkeep:** " + dateFormat.format(DatabaseManager.instance.getLastUpkeep()) + " UTC-7"; - embed.color = Config.ColorDefault; + embed.color = Constants.ColorDefault; res.send(embed); } diff --git a/src/commands/CommandLeaderboard.java b/src/commands/CommandLeaderboard.java index 79a1ff6..8b58323 100644 --- a/src/commands/CommandLeaderboard.java +++ b/src/commands/CommandLeaderboard.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.List; import core.Command; -import core.Config; +import core.Constants; import core.DatabaseManager; import core.LocStrings; import core.ObjectBuilderFactory; @@ -79,7 +79,7 @@ public class CommandLeaderboard extends Command // Configure output final int listSize = 10; KittyEmbed embed = new KittyEmbed(); - embed.color = Config.ColorDefault; + embed.color = Constants.ColorDefault; embed.title = LocStrings.stub("LeaderboardTitle"); embed.descriptionText = ""; diff --git a/src/commands/CommandStats.java b/src/commands/CommandStats.java index 3e6217a..eeaa4ac 100644 --- a/src/commands/CommandStats.java +++ b/src/commands/CommandStats.java @@ -3,7 +3,7 @@ package commands; import core.Command; import core.LocStrings; import core.CommandManager.ThreadData; -import core.Config; +import core.Constants; import dataStructures.KittyChannel; import dataStructures.KittyEmbed; import dataStructures.KittyGuild; @@ -82,7 +82,7 @@ public class CommandStats extends Command KittyEmbed embed = new KittyEmbed(); embed.title = "Bot Stats"; embed.descriptionText = out; - embed.color = Config.ColorDefault; + embed.color = Constants.ColorDefault; res.send(embed); } diff --git a/src/commands/CommandTeey.java b/src/commands/CommandTeey.java index e25a9b8..0bcd7b9 100644 --- a/src/commands/CommandTeey.java +++ b/src/commands/CommandTeey.java @@ -4,7 +4,7 @@ import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import core.Command; -import core.Config; +import core.Constants; import core.LocStrings; import dataStructures.KittyChannel; import dataStructures.KittyGuild; @@ -54,7 +54,7 @@ public class CommandTeey extends Command return; teeyeeFile = new File(yeeteeFilename); - ImageOverlayBuilder builder = new ImageOverlayBuilder(Config.AssetDirectory + "teey/frames/", "teey ", 24, 18); + ImageOverlayBuilder builder = new ImageOverlayBuilder(Constants.AssetDirectory + "teey/frames/", "teey ", 24, 18); builder.overlay(ImageIO.read(teeyeeFile), name); } catch (IOException e) diff --git a/src/commands/CommandYeet.java b/src/commands/CommandYeet.java index ecfc18a..7bcaf2b 100644 --- a/src/commands/CommandYeet.java +++ b/src/commands/CommandYeet.java @@ -4,7 +4,7 @@ import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import core.Command; -import core.Config; +import core.Constants; import core.LocStrings; import dataStructures.KittyChannel; import dataStructures.KittyGuild; @@ -54,7 +54,7 @@ public class CommandYeet extends Command return; yeeteeFile = new File(yeeteeFilename); - ImageOverlayBuilder builder = new ImageOverlayBuilder(Config.AssetDirectory + "yeet/frames/", "yeet ", 24, 18); + ImageOverlayBuilder builder = new ImageOverlayBuilder(Constants.AssetDirectory + "yeet/frames/", "yeet ", 24, 18); builder.overlay(ImageIO.read(yeeteeFile), name); } catch (IOException e) diff --git a/src/core/BaseLocFile.java b/src/core/BaseLocFile.java index a139857..95bfdde 100644 --- a/src/core/BaseLocFile.java +++ b/src/core/BaseLocFile.java @@ -23,7 +23,6 @@ public abstract class BaseLocFile public static final String KittySourceDirectory = "./src"; // Filename - public final String filename; // Example: "localization.config"; public final String functionName; // Example: "Localizer.Stub"; // Local translation storage @@ -38,28 +37,11 @@ public abstract class BaseLocFile protected FileMonitor fileMonitor; // Constructor - public BaseLocFile(String filename, String functionName) + public BaseLocFile(String functionName) { - this.filename = filename; this.functionName = functionName; } - - // Checks the file specified for updates - public void update() - { - synchronized(stringStore) - { - fileMonitor.update(this::onFileChange); - } - } - - // When the file is changed - protected void onFileChange(MonitoredFile file) - { - log("Loc file was modified at path " + file.path); - updateLocFromDisk(); - } - + // Structure used for holding a pair of strings and any other info we need // about localized information that is being looked up. private class LocInfo @@ -127,63 +109,14 @@ public abstract class BaseLocFile return value; } - - // Reads a file to string, adapted from https://stackoverflow.com/a/326440/5383198 - private String readFileAsString(String path, Charset encoding) - { - try - { - byte[] encoded = Files.readAllBytes(Paths.get(path)); - return new String(encoded, encoding); - } - catch (IOException e) - { - warn("No file found to read from!"); - } - - return null; - } // Update localization from the disk on file. Creates the file if it doesn't exist. // This file is internally formatted as an ini file. - public void updateLocFromDisk() + public void updateLocFromString(String fileContents) { - log("Attempting to read localization file: " + filename); + log("Attempting to read localization file contents"); - try - { - String fileContents = readFileAsString(filename, Charset.defaultCharset()); - - if(fileContents == null) - { - File file = new File(filename); - file.createNewFile(); - } - - stringStore = new TaggedPairStore(fileContents); - } - catch(IOException e) - { - error("IO exception during localization file read"); - } - } - - // Rewrites out at the specified filename with existing stubs. - // This preserves existing localized phrases. - public void saveLocToDisk() - { - log("Attempting to write updated localization file"); - - try - { - PrintWriter pw = new PrintWriter(filename); - pw.println(stringStore.toString()); - pw.close(); - } - catch(IOException e) - { - error("IO exception during localization file write"); - } + stringStore = new TaggedPairStore(fileContents); } private void tryStripSpecified(Path path, ArrayList toFill) diff --git a/src/core/CommandEnabler.java b/src/core/CommandEnabler.java index 1bba27e..58878d6 100644 --- a/src/core/CommandEnabler.java +++ b/src/core/CommandEnabler.java @@ -23,7 +23,7 @@ public class CommandEnabler extends BaseKeyValueFile // Local variables private HashMap enabledMap; // Quick lookup private ArrayList keyList; // Tracking ordering for later - private final static String name = Config.AssetDirectory + "commands.config"; + private final static String name = Constants.AssetDirectory + "commands.config"; // Constructor public CommandEnabler() diff --git a/src/core/Config.java b/src/core/Config.java index a34b8e2..2cf3ca0 100644 --- a/src/core/Config.java +++ b/src/core/Config.java @@ -1,9 +1,28 @@ package core; +import java.nio.file.Path; -import java.awt.Color; +import utils.GlobalLog; +import utils.LogFilter; +import utils.io.MonitoredFile; -public final class Config +public class Config { - public static final Color ColorDefault = new Color(7*16, 8*16, 9*16); // A slate-grey - public static final String AssetDirectory = "./assets/"; + private static final Path filepath = Constants.AssetDirectory + Constants.ConfigFilename; + MonitoredFile configFile; + + Config instance; + + public Config() + { + if(instance == null) + { + configFile = new MonitoredFile(filepath); + instance = this; + } + else + { + GlobalLog.error(LogFilter.Core, "You can't have two of the following: " + this.getClass().getSimpleName()); + } + } + } diff --git a/src/core/Constants.java b/src/core/Constants.java new file mode 100644 index 0000000..9a08828 --- /dev/null +++ b/src/core/Constants.java @@ -0,0 +1,11 @@ +package core; + +import java.awt.Color; + +// Things that are internal only and aren't put inside an external config file. +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"; +} diff --git a/src/core/IConfigSection.java b/src/core/IConfigSection.java new file mode 100644 index 0000000..a632a67 --- /dev/null +++ b/src/core/IConfigSection.java @@ -0,0 +1,9 @@ +package core; + +public abstract interface IConfigSection +{ + public abstract String getHeader(); + public abstract void preUpdate(); + public abstract void init(); + public abstract String getContent(); +} diff --git a/src/core/LocCommands.java b/src/core/LocCommands.java index b32e690..f17e9b5 100644 --- a/src/core/LocCommands.java +++ b/src/core/LocCommands.java @@ -8,28 +8,22 @@ import dataStructures.Pair; // Performs the same localization for the strings associated with command names as // is performed with general strings in the application -public class LocCommands extends BaseLocFile +public class LocCommands extends BaseLocFile implements IConfigSection { - public static final String fileName = Config.AssetDirectory + "locCommands.config"; + public static final String HeaderName = "Localized Commands"; public static final String function = "LocCommands.stub"; private static LocCommands instance; public LocCommands() { - super(fileName, function); + super(function); GlobalLog.log(LogFilter.Core, "Initializing " + this.getClass().getSimpleName()); if(instance == null) { instance = this; - - updateLocFromDisk(); - scrapeAll(); - saveLocToDisk(); - - fileMonitor = new FileMonitor(filename); } else { @@ -51,8 +45,27 @@ public class LocCommands extends BaseLocFile return raw; } - public static void upkeep() - { - instance.update(); + @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/LocStrings.java b/src/core/LocStrings.java index d3c6ad6..29e2eb1 100644 --- a/src/core/LocStrings.java +++ b/src/core/LocStrings.java @@ -9,26 +9,20 @@ import utils.io.FileMonitor; // can then be localized. public class LocStrings extends BaseLocFile { - public static final String fileName = Config.AssetDirectory + "locStrings.config"; + public static final String fileName = Constants.AssetDirectory + "locStrings.config"; public static final String function = "LocStrings.stub"; private static LocStrings instance; public LocStrings() { - super(fileName, function); + super(function); GlobalLog.log(LogFilter.Core, "Initializing " + this.getClass().getSimpleName()); if(instance == null) { instance = this; - - updateLocFromDisk(); - scrapeAll(); - saveLocToDisk(); - - fileMonitor = new FileMonitor(filename); } else { diff --git a/src/core/ObjectBuilderFactory.java b/src/core/ObjectBuilderFactory.java index 986187f..65bfbb9 100644 --- a/src/core/ObjectBuilderFactory.java +++ b/src/core/ObjectBuilderFactory.java @@ -492,7 +492,7 @@ public class ObjectBuilderFactory lazyInit(); if(pluginManager == null) - pluginManager = new PluginManager(Config.AssetDirectory + "plugins/"); + pluginManager = new PluginManager(Constants.AssetDirectory + "plugins/"); return pluginManager; } diff --git a/src/core/benchmark/BenchmarkManager.java b/src/core/benchmark/BenchmarkManager.java index bb9711e..63bb096 100644 --- a/src/core/benchmark/BenchmarkManager.java +++ b/src/core/benchmark/BenchmarkManager.java @@ -5,7 +5,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import core.Config; +import core.Constants; import dataStructures.Pair; import utils.io.DirectoryMonitor; import utils.io.FileUtils; @@ -17,7 +17,7 @@ import utils.io.MonitoredFile; public class BenchmarkManager { // Variables - public final String directory = Config.AssetDirectory + "userbench/"; + public final String directory = Constants.AssetDirectory + "userbench/"; public final String extension = ".csv"; public final String lineDelimiter = "\n";