yasirkula / UnityIngameDebugConsole

A uGUI based console to see debug messages and execute commands during gameplay in Unity
MIT License
2.05k stars 217 forks source link

Add method to return all commands. Add delegate to list for any commands executed #92

Closed brogan89 closed 5 months ago

brogan89 commented 5 months ago

I needed a way to remove all commands that I've added as my users were using scene.load etc and wasn't working with my custom scene management. I couldn't find a way to loop over all the commands that didn't require reflection.

The delegate I added was mainly to log to console any command that had run so when I got bug reports in Sentry I could see what they were trying to do.

Below is an example use case for these features.

    private void Start()
    {
        DebugLogConsole.OnCommandExecuted += OnCommandExecuted;
        RemoveStockCommands();
    }

    private static void OnCommandExecuted(string command, object[] parameters)
    {
        if (parameters?.Length > 0)
            Debug.Log($"Command executed '{command}' with parameters: {string.Join(", ", parameters)}");
        else
            Debug.Log($"Command executed: {command}");
    }

    private static void RemoveStockCommands()
    {
        // need to copy as can cause enumeration exceptions
        var comamnds = new List<ConsoleMethodInfo>(DebugLogConsole.GetAllCommands());
        foreach (var m in comamnds)
        {
            if (!m.command.StartsWith('/'))
                DebugLogConsole.RemoveCommand(m.command);
        }
    }
yasirkula commented 5 months ago

Thank you! I've removed the XML comments because the member names were explanatory enough and added an actual null check before the event invocation because Unity 5.6 doesn't support the ?.Invoke syntax (I'm still supporting that version 🙃).

BTW, in the next release, I'll remove all commands except help from the plugin. You're right that the built-in commands should be opt-in, not opt-out.

brogan89 commented 5 months ago

Sounds good! Thanks