@needle-tools/needle-component-compiler 1.7.0 → 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 CHANGED
@@ -4,6 +4,17 @@ 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.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
13
+ - fix UnityEvent codegen
14
+
15
+ ## [1.7.1] - 2022-07-25
16
+ - fix array type codegen, for example ``scenes: Array<AssetReference> = [];``, see issue https://github.com/needle-tools/needle-tiny-playground/issues/285
17
+
7
18
  ## [1.7.0] - 2022-07-14
8
19
  - change: skip non-serializeable variables (private without ``@serializable``)
9
20
  - improve member type generation
package/PUBLISH.bat ADDED
@@ -0,0 +1,8 @@
1
+ @echo off
2
+ echo Press a key to publish! (close the window if you changed your mind)
3
+ pause
4
+ echo Publishing in 2 sec
5
+ timeout 2
6
+ npm set registry https://registry.npmjs.org && npm publish
7
+ echo Finished...
8
+ timeout 10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/needle-component-compiler",
3
- "version": "1.7.0",
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 type = typeMatch.groups["type"];
159
- type = type.replace(/\(/, "").replace(/\)/, "");
160
- console.log("Found type: ", type);
161
- lastTypeFound = type;
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 type = tryResolveTypeRecursive(param);
205
- if (type === undefined)
206
- type = "object";
207
- paramsStr += type + " @" + param.name.getText();
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
- console.log("EMIT member: " + typeString + " " + varName);
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
- newContext.appendLine("public partial class " + name_2 + " : " + typeName_1);
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 type = _c[_b];
360
+ var type_3 = _c[_b];
343
361
  // const symbol = program.getTypeChecker().getSymbolAtLocation(type.expression);
344
362
  // console.log(symbol);
345
- var text = type.expression.getText();
363
+ var text = type_3.expression.getText();
346
364
  if (text === "Component")
347
365
  return true;
348
366
  if (text === "Behaviour")
@@ -363,28 +381,31 @@ function run(program, outputDir, sourceFile) {
363
381
  case ts.SyntaxKind.FirstLiteralToken:
364
382
  return node.getText();
365
383
  case ts.SyntaxKind.NewExpression:
366
- var type = undefined;
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];
370
- // console.log("child", ts.SyntaxKind[ch.kind]);
388
+ var text = ch.getText();
389
+ // console.log("child", ts.SyntaxKind[ch.kind], text);
371
390
  switch (ch.kind) {
391
+ case ts.SyntaxKind.Identifier:
372
392
  case ts.SyntaxKind.PropertyAccessExpression:
373
- type = tryGetTypeFromText(ch.getText());
393
+ type_4 = tryGetTypeFromText(text);
374
394
  break;
375
395
  case ts.SyntaxKind.SyntaxList:
376
- args = ch.getText();
396
+ args = text;
377
397
  break;
378
398
  }
379
399
  }
380
400
  if (!args)
381
401
  args = "";
382
- if (type)
383
- return "new " + type + "(" + args + ")";
402
+ if (type_4)
403
+ return "new " + type_4 + "(" + args + ")";
384
404
  // const expType = node.getChildren().find(c => c.kind === ts.SyntaxKind.Identifier);
385
405
  break;
386
406
  }
387
407
  var str = node.getText();
408
+ console.log("Unknown assignment:", str, ts.SyntaxKind[node.kind]);
388
409
  return str;
389
410
  }
390
411
  function isPublic(node) {
@@ -427,6 +448,14 @@ function run(program, outputDir, sourceFile) {
427
448
  return namespace;
428
449
  }
429
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
+ }
430
459
  var res = dict[typeName];
431
460
  if (res === undefined) {
432
461
  switch (typeName) {
@@ -438,6 +467,7 @@ function run(program, outputDir, sourceFile) {
438
467
  break;
439
468
  }
440
469
  }
470
+ // console.log(typeName, res);
441
471
  return res;
442
472
  }
