-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_api.h
More file actions
149 lines (116 loc) · 4.09 KB
/
plugin_api.h
File metadata and controls
149 lines (116 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#pragma once
#include <stdint.h>
#define ABI_V1 1
#define PRIORITY_DEFAULT 1
#define PRIORITY_FIRST 0
#define PRIORITY_LATER 2
#define DEP_TYPE_REQUIRED 0
#define DEP_TYPE_OPTIONAL 1
#ifdef _WIN32
#define pluginbhvr __cdecl
#else
#define pluginbhvr
#endif
#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C
#endif
#if defined(_WIN32)
#define PLATFORM_EXPORT __declspec(dllexport)
#else
#define PLATFORM_EXPORT __attribute__((visibility("default")))
#endif
#define expose EXTERN_C PLATFORM_EXPORT
#define api expose pluginbhvr
// expose and pluginbhvr are "legacy" but I will leave them since they are more verbose
extern "C" {
struct Dependency {
const char* name;
uint8_t type; // DEP_TYPE_REQUIRED or DEP_TYPE_OPTIONAL
};
struct PluginInfo {
const char* name;
const char* version;
uint32_t abi_version;
char priority = PRIORITY_DEFAULT;
Dependency dependencies[128] = {};
};
// Event callback type
typedef void (*event_callback_t)(const char* eventName, const char* payload);
// Logging callback
typedef void (*log_callback_t)(const char* level, const char* message);
// Host interface passed to plugins
struct PluginHost {
// Event system
void (*send_event)(const char* eventName, const char* payload);
void (*register_event)(const char* eventName, event_callback_t callback);
void (*unregister_event)(event_callback_t callback);
// Plugin management
bool (*load_plugin)(const char* name);
bool (*unload_plugin)(const char* name);
// Logging
void (*log)(const char* level, const char* message);
// Key-value storage for plugins
bool (*set_data)(const char* key, const char* value);
const char* (*get_data)(const char* key);
bool (*has_data)(const char* key);
bool (*delete_data)(const char* key);
// Timer system
uint64_t (*set_timer)(uint32_t ms, event_callback_t callback, bool repeat);
bool (*cancel_timer)(uint64_t timer_id);
};
typedef bool (*plugin_init_t)(PluginHost* host);
typedef void (*plugin_shutdown_t)();
typedef const PluginInfo* (*plugin_get_info_t)();
}
// Helper functions
namespace plugin {
extern PluginHost* host;
inline void send(const char* event, const char* payload = "") {
if (host) host->send_event(event, payload);
}
inline void log(const char* level, const char* message) {
if (host) host->log(level, message);
}
inline void info(const char* message) { log("INFO", message); }
inline void warn(const char* message) { log("WARN", message); }
inline void error(const char* message) { log("ERROR", message); }
inline bool store(const char* key, const char* value) {
return host ? host->set_data(key, value) : false;
}
inline const char* load(const char* key) {
return host ? host->get_data(key) : nullptr;
}
inline uint64_t timer(uint32_t ms, event_callback_t callback, bool repeat = false) {
return host ? host->set_timer(ms, callback, repeat) : 0;
}
inline void on(const char* eventName, event_callback_t callback) {
if (host) host->register_event(eventName, callback);
}
inline void off(event_callback_t callback) {
if (host) host->unregister_event(callback);
}
}
#define manifest(name, version) \
expose pluginbhvr const PluginInfo* plugin_get_info() { \
static PluginInfo info = {name, version, ABI_V1, PRIORITY_DEFAULT}; \
return &info; \
}
#define start() \
namespace plugin { PluginHost* host = nullptr; } \
#define sethost() \
plugin::host = host;
#define dependency(dep_name, dep_type) \
{\
PluginInfo *info = const_cast<PluginInfo*>(plugin_get_info()); \
static Dependency dep = {dep_name, dep_type}; \
for (size_t i = 0; i < 128; i++) { \
if (info->dependencies[i].name == nullptr || info->dependencies[i].name[0] == '\0') { \
info->dependencies[i] = dep; \
break; \
} \
} \
}
#define event_handler(name) \
static void name(const char* eventName, const char* payload)