Skip to content

Handler

BIGDummyHead edited this page May 9, 2022 · 4 revisions

The handler is the main part of this library that can gather all Commands inside of a ICommandModule and use your objects and strings to invoke those commands (methods).

So, here is how we can do it!

Registration

Boiler plate code to allow for simple registration

class Example : ICommandModule
{
     public Example() {} //the ctor MUST be EMPTY for registration to complete!
 
     public ValueTask OnCommandExecute(MethodInfo method, object instance, object[] invokes, object? returnInstance)
     {
          throw new NotImplementedException(); //do not use this code.
     }

     [Command] //this will set the command.name to 'Ex'
     //or:
     //[Command("customName")] 
     void Ex(int a) //we can also make these 'async Task<T>'
     {
         //output:
         Console.WriteLine(a);
     }
     
     [Ignore] //we can tell the handler to ignore this method from being collected.
     [Command] //even with this applied the ignore attribute rules over.
     void Ex2(int a)
     {}


     //we will have to pass in a IStringConverter and int from our side!
     [Command]
     void Ex3(IStringConverter preArg, string a, int aftArg) 
     {
         //do something with the preArg!
         Console.WriteLine(a);
         Console.WriteLine(aftArg);
     }
}

Adding a module

Handler handler = new(); //optionally place in a config

//the type must inherit ICommandModule as show above
handler.RegisterModule<Example>();
//or:
//handler.RegisterModule(typeof(Example)); 

Removing a module

handler.UnRegisterModule<Example>();
//or: 
//handler.UnRegisterModule(typeof(Example));

Invoking

Finally, once we've registered our ICommandModule(s) we can simply invoke our commands!

Using the Example class we created before and registered. Let's go ahead an invoke the Ex(int) method/command.

//our prefix is '?'
handler.Invoke("?ex 10");
//output: "10" ~ to the console

object[] preArgs = Extensions.FastArr(new StringConverter());
object[] aftArgs = Extensions.FastArr(200);

//invoking a command with Pre args and Aft args!
//we can use this to always pass in a constant value for end users to make cooler stuff!
handler.Invoke(preArgs, "?ex3 10", aftArgs);

Config

When making a new Handler you can provide in a Handler.Config that determines how your Invoke method reacts to certain things. For example the config has prefixes, separators, always trim, allow nulls, ignore case, and your logger!

We can set the default configuration as so!

Handler.Config.Default = config;

//we can get it as so

Handler.Config conf = Handler.Config.Default; //this creates a new instance of the default, does not just get an already created instance.

Collected Commands

For every collected command or registered a new instance of CollectedCommand is made!

This class may only be made by the library

This includes information about the command such as:

  • Method ~ The method info of the command
  • CommandAttribute ~ The command attribute collected from the MethodInfo
  • Instance ~ The instance of the Method's type
  • IsIgnored ~ Is the command ignored? This is almost ALWAYS false.
  • Parameters ~ The ParameterInfo[] of the Method
  • ParameterAttributes ~ A dictionary(readonly) that is of ParameterInfo to IEnumerable<CommandParameterAttribute>
  • Name ~ The name of the command. This will either be a custom name or the Method's name.
Clone this wiki locally