-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Element.cpp
289 lines (234 loc) · 8.11 KB
/
Element.cpp
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
/*
* Element.cpp, 9/14/2020 7:13 PM
*/
struct Vec2 {
float x, y;
};
struct Rect {
int x, y, w, h;
};
float g_scale_x = 1.0f, g_scale_y = 1.0f;
std::map<string, int> element_offsets {
{"self", 0x28},
{"childs", 0x68},
{"root", 0xd8},
{"parent", 0xe0},
{"position", 0xe8},
{"scale", 0x100},
{"is_visible", 0x161},
{"is_enabled", 0x165},
{"size", 0x180},
{"highlighted", 0x1b8},
{"text", 0x2e8},
{"item", 0x438},
{"item_pos", 0x440},
};
class Element : public PoEObject {
private:
bool __has_child() {
return child_count();
}
AhkObjRef* __get_child(int* path) {
std::vector<int> indices;
for (int* p = path; *p >= 0; ++p)
indices.push_back(*p);
shared_ptr<Element> e = get_child(indices);
return e ? (AhkObjRef*)*e : nullptr;
}
AhkObjRef* __get_child_by_name(const char* name) {
if (offsets->find(name) != offsets->end())
return (AhkObjRef*)*get_child((*offsets)[name]);
return nullptr;
}
AhkObjRef* __get_childs() {
AhkObj temp_childs;
for (auto& i : get_childs())
temp_childs.__set(L"", (i ? (AhkObjRef*)*i : nullptr), AhkObject, nullptr);
__set(L"childs", (AhkObjRef*)temp_childs, AhkObject, nullptr);
return temp_childs;
}
AhkObjRef* __find_child(const wchar_t* text) {
Element* e = find_child(wstring(text));
return e ? (AhkObjRef*)*e : nullptr;
}
int __get_index(const char* name) {
if (offsets->find(name) != offsets->end())
return (*offsets)[name] + 1;
return 0;
}
AhkObjRef* __get_parent() {
if (get_parent())
return *parent;
return nullptr;
}
AhkObjRef* __get_rect() {
AhkTempObj rect(L"Rect");
Rect r = get_rect();
rect.__set(L"l", r.x, AhkInt,
L"t", r.y, AhkInt,
L"r", r.x + r.w, AhkInt,
L"b", r.y + r.h, AhkInt,
L"w", r.w, AhkInt,
L"h", r.h, AhkInt,
nullptr);
return rect;
}
public:
shared_ptr<Element> parent;
std::vector<shared_ptr<Element>> childs;
wstring text;
Element(addrtype address, FieldOffsets* offsets = &element_offsets)
: PoEObject(address, offsets)
{
if (offsets != &element_offsets) {
offsets->insert(element_offsets.begin(), element_offsets.end());
}
}
void __init() {
PoEObject::__init();
add_method(L"hasChild", this, (MethodType)&Element::__has_child, AhkBool);
add_method(L"getChild", this, (MethodType)&Element::__get_child, AhkObject, ParamList{AhkPointer});
add_method(L"getChildByName", this, (MethodType)&Element::__get_child_by_name, AhkObject, ParamList{AhkString});
add_method(L"getChilds", this, (MethodType)&Element::__get_childs, AhkObject);
add_method(L"findChild", this, (MethodType)&Element::__find_child, AhkObject, ParamList{AhkWString});
add_method(L"getIndex", this, (MethodType)&Element::__get_index, AhkInt, ParamList{AhkString});
add_method(L"getItem", this, (MethodType)&Element::get_item, AhkPointer);
add_method(L"getParent", this, (MethodType)&Element::__get_parent, AhkObject);
add_method(L"getRect", this, (MethodType)&Element::__get_rect, AhkObject);
add_method(L"getText", this, (MethodType)&Element::get_text, AhkWStringPtr);
add_method(L"isHighlighted", this, (MethodType)&Element::is_highlighted, AhkBool);
add_method(L"isVisible", this, (MethodType)&Element::is_visible, AhkBool);
add_method(L"enabled", this, (MethodType)&Element::is_enabled, AhkBool);
}
bool is_valid() {
return this->address == read<addrtype>("self");
}
wstring& get_text() {
text = read<wstring>("text");
return text;
}
shared_ptr<Element> get_parent() {
addrtype addr = read<addrtype>("parent");
if (addr > (addrtype)0x10000000 && addr < (addrtype)0x7F0000000000)
parent = shared_ptr<Element>(new Element(addr));
return parent;
}
int child_count() {
addrtype addr = address + (*offsets)["childs"];
addrtype child_begin = PoEMemory::read<addrtype>(addr);
addrtype child_end = PoEMemory::read<addrtype>(addr + 0x8);
return (child_end - child_begin) / 8;
}
shared_ptr<Element> get_child(int index) {
int n = child_count();
if (index >= n)
return nullptr;
if (childs.size() < n) {
childs.clear();
for (int i = 0; i < n; ++i)
childs.push_back(shared_ptr<Element>());
}
addrtype addr = read<addrtype>("childs", index * 8);
if (!childs[index] || childs[index]->address != addr)
childs[index] = shared_ptr<Element>(new Element(addr));
return childs[index];
}
shared_ptr<Element> get_child(std::vector<int> indices) {
if (indices.size() == 0)
return nullptr;
shared_ptr<Element> child = get_child(indices[0]);
for (int i = 1; i < indices.size(); ++i) {
if (!child)
break;
child = child->get_child(indices[i]);
}
return child;
}
shared_ptr<Element> get_child(const string name) {
if (offsets->find(name) != offsets->end())
return get_child((*offsets)[name]);
return nullptr;
}
std::vector<shared_ptr<Element>>& get_childs() {
std::vector<addrtype> vec = read_array<addrtype>("childs", 0x0, 0x8);
if (childs.size() == vec.size()) {
for (int i = 0; i < childs.size(); ++i) {
if (!vec[i])
childs[i].reset();
else if (!childs[i] || vec[i] != childs[i]->address)
childs[i] = shared_ptr<Element>(new Element(vec[i]));
}
} else {
childs.clear();
for (auto addr : vec) {
if (addr)
childs.push_back(shared_ptr<Element>(new Element(addr)));
else
childs.push_back(shared_ptr<Element>());
}
}
return childs;
}
shared_ptr<Element> operator[](int index) {
return get_child(index);
}
Element* find_child(const wstring& text) {
if (text == get_text())
return this;
for (auto& i : get_childs()) {
Element* e = i->find_child(text);
if (e != nullptr)
return e;
}
return nullptr;
}
bool is_enabled() {
if (!get_parent() || parent->is_visible())
return read<byte>("is_enabled");
return false;
}
bool is_visible() {
if (!get_parent() || parent->is_visible())
return read<byte>("is_visible") & 0x8;
return false;
}
bool is_highlighted() {
return read<byte>("highlighted");
}
float scale() {
return read<float>("scale");
}
Vec2 position() {
Vec2 pos = read<Vec2>("position");
Vec2 parentPos = {.0, .0};
if (get_parent())
parentPos = parent->position();
pos.x += parentPos.x;
pos.y += parentPos.y;
return pos;
}
Vec2 size() {
return read<Vec2>("size");
}
addrtype get_item() {
return read<addrtype>("item");
}
Rect get_rect() {
Vec2 pos = position();
Vec2 size = read<Vec2>("size");
float local_scale = scale();
int x = pos.x * local_scale * g_scale_x;
int y = pos.y * local_scale * g_scale_y;
int w = size.x * local_scale * g_scale_x;
int h = size.y * local_scale * g_scale_y;
return {x, y, w, h};
}
Point get_pos() {
Vec2 pos = position();
Vec2 size = read<Vec2>("size");
float local_scale = scale();
int x = (pos.x + size.x / 2) * local_scale * g_scale_x;
int y = (pos.y + size.y / 2) * local_scale * g_scale_y;
return {x, y};
}
};