root/lang/c/GSD/trunk/hook.cpp @ 6952

Revision 6952, 11.2 kB (checked in by mosa, 5 years ago)

lang/c/GSD: Add APIs.

Line 
1/*
2 * GSD
3 *
4 * Copyright (C) 2008 mosa ��e5bW6vDOJ. <pcyp4g@gmail.com>
5 *
6 * This is free software with ABSOLUTELY NO WARRANTY.
7 *
8 * You can redistribute it and/or modify it under the terms of
9 * the GNU Lesser General Public License.
10 *
11 */
12
13#include <windows.h>
14#include <stdio.h>
15
16#include "common.h"
17#include "tools.h"
18#include "version.h"
19
20#include "hook.h"
21#include "hookd3d9.h"
22#include "hookd3d8.h"
23#include "hookogl.h"
24
25//-----------------------------------------------------------------------------
26
27#pragma data_seg(".sharedata")
28static HHOOK hHook = NULL;
29BOOL dllActive = FALSE;
30#pragma data_seg()
31
32//-----------------------------------------------------------------------------
33
34HINSTANCE g_hInstance = NULL;
35
36static bool hooked9 = false;
37static bool hooked8 = false;
38static bool hookedOGL = false;
39static DWORD dwCheckTimer = 0;
40
41static struct sLocal *lo = NULL;
42
43//-----------------------------------------------------------------------------
44
45typeSetWindowsHookExW extSetWindowsHookExW = NULL;
46void *addrSetWindowsHookExW = NULL;
47
48__declspec(naked) HHOOK WINAPI extSetWindowsHookExWXP(int idHook, HOOKPROC lpfn, HINSTANCE hmod, DWORD dwThreadId)
49{
50        __asm
51        {
52                push    ebp 
53                mov             ebp, esp
54                jmp             addrSetWindowsHookExW           
55        }
56}
57
58//-----------------------------------------------------------------------------
59
60bool InitSub();
61
62VOID CALLBACK CheckTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
63{
64        if(!hooked9)
65        {
66                if(GetModuleHandle("d3d9.dll") != NULL)
67                {
68                        dprintf("Direct3D9 hook...");
69
70                        InitSub();
71
72                        SetHookD3D9();
73                        hooked9 = true;
74                }
75        }
76
77        if(!hooked8)
78        {
79                if(GetModuleHandle("d3d8.dll") != NULL)
80                {
81                        dprintf("Direct3D8 hook...");
82
83                        InitSub();
84
85                        SetHookD3D8();
86                        hooked8 = true;
87                }
88        }
89
90        if(!hookedOGL)
91        {
92                if(GetModuleHandle("opengl32.dll") != NULL)
93                {
94                        dprintf("OpenGL hook...");
95
96                        InitSub();
97
98                        SetHookOGL();
99                        hookedOGL = true;
100                }
101        }
102
103        if(hooked9)
104                CheckHookD3D9();
105        if(hooked8)
106                CheckHookD3D8();
107        if(hookedOGL)
108                CheckHookOGL();
109}
110
111//-----------------------------------------------------------------------------
112
113bool Init()
114{       // �v���Z�X�ɓǂݍ��܂ꂽ�Ƃ��Ăяo��������1��   bool succeed = false;
115
116        CheckTimerProc(NULL, 0, 0, 0);
117
118        do
119        {
120                dwCheckTimer = SetTimer(NULL, 0, 1000, CheckTimerProc);
121                if(dwCheckTimer == 0)
122                {
123                        dprintf("SetTimer() failed.");
124                        break;
125                }
126
127                succeed = true;
128
129        }while(false);
130
131        return succeed;
132}
133
134//-----------------------------------------------------------------------------
135
136bool InitSub()
137{       // Hook�����Ƃ��Ɍďo��������������        if(g == NULL)
138        {
139                g = new sGlobal;
140
141                CMicroTimer::calcError();
142        }
143
144        return true;
145}
146
147//-----------------------------------------------------------------------------
148
149void UninitSub()
150{       // Hook������ꂽ�Ƃ��Ɍďo���� (1��  if(g != NULL)
151                delete g;
152        g = NULL;
153}
154
155//-----------------------------------------------------------------------------
156
157void Uninit()
158{       // �v���Z�X���������[�h�����Ƃ��Ăяo���� (1��     KillTimer(NULL, dwCheckTimer);
159
160        if(hooked9 || hooked8|| hookedOGL)
161        {
162                if(hooked9)
163                        ResetHookD3D9();
164                if(hooked8)
165                        ResetHookD3D8();
166                if(hookedOGL)
167                        ResetHookOGL();
168
169                UninitSub();
170
171                hooked9 = false;
172                hooked8 = false;
173                hookedOGL = false;
174        }
175}
176
177//-----------------------------------------------------------------------------
178
179static bool initiated = false;
180
181BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
182{
183        OSVERSIONINFO ver;
184        void *baseAddr;
185
186        switch(dwReason)
187        {
188        case DLL_PROCESS_ATTACH:
189                g_hInstance = hInstance;
190                DisableThreadLibraryCalls(hInstance);
191
192                LoadLibrary("user32.dll");
193
194                baseAddr = GetProcAddress(GetModuleHandle("user32.dll"), "SetWindowsHookExW");
195
196                ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
197                GetVersionEx(&ver);
198
199                if(ver.dwMajorVersion == 5)
200                {
201                        if(ver.dwMinorVersion > 0)
202                        {
203                                addrSetWindowsHookExW = (char *)baseAddr + 5;
204                                extSetWindowsHookExW = extSetWindowsHookExWXP;
205                        }
206                }
207
208                if(ver.dwMajorVersion == 6)
209                {
210                        addrSetWindowsHookExW = (char *)baseAddr + 5;
211                        extSetWindowsHookExW = extSetWindowsHookExWXP;
212                }
213               
214                if(extSetWindowsHookExW == NULL)
215                        extSetWindowsHookExW = (typeSetWindowsHookExW)baseAddr;
216
217                {
218                        bool pass = true;
219                        char sztemp[MAX_PATH*2], windir[MAX_PATH*2];
220
221                        GetWindowsDirectory(windir, MAX_PATH*2);
222                        GetModuleFileName(GetModuleHandle(NULL), sztemp, MAX_PATH*2);
223
224                        if(dllActive == FALSE)
225                                pass = false;
226
227                        if(strstr(sztemp, windir))
228                                pass = false;
229
230                        if(strstr(sztemp, "rrtest.exe"))
231                                pass = false;
232
233                        if(pass)
234                        {
235                                dprintf("Inject %s (%d)", sztemp, GetCurrentProcessId());
236
237                                Init();
238                                initiated = true;
239                        }
240                }
241                break;
242        case DLL_PROCESS_DETACH:
243
244                if(initiated)
245                        Uninit();
246
247                break;
248        }
249
250    return TRUE;
251}
252
253//-----------------------------------------------------------------------------
254
255LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
256{
257        return CallNextHookEx(hHook, nCode, wParam, lParam);
258}
259
260//-----------------------------------------------------------------------------
261
262DllExport BOOL WINAPI GSD_Initialize()
263{
264        int n1 = 128, n2 = 128, n3 = 128;
265
266        if(lo)
267                return FALSE;
268
269        lo = new sLocal;
270
271        GetOffsetD3D9(&n1);
272        GetOffsetD3D8(&n2);
273        GetOffsetOGL(&n3);
274
275        lo->maxTextureSize = min(n1, min(n2, n3));
276
277        hHook = extSetWindowsHookExW(WH_CBT, HookProc, g_hInstance, 0);
278        if(!hHook)
279        {
280                dprintf("SetWindowsHook() failed.");
281
282                delete lo;
283                lo = NULL;
284
285                return FALSE;
286        }
287
288        dllActive = TRUE;
289
290        return TRUE;
291}
292
293//-----------------------------------------------------------------------------
294
295DllExport void WINAPI GSD_Finalize()
296{
297        dllActive = FALSE;
298
299        if(lo)
300        {
301                delete lo;
302                lo = NULL;
303        }
304
305        if(hHook)
306        {
307                UnhookWindowsHookEx(hHook);
308                hHook = NULL;
309        }
310}
311
312//-----------------------------------------------------------------------------
313
314DllExport int WINAPI GSD_GetMaxTextureNum()
315{
316        return MAX_TEXNUM;
317}
318
319//-----------------------------------------------------------------------------
320
321DllExport int WINAPI GSD_GetMaxTextureSize()
322{
323        if(!lo)
324                return 0;
325
326        return lo->maxTextureSize;
327}
328
329//-----------------------------------------------------------------------------
330
331DllExport DWORD WINAPI GSD_GetLastUpdate()
332{
333        if(!lo || !lo->settings)
334                return 0;
335
336        return lo->settings->lastUpdate;
337}
338
339//-----------------------------------------------------------------------------
340
341DllExport DWORD WINAPI GSD_GetVersion()
342{
343        sVersionInfo info;
344
345        if(ReadVersionModule(g_hInstance, &info))
346        {
347                DWORD out;
348
349                out = ((info.word.wVer1&0xff)<<24) | ((info.word.wVer2&0xff)<<16) | ((info.word.wVer3&0xff)<<8) | (info.word.wVer4&0xff);
350
351                return out;
352        }
353        else
354        {
355                return 0;
356        }
357}
358
359//-----------------------------------------------------------------------------
360
361DllExport BOOL WINAPI GSD_InitTexture(const DWORD *size, int num)
362{
363        int i;
364        DWORD total;
365        char sztemp[256];
366
367        if(!lo || !lo->settings)
368                return FALSE;
369
370        if(!size || num < 1)
371                return FALSE;
372
373        lo->offsets[0] = 0;
374        lo->sizes[0] = size[0];
375
376        for(i=1; i<num; i++)
377        {
378                lo->offsets[i] = lo->offsets[i-1] + size[i-1];
379                lo->sizes[i] = size[i];
380        }
381
382        for(; i<MAX_TEXNUM; i++)
383        {
384                lo->offsets[i] = 0;
385                lo->sizes[i] = 0;
386        }
387
388        total = 0;
389        for(i=0; i<num; i++)
390                total += size[i];
391
392        lo->settings->pixelsRevision++;
393
394        sprintf(sztemp, "%s%d", SHARED_PIXELS, lo->settings->pixelsRevision);
395
396        if(lo->smPixels.create(sztemp, total))
397        {
398                lo->settings->pixelsDataSize = total;
399                lo->maxIndex = num;
400        }
401        else
402        {
403                lo->settings->pixelsDataSize = 0;
404                lo->maxIndex = 0;
405                return FALSE;
406        }
407
408        return TRUE;
409}
410
411//-----------------------------------------------------------------------------
412
413DllExport BOOL WINAPI GSD_SetTexture(int index, const struct GSD_TextureInfo *info)
414{
415        if(!lo || !lo->settings)
416                return FALSE;
417
418        if(!info || index >= MAX_TEXNUM)
419                return FALSE;
420
421        if(index >= lo->maxIndex && info->data)
422                return FALSE;
423
424        if(!lo->smPixels.getData() && info->data)
425                return FALSE;
426
427        lo->settings->texInfo[index].active = info->active;
428        lo->settings->texInfo[index].align = info->align;
429        lo->settings->texInfo[index].color = info->color;
430        lo->settings->texInfo[index].x = info->x;
431        lo->settings->texInfo[index].y = info->y;
432        lo->settings->texInfo[index].texSize = info->texSize;
433
434        if(info->data)
435        {
436                lo->settings->texInfo[index].offset = lo->offsets[index];
437                lo->settings->texInfo[index].revision++;
438
439                memcpy(lo->smPixels.getData() + lo->offsets[index], info->data, lo->sizes[index]);
440        }
441
442        return TRUE;
443}
444
445//-----------------------------------------------------------------------------
446
447DllExport BOOL WINAPI GSD_GetTexture(int index, struct GSD_TextureInfo *info)
448{
449        if(!lo || !lo->settings)
450                return FALSE;
451
452        if(!info || index >= MAX_TEXNUM)
453                return FALSE;
454
455        if(index >= lo->maxIndex && info->data)
456                return FALSE;
457
458        if(!lo->smPixels.getData() && info->data)
459                return FALSE;
460
461        info->active = lo->settings->texInfo[index].active;
462        info->align = lo->settings->texInfo[index].align;
463        info->color = lo->settings->texInfo[index].color;
464        info->x = lo->settings->texInfo[index].x;
465        info->y = lo->settings->texInfo[index].y;
466        info->texSize = lo->settings->texInfo[index].texSize;
467
468        if(info->data)
469                memcpy(info->data, lo->smPixels.getData() + lo->offsets[index], lo->sizes[index]);
470
471        return TRUE;
472}
473
474//-----------------------------------------------------------------------------
475
476DllExport void WINAPI GSD_DelTexture(int index)
477{
478        if(!lo || !lo->settings)
479                return;
480
481        if(index >= MAX_TEXNUM)
482                return;
483
484        lo->settings->texInfo[index].texSize = 0;
485}
486
487//-----------------------------------------------------------------------------
488
489DllExport void WINAPI GSD_SetTimerOrg(__int64 org)
490{
491        if(!lo || !lo->settings)
492                return;
493
494        lo->settings->orgTime = org;
495}
496
497//-----------------------------------------------------------------------------
498
499DllExport void WINAPI GSD_SetFpsLimit(double fps)
500{
501        if(!lo || !lo->settings)
502                return;
503
504        if(fps < 0.0)
505                return;
506
507        lo->settings->fpsLimit = fps;
508}
509
510//-----------------------------------------------------------------------------
511
512DllExport void WINAPI GSD_SetFpsAlign(DWORD align)
513{
514        if(!lo || !lo->settings)
515                return;
516
517        lo->settings->fpsAlign = align;
518}
519
520//-----------------------------------------------------------------------------
521
522DllExport void WINAPI GSD_ShowFps(BOOL show)
523{
524        if(!lo || !lo->settings)
525                return;
526
527        lo->settings->showFPS = show;
528}
529
530//-----------------------------------------------------------------------------
531
532DllExport void WINAPI GSD_UpdateOnActive(BOOL uoa)
533{
534        if(!lo || !lo->settings)
535                return;
536
537        lo->settings->updateOnActive = uoa;
538}
539
540//-----------------------------------------------------------------------------
541
542DllExport void WINAPI GSD_DataLock()
543{
544        if(!lo)
545                return;
546
547        lo->mutexSettings.lock();
548}
549
550//-----------------------------------------------------------------------------
551
552DllExport void WINAPI GSD_DataUnlock()
553{
554        if(!lo)
555                return;
556
557        lo->mutexSettings.unlock();
558}
559
560//-----------------------------------------------------------------------------
Note: See TracBrowser for help on using the browser.