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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#![allow(unused)]

pub mod generated;

pub use generated::*;

pub const LOG_VERBOSE: u16 = 0;
pub const LOG_INFO: u16 = 1;
pub const LOG_WARN: u16 = 2;
pub const LOG_ERROR: u16 = 3;
pub const LOG_FATAL: u16 = 4;

pub const REGULAR: u16 = 0;
pub const IMPERIAL_SHRINE: u16 = 1;
pub const TRIBUNAL_TEMPLE: u16 = 2;

pub const CLIENT_GAMEPLAY: u8 = 0;
pub const CLIENT_CONSOLE: u8 = 1;
pub const CLIENT_DIALOGUE: u8 = 2;
pub const CLIENT_SCRIPT_LOCAL: u8 = 3;
pub const CLIENT_SCRIPT_GLOBAL: u8 = 4;
pub const SERVER_SCRIPT: u8 = 5;

pub const NONE: u8 = 0;
pub const DRAG: u8 = 1;
pub const DROP: u8 = 2;
pub const TAKE_ALL: u8 = 3;

pub const ITEM: u16 = 0;
pub const ITEM_MAGIC: u16 = 1;
pub const MAGIC: u16 = 2;
pub const UNASSIGNED: u16 = 3;

pub const SET: u8 = 0;
pub const ADD: u8 = 1;
pub const REMOVE: u8 = 2;
pub const REQUEST: u8 = 3;

pub const LOAD: u16 = 0;
pub const UNLOAD: u16 = 1;

pub const RANK: u8 = 0;
pub const EXPULSION: u8 = 1;
pub const REPUTATION: u8 = 3;

pub const ENTRY: i16 = 0;
pub const INDEX: i16 = 1;

pub const SPELL: u16 = 0;
pub const POTION: u16 = 1;
pub const ENCHANTMENT: u16 = 2;
pub const NPC: u16 = 3;

/// Calls a function on `EVENTS_INSTANCE` with given parameters
#[macro_export]
macro_rules! call_instance {
    ($call:tt, $($argument:expr),+) => {
        let instance = unsafe {
            EVENTS_INSTANCE
                .as_ref()
                .expect(format!("No events instance created: {}\n", stringify!($call)).as_str())
        };
        instance.$call($($argument),+);
        instance.on_any(stringify!($call));
    };

    ($call:tt) => {
        let instance = unsafe {
            EVENTS_INSTANCE
                .as_ref()
                .expect(format!("No events instance created: {}\n", stringify!($call)).as_str())
        };
        instance.$call();
        instance.on_any(stringify!($call));
    };
}

