Skip to content

Commit 30e9c4f

Browse files
committed
feat: @netcomp support inherit
1 parent 0f93da2 commit 30e9c4f

File tree

7 files changed

+250
-215
lines changed

7 files changed

+250
-215
lines changed

src/base-dirty-data.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ export abstract class ADirty<T> {
1919

2020
@NetComp("Int")
2121
export class Int extends ADirty<number> implements ISerable {
22-
@NetVar(DataType.bool)
22+
@NetVar(DataType.BOOL)
2323
dirty: boolean = false;
2424
private _value: number = 0;
25-
@NetVar(DataType.int)
25+
@NetVar(DataType.INT)
2626
get value() {
2727
return this._value;
2828
}
@@ -57,11 +57,11 @@ export class Int extends ADirty<number> implements ISerable {
5757

5858
@NetComp("Float")
5959
export class Float extends ADirty<number> implements ISerable {
60-
@NetVar(DataType.bool)
60+
@NetVar(DataType.BOOL)
6161
dirty: boolean = false;
6262

6363
private _value: number = 0;
64-
@NetVar(DataType.float)
64+
@NetVar(DataType.FLOAT)
6565
get value() {
6666
return this._value;
6767
}
@@ -95,11 +95,11 @@ export class Float extends ADirty<number> implements ISerable {
9595

9696
@NetComp("Long")
9797
export class Long extends ADirty<number> implements ISerable {
98-
@NetVar(DataType.bool)
98+
@NetVar(DataType.BOOL)
9999
dirty: boolean = false;
100100

101101
private _value: number = 0;
102-
@NetVar(DataType.long)
102+
@NetVar(DataType.LONG)
103103
get value() {
104104
return this._value;
105105
}
@@ -133,7 +133,7 @@ export class Long extends ADirty<number> implements ISerable {
133133

134134
@NetComp("Uint")
135135
export class Uint extends ADirty<number> implements ISerable {
136-
@NetVar(DataType.bool)
136+
@NetVar(DataType.BOOL)
137137
dirty: boolean = false;
138138
private _value: number = 0;
139139
@NetVar(DataType.uint)
@@ -171,11 +171,11 @@ export class Uint extends ADirty<number> implements ISerable {
171171

172172
@NetComp("Double")
173173
export class Double extends ADirty<number> implements ISerable {
174-
@NetVar(DataType.bool)
174+
@NetVar(DataType.BOOL)
175175
dirty: boolean = false;
176176

177177
private _value: number = 0;
178-
@NetVar(DataType.double)
178+
@NetVar(DataType.DOUBLE)
179179
get value() {
180180
return this._value;
181181
}
@@ -209,7 +209,7 @@ export class Double extends ADirty<number> implements ISerable {
209209

210210
@NetComp("Ulong")
211211
export class Ulong extends ADirty<number> implements ISerable {
212-
@NetVar(DataType.bool)
212+
@NetVar(DataType.BOOL)
213213
dirty: boolean = false;
214214

215215
private _value: number = 0;
@@ -247,11 +247,11 @@ export class Ulong extends ADirty<number> implements ISerable {
247247

248248
@NetComp("Short")
249249
export class Short extends ADirty<number> implements ISerable {
250-
@NetVar(DataType.bool)
250+
@NetVar(DataType.BOOL)
251251
dirty: boolean = false;
252252

253253
private _value: number = 0;
254-
@NetVar(DataType.short)
254+
@NetVar(DataType.SHORT)
255255
get value() {
256256
return this._value;
257257
}
@@ -285,7 +285,7 @@ export class Short extends ADirty<number> implements ISerable {
285285

286286
@NetComp("Ulong")
287287
export class Ushort extends ADirty<number> implements ISerable {
288-
@NetVar(DataType.bool)
288+
@NetVar(DataType.BOOL)
289289
dirty: boolean = false;
290290

291291
private _value: number = 0;

src/component-rpc.ts

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ import {
44
DataTypeVoid,
55
genMethodSchema,
66
genSchema,
7+
getOrCreateScheme,
8+
RpcType,
79
} from "./component-schema";
810
import { str as hash } from "./lib/crc-32";
911

10-
export enum RpcType {
11-
SERVER,
12-
CLIENT,
13-
}
14-
1512
export const hash2RpcName = {} as Record<number, string>;
1613
export class Crc32PropertyKeyHashConflict extends Error {}
1714
export function Rpc<RpcReturnType extends void | DataType = void>(
@@ -20,9 +17,7 @@ export function Rpc<RpcReturnType extends void | DataType = void>(
2017
) {
2118
return function (t: any, propertyKey: string): void {
2219
// gen schema
23-
const target: ComponentConstructor = t as any;
24-
if (!target.__schema__) target.__schema__ = genSchema();
25-
const s = target.__schema__;
20+
const s = getOrCreateScheme(t);
2621
if (!s.methods[propertyKey]) {
2722
s.methods[propertyKey] = genMethodSchema();
2823
}
@@ -46,7 +41,7 @@ export function Rpc<RpcReturnType extends void | DataType = void>(
4641
console.warn(
4742
`[Netcode]Rpc function ${propertyKey} at paramIndex(${i}) set the default type DataType.double`
4843
);
49-
ms.paramTypes[i] = DataType.double;
44+
ms.paramTypes[i] = DataType.DOUBLE;
5045
}
5146
}
5247
};
@@ -58,32 +53,11 @@ export function RpcVar(type: DataType) {
5853
propertyKey: string,
5954
parameterIndex: number
6055
): void {
61-
// gen schema
62-
const target: ComponentConstructor = t as any;
63-
if (!target.__schema__) target.__schema__ = genSchema();
64-
const s = target.__schema__;
56+
const s = getOrCreateScheme(t);
6557
if (!s.methods[propertyKey]) {
6658
s.methods[propertyKey] = genMethodSchema();
6759
}
6860
const ms = s.methods[propertyKey];
6961
ms.paramTypes[parameterIndex] = type;
7062
};
7163
}
72-
73-
// export function RpcArr(type: DataType) {
74-
// return function (
75-
// t: any,
76-
// propertyKey: string,
77-
// parameterIndex: number
78-
// ): void {
79-
// // gen schema
80-
// const target: ComponentConstructor = t as any;
81-
// if (!target.__schema__) target.__schema__ = genSchema();
82-
// const s = target.__schema__;
83-
// if (!s.methods[propertyKey]) {
84-
// s.methods[propertyKey] = genMethodSchema();
85-
// }
86-
// const ms = s.methods[propertyKey];
87-
88-
// };
89-
// }

src/component-schema.ts

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { RpcType } from "./component-rpc";
21
import { NULL_NUM, NULL_STR } from "./macro";
32

43
export interface PropSchema {
@@ -31,10 +30,15 @@ export interface MethodSchema {
3130
type: RpcType;
3231
}
3332

33+
export enum RpcType {
34+
SERVER,
35+
CLIENT,
36+
}
37+
3438
// prettier-ignore
3539
export enum DataType {
36-
none, i8 = 1, u8, i16, u16, i32, u32, f32, f64,
37-
short, ushort, int, uint, long, ulong, float, double, string, bool
40+
NONE, I8 = 1, U8, I16, U16, I32, U32, F32, F64,
41+
SHORT, ushort, INT, uint, LONG, ulong, FLOAT, DOUBLE, STRING, BOOL
3842
}
3943
export const DataTypeObect = 99;
4044
export const DataTypeVoid = 98;
@@ -43,24 +47,46 @@ export type ComponentConstructor<T = any> = { new (): T } & {
4347
__schema__: Schema;
4448
};
4549

46-
export function genSchema(): Schema {
47-
return {
48-
hash: NULL_NUM,
49-
name: NULL_STR,
50-
count: 0,
51-
props: Object.create(null),
52-
methods: Object.create(null),
53-
raw: [],
54-
};
50+
export function genSchema(o = Object.create(null)): Schema {
51+
o.hash = NULL_NUM;
52+
o.name = NULL_STR;
53+
o.count = 0;
54+
o.props = Object.create(null);
55+
o.methods = Object.create(null);
56+
o.raw = [];
57+
return o;
58+
}
59+
60+
export function genMethodSchema(o = Object.create(null)): MethodSchema {
61+
o.hash = NULL_NUM;
62+
o.name = NULL_STR;
63+
o.paramCount = 0;
64+
o.paramTypes = [];
65+
o.returnType = DataTypeVoid;
66+
o.type = -1;
67+
return o;
5568
}
5669

57-
export function genMethodSchema(): MethodSchema {
58-
return {
59-
hash: NULL_NUM,
60-
name: NULL_STR,
61-
paramCount: 0,
62-
paramTypes: [],
63-
returnType: DataTypeVoid,
64-
type: -1,
65-
};
70+
export const SCHEME_KEY = "__schema__";
71+
export function getSchemaByPrototype(prototype: any): Schema | null {
72+
if (prototype.hasOwnProperty(SCHEME_KEY)) {
73+
return (prototype as any)[SCHEME_KEY];
74+
}
75+
return null;
76+
}
77+
78+
export function getOrCreateScheme(prototype: any) {
79+
if (prototype.hasOwnProperty(SCHEME_KEY)) {
80+
return (prototype as any)[SCHEME_KEY];
81+
}
82+
83+
const s = genSchema() as Schema;
84+
(prototype as any)[SCHEME_KEY] = s;
85+
const superCtr = Object.getPrototypeOf(prototype);
86+
87+
const superSchema = superCtr[SCHEME_KEY] as Schema;
88+
if (superSchema) {
89+
s.raw.push.apply(s.raw, superSchema.raw);
90+
}
91+
return s;
6692
}

0 commit comments

Comments
 (0)