-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtscriptlib.js
242 lines (225 loc) · 5.77 KB
/
tscriptlib.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
class TorqueVariable {
constructor() {
this.intValue = 0;
this.floatValue = 0;
this.stringValue = "";
this.internalType = -1;
}
getIntValue() {
if (this.internalType < -1) {
return this.intValue;
} else {
if (__simObjects.has(this.stringValue.toLowerCase()))
return __simObjects.get(this.stringValue.toLowerCase()).id;
var intParse = parseInt(this.stringValue);
if (intParse == null)
return 0;
else
return intParse;
}
}
getFloatValue() {
if (this.internalType < -1) {
return this.floatValue;
} else {
if (__simObjects.has(this.stringValue.toLowerCase()))
return __simObjects.get(this.stringValue.toLowerCase()).id;
var floatParse = parseFloat(this.stringValue);
if (isNaN(floatParse))
return 0;
else
return floatParse;
}
}
getStringValue() {
if (this.internalType == -1)
return this.stringValue;
if (this.internalType == -2)
return String(this.floatValue);
if (this.internalType == -3)
return String(this.intValue);
else
return this.stringValue;
}
setIntValue(val) {
if (this.internalType < -1) {
this.intValue = val;
this.floatValue = val;
this.stringValue = null;
this.internalType = -3;
} else {
this.intValue = val;
this.floatValue = val
this.stringValue = String(val);
}
}
setFloatValue(val) {
if (this.internalType < -1) {
this.floatValue = val;
this.intValue = Math.round(val);
this.stringValue = null;
this.internalType = -2;
} else {
this.floatValue = val;
this.intValue = Math.round(this.floatValue);
this.stringValue = String(val);
}
}
setStringValue(val) {
if (this.internalType < -1) {
this.floatValue = parseFloat(val);
this.intValue = Math.round(this.floatValue);
this.internalType = -1;
this.stringValue = val;
} else {
this.floatValue = parseFloat(val);
this.intValue = Math.round(this.floatValue);
this.stringValue = val;
}
}
resolveArray(arrayIndex) {
if (arrayIndex in this) {
return this[arrayIndex];
} else {
let arrayObj = new TorqueVariable();
this[arrayIndex] = arrayObj;
return arrayObj;
}
}
}
const __namespaces = [
{
name: null,
pkg: null,
entries: [],
parent: null
}
];
const find_ns = (nsName) => {
let nsList = __namespaces.filter(x => (x.name != null && nsName != null) ? (x.name.toLowerCase() == nsName.toLowerCase()) : x.name == nsName);
if (nsList.length == 0)
return null;
return nsList[0];
}
const addConsoleFunction = (func, funcName, namespace, pkg) => {
if (namespace == "")
namespace = null;
if (pkg == "")
pkg = null;
let nsList = __namespaces.filter(x => x.name != null ? (x.name.toLowerCase() == namespace.toLowerCase()) : x.name == namespace);
let ns = null;
if (nsList.length == 0) {
ns = {
name: namespace,
pkg: pkg,
entries: [],
parent: null
};
__namespaces.push(ns);
} else {
ns = nsList[0];
}
ns.entries.push({
name: funcName,
namespace: ns,
pkg: pkg,
func: func
});
}
const ns_findfunc = (ns, funcName) => {
for (let entry of ns.entries) {
if (entry.name.toLowerCase() == funcName.toLowerCase()) {
return entry;
}
}
if (ns.parent != null)
return ns_findfunc(ns.parent, funcName);
return null;
}
const __idMap = new Map();
const __simObjects = new Map();
const __dataBlocks = new Map();
let nextObjectId = 2000;
let nextDataBlockId = 1;
class TorqueObject {
constructor(className, name, isDatablock, parentName, props, children) {
if (parentName != null) {
let parentObj = __simObjects.get(parentName);
if (parentObj != null) {
Object.assign(this, parentObj);
}
}
this.className = className;
this.name = name;
this.isDatablock = isDatablock;
this.parentName = parentName;
Object.assign(this, props);
this.children = children;
this.id = isDatablock ? nextDataBlockId : nextObjectId++;
__idMap.set(this.id, this);
if (!isDatablock)
if (!__simObjects.has(this.name.toLowerCase()))
__simObjects.set(this.name.toLowerCase(), this);
if (isDatablock)
__dataBlocks.set(this.id, this);
}
}
const resolveIdent = (str) => {
if (__simObjects.has(str.toLowerCase())) return __simObjects.get(str.toLowerCase());
if (__dataBlocks.has(str.toLowerCase())) return __dataBlocks.get(str.toLowerCase());
}
let __currentNamespace = null;
const callFunc = (namespaceName, funcName, funcArgs, callType) => {
let nsEntry = null;
if (callType == "FunctionCall") {
if (namespaceName == "")
namespaceName = null;
let ns = find_ns(namespaceName);
if (ns != null) {
nsEntry = ns_findfunc(ns, funcName);
} else {
console.warn("Cannot find namespace by name " + findObj.className);
return;
}
} else if (callType == "MethodCall") {
let objName = funcArgs[0];
let findObj = __simObjects.get(objName.toLowerCase());
if (findObj == null)
findObj = __idMap.get(parseInt(objName));
if (findObj == null) {
console.error("Cannot find object or datablock by name '" + objName + "'");
return;
}
let ns = find_ns(findObj.className);
if (ns != null) {
nsEntry = ns_findfunc(ns, funcName);
} else {
console.warn("Cannot find namespace by name " + findObj.className);
return;
}
} else if (callType == "ParentCall") {
if (__currentNamespace != null) {
if (__currentNamespace.parent != null) {
let ns = __currentNamespace.parent;
nsEntry = ns_findfunc(ns, funcName);
}
}
}
if (nsEntry != null) {
let fArgs = [];
for (let val of funcArgs) {
let tvar = new TorqueVariable();
tvar.setStringValue(val);
fArgs.push(tvar);
}
let saveNamespace = __currentNamespace;
__currentNamespace = nsEntry.namespace;
let res = nsEntry.func(fArgs);
__currentNamespace = saveNamespace;
return res;
}
}
const __echo__ = (args) => {
console.log(args[0].getStringValue());
}
addConsoleFunction(__echo__, "echo", "", "");