[Tutorial] Run-time plugin feature detection
#2

While this would work with specially made plugins that support this mechanism, I think a better solution exists that is compatible with already existing plugins and standard means of exporting natives, yet still possible to use without any plugins at all.

I like using negative ids and hooking an already existing standard function, but the ids could be unique to the script:
pawn Code:
const NATIVE_PrintAmxBacktrace = /* unique via macros/enums */;
native PrintAmxBacktrace() = NATIVE_PrintAmxBacktrace;

#define MapNative(%0,%1) getproperty(.id = 0x504C5547, .name = (%0), .value = (%1))

main()
{
    new bool:exists = MapNative("PrintAmxBacktrace", NATIVE_PrintAmxBacktrace);
    if(exists) PrintAmxBacktrace();
}
A plugin like Native Fallback would hook getproperty and look into the table of all exported functions to the script, then create a local mapping between the native and its script-local id. Calling such a native function would lead to amx->callback in the plugin that would simply check the id and find the correct native function based on the mapping.

Without any plugins, getproperty simply returns 0, so all is fine if you ensure the optional native isn't called (without any plugins, it crashes).

This has the advantage of working with all plugins that use names when registering natives (the vast majority of them), and still works without any plugins, so you don't need to introduce any dependency at all. New plugins do not have to implement any special mechanisms for optional natives.
Reply


Messages In This Thread
Run-time plugin feature detection - by Y_Less - 13.02.2019, 16:44
Re: Run-time plugin feature detection - by IllidanS4 - 13.02.2019, 17:34
Re: Run-time plugin feature detection - by Y_Less - 13.02.2019, 18:32

Forum Jump:


Users browsing this thread: 1 Guest(s)