443
473
  function tryResolveTypeRecursive(node) {
@@ -478,13 +508,12 @@ function run(program, outputDir, sourceFile) {
478
508
  console.log("TypeReference:", typeName_2);
479
509
  switch (typeName_2) {
480
510
  case "Array":
481
- res = "[]";
482
511
  break;
483
512
  default:
484
- res = tryGetTypeFromText(typeName_2);
485
- break;
513
+ return tryGetTypeFromText(typeName_2);
486
514
  }
487
- return res;
515
+ // return res;
516
+ break;
488
517
  case ts.SyntaxKind.BooleanKeyword:
489
518
  case ts.SyntaxKind.NumberKeyword:
490
519
  case ts.SyntaxKind.StringKeyword:
@@ -502,6 +531,7 @@ function run(program, outputDir, sourceFile) {
502
531
  res = "[]";
503
532
  break;
504
533
  default:
534
+ // console.log(id.text);
505
535
  // res = tryGetTypeFromText(id.text);
506
536
  break;
507
537
  }
@@ -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
- console.log("EMIT member: " + typeString + " " + varName)
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
- newContext.appendLine("public partial class " + name + " : " + typeName);
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;
@@ -382,13 +400,15 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
382
400
  let type: string | undefined = undefined;
383
401
  let args: string | undefined = undefined;
384
402
  for (const ch of node.getChildren()) {
385
- // console.log("child", ts.SyntaxKind[ch.kind]);
403
+ const text = ch.getText();
404
+ // console.log("child", ts.SyntaxKind[ch.kind], text);
386
405
  switch (ch.kind) {
406
+ case ts.SyntaxKind.Identifier:
387
407
  case ts.SyntaxKind.PropertyAccessExpression:
388
- type = tryGetTypeFromText(ch.getText());
408
+ type = tryGetTypeFromText(text);
389
409
  break;
390
410
  case ts.SyntaxKind.SyntaxList:
391
- args = ch.getText();
411
+ args = text;
392
412
  break;
393
413
  }
394
414
  }
@@ -399,6 +419,7 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
399
419
  break;
400
420
  }
401
421
  const str = node.getText();
422
+ console.log("Unknown assignment:", str, ts.SyntaxKind[node.kind]);
402
423
  return str;
403
424
  }
404
425
 
@@ -441,6 +462,16 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
441
462
  }
442
463
 
443
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
+
444
475
  let res = dict[typeName];
445
476
  if (res === undefined) {
446
477
  switch (typeName) {
@@ -452,6 +483,7 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
452
483
  break;
453
484
  }
454
485
  }
486
+ // console.log(typeName, res);
455
487
  return res;
456
488
  }
457
489
 
@@ -489,6 +521,7 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
489
521
  if (res !== undefined) return res;
490
522
  }
491
523
  break;
524
+
492
525
  case ts.SyntaxKind.ArrayType:
493
526
  res = "[]";
494
527
  break;
@@ -499,13 +532,12 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
499
532
  console.log("TypeReference:", typeName);
500
533
  switch (typeName) {
501
534
  case "Array":
502
- res = "[]";
503
535
  break;
504
536
  default:
505
- res = tryGetTypeFromText(typeName);
506
- break;
537
+ return tryGetTypeFromText(typeName);
507
538
  }
508
- return res;
539
+ // return res;
540
+ break;
509
541
 
510
542
  case ts.SyntaxKind.BooleanKeyword:
511
543
  case ts.SyntaxKind.NumberKeyword:
@@ -525,6 +557,7 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
525
557
  res = "[]";
526
558
  break;
527
559
  default:
560
+ // console.log(id.text);
528
561
  // res = tryGetTypeFromText(id.text);
529
562
  break;
530
563
  }
package/src/test.ts CHANGED
@@ -1,22 +1,49 @@
1
1
  import { ThisExpression } from "typescript";
2
2
 
3
- class Test123 {}
4
-
5
- export class Bla extends Behaviour
6
- {
7
- myNumber:number = 42;
8
- myBool:boolean = true;
9
- myString:string = "test";
10
- numberArr: number[] = [1,2,3];
11
- myColor : THREE.Color = new THREE.Color(255, 0, 0);
12
- renderers = new Array<Renderer>();
13
- renderers2 : Renderer[] = [];
14
- objArr : object[];
15
- colArr: THREE.Color[] = [new THREE.Color(1,2,3)];
16
- map : Map<string> = new Map<string>();
17
- map2 : Map<object> = new Map<object>();
18
- private myThing : Test123;
19
- }
3
+
4
+ // export class ComponentWithAnimationClip extends Behaviour implements IPointerClickHandler {
5
+
6
+ // @serializeable(AnimationClip)
7
+ // animation?: THREE.AnimationClip;
8
+ // }
9
+
10
+ export abstract class MyAbstractComponent extends Behaviour {
11
+ abstract myMethod();
12
+ }
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
+ // }
20
47
 
21
48
  // //@type UnityEngine.MonoBehaviour
22
49
  // export class ButtonObject extends Interactable implements IPointerClickHandler, ISerializable {