~shimon/thd3d8mitigation

ref: 73e1b176f1a04e97cfd83a3fb41835b996960b4a thd3d8mitigation/thd3d8mitigation.c -rw-r--r-- 19.0 KiB
73e1b176Itoh Shimon 7 months ago
                                                                                
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#include "thd3d8mitigation.h"

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define CINTERFACE
#include <d3d8.h>

#include <mmsystem.h>

#include <math.h>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

enum InitStatus {
	INITSTATUS_UNINITED,
	INITSTATUS_SUCCEEDED,
	INITSTATUS_FAILED
};

CRITICAL_SECTION g_CS;

const char* const ConfigWaitForNameTable[3] = {
	[CONFIG_WAITFOR_VSYNC] = "vsync",
	[CONFIG_WAITFOR_NORMAL] = "normal",
	[CONFIG_WAITFOR_AUTO] = "auto"
};
static enum ConfigWaitFor g_ConfigFileWaitFor;

static HMODULE g_D3D8Handle;
static Direct3DCreate8_t g_VanillaDirect3DCreate8;
static struct IDirect3D8ExtraDataTable* g_D3D8ExDataTable;
static struct IDirect3DDevice8ExtraDataTable* g_D3DDev8ExDataTable;

BOOL NeedPresentMitigation(D3DPRESENT_PARAMETERS* pp)
{
	return !pp->Windowed && (pp->FullScreen_PresentationInterval == D3DPRESENT_INTERVAL_DEFAULT ||
								pp->FullScreen_PresentationInterval == D3DPRESENT_INTERVAL_ONE ||
								pp->FullScreen_PresentationInterval == D3DPRESENT_INTERVAL_TWO ||
								pp->FullScreen_PresentationInterval == D3DPRESENT_INTERVAL_THREE ||
								pp->FullScreen_PresentationInterval == D3DPRESENT_INTERVAL_FOUR);
}

HRESULT ModIDirect3DDevice8PresentWithGetRasterStatus(IDirect3DDevice8* me, struct IDirect3DDevice8ExtraData* me_exdata,
	CONST RECT* pSourceRect, CONST RECT* pDestRect,
	HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion)
{
	D3DRASTER_STATUS stat;
	HRESULT ret;

	do
	{
		if (FAILED(me->lpVtbl->GetRasterStatus(me, &stat)))
		{
			Log("%s: error: IDirect3DDevice8::GetRasterStatus failed.", __FUNCTION__);
			return E_FAIL;
		}
		SleepEx(0, TRUE);
	} while (stat.InVBlank);

	ret = me_exdata->VanillaPresent(me, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);

	do
	{
		if (FAILED(me->lpVtbl->GetRasterStatus(me, &stat)))
		{
			Log("%s: error: IDirect3DDevice8::GetRasterStatus failed.", __FUNCTION__);
			return E_FAIL;
		}
		SleepEx(0, TRUE);
	} while (!stat.InVBlank);

	return ret;
}

