Aivaria
Aivaria

Reputation: 5

C# Command parser

im currently expanding my knowledge a little, and wanted to Create a little game for myself.

The Structure is as Following: Programm.cs creates an instance of Gamecontroller. This Gamecontroller is the lowest level i want to Access. It will create instaces of the Views, and from classes like config.

I want to implement an debug Console with Command Input. These Commands should always start at the Gamecontroller level, and should be able to interact with kinda everything i could do with C# code.

So i want to access the Objects, Member and methods withing Gamecontroller, or Within any nested object.

Currently i cant get to the Properties of an Child, because _member returns an "Type" which gets parsed to RuntimeProperty instead of the Class

Example on Parsing:

"objPlayer.name" > "GameController.objPlayer.name"
"objConfig.someSetting = 10" > "GameController.objConfig.someSetting=10"
"objConfig.functionCall()" > "GameController.objConfig.functionCall()"
"objConfig.objPlayer.setName("someName")" > "GameController.objConfig.objPlayer.setName("someName")"
"objPlayer.name" > "GameController.objPlayer.name"

this is what i got so far:

    private void parseComamnd(string Command)
    {
        var actions = Command.Split('.');
        var start = this.GetType();
        var last = actions[actions.Length - 1];
        foreach (var action in actions)
        {
            if (action.Contains("(") && action.Contains(")"))
            {
                _exec(start, action);
            }
            else
            {
                start = _member(start, action);
            }
        }
    }
    private Type _member(Type pHandle, string pStrMember)
    {
        return pHandle.GetProperty(pStrMember).GetType();
    }
    private void _exec(Type pHandle, string pStrFunction)
    {
        var Regex = new Regex(@"\(|,|\)");
        var FunctionParams = Regex.Split(pStrFunction);
        var FunctionName = FunctionParams[0];
        FunctionParams[0] = "";
        FunctionParams = FunctionParams.Where(val => val != "").ToArray();
        pHandle.GetMethod(FunctionName).Invoke(FunctionName, FunctionParams);
    }

Upvotes: 0

Views: 368

Answers (1)

Sergei Russkikh
Sergei Russkikh

Reputation: 123

If I understood right, you want to match some string commands with actions you want to perform. In this case you could use Dictionary as a storage for string-delgate couples to match your string commands to actions you want to perform. As an advantage of this approach, you can change matched couples during program runtime as you wish

class SomeClass
{
    delegate void OperationDelegate(string value);
    IDictionary<string, OperationDelegate> Operations = new Dictionary<string, OperationDelegate>();

    public SomeClass()
    {
        Operations.Add("objPlayer.name", SetName);
        Operations.Add("objConfig.someSetting", SetSetting); 
    }

    public void HandleNewValue(string command, string value)
    {
        try
        {
            if (Operations.ContainsKey(command))
                Operations[command](value);
        }
        catch (Exception e)
        {
            Logger.Error(e);
        }
    }

    private void SetName(string value)
    {
        // Some logic there
    }

    private void SetSetting(string value)
    {
        // Some logic there
    }
}

Upvotes: 1

Related Questions