~shimon/thd3d8mitigation

ref: 73e1b176f1a04e97cfd83a3fb41835b996960b4a thd3d8mitigation/extradatatable.cpp -rw-r--r-- 4.6 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
#include "thd3d8mitigation.h"

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define CINTERFACE
#include <d3d8.h>

#include <stdexcept>
#include <unordered_map>

using d3d8pair = std::pair<IDirect3D8*, struct IDirect3D8ExtraData>;
using d3ddevice8pair = std::pair<IDirect3DDevice8*, struct IDirect3DDevice8ExtraData>;

struct IDirect3D8ExtraDataTable {
	std::unordered_map<IDirect3D8*, struct IDirect3D8ExtraData> ht;
};

struct IDirect3DDevice8ExtraDataTable {
	std::unordered_map<IDirect3DDevice8*, struct IDirect3DDevice8ExtraData> ht;
};

extern "C" struct IDirect3D8ExtraDataTable* IDirect3D8ExtraDataTableNew(void)
{
	try
	{
		return new struct IDirect3D8ExtraDataTable;
	}
	catch (std::exception& e)
	{
		Fatal(1, "%s: fatal: unexpected exception (%s).", __FUNCTION__, e.what());
	}
	catch (...)
	{
		Fatal(1, "%s: fatal: unexpected exception (unknown).", __FUNCTION__);
	}
}

extern "C" BOOL IDirect3D8ExtraDataTableInsert(struct IDirect3D8ExtraDataTable* h, IDirect3D8* k,
	struct IDirect3D8ExtraData v)
{
	try
	{
		return h->ht.insert(d3d8pair{ k, v }).second;
	}
	catch (std::exception& e)
	{
		Fatal(1, "%s: fatal: unexpected exception (%s).", __FUNCTION__, e.what());
	}
	catch (...)
	{
		Fatal(1, "%s: fatal: unexpected exception (unknown).", __FUNCTION__);
	}
}

extern "C" void IDirect3D8ExtraDataTableErase(struct IDirect3D8ExtraDataTable* h, IDirect3D8* k)
{
	try
	{
		h->ht.erase(k);
	}
	catch (std::exception& e)
	{
		Fatal(1, "%s: fatal: unexpected exception (%s).", __FUNCTION__, e.what());
	}
	catch (...)
	{
		Fatal(1, "%s: fatal: unexpected exception (unknown).", __FUNCTION__);
	}
}

extern "C" void IDirect3D8ExtraDataTableShrinkToFit(struct IDirect3D8ExtraDataTable* h)
{
	try
	{
		/* https://ja.wikibooks.org/wiki/More_C%2B%2B_Idioms/%E7%B8%AE%E3%82%81%E3%81%A6%E5%90%88%E3%82%8F%E3%81%9B%E3%82%8B(Shrink-to-fit) */
		std::unordered_map<IDirect3D8*, struct IDirect3D8ExtraData>(h->ht).swap(h->ht);
	}
	catch (std::exception& e)
	{
		Fatal(1, "%s: fatal: unexpected exception (%s).", __FUNCTION__, e.what());
	}
	catch (...)
	{
		Fatal(1, "%s: fatal: unexpected exception (unknown).", __FUNCTION__);
	}
}

extern "C" struct IDirect3D8ExtraData* IDirect3D8ExtraDataTableGet(struct IDirect3D8ExtraDataTable* h, IDirect3D8* k)
{
	try
	{
		try
		{
			return &h->ht.at(k);
		}
		catch (std::out_of_range&)
		{
			return NULL;
		}
	}
	catch (std::exception& e)
	{
		Fatal(1, "%s: fatal: unexpected exception (%s).", __FUNCTION__, e.what());
	}
	catch (...)
	{
		Fatal(1, "%s: fatal: unexpected exception (unknown).", __FUNCTION__);
	}
}

extern "C" struct IDirect3DDevice8ExtraDataTable* IDirect3DDevice8ExtraDataTableNew(void)
{
	try
	{
		return new struct IDirect3DDevice8ExtraDataTable;
	}
	catch (std::exception& e)
	{
		Fatal(1, "%s: fatal: unexpected exception (%s).", __FUNCTION__, e.what());
	}
	catch (...)
	{
		Fatal(1, "%s: fatal: unexpected exception (unknown).", __FUNCTION__);
	}
}

extern "C" BOOL IDirect3DDevice8ExtraDataTableInsert(struct IDirect3DDevice8ExtraDataTable* h, IDirect3DDevice8* k,
	struct IDirect3DDevice8ExtraData v)
{
	try
	{
		return h->ht.insert(d3ddevice8pair{ k, v }).second;
	}
	catch (std::exception& e)
	{
		Fatal(1, "%s: fatal: unexpected exception (%s).", __FUNCTION__, e.what());
	}
	catch (...)
	{
		Fatal(1, "%s: fatal: unexpected exception (unknown).", __FUNCTION__);
	}
}

extern "C" void IDirect3DDevice8ExtraDataTableErase(struct IDirect3DDevice8ExtraDataTable* h, IDirect3DDevice8* k)
{
	try
	{
		h->ht.erase(k);
	}
	catch (std::exception& e)
	{
		Fatal(1, "%s: fatal: unexpected exception (%s).", __FUNCTION__, e.what());
	}
	catch (...)
	{
		Fatal(1, "%s: fatal: unexpected exception (unknown).", __FUNCTION__);
	}
}

extern "C" void IDirect3DDevice8ExtraDataTableShrinkToFit(struct IDirect3DDevice8ExtraDataTable* h)
{
	try
	{
		/* https://ja.wikibooks.org/wiki/More_C%2B%2B_Idioms/%E7%B8%AE%E3%82%81%E3%81%A6%E5%90%88%E3%82%8F%E3%81%9B%E3%82%8B(Shrink-to-fit) */
		std::unordered_map<IDirect3DDevice8*, struct IDirect3DDevice8ExtraData>(h->ht).swap(h->ht);
	}
	catch (std::exception& e)
	{
		Fatal(1, "%s: fatal: unexpected exception (%s).", __FUNCTION__, e.what());
	}
	catch (...)
	{
		Fatal(1, "%s: fatal: unexpected exception (unknown).", __FUNCTION__);
	}
}

extern "C" struct IDirect3DDevice8ExtraData* IDirect3DDevice8ExtraDataTableGet(struct IDirect3DDevice8ExtraDataTable* h,
	IDirect3DDevice8* k)
{
	try
	{
		try
		{
			return &h->ht.at(k);
		}
		catch (std::out_of_range&)
		{
			return NULL;
		}
	}
	catch (std::exception& e)
	{
		Fatal(1, "%s: fatal: unexpected exception (%s).", __FUNCTION__, e.what());
	}
	catch (...)
	{
		Fatal(1, "%s: fatal: unexpected exception (unknown).", __FUNCTION__);
	}
}