Class BaseChatEngine<T>
- Namespace
- BSGameLib.Chat
- Assembly
- BSGameLib.Chat.dll
The base for a chat engine. It runs the command a message starts with, when the message starts with the command character, and raises events for the outcome. A subclass implements Handle(T, string) and decides what to do with lines that are not commands.
public abstract class BaseChatEngine<T> : IChatEngine<T> where T : IMessageSource
Type Parameters
T
- Inheritance
-
BaseChatEngine<T>
- Implements
-
IChatEngine<T>
- Inherited Members
Remarks
Commands from attributes are created by the ICommandFactoryProvider the engine is given, or, when it has none, with a parameterless constructor.
Constructors
BaseChatEngine(IEnumerable<ICommandProvider>, ICommandFactoryProvider, char)
Creates an engine over the given command providers.
protected BaseChatEngine(IEnumerable<ICommandProvider> commandProviders, ICommandFactoryProvider commandFactoryProvider, char commandCharacter = '/')
Parameters
commandProvidersIEnumerable<ICommandProvider>The providers whose command types Initialize() registers.
commandFactoryProviderICommandFactoryProviderCreates command objects. When null, each command is created with its parameterless constructor.
commandCharactercharThe character a command message starts with.
Properties
CommandCharacter
The character that marks a message as a command, '/' by default.
protected char CommandCharacter { get; }
Property Value
Commands
The registered commands and command groups, at the top level.
public IReadOnlyCollection<ICommand> Commands { get; }
Property Value
ReplacementStrings
The tokens registered with RegisterStringReplacement(string, Func<IMessageContext<T>, string>).
public IReadOnlyCollection<string> ReplacementStrings { get; }
Property Value
Methods
CreateCommand(string)
Starts a command built in code. Build on the result registers it.
public ICommandDirector CreateCommand(string name)
Parameters
namestring
Returns
CreateCommandGroup(string)
Starts a command group built in code. Build on the result registers it.
public ICommandGroupDirector CreateCommandGroup(string name)
Parameters
namestring
Returns
DescribeCommands()
Describes every registered command, sorted by name, with the sub-commands of each group under the group.
public IReadOnlyList<CommandDescription> DescribeCommands()
Returns
Handle(T, string)
Handles a line the source sent. Typically it calls TryHandleCommand(T, ref string) first and handles the line as chat when that returns false.
public abstract void Handle(T source, string message)
Parameters
sourceTmessagestring
HasCommandCharacter(string)
Returns whether message starts with the command character.
public bool HasCommandCharacter(string message)
Parameters
messagestring
Returns
Remarks
message must not be empty.
Initialize()
Registers the command types of the command providers. A later call does nothing.
public virtual void Initialize()
RegisterCommand(ICommand)
Registers a command. A command built with CreateCommand(string) or CreateCommandGroup(string) is registered as built. Any other command is registered by its type, from its attributes, and the instance passed is not kept.
public void RegisterCommand(ICommand command)
Parameters
commandICommand
RegisterCommand<TCommand>()
Registers the command type TCommand from its attributes. A
sub-command is added to its group instead.
public void RegisterCommand<TCommand>() where TCommand : ICommand
Type Parameters
TCommand
Exceptions
- ArgumentException
TCommandhas no CommandAttribute.- InvalidOperationException
The engine has no factory provider and the command has no parameterless constructor.
RegisterStringReplacement(string, Func<IMessageContext<T>, string>)
Registers a token that is replaced in every message before it is handled, such as
%LOCATION. Tokens are matched ignoring case, and where two overlap the longer one is
used.
public void RegisterStringReplacement(string replacementText, Func<IMessageContext<T>, string> action)
Parameters
replacementTextstringactionFunc<IMessageContext<T>, string>
Remarks
action is called only when its token is in a message. A null result is
replaced with an empty string.
Exceptions
- ArgumentException
replacementTextis already registered, in any case.
TryHandleCommand(T, ref string)
Runs message as a command when it starts with the command character.
Returns true when the line was handled: a command, a command that does not exist, or a blank
line. Returns false for chat.
public virtual bool TryHandleCommand(T source, ref string message)
Parameters
sourceTmessagestring
Returns
Remarks
Replacement tokens are filled in first, so message is updated whether or
not it is a command. A blank message is handled and returns true.
Exceptions
- ArgumentNullException
sourceis null and the message is not blank.
Events
CommandExecuted
Raised after a command runs successfully.
public event EventHandler<CommandExecutedEventArgs<T>> CommandExecuted
Event Type
CommandNotFound
Raised when a message starts with the command character but names no registered command. The message is still handled; it does not become chat.
public event EventHandler<CommandEventArgs<T>> CommandNotFound
Event Type
MissingParameterOnCommand
Raised once for each required parameter a command was given no value for. The command does not run.
public event EventHandler<MissingParameterEventArgs<T>> MissingParameterOnCommand
Event Type
WrongParameterOnCommand
A command was given a value one of its parameters cannot take; it did not run.
public event EventHandler<WrongParameterEventArgs<T>> WrongParameterOnCommand