@needle-tools/needle-component-compiler 1.7.2 → 1.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Changelog.md +6 -1
- package/PUBLISH.bat +8 -0
- package/package.json +6 -2
- package/src/component-compiler.js +44 -17
- package/src/component-compiler.ts +32 -3
- package/src/test.ts +40 -25
package/Changelog.md
CHANGED
|
@@ -4,7 +4,12 @@ All notable changes to this package will be documented in this file.
|
|
|
4
4
|
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
|
5
5
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
-
## [1.7.
|
|
7
|
+
## [1.7.3] - 2022-09-02
|
|
8
|
+
- Fix codegen for unknown array types
|
|
9
|
+
- Fix codegen for abstract classes
|
|
10
|
+
- Fix codegen for types imported as e.g. THREE.AnimationClip
|
|
11
|
+
|
|
12
|
+
## [1.7.2] - 2022-07-27
|
|
8
13
|
- fix UnityEvent codegen
|
|
9
14
|
|
|
10
15
|
## [1.7.1] - 2022-07-25
|
package/PUBLISH.bat
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@needle-tools/needle-component-compiler",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.3",
|
|
4
4
|
"description": "Compile mock unity components from typescript",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -16,5 +16,9 @@
|
|
|
16
16
|
"name": "Needle",
|
|
17
17
|
"email": "help@needle.tools",
|
|
18
18
|
"url": "https://needle.tools/"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/needle-tools/needle-tiny-component-compiler.git"
|
|
19
23
|
}
|
|
20
|
-
}
|
|
24
|
+
}
|
|
@@ -155,10 +155,10 @@ function run(program, outputDir, sourceFile) {
|
|
|
155
155
|
if (typeMatch && typeMatch.groups) {
|
|
156
156
|
// for some reason our regex does also match surrounding ( ) even tho: https://regex101.com/r/PoWK6V/1
|
|
157
157
|
// so we remove them
|
|
158
|
-
var
|
|
159
|
-
|
|
160
|
-
console.log("Found type: ",
|
|
161
|
-
lastTypeFound =
|
|
158
|
+
var type_1 = typeMatch.groups["type"];
|
|
159
|
+
type_1 = type_1.replace(/\(/, "").replace(/\)/, "");
|
|
160
|
+
console.log("Found type: ", type_1);
|
|
161
|
+
lastTypeFound = type_1;
|
|
162
162
|
}
|
|
163
163
|
var ifdefMatch = ifdefPattern.exec(comment);
|
|
164
164
|
if (ifdefMatch && ifdefMatch.groups) {
|
|
@@ -201,10 +201,10 @@ function run(program, outputDir, sourceFile) {
|
|
|
201
201
|
continue;
|
|
202
202
|
if (paramsStr.length > 0)
|
|
203
203
|
paramsStr += ", ";
|
|
204
|
-
var
|
|
205
|
-
if (
|
|
206
|
-
|
|
207
|
-
paramsStr +=
|
|
204
|
+
var type_2 = tryResolveTypeRecursive(param);
|
|
205
|
+
if (type_2 === undefined)
|
|
206
|
+
type_2 = "object";
|
|
207
|
+
paramsStr += type_2 + " @" + param.name.getText();
|
|
208
208
|
}
|
|
209
209
|
var methodName = meth.name.getText();
|
|
210
210
|
context.onBeforeMethod(methodName);
|
|
@@ -241,7 +241,6 @@ function run(program, outputDir, sourceFile) {
|
|
|
241
241
|
if (typeString === undefined) {
|
|
242
242
|
postFix = " → Could not resolve C# type";
|
|
243
243
|
}
|
|
244
|
-
var prefix = typeString === undefined ? "// " : "";
|
|
245
244
|
var assignment = "";
|
|
246
245
|
if (typeString !== undefined) {
|
|
247
246
|
for (var _j = 0, _k = node.getChildren(); _j < _k.length; _j++) {
|
|
@@ -284,7 +283,13 @@ function run(program, outputDir, sourceFile) {
|
|
|
284
283
|
}
|
|
285
284
|
if (typeString === undefined)
|
|
286
285
|
typeString = typeName;
|
|
287
|
-
|
|
286
|
+
if (typeString === "[]") {
|
|
287
|
+
console.log("Unknown array type for \"" + varName + " " + typeName + "\" - your type is probably not known (did you just create it this session?) and you might need to regenerate the Typemap in Unity. Go to \"Needle Engine/Internal/Generate Type Map for component compiler");
|
|
288
|
+
typeString = "object[]";
|
|
289
|
+
assignment = " = new object[0]";
|
|
290
|
+
}
|
|
291
|
+
console.log("EMIT member: \"" + typeString + "\" " + varName, assignment, "Last type found:", lastTypeFound);
|
|
292
|
+
var prefix = typeString === undefined ? "// " : "";
|
|
288
293
|
context.append(prefix + visibility + " " + typeString + " " + varName + assignment + ";" + postFix + "\n");
|
|
289
294
|
lastTypeFound = null;
|
|
290
295
|
if (requireEndIf) {
|
|
@@ -317,7 +322,20 @@ function run(program, outputDir, sourceFile) {
|
|
|
317
322
|
if (lastTypeFound)
|
|
318
323
|
typeName_1 = lastTypeFound;
|
|
319
324
|
console.log(name_2 + " inherits " + typeName_1);
|
|
320
|
-
|
|
325
|
+
var modifiers = "";
|
|
326
|
+
if (dec.modifiers) {
|
|
327
|
+
for (var _l = 0, _m = dec.modifiers; _l < _m.length; _l++) {
|
|
328
|
+
var mod = _m[_l];
|
|
329
|
+
switch (mod.getText()) {
|
|
330
|
+
case "abstract":
|
|
331
|
+
modifiers += " abstract";
|
|
332
|
+
console.log(name_2 + " is abstract");
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
modifiers += " partial";
|
|
338
|
+
newContext.appendLine("public " + modifiers.trim() + " class " + name_2 + " : " + typeName_1);
|
|
321
339
|
newContext.appendLine("{");
|
|
322
340
|
newContext.indentLevel += 1;
|
|
323
341
|
newContext.classEnd = dec.end;
|
|
@@ -339,10 +357,10 @@ function run(program, outputDir, sourceFile) {
|
|
|
339
357
|
if (h.types.length <= 0)
|
|
340
358
|
continue;
|
|
341
359
|
for (var _b = 0, _c = h.types; _b < _c.length; _b++) {
|
|
342
|
-
var
|
|
360
|
+
var type_3 = _c[_b];
|
|
343
361
|
// const symbol = program.getTypeChecker().getSymbolAtLocation(type.expression);
|
|
344
362
|
// console.log(symbol);
|
|
345
|
-
var text =
|
|
363
|
+
var text = type_3.expression.getText();
|
|
346
364
|
if (text === "Component")
|
|
347
365
|
return true;
|
|
348
366
|
if (text === "Behaviour")
|
|
@@ -363,7 +381,7 @@ function run(program, outputDir, sourceFile) {
|
|
|
363
381
|
case ts.SyntaxKind.FirstLiteralToken:
|
|
364
382
|
return node.getText();
|
|
365
383
|
case ts.SyntaxKind.NewExpression:
|
|
366
|
-
var
|
|
384
|
+
var type_4 = undefined;
|
|
367
385
|
var args = undefined;
|
|
368
386
|
for (var _i = 0, _a = node.getChildren(); _i < _a.length; _i++) {
|
|
369
387
|
var ch = _a[_i];
|
|
@@ -372,7 +390,7 @@ function run(program, outputDir, sourceFile) {
|
|
|
372
390
|
switch (ch.kind) {
|
|
373
391
|
case ts.SyntaxKind.Identifier:
|
|
374
392
|
case ts.SyntaxKind.PropertyAccessExpression:
|
|
375
|
-
|
|
393
|
+
type_4 = tryGetTypeFromText(text);
|
|
376
394
|
break;
|
|
377
395
|
case ts.SyntaxKind.SyntaxList:
|
|
378
396
|
args = text;
|
|
@@ -381,8 +399,8 @@ function run(program, outputDir, sourceFile) {
|
|
|
381
399
|
}
|
|
382
400
|
if (!args)
|
|
383
401
|
args = "";
|
|
384
|
-
if (
|
|
385
|
-
return "new " +
|
|
402
|
+
if (type_4)
|
|
403
|
+
return "new " + type_4 + "(" + args + ")";
|
|
386
404
|
// const expType = node.getChildren().find(c => c.kind === ts.SyntaxKind.Identifier);
|
|
387
405
|
break;
|
|
388
406
|
}
|
|
@@ -430,6 +448,14 @@ function run(program, outputDir, sourceFile) {
|
|
|
430
448
|
return namespace;
|
|
431
449
|
}
|
|
432
450
|
function tryGetTypeFromText(typeName) {
|
|
451
|
+
// if a type is imported via some namespace e.g. THREE.AnimationClip
|
|
452
|
+
// we want to remove that namespace / import name
|
|
453
|
+
var separatorIndex = typeName.lastIndexOf(".");
|
|
454
|
+
if (separatorIndex > 0) {
|
|
455
|
+
var newName = typeName.substring(separatorIndex + 1);
|
|
456
|
+
console.log("Remove import name from type: \"" + typeName + "\" → \"" + newName + "\"");
|
|
457
|
+
typeName = newName;
|
|
458
|
+
}
|
|
433
459
|
var res = dict[typeName];
|
|
434
460
|
if (res === undefined) {
|
|
435
461
|
switch (typeName) {
|
|
@@ -441,6 +467,7 @@ function run(program, outputDir, sourceFile) {
|
|
|
441
467
|
break;
|
|
442
468
|
}
|
|
443
469
|
}
|
|
470
|
+
// console.log(typeName, res);
|
|
444
471
|
return res;
|
|
445
472
|
}
|
|
446
473
|
function tryResolveTypeRecursive(node) {
|
|
@@ -4,6 +4,7 @@ import * as fs from "fs";
|
|
|
4
4
|
import * as path from 'path';
|
|
5
5
|
|
|
6
6
|
import * as types from "./types";
|
|
7
|
+
import { type } from "os";
|
|
7
8
|
const dict = types.dict;
|
|
8
9
|
|
|
9
10
|
// add either of these two comments above a class to enforce code gen or disable it for the next class
|
|
@@ -264,7 +265,6 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
|
|
|
264
265
|
if (typeString === undefined) {
|
|
265
266
|
postFix = " → Could not resolve C# type";
|
|
266
267
|
}
|
|
267
|
-
const prefix = typeString === undefined ? "// " : "";
|
|
268
268
|
let assignment = "";
|
|
269
269
|
if (typeString !== undefined) {
|
|
270
270
|
for (const ch of node.getChildren()) {
|
|
@@ -305,7 +305,13 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
|
|
|
305
305
|
context.appendLine("#ifdef " + ifdefSections.pop());
|
|
306
306
|
}
|
|
307
307
|
if (typeString === undefined) typeString = typeName;
|
|
308
|
-
|
|
308
|
+
if (typeString === "[]") {
|
|
309
|
+
console.log("Unknown array type for \"" + varName + " " + typeName + "\" - your type is probably not known (did you just create it this session?) and you might need to regenerate the Typemap in Unity. Go to \"Needle Engine/Internal/Generate Type Map for component compiler")
|
|
310
|
+
typeString = "object[]";
|
|
311
|
+
assignment = " = new object[0]";
|
|
312
|
+
}
|
|
313
|
+
console.log("EMIT member: \"" + typeString + "\" " + varName, assignment, "Last type found:", lastTypeFound);
|
|
314
|
+
const prefix = typeString === undefined ? "// " : "";
|
|
309
315
|
context.append(prefix + visibility + " " + typeString + " " + varName + assignment + ";" + postFix + "\n");
|
|
310
316
|
lastTypeFound = null;
|
|
311
317
|
if (requireEndIf) {
|
|
@@ -337,7 +343,19 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
|
|
|
337
343
|
if (typeof inheritsComponent === "string") typeName = inheritsComponent;
|
|
338
344
|
if (lastTypeFound) typeName = lastTypeFound;
|
|
339
345
|
console.log(name + " inherits " + typeName);
|
|
340
|
-
|
|
346
|
+
let modifiers = "";
|
|
347
|
+
if (dec.modifiers) {
|
|
348
|
+
for (const mod of dec.modifiers) {
|
|
349
|
+
switch (mod.getText()) {
|
|
350
|
+
case "abstract":
|
|
351
|
+
modifiers += " abstract";
|
|
352
|
+
console.log(name + " is abstract");
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
modifiers += " partial";
|
|
358
|
+
newContext.appendLine("public " + modifiers.trim() + " class " + name + " : " + typeName);
|
|
341
359
|
newContext.appendLine("{");
|
|
342
360
|
newContext.indentLevel += 1;
|
|
343
361
|
newContext.classEnd = dec.end;
|
|
@@ -444,6 +462,16 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
|
|
|
444
462
|
}
|
|
445
463
|
|
|
446
464
|
function tryGetTypeFromText(typeName: string) {
|
|
465
|
+
|
|
466
|
+
// if a type is imported via some namespace e.g. THREE.AnimationClip
|
|
467
|
+
// we want to remove that namespace / import name
|
|
468
|
+
const separatorIndex = typeName.lastIndexOf(".");
|
|
469
|
+
if (separatorIndex > 0) {
|
|
470
|
+
let newName = typeName.substring(separatorIndex + 1);
|
|
471
|
+
console.log("Remove import name from type: \"" + typeName + "\" → \"" + newName + "\"");
|
|
472
|
+
typeName = newName;
|
|
473
|
+
}
|
|
474
|
+
|
|
447
475
|
let res = dict[typeName];
|
|
448
476
|
if (res === undefined) {
|
|
449
477
|
switch (typeName) {
|
|
@@ -455,6 +483,7 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
|
|
|
455
483
|
break;
|
|
456
484
|
}
|
|
457
485
|
}
|
|
486
|
+
// console.log(typeName, res);
|
|
458
487
|
return res;
|
|
459
488
|
}
|
|
460
489
|
|
package/src/test.ts
CHANGED
|
@@ -1,34 +1,49 @@
|
|
|
1
1
|
import { ThisExpression } from "typescript";
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
export class EventComponent extends Behaviour {
|
|
6
|
-
@serializeable(EventList)
|
|
7
|
-
roomChanged: EventList = new EventList();
|
|
8
|
-
}
|
|
4
|
+
// export class ComponentWithAnimationClip extends Behaviour implements IPointerClickHandler {
|
|
9
5
|
|
|
10
|
-
|
|
6
|
+
// @serializeable(AnimationClip)
|
|
7
|
+
// animation?: THREE.AnimationClip;
|
|
8
|
+
// }
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
export abstract class MyAbstractComponent extends Behaviour {
|
|
11
|
+
abstract myMethod();
|
|
14
12
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
export class
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
// export class CameraView extends Behaviour {
|
|
16
|
+
// static views: CameraView[] = [];
|
|
17
|
+
// }
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
// export class EventComponent extends Behaviour {
|
|
21
|
+
// @serializeable(EventList)
|
|
22
|
+
// roomChanged: EventList = new EventList();
|
|
23
|
+
// }
|
|
24
|
+
|
|
25
|
+
// export class SocLoader extends Behaviour {
|
|
26
|
+
|
|
27
|
+
// @serializeable(AssetReference)
|
|
28
|
+
// scenes: Array<AssetReference> = [];
|
|
29
|
+
// }
|
|
30
|
+
// // class Test123 {}
|
|
31
|
+
|
|
32
|
+
// export class Bla extends Behaviour
|
|
33
|
+
// {
|
|
34
|
+
// myNumber:number = 42;
|
|
35
|
+
// myBool:boolean = true;
|
|
36
|
+
// myString:string = "test";
|
|
37
|
+
// numberArr: number[] = [1,2,3];
|
|
38
|
+
// myColor : THREE.Color = new THREE.Color(255, 0, 0);
|
|
39
|
+
// renderers = new Array<Renderer>();
|
|
40
|
+
// renderers2 : Renderer[] = [];
|
|
41
|
+
// objArr : object[];
|
|
42
|
+
// colArr: THREE.Color[] = [new THREE.Color(1,2,3)];
|
|
43
|
+
// map : Map<string> = new Map<string>();
|
|
44
|
+
// map2 : Map<object> = new Map<object>();
|
|
45
|
+
// private myThing : Test123;
|
|
46
|
+
// }
|
|
32
47
|
|
|
33
48
|
// //@type UnityEngine.MonoBehaviour
|
|
34
49
|
// export class ButtonObject extends Interactable implements IPointerClickHandler, ISerializable {
|