-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmodules.c
582 lines (488 loc) · 14.9 KB
/
modules.c
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#include "commonheaders.h"
#include "modules.h"
#include <execinfo.h>
// list of hooks
typedef struct {
NWNXHOOK pfnHook;
HINSTANCE hOwner;
HWND hwnd;
UINT message;
}
THookSubscriber;
typedef struct {
char name[ MAXMODULELABELLENGTH ];
int id;
int subscriberCount;
THookSubscriber* subscriber;
NWNXHOOK pfnHook;
int inintialized;
NWNXHOOK pfnInitialize;
}
THook;
static struct {
THook** items;
int count, limit, increment;
FSortFunc sortFunc;
}
hooks;
// list of services
typedef struct {
DWORD nameHash;
HINSTANCE hOwner;
NWNXSERVICE pfnService;
int isParam;
uintptr_t pParam;
char name[1];
}
TService;
static struct {
TService** items;
int count, limit, increment;
FSortFunc sortFunc;
}
services;
// other static variables
//static CRITICAL_SECTION csHooks,csServices;
static DWORD mainThreadId;
static int hookId = 1;
static HANDLE hMainThread;
static HANDLE hMissingService;
HINSTANCE GetInstByAddress(void* codePtr)
{
return NULL;
}
static int LoadDefaultModules(void)
{
int *disableDefaultModule = 0;
return 0;
}
static int compareServices(const TService* p1, const TService* p2)
{
if (p1->nameHash == p2->nameHash)
return 0;
return (p1->nameHash > p2->nameHash) ? 1 : -1;
}
static int compareHooks(const THook* p1, const THook* p2)
{
return strcmp(p1->name, p2->name);
}
int InitialiseModularEngine(void)
{
services.sortFunc = (FSortFunc) compareServices;
services.increment = 100;
hooks.sortFunc = (FSortFunc) compareHooks;
hooks.increment = 50;
//InitializeCriticalSection(&csHooks);
//InitializeCriticalSection(&csServices);
//mainThreadId=GetCurrentThreadId();
//DuplicateHandle(GetCurrentProcess(),GetCurrentThread(),GetCurrentProcess(),&hMainThread,THREAD_SET_CONTEXT,FALSE,0);
hMissingService = CreateHookableEvent(ME_SYSTEM_MISSINGSERVICE);
return LoadDefaultModules();
}
void DestroyModularEngine(void)
{
int i;
//EnterCriticalSection( &csHooks );
for (i = 0; i < hooks.count; i++) {
if (hooks.items[i]->subscriberCount)
free(hooks.items[i]->subscriber);
free(hooks.items[i]);
}
List_Destroy((SortedList*)&hooks);
//LeaveCriticalSection( &csHooks );
//DeleteCriticalSection( &csHooks );
//EnterCriticalSection( &csServices );
for (i = 0; i < services.count; i++)
free(services.items[i]);
List_Destroy((SortedList*)&services);
//LeaveCriticalSection( &csServices );
//DeleteCriticalSection( &csServices );
//CloseHandle( hMainThread );
}
#if __GNUC__
#define NOINLINEASM
#endif
DWORD NameHashFunction(const char *szStr)
{
#if defined _M_IX86 && !defined _NUMEGA_BC_FINALCHECK && !defined NOINLINEASM
__asm {
xor edx, edx
xor eax, eax
mov esi, szStr
mov al, [esi]
dec esi
xor cl, cl
lph_top: //only 4 of 9 instructions in here don't use AL, so optimal pipe use is impossible
xor edx, eax
inc esi
and cl, 31
movzx eax, byte ptr [esi]
add cl, 5
test al, al
rol eax, cl //rol is u-pipe only, but pairable
//rol doesn't touch z-flag
jnz lph_top //5 clock tick loop. not bad.
xor eax, edx
}
#else
DWORD hash=0;
int i;
int shift=0;
for (i=0; szStr[i]; i++) {
hash ^= szStr[i] << shift;
if (shift > 24) hash ^= (szStr[i] >> (32 - shift)) & 0x7F;
shift = (shift + 5) & 0x1F;
}
return hash;
#endif
}
///////////////////////////////HOOKS
HANDLE CreateHookableEvent(const char *name)
{
THook* ret;
int idx;
if (name == NULL)
return NULL;
//EnterCriticalSection( &csHooks );
if (List_GetIndex((SortedList*)&hooks, (void*)name, &idx)) {
//LeaveCriticalSection( &csHooks );
return NULL;
}
ret = (THook*)malloc(sizeof(THook));
strncpy(ret->name, name, sizeof(ret->name)); ret->name[ MAXMODULELABELLENGTH - 1 ] = 0;
ret->id = hookId++;
ret->subscriberCount = 0;
ret->subscriber = NULL;
ret->pfnHook = NULL;
ret->inintialized = 0;
ret->pfnInitialize = NULL;
List_Insert((SortedList*)&hooks, ret, idx);
//LeaveCriticalSection( &csHooks );
return (HANDLE)ret;
}
int DestroyHookableEvent(HANDLE hEvent)
{
int idx;
THook* p;
//EnterCriticalSection( &csHooks );
if ((idx = List_IndexOf((SortedList*)&hooks, hEvent)) == -1) {
//LeaveCriticalSection(&csHooks);
return 1;
}
p = hooks.items[idx];
if (p->subscriberCount) {
free(p->subscriber);
p->subscriber = NULL;
p->subscriberCount = 0;
}
List_Remove((SortedList*)&hooks, idx);
free(p);
//LeaveCriticalSection( &csHooks );
return 0;
}
int SetHookDefaultForHookableEvent(HANDLE hEvent, NWNXHOOK pfnHook)
{
THook* p = (THook*)hEvent;
//EnterCriticalSection(&csHooks);
if (List_IndexOf((SortedList*)&hooks, hEvent) != -1)
p->pfnHook = pfnHook;
//LeaveCriticalSection(&csHooks);
return 0;
}
static char* currentEventName;
const char *GetCurrentEventName()
{
return currentEventName;
}
int SetHookInitializer(HANDLE hEvent, NWNXHOOK pfnInitialize)
{
THook* p = (THook*)hEvent;
if (List_IndexOf((SortedList*)&hooks, hEvent) != -1)
p->pfnInitialize = pfnInitialize;
return 0;
}
int CallHookSubscribers(HANDLE hEvent, uintptr_t pParam, bool abortable)
{
int i, returnVal = 0;
THook* p = (THook*)hEvent;
//EnterCriticalSection( &csHooks );
if (List_IndexOf((SortedList*)&hooks, p) == -1) {
//LeaveCriticalSection( &csHooks );
return -1;
}
char *oldCurrentEventName = currentEventName;
currentEventName = p->name;
// NOTE: We've got the critical section while all this lot are called. That's mostly safe, though.
for (i = 0; i < p->subscriberCount; i++) {
if (p->subscriber[i].pfnHook != NULL) {
returnVal = p->subscriber[i].pfnHook(pParam);
if (abortable && returnVal)
break;
}
/*else if ( p->subscriber[i].hwnd != NULL ) {
returnVal = SendMessage( p->subscriber[i].hwnd, p->subscriber[i].message, wParam, lParam );
if ( returnVal )
break;
}*/
}
// check for no hooks and call the default hook if any
if (p->subscriberCount == 0 && p->pfnHook != 0)
returnVal = p->pfnHook(pParam);
currentEventName = oldCurrentEventName;
//LeaveCriticalSection(&csHooks);
return returnVal;
}
int NotifyEventHooks(HANDLE hEvent, uintptr_t pParam)
{
return CallHookSubscribers(hEvent, pParam, true);
}
void NotifyEventHooksNotAbortable(HANDLE hEvent, uintptr_t pParam)
{
CallHookSubscribers(hEvent, pParam, false);
}
HANDLE HookEventOptionally(const char* name, NWNXHOOK hookProc)
{
int idx;
THook* p;
HANDLE ret;
//EnterCriticalSection( &csHooks );
if (!List_GetIndex((SortedList*)&hooks, (void*)name, &idx)) {
#ifdef _DEBUG
//OutputDebugStringA("Attempt to hook: \t");
//OutputDebugStringA(name);
//OutputDebugStringA("\n");
#endif
//LeaveCriticalSection(&csHooks);
return NULL;
}
p = hooks.items[ idx ];
p->subscriber = (THookSubscriber*)realloc(p->subscriber,
sizeof(THookSubscriber) * (p->subscriberCount + 1));
p->subscriber[ p->subscriberCount ].pfnHook = hookProc;
p->subscriber[ p->subscriberCount ].hOwner = GetInstByAddress(hookProc);
p->subscriber[ p->subscriberCount ].hwnd = NULL;
p->subscriberCount++;
ret = (HANDLE)((p->id << 16) | p->subscriberCount);
if (p->inintialized == 0 && p->pfnInitialize) {
p->inintialized = 1;
p->pfnInitialize(0);
}
//LeaveCriticalSection( &csHooks );
return ret;
}
HANDLE HookEvent(const char *name, NWNXHOOK hookProc)
{
HANDLE ret = HookEventOptionally(name, hookProc);
if (!ret) {
fprintf(stderr,
"[PluginLink] a plugin requires hook '%s', which noone provides.\n",
name);
fprintf(stderr, "Backtrace:\n");
void* callstack[32];
int i, frames = backtrace(callstack, 32);
char** bt = backtrace_symbols(callstack, frames);
for (i = 0; i < frames; ++i)
fprintf(stderr, " - %s\n", bt[i]);
free(bt);
abort();
}
return ret;
}
HANDLE HookEventMessage(const char* name, HWND hwnd, UINT message)
{
int idx;
THook* p;
HANDLE ret;
//EnterCriticalSection( &csHooks );
if (!List_GetIndex((SortedList*)&hooks, (void*)name, &idx)) {
#ifdef _DEBUG
//MessageBoxA(NULL,"Attempt to hook non-existant event",name,MB_OK);
#endif
//LeaveCriticalSection(&csHooks);
return NULL;
}
p = hooks.items[ idx ];
p->subscriber = (THookSubscriber*)realloc(p->subscriber,
sizeof(THookSubscriber) * (p->subscriberCount + 1));
p->subscriber[ p->subscriberCount ].pfnHook = NULL;
p->subscriber[ p->subscriberCount ].hwnd = hwnd;
p->subscriber[ p->subscriberCount ].message = message;
p->subscriberCount++;
ret = (HANDLE)((p->id << 16) | p->subscriberCount);
//LeaveCriticalSection( &csHooks );
return ret;
}
int UnhookEvent(HANDLE hHook)
{
int i;
THook* p = NULL;
int hookId = (int)hHook >> 16;
int subscriberId = ((int)hHook & 0xFFFF) - 1;
//EnterCriticalSection( &csHooks );
for (i = 0; i < hooks.count; i++) {
if (hooks.items[i]->id == hookId) {
p = hooks.items[i];
break;
}
}
if (p == NULL) {
//LeaveCriticalSection( &csHooks );
return 1;
}
if (subscriberId >= p->subscriberCount || subscriberId < 0) {
//LeaveCriticalSection( &csHooks );
return 1;
}
p->subscriber[subscriberId].pfnHook = NULL;
p->subscriber[subscriberId].hwnd = NULL;
p->subscriber[subscriberId].hOwner = NULL;
while (p->subscriberCount && p->subscriber[p->subscriberCount - 1].pfnHook == NULL
&& p->subscriber[p->subscriberCount - 1].hwnd == NULL)
p->subscriberCount--;
if (p->subscriberCount == 0) {
if (p->subscriber) free(p->subscriber);
p->subscriber = NULL;
}
//LeaveCriticalSection( &csHooks );
return 0;
}
void KillModuleEventHooks(HINSTANCE hInst)
{
int i, j;
//EnterCriticalSection(&csHooks);
for (i = hooks.count - 1; i >= 0; i--) {
if (hooks.items[i]->subscriberCount == 0)
continue;
for (j = hooks.items[i]->subscriberCount - 1; j >= 0; j--) {
if (hooks.items[i]->subscriber[j].hOwner == hInst) {
char szModuleName[ MAX_PATH ];
/*GetModuleFileNameA( hooks.items[i]->subscriber[j].hOwner, szModuleName, sizeof(szModuleName));
Netlib_Logf( NULL, "A hook %08x for event '%s' was abnormally deleted because module '%s' didn't released it",
hooks.items[i]->subscriber[j].pfnHook, hooks.items[i]->name, szModuleName );*/
UnhookEvent((HANDLE)((hooks.items[i]->id << 16) + j + 1));
if (hooks.items[i]->subscriberCount == 0)
break;
}
}
}
//LeaveCriticalSection(&csHooks);
}
/////////////////////SERVICES
static __inline TService* FindServiceByHash(DWORD hash)
{
int idx;
if (List_GetIndex((SortedList*)&services, &hash, &idx))
return services.items[idx];
return NULL;
}
static __inline TService* FindServiceByName(const char *name)
{
return FindServiceByHash(NameHashFunction(name));
}
HANDLE CreateServiceFunction(const char *name, NWNXSERVICE serviceProc)
{
DWORD hash;
int idx;
TService* p;
#ifdef _DEBUG
if (name == NULL) {
MessageBoxA(0, "Someone tried to create a NULL'd service, see call stack for more info", "", 0);
DebugBreak();
return NULL;
}
#else
if (name == NULL) return NULL;
#endif
hash = NameHashFunction(name);
//EnterCriticalSection( &csServices );
if (List_GetIndex((SortedList*)&services, &hash, &idx)) {
//LeaveCriticalSection( &csServices );
return NULL;
}
p = malloc(sizeof(*p) + strlen(name));
strcpy(p->name, name);
p->nameHash = hash;
p->pfnService = serviceProc;
p->hOwner = GetInstByAddress(serviceProc);
p->isParam = 0;
List_Insert((SortedList*)&services, p, idx);
//LeaveCriticalSection( &csServices );
return (HANDLE)hash;
}
int DestroyServiceFunction(HANDLE hService)
{
int idx;
//EnterCriticalSection( &csServices );
if (List_GetIndex((SortedList*)&services, &hService, &idx)) {
free(services.items[idx]);
List_Remove((SortedList*)&services, idx);
}
//LeaveCriticalSection(&csServices);
return 0;
}
int ServiceExists(const char *name)
{
int ret;
if (name == NULL)
return FALSE;
//EnterCriticalSection( &csServices );
ret = FindServiceByName(name) != NULL;
//LeaveCriticalSection( &csServices );
return ret;
}
int CallService(const char *name, uintptr_t pParam)
{
TService *pService;
NWNXSERVICE pfnService;
int isParam;
uintptr_t fnParam;
#ifdef _DEBUG
if (name == NULL) {
//MessageBoxA(0,"Someone tried to CallService(NULL,..) see stack trace for details","",0);
//DebugBreak();
return CALLSERVICE_NOTFOUND;
}
#else
if (name == NULL) return CALLSERVICE_NOTFOUND;
#endif
//EnterCriticalSection(&csServices);
pService = FindServiceByName(name);
if (pService == NULL) {
//LeaveCriticalSection(&csServices);
#ifdef _DEBUG
//MessageBoxA(NULL,"Attempt to call non-existant service",name,MB_OK);
//OutputDebugStringA("Missing service called: \t");
//OutputDebugStringA(name);
//OutputDebugStringA("\n");
#endif
/* { MISSING_SERVICE_PARAMS params = { name, wParam, lParam };
int result = NotifyEventHooks(hMissingService,0,(LPARAM)¶ms);
if (result != 0)
return params.lParam;
} */
return CALLSERVICE_NOTFOUND;
}
pfnService = pService->pfnService;
isParam = pService->isParam;
fnParam = pService->pParam;
//LeaveCriticalSection(&csServices);
if (isParam)
return ((int (*)(uintptr_t, uintptr_t))pfnService)(pParam, fnParam);
else
return ((int (*)(uintptr_t))pfnService)(pParam);
}
void KillModuleServices(HINSTANCE hInst)
{
int i;
//EnterCriticalSection(&csServices);
for (i = services.count - 1; i >= 0; i--) {
if (services.items[i]->hOwner == hInst) {
char szModuleName[ MAX_PATH ];
/*GetModuleFileNameA( services.items[i]->hOwner, szModuleName, sizeof(szModuleName));
Netlib_Logf( NULL, "A service function '%s' was abnormally deleted because module '%s' didn't released it",
services.items[i]->name, szModuleName );*/
DestroyServiceFunction((HANDLE)services.items[i]->nameHash);
}
}
//LeaveCriticalSection(&csServices);
}