-
Notifications
You must be signed in to change notification settings - Fork 14
/
Farsight.cc
294 lines (238 loc) · 8.45 KB
/
Farsight.cc
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
#include <set>
#include <queue>
#include <limits>
#include <stdexcept>
#include <chrono>
#include <napi.h>
#include "Offsets.h"
#include "unit/GameObject.h"
#include "Farsight.h"
#include "windows.h"
#include "psapi.h"
#include "Utils.h"
std::set<std::string> Farsight::championNames{};
Farsight::Farsight()
{
// Some trash object not worth reading, most likely outdated
blacklistedObjectNames.insert("testcube");
blacklistedObjectNames.insert("testcuberender");
blacklistedObjectNames.insert("testcuberender10vision");
blacklistedObjectNames.insert("s5test_wardcorpse");
blacklistedObjectNames.insert("sru_camprespawnmarker");
blacklistedObjectNames.insert("sru_plantrespawnmarker");
blacklistedObjectNames.insert("preseason_turret_shield");
}
bool Farsight::IsLeagueRunning()
{
// Check if the process is running
hWindow = FindWindowA(NULL, "League of Legends (TM) Client");
DWORD h;
GetWindowThreadProcessId(hWindow, &h);
return pid == h;
}
bool Farsight::IsHooked()
{
return Process::IsProcessRunning(pid);
}
void Farsight::UnhookFromProcess()
{
CloseHandle(hProcess);
hProcess = NULL;
pid = 0;
hWindow = NULL;
baseAddress = 0;
size = 0;
isSixtyFourBit = FALSE;
}
void Farsight::HookToProcess()
{
// Get the process id
hWindow = FindWindowA("RiotWindowClass", NULL);
if (hWindow == NULL)
throw WinApiException("Could not find League of Legends window");
GetWindowThreadProcessId(hWindow, &pid);
if (pid == NULL || !Process::IsProcessRunning(pid))
throw WinApiException("Could not find League of Legends process");
// Open the process
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (hProcess == NULL)
throw WinApiException("Could not open League of Legends process");
if (0 == IsWow64Process(hProcess, &isSixtyFourBit))
throw WinApiException("Could not determine bitness of League of Legends process");
HMODULE hMods[1024];
DWORD cbNeeded;
if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
{
baseAddress = (DWORD_PTR)hMods[0];
}
else
{
throw WinApiException("Couldn't retrieve league base address");
}
blacklistedObjects.clear();
}
void Farsight::ClearMissingObjects(Snapshot &snapshot)
{
auto current = snapshot.objectMap.begin();
while (current != snapshot.objectMap.end())
{
if (snapshot.updatedObjects.find(current->first) == snapshot.updatedObjects.end())
{
current = snapshot.objectMap.erase(current);
}
else
++current;
}
}
void Farsight::ReadObjects(Snapshot &snapshot, Napi::Env env)
{
static const int maxObjects = 1024;
static DWORD64 pointerArray[maxObjects];
std::chrono::high_resolution_clock::time_point readTimeBegin;
std::chrono::duration<float, std::milli> readDuration;
readTimeBegin = std::chrono::high_resolution_clock::now();
snapshot.champions.clear();
snapshot.jungle.clear();
snapshot.inhibitors.clear();
snapshot.turrets.clear();
snapshot.other.clear();
snapshot.nextDragonType.clear();
DWORD64 objectManager = Memory::ReadDWORD64(hProcess, baseAddress + Offsets::ObjectManager);
if(objectManager == 0)
return;
static char buff[0x500];
Memory::Read(hProcess, objectManager, buff, 0x100);
DWORD64 rootNode;
memcpy(&rootNode, buff + Offsets::ObjectMapRoot, sizeof(DWORD64));
std::queue<DWORD64> nodesToVisit;
std::set<DWORD64> visitedNodes;
nodesToVisit.push(rootNode);
// Read object pointers from tree
int nrObj = 0;
int reads = 0;
DWORD64 childNode1, childNode2, childNode3, node;
while (reads < maxObjects && nodesToVisit.size() > 0)
{
node = nodesToVisit.front();
nodesToVisit.pop();
if (visitedNodes.find(node) != visitedNodes.end())
continue;
reads++;
visitedNodes.insert(node);
Memory::Read(hProcess, node, buff, 0x50);
memcpy(&childNode1, buff, sizeof(DWORD64));
memcpy(&childNode2, buff + 8, sizeof(DWORD64));
memcpy(&childNode3, buff + 16, sizeof(DWORD64));
nodesToVisit.push(childNode1);
nodesToVisit.push(childNode2);
nodesToVisit.push(childNode3);
unsigned int netId = 0;
memcpy(&netId, buff + Offsets::ObjectMapNodeNetId, sizeof(int));
DWORD64 addr;
memcpy(&addr, buff + Offsets::ObjectMapNodeObject, sizeof(DWORD64));
//Im pretty sure netids did not increase to 64bit integers, so anything above 2^31 is invalid
if (addr == 0 || netId > MAX_UINT || netId == 0)
continue;
pointerArray[nrObj] = addr;
nrObj++;
}
// Read objects from the pointers we just read
for (int i = 0; i < nrObj; ++i)
{
int netId;
Memory::Read(hProcess, pointerArray[i] + Offsets::ObjNetworkID, &netId, sizeof(int));
if (blacklistedObjects.find(netId) != blacklistedObjects.end())
continue;
std::shared_ptr<GameObject> obj;
auto it = snapshot.objectMap.find(netId);
if (it == snapshot.objectMap.end())
{
obj = std::shared_ptr<GameObject>(new GameObject(env));
obj->LoadFromMemory(pointerArray[i], hProcess, true);
snapshot.objectMap[obj->networkId] = obj;
}
else
{
obj = it->second;
obj->LoadFromMemory(pointerArray[i], hProcess, false);
if (netId != obj->networkId)
{
snapshot.objectMap[obj->networkId] = obj;
}
}
if (obj->networkId == 0)
{
continue;
}
snapshot.indexToNetId[obj->objectIndex] = obj->networkId;
snapshot.updatedObjects.insert(obj->networkId);
/* Do not filter out names to include troy objects
if(obj->name.size() < 2 || blacklistedObjectNames.find(obj->name) != blacklistedObjectNames.end()) {
blacklistedObjects.insert(obj->networkId);
continue;
}
*/
std::string name = std::string(obj->name.begin(), obj->name.end());
std::transform(name.begin(), name.end(), name.begin(), [](unsigned char c){ return std::tolower(c); });
if (championNames.find(name) != championNames.end())
{
obj->LoadChampionData();
snapshot.champions.push_back(obj);
continue;
}
const char *inhibitorText = "Barracks_T";
const char *turretText = "Turret_T";
if (Character::VectorStartsWith(obj->displayName, inhibitorText))
{
snapshot.inhibitors.push_back(obj);
continue;
}
if (Character::VectorStartsWith(obj->displayName, turretText))
{
snapshot.turrets.push_back(obj);
continue;
}
if(Character::VectorStartsWith(obj->displayName, "Dragon_Indicator_"))
{
std::vector<char> dragonType(obj->displayName);
// dragonType.erase(dragonType.end() - 5, dragonType.end());
dragonType.erase(dragonType.begin(), dragonType.begin() + 17);
snapshot.nextDragonType = std::string(dragonType.begin(), dragonType.end());
snapshot.jungle.push_back(obj);
continue;
}
if (Character::VectorStartsWith(obj->name, "SRU_Dragon"))
{
snapshot.jungle.push_back(obj);
continue;
}
if (Character::VectorStartsWith(obj->name, "SRU_Baron"))
{
snapshot.jungle.push_back(obj);
continue;
}
if (Character::VectorStartsWith(obj->name, "SRU_RiftHerald"))
{
snapshot.jungle.push_back(obj);
continue;
}
snapshot.other.push_back(obj);
}
readDuration = std::chrono::high_resolution_clock::now() - readTimeBegin;
snapshot.benchmark->readObjectsMs = readDuration.count();
}
void Farsight::ReadChampions(ChampionSnapshot &snapshot, Napi::Env env)
{
}
void Farsight::CreateSnapshot(Snapshot &snapshot, Napi::Env env)
{
Memory::Read(hProcess, baseAddress + Offsets::GameTime, &snapshot.gameTime, sizeof(float));
if (snapshot.gameTime <= 5.0f)
return;
ReadObjects(snapshot, env);
ClearMissingObjects(snapshot);
};
void Farsight::CreateChampionSnapshot(ChampionSnapshot &championSnapshot, Napi::Env env)
{
Memory::Read(hProcess, baseAddress + Offsets::GameTime, &championSnapshot.gameTime, sizeof(float));
}