@colyseus/schema 5.0.2 → 5.0.4

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.
@@ -88,6 +88,13 @@ export declare const Metadata: {
88
88
  */
89
89
  setStreamPriority(metadata: Metadata, fieldName: string, fn: (view: any, element: any) => number): void;
90
90
  getStreamPriority(metadata: Metadata | undefined, index: number): (view: any, element: any) => number;
91
+ /**
92
+ * Install a single field with full encoder wiring: accessor descriptor
93
+ * on the prototype + `metadata[$encoders]` slot for primitives. Shared
94
+ * between `Metadata.setFields` (build path) and
95
+ * `Reflection.makeEncodable` (Reflection upgrade path).
96
+ */
97
+ defineField(target: any, metadata: any, fieldIndex: number, fieldName: string, type: DefinitionType): void;
91
98
  setFields<T extends {
92
99
  new (...args: any[]): InstanceType<T>;
93
100
  } = any>(target: T, fields: { [field in keyof InstanceType<T>]?: DefinitionType; }): T;
@@ -25,6 +25,22 @@ interface ReflectionStatic {
25
25
  * @returns Decoder instance
26
26
  */
27
27
  decode: <T extends Schema = Schema>(bytes: Uint8Array, it?: Iterator) => Decoder<T>;
28
+ /**
29
+ * Upgrade a class produced by `Reflection.decode` so its instances
30
+ * can be used as encode sources (for `InputEncoder` or `Encoder`).
31
+ *
32
+ * `Reflection.decode` reconstructs classes with decoder-only field
33
+ * slots — `inst.x = 7` lands as a direct own property and bypasses
34
+ * the change-tracking + `$values` plumbing that encoders rely on.
35
+ * Calling `makeEncodable(ctor)` installs the same prototype accessor
36
+ * descriptors and `metadata[$encoders]` lookup table that the
37
+ * `schema(...)` / `@type` builders install at class-definition time.
38
+ *
39
+ * Idempotent. Pay-as-you-go: callers that only decode never invoke
40
+ * this and pay nothing extra. Must be called BEFORE any instance of
41
+ * the class is constructed and assigned to.
42
+ */
43
+ makeEncodable: (ctor: typeof Schema) => typeof Schema;
28
44
  }
29
45
  /**
30
46
  * Reflection
@@ -683,10 +683,33 @@ ${generateClassBody$8(klass, indent)}
683
683
  ${namespace ? "}" : ""}
684
684
  `;
685
685
  }
686
+ /**
687
+ * Check if all enum members resolve to non-negative integers,
688
+ * allowing emission as a native C# `enum` (which only supports integral types).
689
+ */
690
+ function canUseNativeEnum(_enum) {
691
+ return _enum.properties.every((prop) => {
692
+ if (!prop.type)
693
+ return true;
694
+ const n = Number(prop.type);
695
+ return Number.isInteger(n) && n >= 0;
696
+ });
697
+ }
686
698
  /**
687
699
  * Generate just the enum body (without imports/namespace) for bundling
688
700
  */
689
701
  function generateEnumBody$1(_enum, indent = "") {
702
+ if (canUseNativeEnum(_enum)) {
703
+ const members = _enum.properties
704
+ .map((prop, i) => {
705
+ const value = prop.type ? Number(prop.type) : i;
706
+ return `${indent}\t${prop.name} = ${value},`;
707
+ })
708
+ .join("\n");
709
+ return `${indent}public enum ${_enum.name} : int {
710
+ ${members}
711
+ ${indent}}`;
712
+ }
690
713
  return `${indent}public struct ${_enum.name} {
691
714
 
692
715
  ${_enum.properties