-
Notifications
You must be signed in to change notification settings - Fork 1
/
decompressor.js
192 lines (144 loc) · 3.76 KB
/
decompressor.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
function _encodeIntegerAlphabetically(current) {
let newKey = current + 1;
if (newKey === 0) {
return "a";
}
const characters = [];
while (newKey > 0) {
const remainder = newKey % 26;
characters.push(String.fromCharCode(remainder + 97));
newKey = Math.floor(newKey / 26);
}
return characters.reverse().join("");
}
function _reconstructValue(value) {
const valueType = typeof value;
if (valueType === "string" && value.match(/^-?\d+(\.\d+)?(:-?\d+(\.\d+)?)+$/m)) {
const values = value.split(":").map(num => parseFloat(num));
const result = {
x: values[0],
y: values[1],
};
if (values.length > 2) {
result.z = values[2];
if (values.length > 3) {
result.w = values[3];
}
}
return result;
}
return value;
}
function _resolveDuplicates(rawDuplicateMap, data) {
const duplicateMap = {};
let current = -1;
const duplicatePairs = rawDuplicateMap.split(";");
for (const duplicate of duplicatePairs) {
const replacement = _encodeIntegerAlphabetically(current);
duplicateMap[replacement] = duplicate;
current++;
}
const resolve = value => {
const valueType = typeof value;
if (valueType === "object") {
if (Array.isArray(value)) {
return value.map(value => resolve(value));
} else {
for (const key in value) {
value[key] = resolve(value[key]);
}
}
} else if (valueType === "string" && value.startsWith("_")) {
const key = value.substring(1),
original = duplicateMap[key];
if (original) {
return original;
}
}
return value;
};
return resolve(data);
}
function _resolveKeys(pKeyMap, data) {
const keyMap = {};
let current = -1;
const keyPairs = pKeyMap.split(";");
for (const key of keyPairs) {
const replacement = _encodeIntegerAlphabetically(current);
keyMap[replacement] = key;
current++;
}
const resolve = value => {
const valueType = typeof value;
if (valueType === "object") {
if (Array.isArray(value)) {
return value.map(value => resolve(value));
}
const decompressed = {};
for (const key in value) {
const newKey = keyMap[key] || key;
decompressed[newKey] = resolve(value[key]);
}
return decompressed;
}
return _reconstructValue(value);
};
return resolve(data);
}
function _decompressData(data) {
if (!data || !data.d) {
return data;
}
const duplicateMap = data.m;
let rawData = data.d;
if (duplicateMap) {
rawData = _resolveDuplicates(duplicateMap, rawData);
}
const keyMap = data.k || "";
rawData = _resolveKeys(keyMap, rawData);
return rawData;
}
export function decompressPlayers(data) {
const decompressed = _decompressData(data);
decompressed.world.baseTime = decompressed.world.baseTime || 0;
decompressed.world.weather = decompressed.world.weather || "";
decompressed.world.instance = decompressed.world.instance || 0;
decompressed.players = decompressed.players
? decompressed.players.map(pPlayer => {
const character = pPlayer.character,
vehicle = pPlayer.vehicle;
return {
source: pPlayer.source || 0,
licenseIdentifier: pPlayer.licenseIdentifier || "",
name: pPlayer.name || "",
countryName: pPlayer.countryName || "",
character: character
? {
id: character.id || 0,
fullName: character.fullName || "",
flags: character.flags || 0,
}
: false,
coords: pPlayer.coords || {
x: 0,
y: 0,
z: 0,
w: 0,
},
vehicle: vehicle
? {
id: vehicle.id || 0,
model: vehicle.model || "",
driving: vehicle.driving || false,
plate: vehicle.plate || "",
}
: false,
speed: pPlayer.speed || 0,
flags: pPlayer.flags || 0,
instanceId: pPlayer.instanceId || 0,
afkSince: pPlayer.afkSince || false,
};
})
: [];
return decompressed;
}