How do I use Map Directives in C#?

I wonder how Map Directives can be used on C#. I have tried to understand the code on the original resources but it feels a bit weird with the lambda expressions.

  • How can I create/use Map Directives on C#?
  • Can Map Directives be used from both the Client and Server side?

Probably something like this, assuming you won’t mind the DLR overhead on initial call:

EventHandlers["getMapDirectives"] += new Action<dynamic>(addCb => 
{
    addCb('whatever', new Action<dynamic, dynamic>((state, arg) =>
    {
        state.add('whatever', arg);

        Debug.WriteLine($"Adding {arg}");
    }, new Action<dynamic>(state =>
    {
        Debug.WriteLine($"Destroying {state.whatever}.");
    });
});

Which leads to a directive like

whatever 'arg'

For a 2-argument chain, simply return a closure like Lua does:

EventHandlers["getMapDirectives"] += new Action<dynamic>(addCb => 
{
    addCb('whatever2', new Action<dynamic, dynamic>((state, arg) =>
    {
        return new Action<dynamic>(arg2 =>
        {
            state.add('whatever', arg);
            state.add('whatever2', arg2.potato);

            Debug.WriteLine($"Added {arg} {arg2.potato}.");
        });
    }, new Action<dynamic>(state =>
    {
        Debug.WriteLine($"Destroying {state.whatever}/{state.whatever2}.");
    });
});

Usage:

whatever2 'meh' { potato = 42 }

Yes.