update:优化精准稳定
This commit is contained in:
@@ -2,13 +2,23 @@ import protobuf from "protobufjs/minimal.js";
|
||||
|
||||
const { Reader, Writer } = protobuf;
|
||||
|
||||
// protobuf 会将这些 repeated 标量默认编码为 packed(length-delimited),
|
||||
// 同时也允许发送端使用逐项的 unpacked 编码。
|
||||
const PACKABLE_SCALAR_KINDS = new Set([
|
||||
"int32",
|
||||
"int64",
|
||||
"float",
|
||||
"double",
|
||||
"bool",
|
||||
]);
|
||||
|
||||
// 比赛服 protobuf 协议适配层:
|
||||
// 小程序环境不支持 protobufjs 反射模式里的动态 Function codegen,
|
||||
// 所以这里使用 minimal Reader/Writer 做静态字段解码和客户端消息编码。
|
||||
// <match-schema-generated>
|
||||
// 此区块由 scripts/generate-match-schema.mjs 自动生成,请勿手动修改。
|
||||
// 来源:src/utils/match.min.js(sha256: ec0371baeba9a9bf)
|
||||
// 协议命名空间:rpc;消息数:12;字段数:163
|
||||
// 来源:src/utils/match.min.js(sha256: 5b72fd5f9e087921)
|
||||
// 协议命名空间:rpc;消息数:12;字段数:164
|
||||
|
||||
export const ServerMessageType = {
|
||||
SERVER_MSG_UNKNOWN: 0,
|
||||
@@ -206,6 +216,7 @@ const SCHEMAS = {
|
||||
61: { name: "delta_qualified_rate", kind: "int32" },
|
||||
62: { name: "hit_rate", kind: "int32" },
|
||||
63: { name: "delta_hit_rate", kind: "int32" },
|
||||
64: { name: "random_ring_areas", kind: "int32", repeated: true },
|
||||
},
|
||||
MatchInfo: {
|
||||
1: { name: "match_id", kind: "string" },
|
||||
@@ -341,6 +352,20 @@ function readValue(reader, field) {
|
||||
return readScalar(reader, field.kind);
|
||||
}
|
||||
|
||||
function readPackedValues(reader, field) {
|
||||
const end = reader.uint32() + reader.pos;
|
||||
const values = [];
|
||||
|
||||
while (reader.pos < end) {
|
||||
values.push(readScalar(reader, field.kind));
|
||||
}
|
||||
if (reader.pos !== end) {
|
||||
throw new Error(`Invalid packed protobuf field: ${field.name}`);
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
function decodeMessage(schemaName, readerOrData, length) {
|
||||
const schema = SCHEMAS[schemaName];
|
||||
const reader =
|
||||
@@ -351,9 +376,10 @@ function decodeMessage(schemaName, readerOrData, length) {
|
||||
while (reader.pos < end) {
|
||||
const tag = reader.uint32();
|
||||
const field = schema[tag >>> 3];
|
||||
const wireType = tag & 7;
|
||||
|
||||
if (!field) {
|
||||
reader.skipType(tag & 7);
|
||||
reader.skipType(wireType);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -365,6 +391,17 @@ function decodeMessage(schemaName, readerOrData, length) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
field.repeated &&
|
||||
PACKABLE_SCALAR_KINDS.has(field.kind) &&
|
||||
wireType === 2
|
||||
) {
|
||||
const values = message[field.name] || [];
|
||||
values.push(...readPackedValues(reader, field));
|
||||
message[field.name] = values;
|
||||
continue;
|
||||
}
|
||||
|
||||
const value = readValue(reader, field);
|
||||
|
||||
if (field.repeated) {
|
||||
|
||||
Reference in New Issue
Block a user