@colyseus/schema 1.0.44 → 1.0.46

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.
Files changed (72) hide show
  1. package/build/cjs/index.js +0 -3
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.mjs +4 -3
  4. package/build/esm/index.mjs.map +1 -1
  5. package/build/umd/index.js +0 -3
  6. package/lib/Reflection.js +77 -109
  7. package/lib/Reflection.js.map +1 -1
  8. package/lib/Schema.js +194 -255
  9. package/lib/Schema.js.map +1 -1
  10. package/lib/annotations.d.ts +6 -6
  11. package/lib/annotations.js +56 -89
  12. package/lib/annotations.js.map +1 -1
  13. package/lib/changes/ChangeTree.d.ts +1 -1
  14. package/lib/changes/ChangeTree.js +86 -96
  15. package/lib/changes/ChangeTree.js.map +1 -1
  16. package/lib/codegen/api.js +9 -9
  17. package/lib/codegen/api.js.map +1 -1
  18. package/lib/codegen/argv.js +11 -11
  19. package/lib/codegen/argv.js.map +1 -1
  20. package/lib/codegen/cli.js +20 -9
  21. package/lib/codegen/cli.js.map +1 -1
  22. package/lib/codegen/languages/cpp.js +126 -77
  23. package/lib/codegen/languages/cpp.js.map +1 -1
  24. package/lib/codegen/languages/csharp.js +87 -57
  25. package/lib/codegen/languages/csharp.js.map +1 -1
  26. package/lib/codegen/languages/haxe.js +34 -26
  27. package/lib/codegen/languages/haxe.js.map +1 -1
  28. package/lib/codegen/languages/java.js +39 -27
  29. package/lib/codegen/languages/java.js.map +1 -1
  30. package/lib/codegen/languages/js.js +48 -32
  31. package/lib/codegen/languages/js.js.map +1 -1
  32. package/lib/codegen/languages/lua.js +35 -24
  33. package/lib/codegen/languages/lua.js.map +1 -1
  34. package/lib/codegen/languages/ts.js +63 -72
  35. package/lib/codegen/languages/ts.js.map +1 -1
  36. package/lib/codegen/parser.d.ts +2 -1
  37. package/lib/codegen/parser.js +62 -47
  38. package/lib/codegen/parser.js.map +1 -1
  39. package/lib/codegen/types.d.ts +8 -0
  40. package/lib/codegen/types.js +64 -54
  41. package/lib/codegen/types.js.map +1 -1
  42. package/lib/encoding/decode.js +15 -15
  43. package/lib/encoding/decode.js.map +1 -1
  44. package/lib/encoding/encode.js +14 -14
  45. package/lib/encoding/encode.js.map +1 -1
  46. package/lib/events/EventEmitter.d.ts +1 -1
  47. package/lib/events/EventEmitter.js +16 -51
  48. package/lib/events/EventEmitter.js.map +1 -1
  49. package/lib/filters/index.js +7 -8
  50. package/lib/filters/index.js.map +1 -1
  51. package/lib/index.js +7 -7
  52. package/lib/index.js.map +1 -1
  53. package/lib/types/ArraySchema.d.ts +1 -1
  54. package/lib/types/ArraySchema.js +157 -219
  55. package/lib/types/ArraySchema.js.map +1 -1
  56. package/lib/types/CollectionSchema.d.ts +1 -1
  57. package/lib/types/CollectionSchema.js +60 -68
  58. package/lib/types/CollectionSchema.js.map +1 -1
  59. package/lib/types/HelperTypes.d.ts +9 -9
  60. package/lib/types/MapSchema.js +63 -74
  61. package/lib/types/MapSchema.js.map +1 -1
  62. package/lib/types/SetSchema.js +59 -68
  63. package/lib/types/SetSchema.js.map +1 -1
  64. package/lib/types/index.js +1 -1
  65. package/lib/types/index.js.map +1 -1
  66. package/lib/utils.js +10 -13
  67. package/lib/utils.js.map +1 -1
  68. package/package.json +1 -1
  69. package/src/codegen/languages/csharp.ts +48 -5
  70. package/src/codegen/parser.ts +24 -1
  71. package/src/codegen/types.ts +14 -1
  72. package/src/types/ArraySchema.ts +4 -3