HRESULT ModIDirect3DDevice8PresentVanilla(IDirect3DDevice8* me, struct IDirect3DDevice8ExtraData* me_exdata,
	CONST RECT* pSourceRect, CONST RECT* pDestRect,
	HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion)
{
	return me_exdata->VanillaPresent(me, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
}

/*
 * Windows 10以降、IDirect3DDevice8::PresentはD3DPRESENT_PARAMETERS.FullScreen_PresentationIntervalに適切な値を設定しても
 * 待機しなくなった。これにより紅魔郷のゲームスピードが高速化してしまうようになった。そのため、必要であれば独自の方法で
 * 待機する。
 */
HRESULT __stdcall ModIDirect3DDevice8Present(IDirect3DDevice8* me, CONST RECT* pSourceRect, CONST RECT* pDestRect,
	HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion)
{
	struct IDirect3DDevice8ExtraData* me_exdata;

	/* load from critical section */
	EnterCriticalSection(&g_CS);
	me_exdata = IDirect3DDevice8ExtraDataTableGet(g_D3DDev8ExDataTable, me);
	LeaveCriticalSection(&g_CS);

	if (me_exdata == NULL)
	{
		Log("%s: bug, error: IDirect3DDevice8ExtraDataTableGet failed.", __FUNCTION__);
		return E_FAIL;
	}

	return me_exdata->ModPresent(me, me_exdata, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
}

ULONG cs_ModIDirect3DDevice8ReleaseImpl(IDirect3DDevice8* me)
{
	ULONG ret;
	struct IDirect3DDevice8ExtraData* me_exdata;

	if ((me_exdata = IDirect3DDevice8ExtraDataTableGet(g_D3DDev8ExDataTable, me)) == NULL)
	{
		Log("%s: bug, warning: IDirect3DDevice8ExtraDataTableGet failed. Resource is leaking.", __FUNCTION__);
		return 0;
	}

	if ((ret = me_exdata->VanillaRelease(me)) == 0)
	{
		IDirect3DDevice8ExtraDataTableErase(g_D3DDev8ExDataTable, me);
		IDirect3DDevice8ExtraDataTableShrinkToFit(g_D3DDev8ExDataTable);
	}

	return ret;
}

ULONG __stdcall ModIDirect3DDevice8Release(IDirect3DDevice8* me)
{
	ULONG ret;

	EnterCriticalSection(&g_CS);
	ret = cs_ModIDirect3DDevice8ReleaseImpl(me);
	LeaveCriticalSection(&g_CS);
	return ret;
}

BOOL tm_MeasureNormalFrameSecond(double* ret_frame_second, IDirect3DDevice8* me, struct IDirect3DDevice8ExtraData* me_exdata)
{
	double absolute_deviation_threshold = 3.0;

	DWORD starttime, endtime, sum;
	DWORD frame_second_list[10];
	double average, absolute_deviation, absolute_deviation_max;
	int i, j;

	for (i = 0; i < ARRAY_SIZE(frame_second_list); i++)
	{
		starttime = timeGetTime();
		if (FAILED(me_exdata->VanillaPresent(me, NULL, NULL, NULL, NULL)))
			return FALSE;
		endtime = timeGetTime();
		frame_second_list[i] = endtime - starttime;
	}

	for (i = 0; i < ARRAY_SIZE(frame_second_list) * 10; i++)
	{
		absolute_deviation_max = 0.0;
		sum = 0;
		for (j = 0; j < ARRAY_SIZE(frame_second_list); j++)
			sum += frame_second_list[j];
		average = (double)sum / (double)ARRAY_SIZE(frame_second_list);
		for (j = 0; j < ARRAY_SIZE(frame_second_list); j++)
		{
			absolute_deviation = fabs(average - frame_second_list[j]);
			if (absolute_deviation_max < absolute_deviation)
				absolute_deviation_max = absolute_deviation;
		}
		/* 入力が安定したら測定を終了する。 */
		if (absolute_deviation_max < absolute_deviation_threshold)
			break;

		starttime = timeGetTime();
		if (FAILED(me_exdata->VanillaPresent(me, NULL, NULL, NULL, NULL)))
			return FALSE;
		endtime = timeGetTime();

		frame_second_list[i % ARRAY_SIZE(frame_second_list)] = endtime - starttime;
	}

	*ret_frame_second = average;
	return TRUE;
}

BOOL tm_DetectProperConfig(IDirect3DDevice8* me, struct IDirect3DDevice8ExtraData* me_exdata,
	enum ConfigWaitFor* config_wait_for)
{
	double frame_second = 0.0;
	double frame_second_threshold = 15.0; /* 66.6666... FPS */

	Log("%s: Detecting proper config...", __FUNCTION__);

	if (!tm_MeasureNormalFrameSecond(&frame_second, me, me_exdata))
	{
		Log("%s: MeasureNormalFrameSecond failed.", __FUNCTION__);
		return FALSE;
	}

	if (frame_second > frame_second_threshold)
	{
		*config_wait_for = CONFIG_WAITFOR_NORMAL;
		Log("%s: Detected proper config: normal", __FUNCTION__);
		return TRUE;
	}

	*config_wait_for = CONFIG_WAITFOR_VSYNC;
	Log("%s: Detected proper config: vsync", __FUNCTION__);
	return TRUE;
}

BOOL DetectProperConfig(IDirect3DDevice8* me, struct IDirect3DDevice8ExtraData* me_exdata,
	enum ConfigWaitFor* config_wait_for)
{
	BOOL ret;

	timeBeginPeriod(1);
	ret = tm_DetectProperConfig(me, me_exdata, config_wait_for);
	timeEndPeriod(1);
	return ret;
}

BOOL InitIDirect3DDevice8ExtraDataModPresent(IDirect3DDevice8* me, struct IDirect3DDevice8ExtraData* me_exdata)
{
	enum ConfigWaitFor wait_for;

	/* load from critical section */
	EnterCriticalSection(&g_CS);
	wait_for = g_ConfigFileWaitFor;
	LeaveCriticalSection(&g_CS);

	if (NeedPresentMitigation(&me_exdata->pp))
	{
		if (wait_for == CONFIG_WAITFOR_AUTO)
		{
			if (!DetectProperConfig(me, me_exdata, &wait_for))
			{
				Log("%s: error: DetectProperConfig failed.", __FUNCTION__);
				return FALSE;
			}
		}

		switch (wait_for)
		{
		case CONFIG_WAITFOR_NORMAL:
			me_exdata->ModPresent = ModIDirect3DDevice8PresentVanilla;
			break;
		case CONFIG_WAITFOR_VSYNC:
			me_exdata->ModPresent = ModIDirect3DDevice8PresentWithGetRasterStatus;
			break;
		default:
			Log("%s: bug, error: unreachable.", __FUNCTION__);
			return FALSE;
		}
	}
	else
		me_exdata->ModPresent = ModIDirect3DDevice8PresentVanilla;

	return TRUE;
}

HRESULT cs_ModIDirect3DDevice8ResetImpl(IDirect3DDevice8* me, D3DPRESENT_PARAMETERS* pPresentationParameters)
{
	HRESULT ret;
	struct IDirect3DDevice8ExtraData* me_exdata;

	if ((me_exdata = IDirect3DDevice8ExtraDataTableGet(g_D3DDev8ExDataTable, me)) == NULL)
	{
		Log("%s: bug, error: IDirect3DDevice8ExtraDataTableGet failed.", __FUNCTION__);
		return E_FAIL;
	}

	if (SUCCEEDED(ret = me_exdata->VanillaReset(me, pPresentationParameters)))
	{
		me_exdata->pp = *pPresentationParameters;
		if (!InitIDirect3DDevice8ExtraDataModPresent(me, me_exdata))
		{
			Log("%s: error: InitIDirect3DDevice8ExtraDataModPresent failed.", __FUNCTION__);
			return E_FAIL;
		}
	}

	return ret;
}

HRESULT __stdcall ModIDirect3DDevice8Reset(IDirect3DDevice8* me, D3DPRESENT_PARAMETERS* pPresentationParameters)
{
	HRESULT ret;

	EnterCriticalSection(&g_CS);
	ret = cs_ModIDirect3DDevice8ResetImpl(me, pPresentationParameters);
	LeaveCriticalSection(&g_CS);
	return ret;
}

HRESULT cs_ModIDirect3D8CreateDeviceImpl(IDirect3D8* me, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
	DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
	IDirect3DDevice8** ppReturnedDeviceInterface)
{
	HRESULT ret;
	struct IDirect3D8ExtraData* me_exdata;
	DWORD orig_protect;
	IDirect3DDevice8* d3ddev8;
	IDirect3DDevice8Vtbl* vtbl;
	struct IDirect3DDevice8ExtraData d3ddev8_exdata;

	if ((me_exdata = IDirect3D8ExtraDataTableGet(g_D3D8ExDataTable, me)) == NULL)
	{
		Log("%s: bug, error: IDirect3D8ExtraDataTableGet failed.", __FUNCTION__);
		return E_FAIL;
	}

	if (FAILED(ret = me_exdata->VanillaCreateDevice(me, Adapter, DeviceType, hFocusWindow, BehaviorFlags,
				   pPresentationParameters, ppReturnedDeviceInterface)))
		return ret;

	/*
	 * hook IDirect3DDevice8::Present, IDirect3DDevice8::Release (inherit from IUnknown::Release), and
	 * IDirect3DDevice8::Reset
	 */

	d3ddev8 = *ppReturnedDeviceInterface;
	vtbl = d3ddev8->lpVtbl;

	if (!VirtualProtect(vtbl, sizeof(*vtbl), PAGE_READWRITE, &orig_protect))
	{
		LogWithErrorCode(GetLastError(), "%s: error: VirtualProtect (PAGE_READWRITE) failed", __FUNCTION__);
		return E_FAIL;
	}

	d3ddev8_exdata = (struct IDirect3DDevice8ExtraData){ .VanillaPresent = vtbl->Present,
		.VanillaRelease = vtbl->Release,
		.VanillaReset = vtbl->Reset,
		.pp = *pPresentationParameters };
	if (!InitIDirect3DDevice8ExtraDataModPresent(d3ddev8, &d3ddev8_exdata))
	{
		Log("%s: error: InitIDirect3DDevice8ExtraDataModPresent failed.", __FUNCTION__);
		return E_FAIL;
	}

	if (!IDirect3DDevice8ExtraDataTableInsert(g_D3DDev8ExDataTable, d3ddev8, d3ddev8_exdata))
	{
		Log("%s: bug, error: IDirect3DDevice8ExtraDataTableInsert failed.", __FUNCTION__);
		return E_FAIL;
	}

	vtbl->Present = ModIDirect3DDevice8Present;
	vtbl->Release = ModIDirect3DDevice8Release;
	vtbl->Reset = ModIDirect3DDevice8Reset;

	/* best effort */
	if (!VirtualProtect(vtbl, sizeof(*vtbl), orig_protect, &orig_protect))
		LogWithErrorCode(GetLastError(), "%s: warning: VirtualProtect (original protect) failed.", __FUNCTION__);

	return ret;
}

HRESULT __stdcall ModIDirect3D8CreateDevice(IDirect3D8* me, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
	DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
	IDirect3DDevice8** ppReturnedDeviceInterface)
{
	HRESULT ret;

	EnterCriticalSection(&g_CS);
	ret = cs_ModIDirect3D8CreateDeviceImpl(me, Adapter, DeviceType, hFocusWindow, BehaviorFlags,
		pPresentationParameters, ppReturnedDeviceInterface);
	LeaveCriticalSection(&g_CS);
	return ret;
}

ULONG __stdcall cs_ModIDirect3D8ReleaseImpl(IDirect3D8* me)
{
	ULONG ret;
	struct IDirect3D8ExtraData* me_exdata;

	if ((me_exdata = IDirect3D8ExtraDataTableGet(g_D3D8ExDataTable, me)) == NULL)
	{
		Log("%s: bug, warning: IDirect3D8ExtraDataTableGet failed. Resource is leaking.", __FUNCTION__);
		return 0;
	}

	if ((ret = me_exdata->VanillaRelease(me)) == 0)
	{
		IDirect3D8ExtraDataTableErase(g_D3D8ExDataTable, me);
		IDirect3D8ExtraDataTableShrinkToFit(g_D3D8ExDataTable);
	}

	return ret;
}

ULONG __stdcall ModIDirect3D8Release(IDirect3D8* me)
{
	ULONG ret;

	EnterCriticalSection(&g_CS);
	ret = cs_ModIDirect3D8ReleaseImpl(me);
	LeaveCriticalSection(&g_CS);
	return ret;
}

IDirect3D8* cs_ModDirect3DCreate8Impl(UINT SDKVersion)
{
	IDirect3D8* ret;
	DWORD orig_protect;
	IDirect3D8Vtbl* vtbl;
	struct IDirect3D8ExtraData d3d8_exdata;

	if ((ret = g_VanillaDirect3DCreate8(SDKVersion)) == NULL)
		return NULL;

	/* hook IDirect3D8::CreateDevice and IDirect3D8::Release (inherit from IUnknown::Release) */

	vtbl = ret->lpVtbl;

	if (!VirtualProtect(vtbl, sizeof(*vtbl), PAGE_READWRITE, &orig_protect))
	{
		LogWithErrorCode(GetLastError(), "%s: error: VirtualProtect (PAGE_READWRITE) failed.", __FUNCTION__);
		return NULL;
	}

	d3d8_exdata =
		(struct IDirect3D8ExtraData){ .VanillaCreateDevice = vtbl->CreateDevice, .VanillaRelease = vtbl->Release };
	if (!IDirect3D8ExtraDataTableInsert(g_D3D8ExDataTable, ret, d3d8_exdata))
	{
		Log("%s: bug, error: IDirect3D8ExtraDataTableInsert failed.", __FUNCTION__);
		return NULL;
	}

	vtbl->CreateDevice = ModIDirect3D8CreateDevice;
	vtbl->Release = ModIDirect3D8Release;

	/* best effort */
	if (!VirtualProtect(vtbl, sizeof(*vtbl), orig_protect, &orig_protect))
		LogWithErrorCode(GetLastError(), "%s: warning: VirtualProtect (original protect) failed.", __FUNCTION__);

	return ret;
}

int AllocateDataDirPath(char** strp)
{
	char exepath[MAX_PATH];
	char exedrivepath[_MAX_DRIVE];
	char exedirpath[_MAX_DIR];
	DWORD len;

	if ((len = GetModuleFileNameA(NULL, exepath, sizeof(exepath))) == 0)
	{
		LogWithErrorCode(GetLastError(), "%s: error: GetModuleFileNameA failed.", __FUNCTION__);
		return -1;
	}

	if (len >= sizeof(exepath))
	{
		LogWithErrorCode(ERROR_INSUFFICIENT_BUFFER, "%s: error: GetModuleFileNameA failed.", __FUNCTION__);
		return -1;
	}

	if (_splitpath_s(exepath, exedrivepath, sizeof(exedrivepath), exedirpath, sizeof(exedirpath), NULL, 0, NULL, 0) !=
		0)
	{
		Log("%s: error: _splitpath_s failed.", __FUNCTION__);
		return -1;
	}

	return myasprintf(strp, "%s%s", exedrivepath, exedirpath);
}

int AllocateDataFilePath(char** strp, char* filename)
{
	char* datadirpath;
	int ret;

	if (AllocateDataDirPath(&datadirpath) < 0)
	{
		Log("%s: error: AllocateDataDirPath failed.", __FUNCTION__);
		return -1;
	}

	ret = myasprintf(strp, "%s%s", datadirpath, filename);
	free(datadirpath);
	return ret;
}

BOOL ExistsFile(char* path)
{
	return GetFileAttributesA(path) != INVALID_FILE_ATTRIBUTES;
}

BOOL cs_InitConfig(void)
{
	const char* section_present = "presentation";
	const char* key_wait_for = "wait_for";

	enum ConfigWaitFor config_wait_for_default = CONFIG_WAITFOR_AUTO;
	const char* value_wait_for_default = ConfigWaitForNameTable[CONFIG_WAITFOR_AUTO];

	char* path = NULL;
	char buf[32];
	BOOL ret = FALSE;

	if (AllocateDataFilePath(&path, "thd3d8mitigationcfg.ini") < 0)
	{
		Log("%s: error: AllocateDataFilePath failed.", __FUNCTION__);
		goto cleanup;
	}

	if (!ExistsFile(path))
		if (WritePrivateProfileStringA(section_present, key_wait_for, value_wait_for_default, path) == 0)
		{
			LogWithErrorCode(GetLastError(), "%s: error: WritePrivateProfileStringA (%s) failed.", __FUNCTION__,
				key_wait_for);
			goto cleanup;
		}

	g_ConfigFileWaitFor = config_wait_for_default;
	if (GetPrivateProfileStringA(section_present, key_wait_for, value_wait_for_default, buf, sizeof(buf), path) >=
		sizeof(buf) - 1)
		/* no op */;
	else if (strcmp(buf, ConfigWaitForNameTable[CONFIG_WAITFOR_VSYNC]) == 0)
		g_ConfigFileWaitFor = CONFIG_WAITFOR_VSYNC;
	else if (strcmp(buf, ConfigWaitForNameTable[CONFIG_WAITFOR_NORMAL]) == 0)
		g_ConfigFileWaitFor = CONFIG_WAITFOR_NORMAL;
	else if (strcmp(buf, ConfigWaitForNameTable[CONFIG_WAITFOR_AUTO]) == 0)
		g_ConfigFileWaitFor = CONFIG_WAITFOR_AUTO;
	else
		/* no op */;
	Log("%s: config file %s: %s", __FUNCTION__, key_wait_for, ConfigWaitForNameTable[g_ConfigFileWaitFor]);

	ret = TRUE;
cleanup:
	free(path);
	return ret;
}

BOOL InitD3D8Handle(HMODULE* ret)
{
	char sysdirpath[MAX_PATH];
	char* sysdllpath;
	unsigned int len;
	DWORD err;

	if ((len = GetSystemDirectoryA(sysdirpath, sizeof(sysdirpath))) == 0)
	{
		LogWithErrorCode(GetLastError(), "%s: error: GetSystemDirectoryA failed.", __FUNCTION__);
		return FALSE;
	}

	if (len >= sizeof(sysdirpath))
	{
		LogWithErrorCode(ERROR_INSUFFICIENT_BUFFER, "%s: error: GetSystemDirectoryA failed.", __FUNCTION__);
		return FALSE;
	}

	if (myasprintf(&sysdllpath, "%s\\d3d8.dll", sysdirpath) < 0)
	{
		Log("%s: error: myasprintf failed.", __FUNCTION__);
		return FALSE;
	}

	*ret = LoadLibraryA(sysdllpath);
	err = GetLastError();
	free(sysdllpath);
	if (*ret == NULL)
	{
		LogWithErrorCode(err, "%s: error: LoadLibraryA failed.", __FUNCTION__);
		return FALSE;
	}

	return TRUE;
}

BOOL InitVanillaDirect3DCreate8(HMODULE D3D8Handle, Direct3DCreate8_t* ret)
{
	if ((*ret = (Direct3DCreate8_t)GetProcAddress(D3D8Handle, "Direct3DCreate8")) == NULL)
	{
		LogWithErrorCode(GetLastError(), "%s: error: GetProcAddress failed.", __FUNCTION__);
		return FALSE;
	}

	return TRUE;
}

BOOL cs_LogInit(void)
{
	char* logpath;
	DWORD err;

	if (AllocateDataFilePath(&logpath, "thd3d8mitigationlog.txt") < 0)
	{
		Log("%s: error: AllocateDataFilePath failed.", __FUNCTION__);
		return FALSE;
	}

	g_LogFile = CreateFileA(logpath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	err = GetLastError();
	free(logpath);
	if (g_LogFile == INVALID_HANDLE_VALUE)
	{
		LogWithErrorCode(err, "%s: error: CreateFileA failed.", __FUNCTION__);
		return FALSE;
	}

	return TRUE;
}

BOOL cs_InitImpl(void)
{
	if (!cs_LogInit())
	{
		Log("%s: error: log initialization failed.", __FUNCTION__);
		return FALSE;
	}
	/* これ以降はログがファイルにも記録される */

	Log("%s: Version: %s", __FUNCTION__, PROGRAM_VERSION);

	if (!InitD3D8Handle(&g_D3D8Handle))
	{
		Log("%s: error: InitD3D8Handle failed.", __FUNCTION__);
		return FALSE;
	}

	if (!InitVanillaDirect3DCreate8(g_D3D8Handle, &g_VanillaDirect3DCreate8))
	{
		Log("%s: error: InitVanillaDirect3DCreate8 failed.", __FUNCTION__);
		return FALSE;
	}

	g_D3D8ExDataTable = IDirect3D8ExtraDataTableNew();
	g_D3DDev8ExDataTable = IDirect3DDevice8ExtraDataTableNew();

	if (!cs_InitConfig())
	{
		Log("%s: error: cs_InitConfig failed.", __FUNCTION__);
		return FALSE;
	}

	return TRUE;
}

BOOL cs_Init(void)
{
	static enum InitStatus g_initstatus = INITSTATUS_UNINITED;

	BOOL ret;

	switch (g_initstatus)
	{
	case INITSTATUS_SUCCEEDED:
		return TRUE;
	case INITSTATUS_FAILED:
		return FALSE;
	case INITSTATUS_UNINITED:
		break;
	default:
		Log("%s: bug, error: unreachable.", __FUNCTION__);
		return FALSE;
	}

	ret = cs_InitImpl();
	g_initstatus = ret ? INITSTATUS_SUCCEEDED : INITSTATUS_FAILED;
	return ret;
}

BOOL Init(void)
{
	BOOL ret;

	EnterCriticalSection(&g_CS);
	ret = cs_Init();
	LeaveCriticalSection(&g_CS);
	return ret;
}

IDirect3D8* WINAPI ModDirect3DCreate8(UINT SDKVersion)
{
	IDirect3D8* ret;

	/* 実質的なエントリーポイントなので、最初は初期化処理を行う */
	if (!Init())
	{
		Log("%s: error: initialization failed.", __FUNCTION__);
		return NULL;
	}

	EnterCriticalSection(&g_CS);
	ret = cs_ModDirect3DCreate8Impl(SDKVersion);
	LeaveCriticalSection(&g_CS);

	return ret;
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		/* https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-best-practices#general-best-practices */
		OutputDebugStringA(LOG_PREFIX __FUNCTION__ ": Version: " PROGRAM_VERSION "\r\n");
		OutputDebugStringA(LOG_PREFIX __FUNCTION__ ": Attaching to the process: begin\r\n");
		InitializeCriticalSection(&g_CS);
		OutputDebugStringA(LOG_PREFIX __FUNCTION__ ": Attaching to the process: succeeded\r\n");
		break;
	case DLL_PROCESS_DETACH:
		/* https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-best-practices#best-practices-for-synchronization */
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
		break;
	}

	return TRUE;
}