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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
pub mod plugin;
pub mod proto;
pub mod util;
super::version!();
#[allow(dead_code)]
extern "C" {
fn GetValidateArgIterator() -> i32;
fn ResetValidateArgIterator() -> i32;
fn NewTimerTask(tid: i32, milliseconds: i32) -> i32;
fn RegisterMessageAction(action: i32) -> i32;
fn GetHistoryEventMessage(action: i32) -> i32;
}
static mut ARGS_INDEX: usize = 0;
static mut ALLOW_ARGS_INDEX: usize = 0;
static mut INSTANCES: Vec<Box<dyn plugin::BasePlugin>> = Vec::new();
static mut INSTANCES_INDEX: usize = 0;
pub fn export_plugin(p: Box<dyn plugin::BasePlugin>) {
unsafe {
INSTANCES.push(p);
}
}
pub fn register_task() {
unsafe {
let task = INSTANCES[INSTANCES_INDEX].register_task();
for item in task {
NewTimerTask(item.get_tid(), item.get_milliseconds());
}
}
}
pub fn register_message() {
unsafe {
let task = INSTANCES[INSTANCES_INDEX].register_message_keys();
for item in task {
RegisterMessageAction(item as i32);
}
}
}
pub fn get_history_message(action: proto::keys::EventMessageAction) -> String {
unsafe {
let msg_index = GetHistoryEventMessage(action as i32);
util::string::DynamicString::receive(msg_index).unwrap()
}
}
#[no_mangle]
pub extern "C" fn GetName() -> i32 {
unsafe {
util::string::DynamicString::from(INSTANCES[INSTANCES_INDEX].get_name().as_bytes())
.get_index()
}
}
#[no_mangle]
pub extern "C" fn GetFilterName() -> i32 {
unsafe {
util::string::DynamicString::from(INSTANCES[INSTANCES_INDEX].get_filter_name().as_bytes())
.get_index()
}
}
#[no_mangle]
pub extern "C" fn GetAuthor() -> i32 {
unsafe {
util::string::DynamicString::from(INSTANCES[INSTANCES_INDEX].get_author().as_bytes())
.get_index()
}
}
#[no_mangle]
pub extern "C" fn GetMediaType() -> i32 {
unsafe {
let media_type: plugin::MediaType = INSTANCES[INSTANCES_INDEX].get_media_type();
media_type as i32
}
}
#[no_mangle]
pub extern "C" fn GetArgIterator() -> i32 {
let mut custom_args: Vec<String> = Vec::new();
unsafe {
loop {
let re_index = GetValidateArgIterator();
if re_index == 0 {
break;
}
match util::string::DynamicString::receive(re_index) {
Ok(_ok) => custom_args.push(_ok),
Err(_err) => {
return util::string::DynamicString::from(
String::from("plugin receive args error").as_bytes(),
)
.get_index()
}
}
}
let args = INSTANCES[INSTANCES_INDEX]
.get_args(util::argument::args_vec_to_map(custom_args).unwrap());
if ARGS_INDEX >= args.len() {
return 0;
}
let iterator = util::string::DynamicString::from(args[ARGS_INDEX].as_bytes()).get_index();
ARGS_INDEX = ARGS_INDEX + 1;
iterator
}
}
#[no_mangle]
pub extern "C" fn GetAllowArgIterator() -> i32 {
unsafe {
let args = INSTANCES[INSTANCES_INDEX].get_allow_custom_args();
if ALLOW_ARGS_INDEX >= args.len() {
return 0;
}
let iterator =
util::string::DynamicString::from(args[ALLOW_ARGS_INDEX].as_bytes()).get_index();
ALLOW_ARGS_INDEX = ALLOW_ARGS_INDEX + 1;
iterator
}
}
#[no_mangle]
pub extern "C" fn ValidateUserArgs() -> i32 {
let mut args: Vec<String> = Vec::new();
unsafe {
loop {
let re_index = GetValidateArgIterator();
if re_index == 0 {
break;
}
match util::string::DynamicString::receive(re_index) {
Ok(_ok) => args.push(_ok),
Err(_err) => {
return util::string::DynamicString::from(
String::from("plugin receive args error").as_bytes(),
)
.get_index()
}
}
}
match INSTANCES[INSTANCES_INDEX]
.validate_user_args(util::argument::args_vec_to_map(args).unwrap())
{
Ok(_s) => 0,
Err(_err) => util::string::DynamicString::from(_err.as_bytes()).get_index(),
}
}
}
#[no_mangle]
pub extern "C" fn NotifyTask(_tid: i32) -> i32 {
unsafe {
INSTANCES[INSTANCES_INDEX].execute_task(_tid as u32);
}
0
}
#[no_mangle]
pub extern "C" fn NotifyMessage(action: i32, message: i32) -> i32 {
unsafe {
let body = util::string::DynamicString::receive(message).unwrap();
INSTANCES[INSTANCES_INDEX].execute_message(action, body);
}
0
}
#[no_mangle]
pub extern "C" fn GetInstanceCount() -> i64 {
unsafe { INSTANCES.len() as i64 }
}
#[no_mangle]
pub extern "C" fn SetInstanceIndex(index: i64) -> i64 {
unsafe {
let count = INSTANCES.len() as i64;
if index > count - 1 {
return -1;
}
INSTANCES_INDEX = index as usize;
index
}
}
#[no_mangle]
pub extern "C" fn GetInstanceType(index: i64) -> i32 {
unsafe {
let count = INSTANCES.len() as i64;
if index > count - 1 {
return -1;
}
let instance_type = INSTANCES[index as usize].get_instance_type();
instance_type as i32
}
}
#[no_mangle]
pub extern "C" fn HookCreated() -> i32 {
unsafe { INSTANCES[INSTANCES_INDEX].hook_created() }
}