dynamically loaded library object.
(mutable) string of the dll path with file name.
last modification time of the dll object.
int value that stores specific error codes when a plugin issue arises.
error code indicating no error.
error code indicating the 'path' member (see struct Plugin) wasn't allocated nor initialized.
error code indicating the dynamically loaded library failed to load.
error code indicating that there's no DLL loaded to continue plugin operations.
error code indicating the DLL failed to reload.
plugin callback signature for when a plugin has been loaded, unloaded, or reloaded.
void PluginEvent(struct Plugin *pl_ctxt, void *userdata, bool reloading);
plugin callback signature for when iterating a plugin directory and a plugin with the appropriate extension has been found.
void PluginDirEvent(const char pl_path[], const char pl_name[], void *userdata);
bool plugin_load(struct Plugin *pl, const char path[], PluginEvent load_fn, void *userdata);
Loads a dynamically loaded library object (DLL) to a Plugin instance and invokes an optional plugin load callback.
pl
- pointer to aPlugin
instance.path
- file path to the DLL object.load_fn
- pointer to a loading function, can be NULL.userdata
- pointer to data that is given toload_fn
when invoked.
true if loading was successful, false if an error occurred, check the err
member and use plugin_get_err
to get an error message string.
void on_load_dll_example(struct Plugin *pl_ctxt, void *userdata, bool reloading) {
puts(userdata);
}
int main(void)
{
struct Plugin pl = {0};
if( !plugin_load(&pl, "./libexample.so", &on_load_dll_example, "loaded libexample!") ) {
puts(plugin_get_err(&pl));
return 1;
}
}
void plugin_clear(struct Plugin *pl, PluginEvent unload_fn, void *userdata)
unloads and deallocates/cleans up internal data for a plugin instance.
pl
- pointer to plugin instance.unload_fn
- function pointer to call before cleaning up, can be NULL.userdata
- pointer to data to pass tounload_fn
.
None
void on_load_dll_example(struct Plugin *pl_ctxt, void *userdata, bool reloading) {
puts(userdata);
}
void on_unload_dll_example(struct Plugin *pl_ctxt, void *userdata, bool reloading) {
puts(userdata);
}
int main(void)
{
struct Plugin pl = {0};
if( !plugin_load(&pl, "./libexample.so", &on_load_dll_example, "loaded libexample!") ) {
puts(plugin_get_err(&pl));
return 1;
}
...
plugin_clear(&pl, &on_unload_dll_example, "unloaded libexample!");
}
bool plugin_changed(struct Plugin *pl);
checks if a plugin's DLL object file was changed.
pl
- pointer to raw script data.
true if the file did change, false otherwise.
struct Plugin pl;
...
if( plugin_changed(&pl) ) {
/// code.
}
bool plugin_reload(struct Plugin *pl, PluginEvent load_fn, PluginEvent unload_fn, void *userdata);
Unloads and then reloads a plugin's DLL object.
pl
- pointer to Plugin instance.load_fn
- function pointer invoked when plugin has been reloaded, can be NULL.unload_fn
- function pointer invoked when plugin has been unloaded, can be NULL.userdata
- pointer to data passed toload_fn
andunload_fn
.
true if plugin was reloaded, false otherwise. Errors return false. check err
field and use plugin_get_err
for error string message.
struct Plugin pl;
...
if( plugin_changed(&pl) && !plugin_reload(&pl, on_plugin_load, on_plugin_unload, &data) ) {
puts(plugin_get_err(&pl));
}
const char *plugin_get_err(const struct Plugin *pl);
gives an appropriate string message for a specific error code on a plugin instance.
pl
- pointer to a plugin instance.
pointer to a string.
void *plugin_get_obj(struct Plugin *pl, const char name[]);
retrieves an object pointer from the Plugin DLL.
pl
- pointer to Plugin instance.name
- string name of the object to get.
pointer to the object, NULL if error occurred.
void on_plugin_load(struct Plugin *pl_ctxt, void *userdata, bool reloading) {
struct Data *d = userdata;
d->start_func = plugin_get_obj(pl_ctxt, "start_func");
d->stop_func = plugin_get_obj(pl_ctxt, "stop_func");
d->update_func = plugin_get_obj(pl_ctxt, "update_func");
d->global_data = plugin_get_obj(pl_ctxt, "global_data");
}
bool plugin_dir_open(const char dir[], PluginDirEvent dir_fn, void *userdata, const char custom_ext[]);
searches through a specific directory for plugins. This also searches subfolders as well!
dir
- folder path that will hold plugins.dir_fn
- function pointer invoked when a plugin with the system or custom extension name was found.userdata
- pointer to data passed todir_fn
.custom_ext
- string of a custom file extension for application specific plugins.
true if directory was accessible, false otherwise.
struct Data {
struct Plugin pl;
...;
};
void load_our_plugins(const char pl_path[], const char pl_name[], void *userdata) {
struct Data *d = userdata;
if( !plugin_load(&d->pl, pl_path, on_plugin_load, d) ) {
puts(plugin_get_err(&d->pl));
}
}
int main(void) {
struct Data d = {0};
if( !plugin_dir_open("plugins", load_our_plugins, &d, NULL) ) {
puts(plugin_get_err(&d.pl));
return 1;
}
}