@@ -1,4 +1,11 @@
1
- import { Class, Property, File, getCommentHeader, Interface } from "../types";
1
+ import {
2
+ Class,
3
+ Property,
4
+ File,
5
+ getCommentHeader,
6
+ Interface,
7
+ Enum,
8
+ } from "../types";
2
9
  import { GenerateOptions } from "../api";
3
10
  import { Context } from "../types";
4
11
 
@@ -26,7 +33,11 @@ const capitalize = (s) => {
26
33
  return s.charAt(0).toUpperCase() + s.slice(1);
27
34
  }
28
35
 
29
- export function generate (context: Context, options: GenerateOptions): File[] {
36
+ export function generate(context: Context, options: GenerateOptions): File[] {
37
+ // enrich typeMaps with enums
38
+ context.enums.forEach((structure) => {
39
+ typeMaps[structure.name] = structure.name;
40
+ });
30
41
  return [
31
42
  ...context.classes.map(structure => ({
32
43
  name: `${structure.name}.cs`,
@@ -34,8 +45,12 @@ export function generate (context: Context, options: GenerateOptions): File[] {
34
45
  })),
35
46
  ...context.interfaces.map(structure => ({
36
47
  name: `${structure.name}.cs`,
37
- content: generateInterface(structure, options.namespace)
38
- }))
48
+ content: generateInterface(structure, options.namespace),
49
+ })),
50
+ ...context.enums.filter(structure => structure.name !== 'OPERATION').map((structure) => ({
51
+ name: `${structure.name}.cs`,
52
+ content: generateEnum(structure, options.namespace),
53
+ })),
39
54
  ];
40
55
  }
41
56
 
