forked from floh22/native-farsight-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
271 lines (206 loc) · 7.04 KB
/
index.js
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
const addon = require('bindings')('farsight');
var offsetsSet = false;
var championsSet = false;
var connected = false;
var autoImportChampions = true;
var autoImportOffsets = true;
var gameVersion = [0,0];
var offsetRepository = "https://api.github.com/repos/floh22/native-farsight-module/contents/offsets?ref=main";
async function importChampionsFromCDragon() {
if(championsSet) {
console.error("Champions already set! Cannot import from community dragon.");
return;
}
console.log("Getting champions from community dragon...");
var champions = [];
var response = await fetch('https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/champion-summary.json');
var data = await response.json();
for (var i = 0; i < data.length; i++) {
champions.push(data[i].alias.toLowerCase());
}
console.log("Champions received. Importing...");
var added = setChampions(champions);
if(!added) {
console.error("Champions could not be imported into native module! Farsight may not work properly.");
}
}
async function getLatestPatchFileUrl() {
var response = await fetch(offsetRepository);
if(!response.ok) {
console.error("Could not reach offset directory from repository. Status: " + response.status);
return null;
}
var data = await response.json();
var latestPatchFile = null;
var latestPatchVersion = -1;
for(var i = 0; i < data.length; i++) {
var file = data[i];
var offsetVersion = file.name.replace(/[^\d.]/g, '');
if(offsetVersion.startsWith(gameVersion[0] + "." + gameVersion[1])) {
var patchVersion = offsetVersion.split(".")[2];
if(latestPatchFile === null || patchVersion > latestPatchVersion) {
latestPatchFile = file;
latestPatchVersion = patchVersion;
}
}
}
if(latestPatchFile === null) {
return null;
}
return latestPatchFile.download_url;
}
async function importOffsets() {
console.log("Getting offsets from repository...");
if(gameVersion[0] === 0 && gameVersion[1] === 0) {
var response = await fetch('https://raw.communitydragon.org/latest/content-metadata.json');
if(!response.ok) {
console.error("Could not fetch game version. Status: " + response.status);
return;
}
var data = await response.json();
gameVersion = data.version.split(".").slice(0, 2);
}
console.log("Game version: " + gameVersion[0] + "." + gameVersion[1]);
var url = await getLatestPatchFileUrl();
if(url === null) {
console.error("Could not find offsets for game version " + gameVersion[0] + "." + gameVersion[1]);
return;
}
var response = await fetch(url);
if(!response.ok) {
console.error("Could not fetch offsets from repository. Status: " + response.status);
return;
}
var data = await response.json();
Object.keys(data).forEach(key => {
data[key] = parseInt(data[key]);
});
console.log("Offsets received. Importing...");
var res = setOffsets(data);
if(!res) {
console.error("Offsets could not be imported into native module! Farsight will not work properly.");
}
}
function convertObjectDisplayNames(snapshot) {
var textDecoder = new TextDecoder();
for (var i = 0; i < snapshot.champions.length; i++) {
var champion = snapshot.champions[i];
champion.name = textDecoder.decode(champion.name);
champion.displayName = textDecoder.decode(champion.displayName);
}
for(var i = 0; i < snapshot.jungle.length; i++) {
var jungle = snapshot.jungle[i];
jungle.name = textDecoder.decode(jungle.name);
jungle.displayName = textDecoder.decode(jungle.displayName);
}
for(var i = 0; i < snapshot.other.length; i++) {
var other = snapshot.other[i];
other.name = textDecoder.decode(other.name);
other.displayName = textDecoder.decode(other.displayName);
}
snapshot.nextDragonType = snapshot.nextDragonType.replace(".troy", "");
}
function makeSnapshot() {
if (!offsetsSet) {
console.error("Offsets not set!. Cannot create snapshot.");
return {
gameTime: 0
}
}
if(!championsSet) {
console.error("Champions not set!. Cannot create snapshot.");
return {
gameTime: 0
}
}
if(!connected) {
console.error("Not connected to league!. Cannot create snapshot.");
return {
gameTime: 0
}
}
var snapshot = addon.makeSnapshot();
if((!snapshot.success && !snapshot.gameTime) || snapshot.gameTime === 0) {
disconnectFromLeague();
return {
gameTime: 0
};
}
convertObjectDisplayNames(snapshot)
return snapshot;
}
async function connectToLeague () {
if(connected) {
console.error("Already connected to league!");
return false;
}
console.log("Connecting to league...");
var res = addon.connectToLeague();
if(res.success) {
connected = true;
console.log("Connected!");
if(autoImportChampions && !championsSet) {
await importChampionsFromCDragon();
}
if(autoImportOffsets && !offsetsSet) {
await importOffsets();
}
return true;
}
console.log("Failed to connect to league...");
console.error(res.error);
return false;
}
function disconnectFromLeague() {
if(!connected) {
console.error("Not connected to league!");
return false;
}
var res = addon.disconnectFromLeague();
if(res.success) {
console.log("Disconnected from league.");
connected = false;
return true;
}
console.error(res.error);
return false;
}
function setOffsets(offsetObject) {
if (offsetsSet) {
console.error("Offsets already set!");
}
var res = addon.setOffsets(offsetObject);
if(res.success) {
offsetsSet = true;
return true;
}
console.error("Offsets could not be set! Farsight will not work properly.");
return false;
}
function setChampions(championList) {
if (championsSet) {
console.error("Champions already set!");
}
var res = addon.setChampionNames(championList);
if(res === championList.length) {
championsSet = true;
return true;
}
console.error(res.error);
return false;
}
function isReady() {
return connected && offsetsSet && championsSet;
}
function setVersion(version) {
gameVersion = version.split(".").slice(0, 2);
}
module.exports.connectToLeague = connectToLeague;
module.exports.disconnectFromLeague = disconnectFromLeague;
module.exports.makeSnapshot = makeSnapshot;
module.exports.setOffsets = setOffsets;
module.exports.isReady = isReady;
module.exports.setVersion = setVersion;
module.exports.setChampionNames = addon.setChampionNames;
module.exports.autoImportChampions = autoImportChampions;
module.exports.autoImportOffsets = autoImportOffsets;