///
/// create and bind C symbols to given struct, should implement Events
///
#[macro_export]
macro_rules! use_events {
    ($events:ident) => {
        use std::ffi::CStr;

        static mut EVENTS_INSTANCE: Option<$events> = None;

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnServerInit() {
            unsafe {
                if (EVENTS_INSTANCE.is_none()) {
                    EVENTS_INSTANCE = Some($events::new());
                }
            }

            call_instance!(on_server_init);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnServerPostInit() {
            call_instance!(on_server_post_init);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnServerExit(is_error: bool) {
            call_instance!(on_server_exit, is_error);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnActorAI(player_id: u16, description: *const i8) {
            call_instance!(on_actor_ai, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnActorCellChange(player_id: u16, description: *const i8) {
            call_instance!(on_actor_cell_change, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnActorDeath(player_id: u16, description: *const i8) {
            call_instance!(on_actor_death, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnActorEquipment(player_id: u16, description: *const i8) {
            call_instance!(on_actor_equipment, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnActorList(player_id: u16, description: *const i8) {
            call_instance!(on_actor_list, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnActorTest(player_id: u16, description: *const i8) {
            call_instance!(on_actor_test, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnCellDeletion(description: *const i8) {
            call_instance!(on_cell_deletion, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnCellLoad(description: *const i8) {
            call_instance!(on_cell_load, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnCellUnload(description: *const i8) {
            call_instance!(on_cell_unload, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnContainer(player_id: u16, description: *const i8) {
            call_instance!(on_container, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnDoorState(player_id: u16, description: *const i8) {
            call_instance!(on_door_state, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnGUIAction(player_id: u16, message_box_id: i16, data: *const i8) {
            call_instance!(on_gui_action, player_id, message_box_id, unsafe {
                CStr::from_ptr(data).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnMpNumIncrement(current_mp_num: i16) {
            call_instance!(on_mp_num_increment, current_mp_num);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnObjectActivate(player_id: u16, description: *const i8) {
            call_instance!(on_object_activate, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnObjectDelete(player_id: u16, description: *const i8) {
            call_instance!(on_object_delete, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnObjectLock(player_id: u16, description: *const i8) {
            call_instance!(on_object_lock, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnObjectPlace(player_id: u16, description: *const i8) {
            call_instance!(on_object_place, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnObjectScale(player_id: u16, description: *const i8) {
            call_instance!(on_object_scale, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnObjectSpawn(player_id: u16, description: *const i8) {
            call_instance!(on_object_spawn, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnObjectState(player_id: u16, description: *const i8) {
            call_instance!(on_object_state, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnObjectTrap(player_id: u16, description: *const i8) {
            call_instance!(on_object_trap, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerAttribute(player_id: u16) {
            call_instance!(on_player_attribute, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerBook(player_id: u16) {
            call_instance!(on_player_book, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerBounty(player_id: u16) {
            call_instance!(on_player_bounty, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerCellChange(player_id: u16) {
            call_instance!(on_player_cell_change, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerConnect(player_id: u16) {
            call_instance!(on_player_connect, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerDeath(player_id: u16) {
            call_instance!(on_player_death, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerDisconnect(player_id: u16) {
            call_instance!(on_player_disconnect, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerDisposition(player_id: u16) {
            call_instance!(on_player_disposition, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerEndCharGen(player_id: u16) {
            call_instance!(on_player_end_char_gen, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerEquipment(player_id: u16) {
            call_instance!(on_player_equipment, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerFaction(player_id: u16) {
            call_instance!(on_player_faction, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerInput(player_id: u16) {
            call_instance!(on_player_input, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerInventory(player_id: u16) {
            call_instance!(on_player_inventory, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerItemUse(player_id: u16) {
            call_instance!(on_player_item_use, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerJournal(player_id: u16) {
            call_instance!(on_player_journal, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerLevel(player_id: u16) {
            call_instance!(on_player_level, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerMiscellaneous(player_id: u16) {
            call_instance!(on_player_miscellaneous, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerQuickKeys(player_id: u16) {
            call_instance!(on_player_quick_keys, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerReputation(player_id: u16) {
            call_instance!(on_player_reputation, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerRest(player_id: u16) {
            call_instance!(on_player_rest, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerResurrect(player_id: u16) {
            call_instance!(on_player_resurrect, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerSendMessage(player_id: u16, message: *const i8) {
            call_instance!(on_player_send_message, player_id, unsafe {
                CStr::from_ptr(message).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerShapeshift(player_id: u16) {
            call_instance!(on_player_shapeshift, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerSkill(player_id: u16) {
            call_instance!(on_player_skill, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerSpellbook(player_id: u16) {
            call_instance!(on_player_spellbook, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnPlayerTopic(player_id: u16) {
            call_instance!(on_player_topic, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnRecordDynamic(player_id: u16) {
            call_instance!(on_record_dynamic, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnRequestDataFileList() {
            call_instance!(on_request_data_file_list);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnScriptGlobalShort(player_id: u16) {
            call_instance!(on_script_global_short, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnServerScriptCrash(error: *const i8) {
            call_instance!(on_server_script_crash, unsafe {
                CStr::from_ptr(error).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnVideoPlay(player_id: u16, description: *const i8) {
            call_instance!(on_video_play, player_id, unsafe {
                CStr::from_ptr(description).to_str().unwrap_or_default()
            });
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnWorldKillCount(player_id: u16) {
            call_instance!(on_world_kill_count, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnWorldMap(player_id: u16) {
            call_instance!(on_world_map, player_id);
        }

        #[no_mangle]
        #[allow(non_snake_case)]
        pub fn OnWorldWeather(player_id: u16) {
            call_instance!(on_world_weather, player_id);
        }
    };
}

/// Trait implementing all known events TES3MP server can trigger
pub trait Events: Sized {
    fn new() -> Self;
    fn on_any(&self, event_name: &str) {}

    fn on_actor_ai(&self, player_id: u16, description: &str) {}
    fn on_actor_cell_change(&self, player_id: u16, description: &str) {}
    fn on_actor_death(&self, player_id: u16, description: &str) {}
    fn on_actor_equipment(&self, player_id: u16, description: &str) {}
    fn on_actor_list(&self, player_id: u16, description: &str) {}
    fn on_actor_test(&self, player_id: u16, description: &str) {}

    fn on_cell_deletion(&self, description: &str) {}
    fn on_cell_load(&self, description: &str) {}
    fn on_cell_unload(&self, description: &str) {}

    fn on_container(&self, player_id: u16, description: &str) {}
    fn on_door_state(&self, player_id: u16, description: &str) {}

    fn on_gui_action(&self, player_id: u16, message_box_id: i16, data: &str) {}

    fn on_mp_num_increment(&self, current_mp_num: i16) {}

    fn on_object_activate(&self, player_id: u16, description: &str) {}
    fn on_object_delete(&self, player_id: u16, description: &str) {}
    fn on_object_lock(&self, player_id: u16, description: &str) {}
    fn on_object_place(&self, player_id: u16, description: &str) {}
    fn on_object_scale(&self, player_id: u16, description: &str) {}
    fn on_object_spawn(&self, player_id: u16, description: &str) {}
    fn on_object_state(&self, player_id: u16, description: &str) {}
    fn on_object_trap(&self, player_id: u16, description: &str) {}

    fn on_player_attribute(&self, player_id: u16) {}
    fn on_player_book(&self, player_id: u16) {}
    fn on_player_bounty(&self, player_id: u16) {}
    fn on_player_cell_change(&self, player_id: u16) {}
    fn on_player_connect(&self, player_id: u16) {}
    fn on_player_death(&self, player_id: u16) {}
    fn on_player_disconnect(&self, player_id: u16) {}
    fn on_player_disposition(&self, player_id: u16) {}
    fn on_player_end_char_gen(&self, player_id: u16) {}
    fn on_player_equipment(&self, player_id: u16) {}
    fn on_player_faction(&self, player_id: u16) {}
    fn on_player_input(&self, player_id: u16) {}
    fn on_player_inventory(&self, player_id: u16) {}
    fn on_player_item_use(&self, player_id: u16) {}
    fn on_player_journal(&self, player_id: u16) {}
    fn on_player_level(&self, player_id: u16) {}
    fn on_player_miscellaneous(&self, player_id: u16) {}
    fn on_player_quick_keys(&self, player_id: u16) {}
    fn on_player_reputation(&self, player_id: u16) {}
    fn on_player_rest(&self, player_id: u16) {}
    fn on_player_resurrect(&self, player_id: u16) {}
    fn on_player_send_message(&self, player_id: u16, message: &str) {}
    fn on_player_shapeshift(&self, player_id: u16) {}
    fn on_player_skill(&self, player_id: u16) {}
    fn on_player_spellbook(&self, player_id: u16) {}
    fn on_player_topic(&self, player_id: u16) {}

    fn on_record_dynamic(&self, player_id: u16) {}

    fn on_request_data_file_list(&self) {}

    fn on_script_global_short(&self, player_id: u16) {}

    fn on_server_exit(&self, is_error: bool) {}
    fn on_server_init(&self) {}
    fn on_server_post_init(&self) {}
    fn on_server_script_crash(&self, error: &str) {}

    fn on_video_play(&self, player_id: u16, description: &str) {}

    fn on_world_kill_count(&self, player_id: u16) {}
    fn on_world_map(&self, player_id: u16) {}
    fn on_world_weather(&self, player_id: u16) {}
}