alliedmodders / sourcemod

SourceMod - Source Engine Scripting and Administration
http://www.sourcemod.net/
972 stars 420 forks source link

Add OnCommandRegistered() forward #1481

Open dragokas opened 3 years ago

dragokas commented 3 years ago

Use case: !COMMAND eater.

When some plugin gets dynamically loaded/unloaded we have no way to identify what commands are added other than making a full commands enumeration with a significant CPU cycle loss. We don't even know when to do such enumeration, because no OnPluginLoaded() forward exists at all (second problem).

So, I suggest to add:

forward OnCommandRegistered(char[] name)

forward OnCommandUnregistered(char[] name)

making life much easy.

Thank you.

Headline commented 3 years ago

Hi! What's your use case for this proposed feature?

dragokas commented 3 years ago

Adding command name to a global cache for further checking in real time - is particular command exists. CommandExist() doesn't do its job: https://github.com/alliedmodders/sourcemod/issues/1480

KyleSanderson commented 3 years ago

GetCommandIterator is the proper way to do this.

dragokas commented 3 years ago

There is no way to use GetCommandIterator() to retrieve an updated list of commands just for 1 single and simple check, because it gives like ~0.5-1 second lag to iterate the whole convars and commands list.

dvander commented 3 years ago

A global callback for when commands are registered or unregistered sounds acceptable. But it could be very spammy, so I think it would need to be an opt-in thing (eg, so you can do an initial scan on plugin load, and then activate the callback after). Eg, this kind of signature:

// status == true, adding command
// status == false, removing command - bonus points for this being an enum
typedef CommandRegistrationCallback = function void (Handle plugin, const char[] name, bool status);

void AddCommandRegistrationListener(CommandRegistrationCallback callback);

I'd be happy to review a PR for this.

dragokas commented 3 years ago

Thanks for the suggestion. I'm writing in C++ very rarely. I'll give a chance to somebody more skilled.