Files
armorpaint/paint/sources/plugin.c
T

54 lines
1.2 KiB
C
Raw Normal View History

2026-03-09 13:17:15 +01:00
#include "global.h"
2026-04-05 16:09:00 +02:00
char *_plugin_name;
2026-03-06 12:56:38 +01:00
plugin_t *plugin_create() {
2026-08-21 22:35:41 +02:00
plugin_t *p = ALLOC_INIT(plugin_t, {0});
2026-03-06 12:56:38 +01:00
p->name = string_copy(_plugin_name);
2026-06-08 15:35:04 +02:00
any_map_set(g_plugins, p->name, p);
2026-03-06 12:56:38 +01:00
return p;
}
2026-03-07 21:12:59 +01:00
void plugin_start(char *plugin) {
2026-08-21 22:35:41 +02:00
char *file = string("plugins/%s", plugin);
buffer_t *blob = data_get_blob(file);
if (blob == NULL) {
return;
}
2026-08-21 22:40:25 +02:00
_plugin_name = string_copy(plugin);
2026-03-24 14:24:11 +01:00
minic_ctx_t *ctx = minic_eval_named(sys_buffer_to_string(blob), plugin);
2026-08-21 22:35:41 +02:00
data_delete_blob(file);
2026-03-23 23:20:02 +01:00
// Store context on the plugin so callbacks can use it and it can be freed on stop
2026-06-08 15:35:04 +02:00
plugin_t *p = any_map_get(g_plugins, plugin);
2026-08-21 22:35:41 +02:00
if (p == NULL) { // Script did not call plugin_create()
p = plugin_create();
}
p->ctx = ctx;
2026-03-06 12:56:38 +01:00
}
2026-03-07 21:12:59 +01:00
void plugin_stop(char *plugin) {
2026-06-08 15:35:04 +02:00
plugin_t *p = any_map_get(g_plugins, plugin);
2026-08-21 22:35:41 +02:00
if (p == NULL) {
return;
}
2026-03-07 21:12:59 +01:00
if (p->on_delete != NULL) {
2026-03-23 23:20:02 +01:00
minic_ctx_call_fn(p->ctx, p->on_delete, NULL, 0);
2026-03-06 12:56:38 +01:00
}
2026-03-23 23:20:02 +01:00
minic_ctx_free(p->ctx);
p->ctx = NULL;
2026-06-08 15:35:04 +02:00
map_delete(g_plugins, plugin);
2026-03-06 12:56:38 +01:00
}
2026-03-10 21:00:00 +01:00
void plugin_notify_on_ui(plugin_t *plugin, void *f) {
2026-03-06 12:56:38 +01:00
plugin->on_ui = f;
}
2026-03-10 21:00:00 +01:00
void plugin_notify_on_update(plugin_t *plugin, void *f) {
2026-03-06 12:56:38 +01:00
plugin->on_update = f;
}
2026-03-10 21:00:00 +01:00
void plugin_notify_on_delete(plugin_t *plugin, void *f) {
2026-03-06 12:56:38 +01:00
plugin->on_delete = f;
}