Virtualized the CommandsManager rather than using an interface for it
This commit is contained in:
parent
c89cd76396
commit
c48543138d
@ -3,7 +3,7 @@
|
|||||||
using Cogwheel;
|
using Cogwheel;
|
||||||
|
|
||||||
ICogwheelConsole cogwheelConsole = new DeafultCogwheelConsole();
|
ICogwheelConsole cogwheelConsole = new DeafultCogwheelConsole();
|
||||||
CommandsManager commandsManager = new CommandsManager(cogwheelConsole);
|
CommandsManager commandsManager = new CommandsManager();
|
||||||
|
|
||||||
COGWHEEL.Initialize(commandsManager, cogwheelConsole);
|
COGWHEEL.Initialize(commandsManager, cogwheelConsole);
|
||||||
|
|
||||||
|
@ -2,23 +2,15 @@ namespace Cogwheel;
|
|||||||
|
|
||||||
public static class COGWHEEL
|
public static class COGWHEEL
|
||||||
{
|
{
|
||||||
/* TODO:
|
|
||||||
* -[X] Initialize the CommandsManager
|
|
||||||
* -[ ] Public static methods for the CommandsManager
|
|
||||||
* -[X] Create built-in commands
|
|
||||||
* -[ ]
|
|
||||||
* -[ ]
|
|
||||||
* -[ ]
|
|
||||||
*/
|
|
||||||
|
|
||||||
private static CommandsManager _commandsManager;
|
private static CommandsManager _commandsManager;
|
||||||
private static ICogwheelConsole _console;
|
private static ICogwheelConsole _console;
|
||||||
|
|
||||||
public static void Initialize(CommandsManager commandsManager, ICogwheelConsole console)
|
public static void Initialize(CommandsManager commandsManager, ICogwheelConsole console)
|
||||||
{
|
{
|
||||||
_commandsManager = commandsManager;
|
|
||||||
_console = console;
|
_console = console;
|
||||||
|
_commandsManager = commandsManager;
|
||||||
|
|
||||||
|
_commandsManager.Initialize(_console);
|
||||||
_console.Initialize(_commandsManager);
|
_console.Initialize(_commandsManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,30 +8,28 @@ namespace Cogwheel;
|
|||||||
|
|
||||||
public class CommandsManager
|
public class CommandsManager
|
||||||
{
|
{
|
||||||
public ICogwheelConsole CogwheelConsole { get; set; }
|
protected virtual ICogwheelConsole CogwheelConsole { get; set; }
|
||||||
|
protected virtual string CommandPattern { get; set; } = "(?<val>(\\([^\\)]+\\)))|\"(?<val>[^\"]+)\"|'(?<val>[^']+)'|(?<val>[^\\s]+)";
|
||||||
|
|
||||||
public string CommandPattern { get; set; } = "(?<val>(\\([^\\)]+\\)))|\"(?<val>[^\"]+)\"|'(?<val>[^']+)'|(?<val>[^\\s]+)";
|
protected virtual List<Assembly> Assemblies { get; set; } = [];
|
||||||
public List<Assembly> Assemblies { get; set; } = [];
|
public virtual Dictionary<string, ICommand> Commands { get; set; } = new();
|
||||||
public Dictionary<string, ICommand> Commands { get; set; } = new();
|
public virtual Dictionary<Type, Func<string, object>> CustomParsers { get; set; } = new();
|
||||||
public Dictionary<Type, Func<string, object>> CustomParsers { get; set; } = new();
|
|
||||||
|
|
||||||
// Context related stuff
|
// Context related stuff
|
||||||
public object? CurrentContext { get; set; }
|
public virtual object? CurrentContext { get; set; }
|
||||||
public Guid? CurrentContextGuid { get; set; }
|
public virtual Guid? CurrentContextGuid { get; set; }
|
||||||
public Dictionary<Guid, object> RegisteredObjectInstances { get; set; } = new();
|
public virtual Dictionary<Guid, object> RegisteredObjectInstances { get; set; } = new();
|
||||||
public Dictionary<object, Guid> RegisteredObjectGuids { get; set; } = new();
|
public virtual Dictionary<object, Guid> RegisteredObjectGuids { get; set; } = new();
|
||||||
|
|
||||||
|
public virtual void Initialize(ICogwheelConsole console)
|
||||||
public CommandsManager(ICogwheelConsole console)
|
|
||||||
{
|
{
|
||||||
CogwheelConsole = console;
|
CogwheelConsole = console;
|
||||||
|
|
||||||
RefreshCommandsList();
|
RefreshCommandsList();
|
||||||
CogwheelConsole.Log($"CommandsManager initialized, Commands found: {Commands.Count}");
|
CogwheelConsole.Log($"CommandsManager initialized, Commands found: {Commands.Count}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public (ICommand, List<object>)? GetCommandAndArgsFromString(string input)
|
public virtual (ICommand, List<object>)? GetCommandAndArgsFromString(string input)
|
||||||
{
|
{
|
||||||
var splitString = Regex.Matches(input, CommandPattern).Select(m => m.Groups["val"].Value).ToArray();
|
var splitString = Regex.Matches(input, CommandPattern).Select(m => m.Groups["val"].Value).ToArray();
|
||||||
var commandName = splitString[0];
|
var commandName = splitString[0];
|
||||||
@ -49,7 +47,7 @@ public class CommandsManager
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool RunCommand(string input, object? context = null)
|
public virtual bool RunCommand(string input, object? context = null)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(input)) return false;
|
if (string.IsNullOrWhiteSpace(input)) return false;
|
||||||
var command = GetCommandAndArgsFromString(input);
|
var command = GetCommandAndArgsFromString(input);
|
||||||
@ -90,7 +88,7 @@ public class CommandsManager
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetCommandUsage(ICommand command)
|
public virtual string GetCommandUsage(ICommand command)
|
||||||
{
|
{
|
||||||
string paramUsage = string.Join(" ",
|
string paramUsage = string.Join(" ",
|
||||||
command.Method.GetParameters().Select(param =>
|
command.Method.GetParameters().Select(param =>
|
||||||
@ -99,7 +97,7 @@ public class CommandsManager
|
|||||||
return $"{command.Name}: {paramUsage}";
|
return $"{command.Name}: {paramUsage}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ExecuteCommand(object obj, (ICommand command, List<object> args) command)
|
public virtual bool ExecuteCommand(object obj, (ICommand command, List<object> args) command)
|
||||||
{
|
{
|
||||||
var parameters = command.command.Method.GetParameters();
|
var parameters = command.command.Method.GetParameters();
|
||||||
for (var parameterIndex = 0; parameterIndex < parameters.Length; parameterIndex++)
|
for (var parameterIndex = 0; parameterIndex < parameters.Length; parameterIndex++)
|
||||||
@ -163,7 +161,7 @@ public class CommandsManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool TryParseParameter(Type parameterType, string parameterString, out object parsedValue)
|
public virtual bool TryParseParameter(Type parameterType, string parameterString, out object parsedValue)
|
||||||
{
|
{
|
||||||
if (parameterType == typeof(string))
|
if (parameterType == typeof(string))
|
||||||
{
|
{
|
||||||
@ -209,14 +207,14 @@ public class CommandsManager
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ICommand? GetCommandByName(string commandName)
|
public virtual ICommand? GetCommandByName(string commandName)
|
||||||
{
|
{
|
||||||
return Commands.GetValueOrDefault(commandName);
|
return Commands.GetValueOrDefault(commandName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Context related stuff
|
// Context related stuff
|
||||||
|
|
||||||
public void RegisterObject(object obj)
|
public virtual void RegisterObject(object obj)
|
||||||
{
|
{
|
||||||
// Use a combination of the object's type and a hash of its properties to generate a deterministic GUID
|
// Use a combination of the object's type and a hash of its properties to generate a deterministic GUID
|
||||||
string seed = obj.GetType().FullName + GetObjectPropertiesHash(obj);
|
string seed = obj.GetType().FullName + GetObjectPropertiesHash(obj);
|
||||||
@ -238,7 +236,7 @@ public class CommandsManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsCommandContextValid(ICommand command, object? context = null)
|
public virtual bool IsCommandContextValid(ICommand command, object? context = null)
|
||||||
{
|
{
|
||||||
if (context is not null)
|
if (context is not null)
|
||||||
{
|
{
|
||||||
@ -247,7 +245,7 @@ public class CommandsManager
|
|||||||
return command.Method.DeclaringType == CurrentContext?.GetType();
|
return command.Method.DeclaringType == CurrentContext?.GetType();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SetContext(object context)
|
public virtual bool SetContext(object context)
|
||||||
{
|
{
|
||||||
CurrentContext = context;
|
CurrentContext = context;
|
||||||
if (!RegisteredObjectGuids.ContainsKey(context))
|
if (!RegisteredObjectGuids.ContainsKey(context))
|
||||||
@ -259,7 +257,7 @@ public class CommandsManager
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SetContext(Guid guid)
|
public virtual bool SetContext(Guid guid)
|
||||||
{
|
{
|
||||||
if (RegisteredObjectInstances.ContainsKey(guid))
|
if (RegisteredObjectInstances.ContainsKey(guid))
|
||||||
{
|
{
|
||||||
@ -275,7 +273,7 @@ public class CommandsManager
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SetContext(string guidString)
|
public virtual bool SetContext(string guidString)
|
||||||
{
|
{
|
||||||
if (Guid.TryParse(guidString, out var guid))
|
if (Guid.TryParse(guidString, out var guid))
|
||||||
{
|
{
|
||||||
@ -286,12 +284,12 @@ public class CommandsManager
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public object? GetFirstValidRegisteredContext(Type type)
|
public virtual object? GetFirstValidRegisteredContext(Type type)
|
||||||
{
|
{
|
||||||
return RegisteredObjectInstances.Values.FirstOrDefault(obj => obj.GetType() == type);
|
return RegisteredObjectInstances.Values.FirstOrDefault(obj => obj.GetType() == type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Guid GetGuidFromContext(object? context = null)
|
public virtual Guid GetGuidFromContext(object? context = null)
|
||||||
{
|
{
|
||||||
if (context is not null)
|
if (context is not null)
|
||||||
{
|
{
|
||||||
@ -301,7 +299,7 @@ public class CommandsManager
|
|||||||
return CurrentContextGuid ?? Guid.Empty;
|
return CurrentContextGuid ?? Guid.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetObjectPropertiesHash(object obj)
|
protected virtual string GetObjectPropertiesHash(object obj)
|
||||||
{
|
{
|
||||||
var properties = obj.GetType().GetProperties();
|
var properties = obj.GetType().GetProperties();
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
@ -315,7 +313,7 @@ public class CommandsManager
|
|||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RefreshCommandsList()
|
protected virtual void RefreshCommandsList()
|
||||||
{
|
{
|
||||||
foreach (var type in Assembly.GetCallingAssembly().GetTypes())
|
foreach (var type in Assembly.GetCallingAssembly().GetTypes())
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user