forked from Zezombye/overpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalVars.js
454 lines (393 loc) · 11.4 KB
/
globalVars.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* This file is part of OverPy (https://github.com/Zezombye/overpy).
* Copyright (c) 2019 Zezombye.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
var globalVariables;
var playerVariables;
var subroutines;
var currentLanguage;
const ELEMENT_LIMIT = 32768;
//If it is in a browser then it is assumed to be in debug mode.
const IS_IN_BROWSER = (typeof window !== "undefined");
const DEBUG_MODE = IS_IN_BROWSER && window.location.host !== "vscode.dev";
var evalVm = null
var VM = null
if (!IS_IN_BROWSER) {
({VM} = require('vm2'));
}
//Compilation variables - are reset at each compilation.
//The absolute path of the folder containing the main file. Used for relative paths.
var rootPath;
//Global variables used to keep track of the name for the current array element/index.
//Should be null at the beginning and end of each rule; if not, throws an error. (for compilation and decompilation)
var currentArrayElementName;
var currentArrayIndexName;
//Set at each rule, to check whether it is legal to use "eventPlayer" and related.
var currentRuleEvent;
//The encountered labels throughout the rule, to not have duplicate labels. Set at each rule.
var currentRuleLabels;
//The number of times the specified label is referenced. If that number is 0, then the label is considered as not accessed.
var currentRuleLabelAccess;
var currentRuleHasVariableGoto;
//Settings for whether to enable obfuscation techniques.
var obfuscationSettings;
//Optimization settings.
var enableOptimization;
var optimizeForSize;
//Contains all macros.
var macros;
var encounteredWarnings;
var suppressedWarnings;
var globalSuppressedWarnings;
//A list of imported files, to prevent import loops.
var importedFiles;
var disableUnusedVars;
var compiledCustomGameSettings;
//The stack of the files (macros count as "files").
var fileStack;
//An unique number for automatically generated labels.
var uniqueNumber;
//Initialization directives for global and player variables.
var globalInitDirectives = [];
var playerInitDirectives = [];
//Workshop setting names, as each name must be unique even if belonging to different categories.
var workshopSettingNames = [];
//User-declared enums.
var enumMembers = {};
//Replacements for 0, 1, and Team.1. Those are functions that give exactly those values, and are able to be applied to all inputs. As such, they are not function dependent.
var replacementFor0;
var replacementFor1;
var replacementForTeam1;
//The number of elements the gamemode takes.
var nbElements;
//For the weird behavior where element count goes up by 1 for every 2 hero literals in the parameters of an action argument.
var nbHeroesInValue;
//The extensions that are activated in the current gamemode.
var activatedExtensions;
//The amount of available extension points.
var availableExtensionPoints;
//Decompilation variables
//Global variable used for "skip", to keep track of where the skip ends.
//Is reset at each rule.
var decompilerGotos;
//Global variable used for the number of tabs.
//Is reset at each rule.
var nbTabs;
//Global variable used to mark the action number of the last loop in the rule.
//Is reset at each rule.
var decompilationLabelNumber;
//Global variable used to keep track of operator precedence.
//Is reset at each action and rule condition.
var operatorPrecedenceStack;
//If current map is used, add the workaround for the map pasting bug.
var isCurrentMapUsed;
var disableMapDetectionFix;
function resetGlobalVariables(language) {
rootPath = "";
currentArrayElementName = null;
currentArrayIndexName = null;
currentLanguage = language;
currentRuleEvent = "";
obfuscationSettings = {
obfuscateNames: false,
obfuscateComments: false,
obfuscateStrings: false,
obfuscateConstants: false,
obfuscateInspector: false,
ruleFilling: false,
copyProtection: false,
}
macros = [];
fileStack = [];
decompilerGotos = [];
nbTabs = 0;
operatorPrecedenceStack = [];
globalVariables = [];
playerVariables = [];
subroutines = [];
encounteredWarnings = [];
suppressedWarnings = [];
globalSuppressedWarnings = [];
importedFiles = [];
disableUnusedVars = false;
compiledCustomGameSettings = "";
enableOptimization = true;
optimizeForSize = false;
uniqueNumber = 1;
globalInitDirectives = [];
playerInitDirectives = [];
workshopSettingNames = [];
enumMembers = {};
replacementFor0 = null;
replacementFor1 = null;
replacementForTeam1 = null;
nbElements = 0;
activatedExtensions = [];
availableExtensionPoints = 0;
isCurrentMapUsed = false;
disableMapDetectionFix = false;
if (!IS_IN_BROWSER) {
evalVm = new VM({
timeout: 1000,
allowAsync: false,
sandbox: {}
});
}
}
//Other constants
//Operator precedence, from lowest to highest.
const operatorPrecedence = {
"=": 1,
"+=": 1,
"-=": 1,
"*=": 1,
"/=": 1,
"%=": 1,
"**=": 1,
"min=": 1,
"max=": 1,
"if": 2,
"or": 3,
"and": 4,
"not": 5,
"in": 7,
"==": 7,
"!=": 7,
"<=": 7,
">=": 7,
">": 7,
"<": 7,
"+": 8,
"-": 8,
"*": 9,
"/": 9,
"%": 9,
//unary plus/minus: 10,
"**": 11,
};
const astOperatorPrecedence = {
"__ifThenElse__": 2,
"__or__": 3,
"__and__": 4,
"__not__": 5,
"__arrayContains__": 6,
"__equals__": 6,
"__inequals__": 6,
"__lessThanOrEquals__": 6,
"__greaterThanOrEquals__": 6,
"__lessThan__": 6,
"__greaterThan__": 6,
"__add__": 7,
"__subtract__": 7,
"__multiply__": 8,
"__divide__": 8,
"__modulo__": 8,
"__negate__": 9,
"__raiseToPower__": 9,
}
//Text that gets inserted on top of all js scripts.
const builtInJsFunctions = `
function vect(x,y,z) {
return ({
x:x,
y:y,
z:z,
toString: function() {
return "vect("+this.x+","+this.y+","+this.z+")";
}
});
}
var Map = {${Object.keys(mapKw).map(x => camelCaseToUpperCase(x) + ":'Map."+camelCaseToUpperCase(x)+"'").join(",")}}
`;
const builtInJsFunctionsNbLines = builtInJsFunctions.split("\n").length;
const defaultVarNames = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ', 'BA', 'BB', 'BC', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BK', 'BL', 'BM', 'BN', 'BO', 'BP', 'BQ', 'BR', 'BS', 'BT', 'BU', 'BV', 'BW', 'BX', 'BY', 'BZ', 'CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CI', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CQ', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ', 'DA', 'DB', 'DC', 'DD', 'DE', 'DF', 'DG', 'DH', 'DI', 'DJ', 'DK', 'DL', 'DM', 'DN', 'DO', 'DP', 'DQ', 'DR', 'DS', 'DT', 'DU', 'DV', 'DW', 'DX'];
//Sub0 to Sub127
const defaultSubroutineNames = Array(128).fill().map((e,i)=>i).map(x => "Sub"+x);
//Names that cannot be used for global variables or subroutines.
const reservedNames = Object.keys(opyKeywords);
const reservedSubroutineNames = Object.keys(opyKeywords);
for (var func in funcKw) {
if (funcKw[func].args === null) {
reservedNames.push(func);
} else if (funcKw[func].args.length === 0) {
reservedSubroutineNames.push(func);
}
}
//Characters that are visually the same as normal ASCII characters (when uppercased), but make the string appear in "big letters" (the i18n font).
//For now, only greek letters and the "line separator" character.
//Let me know if you find any other such characters.
const bigLettersMappings = {
a: "Α",
A: "Α",
b: "Β",
B: "Β",
e: "Ε",
E: "Ε",
h: "Η",
H: "Η",
i: "Ι",
I: "Ι",
k: "Κ",
K: "Κ",
m: "Μ",
M: "Μ",
n: "Ν",
N: "Ν",
o: "Ο",
O: "Ο",
p: "Ρ",
P: "Ρ",
t: "Τ",
T: "Τ",
x: "Χ",
X: "Χ",
y: "Υ",
Y: "Υ",
z: "Ζ",
Z: "Ζ",
" ": "\u2028", //line separator
}
//Fullwidth characters
var fullwidthMappings = {
" ": " ",
"¥": "¥",
"₩": "₩",
"¢": "¢",
"£": "£",
"¯": " ̄",
"¬": "¬",
"¦": "¦",
}
for (var char of '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~') {
fullwidthMappings[char] = String.fromCodePoint(char.charCodeAt(0)+0xFEE0);
}
//Combinations of 0x01 through 0x1F (excluding 0x09, 0x0A and 0x0D). Used for workshop settings to prevent duplicates.
//These characters render as zero-width spaces in Overwatch.
//For some reason, 0x0B and 0x0C aren't sorted according to their ascii value.
var workshopSettingWhitespaceChars = [0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,/*0x0b,0x0c,*/0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d]
var workshopSettingWhitespace = []
for (var chr of workshopSettingWhitespaceChars) {
workshopSettingWhitespace.push(String.fromCodePoint(chr));
workshopSettingWhitespace.push(String.fromCodePoint(0x1e, chr));
workshopSettingWhitespace.push(String.fromCodePoint(0x1f, chr));
}
workshopSettingWhitespace.sort();
const typeTree = [
{"Object": [
"Player",
{"float": [
{"FloatLiteral": [
{"IntLiteral": [
"UnsignedIntLiteral",
"SignedIntLiteral",
]},
{"UnsignedFloatLiteral": [
"UnsignedIntLiteral",
]},
{"SignedFloatLiteral": [
"SignedIntLiteral",
]},
]},
{"unsigned float": [
"unsigned int",
]},
{"signed float": [
"signed int",
]},
{"int": [
{"IntLiteral": [
"UnsignedIntLiteral",
"SignedIntLiteral",
]},
"unsigned int",
"signed int",
]}
]},
{"bool": ["BoolLiteral"]},
"DamageModificationId",
"HealingModificationId",
"DotId",
"HotId",
"EntityId",
"TextId",
"HealthPoolId",
"AssistId",
"String",
{"Direction": ["Vector"]},
{"Position": ["Vector"]},
{"Velocity": ["Vector"]},
"Hero",
"Map",
"Team",
"Gamemode",
"Button",
"Color",
]},
"Array",
"void",
"Type",
"Lambda",
"Label",
"DictElem",
"Raycast",
"Subroutine",
"GlobalVariable",
"PlayerVariable",
"HeroLiteral",
"MapLiteral",
"GamemodeLiteral",
"TeamLiteral",
"ButtonLiteral",
"ColorLiteral",
{"StringLiteral": [
"LocalizedStringLiteral",
{"CustomStringLiteral": [
"FullwidthStringLiteral",
"BigLettersStringLiteral",
"PlaintextStringLiteral",
"CaseSensitiveStringLiteral",
]}
]},
"Value",
"Raycast",
].concat(Object.keys(constantValues));
//Which types are suitable for a given type.
//For example, typeMatrix["float"] = ["float", "int", etc].
const typeMatrix = {};
function fillTypeMatrix(tree) {
if (typeof tree === "string") {
typeMatrix[tree] = [tree];
} else {
var type = Object.keys(tree)[0];
typeMatrix[type] = [type];
for (var child of tree[type]) {
fillTypeMatrix(child);
if (typeof child === "string") {
typeMatrix[type].push(...typeMatrix[child]);
} else {
typeMatrix[type].push(...typeMatrix[Object.keys(child)[0]]);
}
}
}
}
for (var elem of typeTree) {
fillTypeMatrix(elem);
}
typeMatrix["Vector"].push("Direction", "Position", "Velocity");
reservedNames.push(...Object.keys(typeMatrix));
const reservedMemberNames = ["x", "y", "z"];
//An array of functions for ast parsing (to not have a 4k lines file with all the functions and be able to handle each function in a separate file).
var astParsingFunctions = {};