= Updated initialization of ObjectBuilderFactory to include localizers

This commit is contained in:
Matthew Cech
2019-04-14 00:29:31 -07:00
parent f00160714a
commit 841cc51ed1
6 changed files with 80 additions and 46 deletions
+17
View File
@@ -0,0 +1,17 @@
package core;
import java.util.HashMap;
public class CommandEnabler
{
// Config/const variables
public static final String filename = "commands.config";
// Local variables
private HashMap<String, Boolean> enabledMap;
public CommandEnabler()
{
enabledMap = new HashMap<>();
}
}
+3 -3
View File
@@ -8,7 +8,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import dataStructures.SectionedKeyValueStore;
import dataStructures.TaggedPairStore;
import utils.FileUtils;
import utils.GlobalLog;
import utils.LogFilter;
@@ -25,7 +25,7 @@ public abstract class LocBase
public final String functionName; // Example: "Localizer.Stub";
// Local translation storage
private SectionedKeyValueStore stringStore;
protected TaggedPairStore stringStore;
// Logging
private void Log(String str) { GlobalLog.Log(LogFilter.Strings, str); }
@@ -139,7 +139,7 @@ public abstract class LocBase
file.createNewFile();
}
stringStore = new SectionedKeyValueStore(fileContents);
stringStore = new TaggedPairStore(fileContents);
}
catch(IOException e)
{
+12
View File
@@ -1,8 +1,12 @@
package core;
import java.util.ArrayList;
import utils.GlobalLog;
import utils.LogFilter;
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 LocBase
{
public static final String fileName = "locCommands.config";
@@ -34,4 +38,12 @@ public class LocCommands extends LocBase
{
return instance.GetKey(toStub);
}
// Gets all of the un-translated defaults in the commands list.
public static ArrayList<String> GetUnlocalizedCommands()
{
ArrayList<String> raw = new ArrayList<>();
instance.stringStore.ForEach((pair) -> raw.add((String)((Pair<?, ?>)pair).First ));
return raw;
}
}
+20 -7
View File
@@ -13,7 +13,7 @@ import utils.LogFilter;
// NOTE(wisp): Isolated factory to assist with storage and caching if needed.
// This also minimizes the number of places JDA interacts with our codebase.
// As it stands, if the object name begins with Kitty, it's constructed here.
// TODO: Make all methods ID based instead of event based
// TODO: Make all methods ID based instead of event based
public class ObjectBuilderFactory
{
// Key: guild string id, Value: guild information
@@ -34,9 +34,17 @@ public class ObjectBuilderFactory
// RPManger for tracking RP system
private static RPManager rpManager;
// Lazy initialization style for
// Localization classes - these are singletons, but should be initialized before almost all other
// things so their inclusion in the factory is to ensure they're started at the correct time.
@SuppressWarnings("unused") private static Localizer locStrings;
@SuppressWarnings("unused") private static LocCommands locCommands;
// Lazy initialization multithreaded mutex stuff to prevent explosions.
// TODO: Investigate using 'synchronized' instead potentially
private static boolean hasInitialized;
private static Semaphore initMutex = new Semaphore(1);
// This is it, this is how the lazy init starts!
private static void LazyInit()
{
if(hasInitialized)
@@ -47,26 +55,31 @@ public class ObjectBuilderFactory
initMutex.acquire();
try
{
// Initialization here. This is where we could read from something external.
// structure initialization
// Construct necessary data structures.
guildCache = new HashMap<String, KittyGuild>();
userCache = new HashMap<String, KittyUser>();
channelCache = new HashMap<String, KittyChannel>();
database = null;
stats = 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.
locStrings = new Localizer();
locCommands = new LocCommands();
}
finally
{
initMutex.release();
hasInitialized = true;
}
}
catch(InterruptedException ie)
{
GlobalLog.Error(LogFilter.Core, "Issue during object builder lazy initialization."
+ " The factory was not initialized, "
+ "and kitty will not be able to continue functionally.");
+ " The factory was not initialized, and kitty will not be able to continue functionally.");
}
hasInitialized = true;
}
// Explicitly locks: guildCache