@@ -46,12 +61,40 @@ function generateClass(klass: Class, namespace: string) {
46
61
  using Colyseus.Schema;
47
62
  ${namespace ? `\nnamespace ${namespace} {` : ""}
48
63
  ${indent}public partial class ${klass.name} : ${klass.extends} {
49
- ${klass.properties.map(prop => generateProperty(prop, indent)).join("\n\n")}
64
+ ${klass.properties.map((prop) => generateProperty(prop, indent)).join("\n\n")}
50
65
  ${indent}}
51
66
  ${namespace ? "}" : ""}
52
67
  `;
53
68
  }
54
69
 
70
+ function generateEnum(_enum: Enum, namespace: string) {
71
+ const indent = namespace ? "\t" : "";
72
+ return `${getCommentHeader()}
73
+ ${namespace ? `\nnamespace ${namespace} {` : ""}
74
+ ${indent}public struct ${_enum.name} {
75
+
76
+ ${_enum.properties
77
+ .map((prop) => {
78
+ let dataType: string = "int";
79
+ let value: any;
80
+
81
+ if(prop.type) {
82
+ if(isNaN(Number(prop.type))) {
83
+ value = prop.type;
84
+ dataType = "string";
85
+ } else {
86
+ value = Number(prop.type);
87
+ dataType = Number.isInteger(value)? 'int': 'float';
88
+ }
89
+ } else {
90
+ value = _enum.properties.indexOf(prop);
91
+ }
92
+ return `${indent}\tpublic const ${dataType} ${prop.name} = ${value};`;
93
+ })
94
+ .join("\n")}
95
+ ${indent}}`
96
+ }
97
+
55
98
  function generateProperty(prop: Property, indent: string = "") {
56
99
  let typeArgs = `"${prop.type}"`;
57
100
  let property = "public";
@@ -1,7 +1,7 @@
1
1
  import * as ts from "typescript";
2
2
  import * as path from "path";
3
3
  import { readFileSync } from "fs";
4
- import { IStructure, Class, Interface, Property, Context } from "./types";
4
+ import { IStructure, Class, Interface, Property, Context, Enum } from "./types";
5
5
 
6
6
  let currentStructure: IStructure;
7
7
  let currentProperty: Property;
@@ -62,6 +62,15 @@ function inspectNode(node: ts.Node, context: Context, decoratorName: string) {
62
62
  }
63
63
  break;
64
64
 
65
+ case ts.SyntaxKind.EnumDeclaration:
66
+ const enumName = (
67
+ node as ts.EnumDeclaration
68
+ ).name.escapedText.toString();
69
+ currentStructure = new Enum();
70
+ currentStructure.name = enumName;
71
+ context.addStructure(currentStructure);
72
+ break;
73
+
65
74
  case ts.SyntaxKind.ExtendsKeyword:
66
75
  // console.log(node.getText());
67
76
  break;
@@ -182,6 +191,20 @@ function inspectNode(node: ts.Node, context: Context, decoratorName: string) {
182
191
  currentProperty = undefined;
183
192
 
184
193
  break;
194
+
195
+ case ts.SyntaxKind.EnumMember:
196
+ if (currentStructure instanceof Enum) {
197
+ const initializer = (node as any).initializer?.getText();
198
+ const name = node.getFirstToken().getText();
199
+ const property = currentProperty || new Property();
200
+ property.name = name;
201
+ if (initializer !== undefined) {
202
+ property.type = initializer;
203
+ }
204
+ currentStructure.addProperty(property);
205
+ currentProperty = undefined;
206
+ }
207
+ break;
185
208
  }
186
209
 
187
210
  ts.forEachChild(node, (n) => inspectNode(n, context, decoratorName));
@@ -15,6 +15,7 @@ export function getCommentHeader(singleLineComment: string = "//") {
15
15
  export class Context {
16
16
  classes: Class[] = [];
17
17
  interfaces: Interface[] = [];
18
+ enums: Enum[] = [];
18
19
 
19
20
  getStructures() {
20
21
  return {
@@ -33,6 +34,7 @@ export class Context {
33
34
  return false;
34
35
  }),
35
36
  interfaces: this.interfaces,
37
+ enums: this.enums,
36
38
  };
37
39
  }
38
40
 
@@ -41,9 +43,10 @@ export class Context {
41
43
 
42
44
  if (structure instanceof Class) {
43
45
  this.classes.push(structure);
44
-
45
46
  } else if (structure instanceof Interface) {
46
47
  this.interfaces.push(structure);
48
+ } else if (structure instanceof Enum) {
49
+ this.enums.push(structure);
47
50
  }
48
51
  }
49
52
 
@@ -134,6 +137,16 @@ export class Class implements IStructure {
134
137
  }
135
138
  }
136
139
 
140
+ export class Enum implements IStructure {
141
+ context: Context;
142
+ name: string;
143
+ properties: Property[] = [];
144
+
145
+ addProperty(property: Property) {
146
+ this.properties.push(property);
147
+ }
148
+ }
149
+
137
150
  export class Property {
138
151
  index: number;
139
152
  name: string;
@@ -492,9 +492,10 @@ export class ArraySchema<V=any> implements Array<V>, SchemaDecoderCallbacks {
492
492
  return Array.from(this.$items.values())[Symbol.iterator]();
493
493
  }
494
494
 
495
- [Symbol.unscopables]() {
496
- return this.$items[Symbol.unscopables]();
497
- }
495
+ // WORKAROUND for compatibility
496
+ // - TypeScript 4 defines @@unscopables as a function
497
+ // - TypeScript 5 defines @@unscopables as an object
498
+ [Symbol.unscopables]: any;
498
499
 
499
500
  /**
500
501
  * Returns an iterable of key, value pairs for every entry in the array