= Untested first pass enabling/disabling test

This commit is contained in:
Matthew Cech
2019-04-14 01:43:55 -07:00
parent fcfaa842d1
commit 04e9c60f02
6 changed files with 144 additions and 20 deletions
+95 -1
View File
@@ -1,17 +1,111 @@
package core; package core;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import utils.FileUtils;
import utils.GlobalLog;
import utils.LogFilter;
// While some of the patterns in this file are similar to the localization files,
// 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 public class CommandEnabler
{ {
// Config/const variables // Config/const variables
public static final String filename = "commands.config"; public static final String filename = "commands.config";
public static final String pairSplit = "=";
public static final char pairSeparator = '\n';
public static final String enabled = "on";
public static final String disabled = "off";
public static final boolean defaultEnabledState = true;
// Local variables // Local variables
private HashMap<String, Boolean> enabledMap; private HashMap<String, Boolean> enabledMap; // Quick lookup
private ArrayList<String> keyList; // Tracking ordering for later
public CommandEnabler() public CommandEnabler()
{ {
enabledMap = new HashMap<>(); enabledMap = new HashMap<>();
keyList = new ArrayList<>();
ReadIn();
GetTrackedCommands();
WriteOut();
}
// Reads in the config file and parses it, keeping tabs on the order it read things
private void ReadIn()
{
File f = new File(filename);
if(f.isFile() && f.canRead())
{
String content = FileUtils.ReadContent(f);
String[] lines = content.split("" + pairSeparator);
for(int i = 0; i < lines.length; ++i)
{
String[] pair = lines[i].split(pairSplit);
String key = pair[0].trim();
String value = pair[0].trim().toLowerCase();
keyList.add(key);
if(value == enabled)
enabledMap.putIfAbsent(key, true);
else
enabledMap.putIfAbsent(key, false);
}
}
}
// Look up the already scraped values from the localizer and store them if they
// don't already exist in the lookup. Defaults to defaultEnabledState.
private void GetTrackedCommands()
{
ArrayList<String> unloc = LocCommands.GetUnlocalizedCommands();
for(int i = 0; i < unloc.size(); ++i)
enabledMap.putIfAbsent(unloc.get(i), defaultEnabledState);
}
// Write out enabled/disabled file info.
private void WriteOut()
{
try
{
String outString = "";
for(int i = 0; i < keyList.size(); ++i)
{
String key = keyList.get(i);
String value = enabled;
if(enabledMap.get(key) == false)
value = disabled;
outString += key + pairSplit + value + pairSeparator;
}
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write(outString);
writer.close();
}
catch (IOException e)
{
GlobalLog.Error(LogFilter.Core, "Command enabler issue writing file! " + e.getMessage());
}
}
public boolean IsEnabled(String key)
{
if(enabledMap.containsKey(key))
return enabledMap.get(key);
return true;
} }
} }
+21 -9
View File
@@ -6,6 +6,7 @@ import java.util.Map.Entry;
import dataStructures.KittyChannel; import dataStructures.KittyChannel;
import dataStructures.KittyGuild; import dataStructures.KittyGuild;
import dataStructures.KittyUser; import dataStructures.KittyUser;
import dataStructures.Pair;
import dataStructures.Response; import dataStructures.Response;
import dataStructures.UserInput; import dataStructures.UserInput;
import utils.GlobalLog; import utils.GlobalLog;
@@ -16,25 +17,36 @@ public class CommandManager
// Variables // Variables
private HashMap<String, Command> commands; private HashMap<String, Command> commands;
private ArrayList<CommandThread> threadAccumulator; private ArrayList<CommandThread> threadAccumulator;
private CommandEnabler commandEnabler;
private long invokeCount; private long invokeCount;
// Default Constructor // Default Constructor
public CommandManager() public CommandManager(CommandEnabler commandEnabler)
{ {
commands = new HashMap<String, Command>(); this.commands = new HashMap<String, Command>();
threadAccumulator = new ArrayList<CommandThread>(); this.threadAccumulator = new ArrayList<CommandThread>();
invokeCount = 0; this.invokeCount = 0;
this.commandEnabler = commandEnabler;
} }
// Allows the command manager to keep track of a command. // Allows the command manager to keep track of a command. Takes a pair (the un-localized and localzied commands)
public void Register(String key, Command command) // and the command associated with the localized commands.
public void Register(Pair<String, String> pair, Command command)
{ {
if(key == null) if(pair == null || pair.Second == null)
return; return;
// If we haven't already split a multisplit command (or even assessed that),
// then verify if we even need to register the commands at all. If it's
// not enabled, don't register it.
if(pair.First != null && !commandEnabler.IsEnabled(pair.First))
return;
String key = pair.Second;
if(key.contains(",")) if(key.contains(","))
{ {
String[] keys = key.split(","); String[] keys = key.split(",");
Register(keys, command); Register(keys, command);
return; return;
@@ -58,7 +70,7 @@ public class CommandManager
public void Register(String[] keys, Command command) public void Register(String[] keys, Command command)
{ {
for(int i = 0; i < keys.length; ++i) for(int i = 0; i < keys.length; ++i)
Register(keys[i].trim(), command); Register(new Pair<String, String>(null, keys[i].trim()), command);
} }
// Calls the command but on a whole new thread! // Calls the command but on a whole new thread!
+3 -2
View File
@@ -34,9 +34,10 @@ public class LocCommands extends LocBase
} }
} }
public static String Stub(String toStub) // Returns a pair, the raw key and the stub key
public static Pair<String, String> Stub(String toStub)
{ {
return instance.GetKey(toStub); return new Pair<String, String>(toStub, instance.GetKey(toStub));
} }
// Gets all of the un-translated defaults in the commands list. // Gets all of the un-translated defaults in the commands list.
+21 -7
View File
@@ -39,6 +39,9 @@ public class ObjectBuilderFactory
@SuppressWarnings("unused") private static LocStrings locStrings; @SuppressWarnings("unused") private static LocStrings locStrings;
@SuppressWarnings("unused") private static LocCommands locCommands; @SuppressWarnings("unused") private static LocCommands locCommands;
// Handles if we can or can't use specific commands, parsing a config file based on loc data to do so.
private static CommandEnabler commandEnabler;
// 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
private static boolean hasInitialized; private static boolean hasInitialized;
@@ -289,15 +292,26 @@ public class ObjectBuilderFactory
user.avatarID = member.getUser().getAvatarUrl(); user.avatarID = member.getUser().getAvatarUrl();
} }
// Default construction of the command manager. // Constructs a CommandEnabler if it doesn't exist, and gets the existing one if it does.
// TODO(wisp): We want to be able to keep all this data public static CommandEnabler ConstructCommandEnabler()
// stored off in a file at some point, so we can reflect it onto the {
// project and build it per-guild. That's for later now tho. LazyInit();
public static CommandManager ConstructCommandManager()
if(commandEnabler == null)
commandEnabler = new CommandEnabler();
return commandEnabler;
}
// Default construction of the command manager. In order to remotely resolve command enabling
// and disabling, what we do is construct the commands with a localized pair that is checked against
// the CommandEnabler object passed in. In theory, we could have multiple CommandManagers, tho we can
// only have one CommandEnabler.
public static CommandManager ConstructCommandManager(CommandEnabler commandEnabler)
{ {
LazyInit(); LazyInit();
CommandManager manager = new CommandManager(); CommandManager manager = new CommandManager(commandEnabler);
manager.Register(LocCommands.Stub("work"), new CommandDoWork(KittyRole.Dev, KittyRating.Safe)); manager.Register(LocCommands.Stub("work"), new CommandDoWork(KittyRole.Dev, KittyRating.Safe));
manager.Register(LocCommands.Stub("shutdown"), new CommandShutdown(KittyRole.Dev, KittyRating.Safe)); manager.Register(LocCommands.Stub("shutdown"), new CommandShutdown(KittyRole.Dev, KittyRating.Safe));
@@ -341,7 +355,7 @@ public class ObjectBuilderFactory
return manager; return manager;
} }
// NOTE(wisp): Default database manager construction. It can be constructed // Default database manager construction. It can be constructed
// in different ways, and so we construct it outside of the constructor for // in different ways, and so we construct it outside of the constructor for
// the factory since it doesn't have to be present / can be elsewhere. // the factory since it doesn't have to be present / can be elsewhere.
// Effectively we cache the database here. // Effectively we cache the database here.
+3 -1
View File
@@ -25,6 +25,7 @@ public class Main extends ListenerAdapter
// Variables and stuff // Variables and stuff
private static JDA kitty; private static JDA kitty;
private static CommandManager commandManager; private static CommandManager commandManager;
private static CommandEnabler commandEnabler;
private static DatabaseManager databaseManager; private static DatabaseManager databaseManager;
private static Stats stats; private static Stats stats;
private static RPManager rpManager; private static RPManager rpManager;
@@ -34,7 +35,8 @@ public class Main extends ListenerAdapter
{ {
// Factory startup. The ordering is intentional. // Factory startup. The ordering is intentional.
databaseManager = ObjectBuilderFactory.ConstructDatabaseManager(); databaseManager = ObjectBuilderFactory.ConstructDatabaseManager();
commandManager = ObjectBuilderFactory.ConstructCommandManager(); commandEnabler = ObjectBuilderFactory.ConstructCommandEnabler();
commandManager = ObjectBuilderFactory.ConstructCommandManager(commandEnabler);
rpManager = ObjectBuilderFactory.ConstructRPManager(); rpManager = ObjectBuilderFactory.ConstructRPManager();
stats = ObjectBuilderFactory.ConstructStats(commandManager); stats = ObjectBuilderFactory.ConstructStats(commandManager);
+1
View File
@@ -12,6 +12,7 @@ import java.util.stream.Stream;
public class FileUtils public class FileUtils
{ {
// Reads all lines from a file as a string // Reads all lines from a file as a string
public static String ReadContent(File file) { return ReadContent(file.toPath()); }
public static String ReadContent(Path filePath) public static String ReadContent(Path filePath)
{ {
StringBuilder contentBuilder = new StringBuilder(); StringBuilder contentBuilder = new StringBuilder();