alliedmodders / sourcemod

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

Add global forward OnPluginLoad #1482

Open dragokas opened 3 years ago

dragokas commented 3 years ago

A counterweight to https://github.com/alliedmodders/sourcemod/issues/890 I beg you to add OnPluginLoad() forward to know when some (or any) plugin is loaded.

Use cases:

1) Left 4 DHooks Direct Left 4 DHooks does not hook everything at once (for CPU saving). It only hooks what we need and what other plugins use at the moment and provides only those forwards.

So, when some plugin is reloaded, or late loaded, with OnPluginLoad() you can run a detection routine to check if any new forward is required (if the plugin does not use "RegPluginLibrary", for instance) and such a way it starts appropriate hooks and forwards required by that plugin.

2) Command Eater It uses a special command cache built from the commands enumerator (which consuming quite a lot CPU cycles). In case some plugin is dynamically loaded / unloaded, with OnPluginLoad() we can detect this action, and start to rebuild those commands cache. It should be always in actual state.

Thanks for your attention and hope for a positive answer.

Wend4r commented 3 years ago

In which case it may be useful to use OnPluginLoad()? I think you can do this in OnAllPluginsLoaded(). Go through all the plugins through GetPluginIterator()

dragokas commented 3 years ago

In which case it may be useful to use OnPluginLoad()?

@Wend4r, sorry, I don't understand your question. Use cases are described above + one more in linked issue + as alternative to https://github.com/alliedmodders/sourcemod/issues/1481 If you mean concrete source code, here is one sample from Left 4 Dhooks:

// Check if used
else if( GetFunctionByName(hPlug, forwards) != INVALID_FUNCTION )

The detection routine is too long to post. See CheckRequiredDetours() in the full source code in the thread's topic.

think you can do this in OnAllPluginsLoaded(). Go through all the plugins through GetPluginIterator()

OnAllPluginsLoaded() is not called when some plugins are loaded / unloaded dynamically, which e.g. may happen in plugins like "Map Config" and other cases.

Wend4r commented 3 years ago

It seems to me that it would be more convenient to put this functionality in a separate one Extension, which would secretly look at the registration of commands by plugins and give the handler with the desired result.