ISource2ModNatives

Natives for SourcePawn plugins, from a native (C++) plugin.

What a SourceMod extension does for its scripts: register natives a .sp declares and calls, a library its scripts can test for, and handles of a type of its own. The natives are SourcePawn's -- SPVM_NATIVE_FUNC, called with the script's IPluginContext and its parameters -- so the SourcePawn headers come with this one (public/sourcepawn, the core's VM version).

Not part of SOURCE2MOD_FILLVARS(): a plugin that wants it asks for it,

auto* natives = (ISource2ModNatives*)g_Source2ModAPI->Source2ModFactory(SOURCE2MOD_NATIVES_INTERFACE, nullptr, nullptr);
natives->AddNatives(g_PluginID, g_MyNatives);

Everything registered here goes when the plugin unloads: its natives become unbound (a script calling one gets an error), its libraries are removed (OnLibraryRemoved) and its handle types are destroyed together with every handle of them.

Interface id ISource2ModNatives001 (SOURCE2MOD_NATIVES_INTERFACE) · Header source2mod/ISource2ModNatives.h

Types

Source2ModHandleType

using Source2ModHandleType = uint32_t;

A handle type registered through CreateHandleType(); 0 is none.

Source2ModHandleDestructor

using Source2ModHandleDestructor = void (*)(void* object);

Takes a handle's object when its last handle is closed.

Methods

AddNatives

virtual bool AddNatives(PluginId owner, const sp_nativeinfo_t* natives) = 0;

Registers natives for scripts.

Scripts already loaded get the ones they were missing; a script that failed to load for want of one of them is loaded again. A name that a live plugin already registered is refused (and logged), the rest of the list still goes in.

ParameterTypeDescription
ownerPluginIdThe plugin's id (g_PluginID)
nativesconst sp_nativeinfo_t*Name and function pairs, ending in { nullptr, nullptr }; copied, the array need not outlive the call

Returns bool — false when any of them was refused

RegisterLibrary

virtual bool RegisterLibrary(PluginId owner, const char* name) = 0;

Registers a library, what LibraryExists() and OnLibraryAdded() in a script see. Scripts that are running get OnLibraryAdded now.

Several plugins may register the same library; it is removed (OnLibraryRemoved) when the last of them unloads.

ParameterType
ownerPluginId
nameconst char*

Returns bool — false on an empty name

LibraryExists

virtual bool LibraryExists(const char* name) = 0;

Whether a plugin (script or native) registered this library.

ParameterType
nameconst char*

GetScriptPlugin

virtual PluginId GetScriptPlugin(SourcePawn::IPluginContext* ctx) = 0;

The script plugin a native was called from.

ParameterType
ctxSourcePawn::IPluginContext*

Returns PluginId — Its plugin id, 0 when the context is no script's

FormatString

virtual bool FormatString(SourcePawn::IPluginContext* ctx, const cell_t* params, int fmtParam, char* buffer, size_t maxlen, size_t* written = nullptr) = 0;

SourceMod's format, as Format() and PrintToServer() do it: the format at params[fmtParam], its arguments after it.

ParameterTypeDefault
ctxSourcePawn::IPluginContext*
paramsconst cell_t*
fmtParamint
bufferchar*
maxlensize_t
writtensize_t*nullptr

Returns bool — false after reporting the error to the script

CreateHandleType

virtual Source2ModHandleType CreateHandleType(PluginId owner, const char* name, Source2ModHandleDestructor destroy) = 0;

Registers a handle type for the objects this plugin hands out.

ParameterTypeDescription
ownerPluginId
nameconst char*
destroySource2ModHandleDestructorCalled with the object when its last handle is closed (CloseHandle / delete, the owning script unloading); may be nullptr

CreateHandle

virtual cell_t CreateHandle(Source2ModHandleType type, void* object, SourcePawn::IPluginContext* ctx) = 0;

A new handle to object, owned by the script that ctx is -- closed when that script unloads.

ParameterType
typeSource2ModHandleType
objectvoid*
ctxSourcePawn::IPluginContext*

Returns cell_t — The handle, 0 on an unknown type or a full handle table

ReadHandle

virtual void* ReadHandle(SourcePawn::IPluginContext* ctx, cell_t handle, Source2ModHandleType type) = 0;

The object behind a handle of this type.

ParameterType
ctxSourcePawn::IPluginContext*
handlecell_t
typeSource2ModHandleType

Returns void* — The object, or nullptr after reporting SourceMod's "Invalid Handle" error to the script

CloseHandle

virtual bool CloseHandle(cell_t handle) = 0;

Closes a handle whoever owns it.

ParameterType
handlecell_t

On this page