[Tutorial] Run-time plugin feature detection
#1

Plugins provide natives to do things. This is great, but if you write a library sometimes you want to do one thing if the plugin exists and another thing if it doesn’t. Until now the best way was to include (or tryinclude) the plugin’s include and use them, but this would cause issues if a user had the include but didn’t load the plugin. Simply calling the native won’t work - all the natives are checked for existence prior to the script starting - hence all the famous runtime error 19s, and nativecheckers. The goal has always been to be able to detect which natives exist at run-time, use them if they do, but not error out if they don’t. I finally figured out a way to do this based on an obscure AMX feature - you can use negative native indexes to not put them in the AMX header:

Code:
#define NATIVE_PrintAmxBacktrace (-5)

native PrintAmxBacktrace() = NATIVE_PrintAmxBacktrace;

main()
{
    if (QueryNative(NATIVE_PrintAmxBacktrace)) PrintAmxBacktrace();
}
There are at least two possible ways to write QueryNative while being backwards-compatible.

V1:

Code:
stock QueryNative(id)
{
    #emit LOAD.S.alt id
    #emit ZERO.pri
    #emit LCTRL 10
    #emit RETN
    return 0;
}
LCTRL 10 would be a new control register, somehow hooked by every plugin, that returns 1 if the specified native index exists. This would require a lot of AMX_Exec hooking though.

V2:

Code:
#define QueryNative(%0) getproperty(.id = 0x504C5547, .value = (%0)) // !"PLUG"
This is simpler - just a hook of the getproperty native looking for a special domain (id) for plugin natives.

I’ve not worked out all the details of either yet, since every negative ID will need to be unique among plugins, and they should only respond to their own IDs, or pass along the chain otherwise. This implies some sort of global registry, at least of ranges - allocated say 100 at once, so -500 – -599 = mysql, -600 – -699 = streamer, etc (-1 – -99 probably reserved for local testing and errors).
Reply
#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
#3

After quite a debate, I do see the point of using a plugin to detect plugins, especially when as you say it means existing plugins are already compatible.

Side note, I already tried enums for the IDs and they don't work.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)