@colyseus/schema 4.0.18 → 4.0.20

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/Schema.d.ts CHANGED
@@ -53,7 +53,7 @@ export declare class Schema<C = any> implements IRef {
53
53
  * @param jsonData JSON data to restore the instance from
54
54
  * @returns
55
55
  */
56
- restore<T extends Partial<this>>(jsonData: ToJSON<T>): this;
56
+ restore(jsonData: ToJSON<this>): this;
57
57
  /**
58
58
  * (Server-side): Flag a property to be encoded for the next patch.
59
59
  * @param instance Schema instance
@@ -62,7 +62,7 @@ export declare class Schema<C = any> implements IRef {
62
62
  */
63
63
  setDirty<K extends NonFunctionPropNames<this>>(property: K | number, operation?: OPERATION): void;
64
64
  clone(): this;
65
- toJSON(this: any): ToJSON<Partial<this>>;
65
+ toJSON(this: any): ToJSON<this>;
66
66
  /**
67
67
  * Used in tests only
68
68
  * @internal
@@ -63,7 +63,7 @@ export declare function view<T>(tag?: number): (target: T, fieldName: string) =>
63
63
  export declare function unreliable<T>(target: T, field: string): void;
64
64
  export declare function type(type: DefinitionType, options?: TypeOptions): PropertyDecorator;
65
65
  export declare function getPropertyDescriptor(fieldCached: string, fieldIndex: number, type: DefinitionType, complexTypeKlass: TypeDefinition): {
66
- get: (this: Schema) => number | ((index: number) => any) | ((index: number) => void) | (() => Schema<any>) | (<T extends Partial<Schema<any>>>(props: AssignableProps<T>) => Schema<any>) | (<T extends Partial<Schema<any>>>(jsonData: import("./index.js").ToJSON<T>) => Schema<any>) | (<K extends "~refId">(property: number | K, operation?: OPERATION) => void) | ((this: any) => import("./index.js").ToJSON<Partial<Schema<any>>>) | (() => void);
66
+ get: (this: Schema) => number | ((index: number) => any) | ((index: number) => void) | (() => Schema<any>) | (<T extends Partial<Schema<any>>>(props: AssignableProps<T>) => Schema<any>) | ((jsonData: import("./index.js").ToJSON<Schema<any>>) => Schema<any>) | (<K extends "~refId">(property: number | K, operation?: OPERATION) => void) | ((this: any) => import("./index.js").ToJSON<Schema<any>>) | (() => void);
67
67
  set: (this: Schema, value: any) => void;
68
68
  enumerable: boolean;
69
69
  configurable: boolean;
@@ -643,10 +643,33 @@ ${generateClassBody$8(klass, indent)}
643
643
  ${namespace ? "}" : ""}
644
644
  `;
645
645
  }
646
+ /**
647
+ * Check if all enum members resolve to non-negative integers,
648
+ * allowing emission as a native C# `enum` (which only supports integral types).
649
+ */
650
+ function canUseNativeEnum(_enum) {
651
+ return _enum.properties.every((prop) => {
652
+ if (!prop.type)
653
+ return true;
654
+ const n = Number(prop.type);
655
+ return Number.isInteger(n) && n >= 0;
656
+ });
657
+ }
646
658
  /**
647
659
  * Generate just the enum body (without imports/namespace) for bundling
648
660
  */
649
661
  function generateEnumBody$1(_enum, indent = "") {
662
+ if (canUseNativeEnum(_enum)) {
663
+ const members = _enum.properties
664
+ .map((prop, i) => {
665
+ const value = prop.type ? Number(prop.type) : i;
666
+ return `${indent}\t${prop.name} = ${value},`;
667
+ })
668
+ .join("\n");
669
+ return `${indent}public enum ${_enum.name} : int {
670
+ ${members}
671
+ ${indent}}`;
672
+ }
650
673
  return `${indent}public struct ${_enum.name} {
651
674
 
652
675
  ${_enum.properties