mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-17 05:18:04 +00:00
Merge branch 'refactor/proto' of https://github.com/pk5ls20/NapCatQQ into pr/436
This commit is contained in:
commit
095f1c270b
@ -64,7 +64,7 @@ export function ProtoField(no: number, type: ScalarType | (() => ProtoMessageTyp
|
|||||||
type ProtoFieldReturnType<T extends unknown, E extends boolean> = NonNullable<T> extends ScalarProtoFieldType<infer S, infer O, infer R>
|
type ProtoFieldReturnType<T extends unknown, E extends boolean> = NonNullable<T> extends ScalarProtoFieldType<infer S, infer O, infer R>
|
||||||
? ScalarTypeToTsType<S>
|
? ScalarTypeToTsType<S>
|
||||||
: T extends NonNullable<MessageProtoFieldType<infer S, infer O, infer R>>
|
: T extends NonNullable<MessageProtoFieldType<infer S, infer O, infer R>>
|
||||||
? NonNullable<ProtoStructType<ReturnType<S>, E>>
|
? NonNullable<NapProtoStructType<ReturnType<S>, E>>
|
||||||
: never;
|
: never;
|
||||||
|
|
||||||
type RequiredFieldsBaseType<T extends unknown, E extends boolean> = {
|
type RequiredFieldsBaseType<T extends unknown, E extends boolean> = {
|
||||||
@ -85,14 +85,18 @@ type RequiredFieldsType<T extends unknown, E extends boolean> = E extends true ?
|
|||||||
|
|
||||||
type OptionalFieldsType<T extends unknown, E extends boolean> = E extends true ? Partial<OptionalFieldsBaseType<T, E>> : OptionalFieldsBaseType<T, E>;
|
type OptionalFieldsType<T extends unknown, E extends boolean> = E extends true ? Partial<OptionalFieldsBaseType<T, E>> : OptionalFieldsBaseType<T, E>;
|
||||||
|
|
||||||
type ProtoStructType<T extends unknown, E extends boolean> = RequiredFieldsType<T, E> & OptionalFieldsType<T, E>;
|
type NapProtoStructType<T extends unknown, E extends boolean> = RequiredFieldsType<T, E> & OptionalFieldsType<T, E>;
|
||||||
|
|
||||||
const NapProtoMsgCache = new Map<ProtoMessageType, MessageType<ProtoStructType<ProtoMessageType, boolean>>>();
|
export type NapProtoEncodeStructType<T extends unknown> = NapProtoStructType<T, true>;
|
||||||
|
|
||||||
|
export type NapProtoDecodeStructType<T extends unknown> = NapProtoStructType<T, false>;
|
||||||
|
|
||||||
|
const NapProtoMsgCache = new Map<ProtoMessageType, MessageType<NapProtoStructType<ProtoMessageType, boolean>>>();
|
||||||
|
|
||||||
export class NapProtoMsg<T extends ProtoMessageType> {
|
export class NapProtoMsg<T extends ProtoMessageType> {
|
||||||
private readonly _msg: T;
|
private readonly _msg: T;
|
||||||
private readonly _field: PartialFieldInfo[];
|
private readonly _field: PartialFieldInfo[];
|
||||||
private readonly _proto_msg: MessageType<ProtoStructType<T, boolean>>;
|
private readonly _proto_msg: MessageType<NapProtoStructType<T, boolean>>;
|
||||||
|
|
||||||
constructor(fields: T) {
|
constructor(fields: T) {
|
||||||
this._msg = fields;
|
this._msg = fields;
|
||||||
@ -127,14 +131,14 @@ export class NapProtoMsg<T extends ProtoMessageType> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}) as PartialFieldInfo[];
|
}) as PartialFieldInfo[];
|
||||||
this._proto_msg = new MessageType<ProtoStructType<T, boolean>>('nya', this._field);
|
this._proto_msg = new MessageType<NapProtoStructType<T, boolean>>('nya', this._field);
|
||||||
}
|
}
|
||||||
|
|
||||||
encode(data: ProtoStructType<T, true>): Uint8Array {
|
encode(data: NapProtoEncodeStructType<T>): Uint8Array {
|
||||||
return this._proto_msg.toBinary(this._proto_msg.create(data as PartialMessage<ProtoStructType<T, boolean>>));
|
return this._proto_msg.toBinary(this._proto_msg.create(data as PartialMessage<NapProtoEncodeStructType<T>>));
|
||||||
}
|
}
|
||||||
|
|
||||||
decode(data: Uint8Array): ProtoStructType<T, false> {
|
decode(data: Uint8Array): NapProtoDecodeStructType<T> {
|
||||||
return this._proto_msg.fromBinary(data) as ProtoStructType<T, false>;
|
return this._proto_msg.fromBinary(data) as NapProtoDecodeStructType<T>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user