= Iterative step towards combined config file
file shall be config.config, because config.
This commit is contained in:
@@ -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<LocInfo> toFill)
|
||||
|
||||
@@ -23,7 +23,7 @@ public class CommandEnabler extends BaseKeyValueFile
|
||||
// Local variables
|
||||
private HashMap<String, Boolean> enabledMap; // Quick lookup
|
||||
private ArrayList<String> 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()
|
||||
|
||||
+23
-4
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
+25
-12
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user