diff --git a/packages/protocol/src/oidb-services/sys-faces/fetch-sys-faces.ts b/packages/protocol/src/oidb-services/sys-faces/fetch-sys-faces.ts index 6f38d680..bedc2e32 100644 --- a/packages/protocol/src/oidb-services/sys-faces/fetch-sys-faces.ts +++ b/packages/protocol/src/oidb-services/sys-faces/fetch-sys-faces.ts @@ -6,7 +6,12 @@ // `aniSticker*` metadata to pick the right wire encoding for a face id (see // element-builder's makeFaceElem + sys-face-store). -import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; +import { createLogger } from '@snowluma/common/logger'; +import { + protobuf_decode, + protobuf_encode, + protobuf_getUnknownFieldMetadata, +} from '@snowluma/proton'; import type { OidbBase } from '@snowluma/proto-defs/oidb'; import type { OidbFaceEmoji, @@ -15,6 +20,8 @@ import type { } from '@snowluma/proto-defs/oidb-actions/sys-faces'; import { invokeOidb, type OidbSender } from '../../oidb-service'; +const log = createLogger('SysFace'); + /** A single system face / emoji. Field names match Lagrange's `SysFaceEntry`; * `qSid` is the numeric face ID as a string. */ export interface SysFaceEntry { @@ -37,9 +44,44 @@ export interface SysFacePackEntry { emojis: SysFaceEntry[]; } -function emojiToEntry(e: OidbFaceEmoji): SysFaceEntry { +interface FaceLocation { + source: 'common' | 'special-big' | 'magic'; + packIndex: number; + faceIndex: number; +} + +function emojiToEntry(e: OidbFaceEmoji, location: FaceLocation): SysFaceEntry | null { + const qSid = e.qSid; + if (qSid == null || qSid === '') { + const metadata = protobuf_getUnknownFieldMetadata(e); + const unexpectedIdFields = metadata.fields.filter((field) => field.fieldNumber === 1); + if (unexpectedIdFields.length > 0) { + throw new Error( + `system face id uses unsupported wire encoding at source ${location.source}, ` + + `pack ${location.packIndex}, face ${location.faceIndex}: ` + + `wireTypes=${unexpectedIdFields.map((field) => field.wireType).join(',')}`, + ); + } + log.warn( + 'system face catalog skipped entry without id: source=%s pack=%d face=%d ' + + 'hasDescription=%s hasCode=%s unknownFields=%d', + location.source, + location.packIndex, + location.faceIndex, + Boolean(e.qDes), + Boolean(e.emCode), + metadata.totalOccurrences, + ); + return null; + } + if (typeof qSid !== 'string') { + throw new Error( + `system face id has invalid decoded type at source ${location.source}, ` + + `pack ${location.packIndex}, face ${location.faceIndex}: ${typeof qSid}`, + ); + } return { - qSid: e.qSid ?? '', + qSid, qDes: e.qDes ?? '', emCode: e.emCode ?? '', qCid: e.qCid ?? null, @@ -53,6 +95,19 @@ function emojiToEntry(e: OidbFaceEmoji): SysFaceEntry { }; } +function emojisToEntries( + emojis: OidbFaceEmoji[], + source: FaceLocation['source'], + packIndex: number, +): SysFaceEntry[] { + const entries: SysFaceEntry[] = []; + for (const [faceIndex, emoji] of emojis.entries()) { + const entry = emojiToEntry(emoji, { source, packIndex, faceIndex }); + if (entry) entries.push(entry); + } + return entries; +} + export namespace FetchSysFaces { export const command = 0x9154; export const subCommand = 1; @@ -70,11 +125,15 @@ export namespace FetchSysFaces { const packs: SysFacePackEntry[] = []; // common + special-big share the same content shape. - for (const content of [body.commonFace, body.specialBigFace]) { + for (const [source, content] of [ + ['common', body.commonFace], + ['special-big', body.specialBigFace], + ] as const) { for (const list of content?.emojiList ?? []) { + const packIndex = packs.length; packs.push({ packName: list.emojiPackName ?? '', - emojis: (list.emojiDetail ?? []).map(emojiToEntry), + emojis: emojisToEntries(list.emojiDetail ?? [], source, packIndex), }); } } @@ -83,7 +142,11 @@ export namespace FetchSysFaces { // "MagicFace" client-side, so we match for parity. const magicEmojis = body.specialMagicFace?.field1?.emojiList ?? []; if (magicEmojis.length > 0) { - packs.push({ packName: 'MagicFace', emojis: magicEmojis.map(emojiToEntry) }); + const packIndex = packs.length; + packs.push({ + packName: 'MagicFace', + emojis: emojisToEntries(magicEmojis, 'magic', packIndex), + }); } return packs; diff --git a/packages/protocol/src/sys-face-store.ts b/packages/protocol/src/sys-face-store.ts index 6d32e78b..1d49faf3 100644 --- a/packages/protocol/src/sys-face-store.ts +++ b/packages/protocol/src/sys-face-store.ts @@ -297,7 +297,10 @@ function validateCatalog(packs: SysFacePackEntry[]): SysFacePackEntry[] { for (const [faceIndex, face] of pack.emojis.entries()) { if (!face || typeof face.qSid !== 'string' || !/^(?:0|[1-9]\d*)$/.test(face.qSid) || !Number.isSafeInteger(Number(face.qSid)) || Number(face.qSid) < 0) { - throw new Error(`invalid system face id at pack ${packIndex}, face ${faceIndex}`); + throw new Error( + `invalid system face id at pack ${packIndex}, face ${faceIndex}: ` + + `value=${JSON.stringify(face?.qSid)}`, + ); } if (seen.has(face.qSid)) { throw new Error(`duplicate system face id ${face.qSid}`); diff --git a/packages/protocol/tests/sys-faces.test.ts b/packages/protocol/tests/sys-faces.test.ts index 52251420..a44946e6 100644 --- a/packages/protocol/tests/sys-faces.test.ts +++ b/packages/protocol/tests/sys-faces.test.ts @@ -110,6 +110,22 @@ describe('FetchSysFaces.deserialize', () => { expect(packs.map((p) => p.packName)).toEqual(['经典', '超级', 'MagicFace']); expect(packs[1].emojis[0].aniStickerType).toBe(3); }); + + it('fails closed when an id field uses an unsupported wire encoding', () => { + const driftedFace = { qDes: '/未知表情' }; + Object.defineProperty(driftedFace, Symbol.for('snowluma.proton.unknownFields'), { + value: { + fields: [{ fieldNumber: 1, wireType: 0, count: 1, totalByteLength: 1 }], + totalOccurrences: 1, + omittedOccurrences: 0, + omittedByteLength: 0, + }, + }); + + expect(() => FetchSysFaces.deserialize({} as never, { + specialMagicFace: { field1: { emojiList: [driftedFace] } }, + } as never)).toThrow(/unsupported wire encoding.*wireTypes=0/); + }); }); describe('SysFaceStore — persistent catalog and query seam', () => { @@ -178,6 +194,42 @@ describe('SysFaceStore — persistent catalog and query seam', () => { }]); }); + it('keeps usable faces when a server pack contains an id-less record', async () => { + const storage = new MemoryCatalogStorage(); + const response = FetchSysFaces.deserialize(sender, { + commonFace: { + emojiList: [ + { emojiPackName: '经典', emojiDetail: [{ qSid: '14', qDes: '/微笑' }] }, + { emojiPackName: '小表情', emojiDetail: [{ qSid: '424', qDes: '/比心' }] }, + ], + }, + specialBigFace: { + emojiList: [{ + emojiPackName: '超级表情', + emojiDetail: [{ qSid: '392', qDes: '/龙年快乐' }], + }], + }, + specialMagicFace: { + field1: { + emojiList: [ + { qDes: '/目录占位' }, + { qSid: '999', qDes: '/魔法表情' }, + ], + }, + }, + } as never); + const store = new SysFaceStore({ + storage, + now: () => 456, + fetchCatalog: async () => response, + }); + + await expect(store.refresh(sender)).resolves.toHaveLength(4); + expect(store.lookup(14)?.qDes).toBe('/微笑'); + expect(store.lookup(999)?.qDes).toBe('/魔法表情'); + expect(storage.saved[0]?.packs[3]?.emojis.map((face) => face.qSid)).toEqual(['999']); + }); + it('refreshes a cache miss once, then keeps an authoritative not-found result', async () => { const storage = new MemoryCatalogStorage({ schemaVersion: 1,