@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.
- package/build/cjs/index.js +0 -3
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +4 -3
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +0 -3
- package/lib/Reflection.js +77 -109
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.js +194 -255
- package/lib/Schema.js.map +1 -1
- package/lib/annotations.d.ts +6 -6
- package/lib/annotations.js +56 -89
- package/lib/annotations.js.map +1 -1
- package/lib/changes/ChangeTree.d.ts +1 -1
- package/lib/changes/ChangeTree.js +86 -96
- package/lib/changes/ChangeTree.js.map +1 -1
- package/lib/codegen/api.js +9 -9
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/argv.js +11 -11
- package/lib/codegen/argv.js.map +1 -1
- package/lib/codegen/cli.js +20 -9
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/languages/cpp.js +126 -77
- package/lib/codegen/languages/cpp.js.map +1 -1
- package/lib/codegen/languages/csharp.js +87 -57
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/languages/haxe.js +34 -26
- package/lib/codegen/languages/haxe.js.map +1 -1
- package/lib/codegen/languages/java.js +39 -27
- package/lib/codegen/languages/java.js.map +1 -1
- package/lib/codegen/languages/js.js +48 -32
- package/lib/codegen/languages/js.js.map +1 -1
- package/lib/codegen/languages/lua.js +35 -24
- package/lib/codegen/languages/lua.js.map +1 -1
- package/lib/codegen/languages/ts.js +63 -72
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.d.ts +2 -1
- package/lib/codegen/parser.js +62 -47
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.d.ts +8 -0
- package/lib/codegen/types.js +64 -54
- package/lib/codegen/types.js.map +1 -1
- package/lib/encoding/decode.js +15 -15
- package/lib/encoding/decode.js.map +1 -1
- package/lib/encoding/encode.js +14 -14
- package/lib/encoding/encode.js.map +1 -1
- package/lib/events/EventEmitter.d.ts +1 -1
- package/lib/events/EventEmitter.js +16 -51
- package/lib/events/EventEmitter.js.map +1 -1
- package/lib/filters/index.js +7 -8
- package/lib/filters/index.js.map +1 -1
- package/lib/index.js +7 -7
- package/lib/index.js.map +1 -1
- package/lib/types/ArraySchema.d.ts +1 -1
- package/lib/types/ArraySchema.js +157 -219
- package/lib/types/ArraySchema.js.map +1 -1
- package/lib/types/CollectionSchema.d.ts +1 -1
- package/lib/types/CollectionSchema.js +60 -68
- package/lib/types/CollectionSchema.js.map +1 -1
- package/lib/types/HelperTypes.d.ts +9 -9
- package/lib/types/MapSchema.js +63 -74
- package/lib/types/MapSchema.js.map +1 -1
- package/lib/types/SetSchema.js +59 -68
- package/lib/types/SetSchema.js.map +1 -1
- package/lib/types/index.js +1 -1
- package/lib/types/index.js.map +1 -1
- package/lib/utils.js +10 -13
- package/lib/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/codegen/languages/csharp.ts +48 -5
- package/src/codegen/parser.ts +24 -1
- package/src/codegen/types.ts +14 -1
- package/src/types/ArraySchema.ts +4 -3
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
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
|
|
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";
|
package/src/codegen/parser.ts
CHANGED
|
@@ -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));
|
package/src/codegen/types.ts
CHANGED
|
@@ -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;
|
package/src/types/ArraySchema.ts
CHANGED
|
@@ -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
|
-
|
|
496
|
-
|
|
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
|