Table of Contents

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
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

commandProviders IEnumerable<ICommandProvider>

The providers whose command types Initialize() registers.

commandFactoryProvider ICommandFactoryProvider

Creates command objects. When null, each command is created with its parameterless constructor.

commandCharacter char

The 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

char

Commands

The registered commands and command groups, at the top level.

public IReadOnlyCollection<ICommand> Commands { get; }

Property Value

IReadOnlyCollection<ICommand>

ReplacementStrings

public IReadOnlyCollection<string> ReplacementStrings { get; }

Property Value

IReadOnlyCollection<string>

Methods

CreateCommand(string)

Starts a command built in code. Build on the result registers it.

public ICommandDirector CreateCommand(string name)

Parameters

name string

Returns

ICommandDirector

CreateCommandGroup(string)

Starts a command group built in code. Build on the result registers it.

public ICommandGroupDirector CreateCommandGroup(string name)

Parameters

name string

Returns

ICommandGroupDirector

DescribeCommands()

Describes every registered command, sorted by name, with the sub-commands of each group under the group.

public IReadOnlyList<CommandDescription> DescribeCommands()

Returns

IReadOnlyList<CommandDescription>

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

source T
message string

HasCommandCharacter(string)

Returns whether message starts with the command character.

public bool HasCommandCharacter(string message)

Parameters

message string

Returns

bool

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

command ICommand

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

TCommand has 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

replacementText string
action Func<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

replacementText is 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

source T
message string

Returns

bool

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

source is null and the message is not blank.

Events

CommandExecuted

Raised after a command runs successfully.

public event EventHandler<CommandExecutedEventArgs<T>> CommandExecuted

Event Type

EventHandler<CommandExecutedEventArgs<T>>

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

EventHandler<CommandEventArgs<T>>

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

EventHandler<MissingParameterEventArgs<T>>

WrongParameterOnCommand

A command was given a value one of its parameters cannot take; it did not run.

public event EventHandler<WrongParameterEventArgs<T>> WrongParameterOnCommand

Event Type

EventHandler<WrongParameterEventArgs<T>>