feat(battle): serve game modes from extracted mission data
Publish stable mode IDs and matching responses in the stock format. Build the mission pools from the multiplayer mission data and provide required fields and localized names. Load datasets before listening and reuse cached results when the source file digests match. Preserve invalid UTF-8 bytes when round-tripping BLK strings and names. The game mode list appeared in the offline client.
This commit is contained in:
1 parent
d88aa3ff4f
commit
1d9c92a9d0
26 files changed
+943
-28
No files matched your search
@@ -4,14 +4,12 @@
|
||||
* Layout (DagorEngine blk_serialize.cpp, dblk::write_names_base / read_names_base):
|
||||
* varUint count
|
||||
* varUint total byte size of the names (only when count > 0)
|
||||
* names, each UTF-8 and NUL-terminated
|
||||
* names, each UTF-8 and NUL-terminated (invalid bytes kept, text-bytes.ts)
|
||||
*/
|
||||
import { BlkFormatError } from '../errors.ts';
|
||||
import type { ByteReader } from './byte-reader.ts';
|
||||
import type { ByteWriter } from './byte-writer.ts';
|
||||
|
||||
const utf8Decoder = new TextDecoder('utf-8', { fatal: true });
|
||||
const utf8Encoder = new TextEncoder();
|
||||
import { decodeText, encodeText } from './text-bytes.ts';
|
||||
|
||||
export function readNameTable(reader: ByteReader): string[] {
|
||||
const count = reader.varUint();
|
||||
@@ -25,7 +23,7 @@ export function readNameTable(reader: ByteReader): string[] {
|
||||
while (names.length < count) {
|
||||
const end = blob.indexOf(0, start);
|
||||
if (end < 0) throw new BlkFormatError(`name table ends after ${names.length} of ${count} names`);
|
||||
names.push(utf8Decoder.decode(blob.subarray(start, end)));
|
||||
names.push(decodeText(blob.subarray(start, end)));
|
||||
start = end + 1;
|
||||
}
|
||||
return names;
|
||||
@@ -34,7 +32,7 @@ export function readNameTable(reader: ByteReader): string[] {
|
||||
export function writeNameTable(writer: ByteWriter, names: readonly string[]): void {
|
||||
writer.varUint(names.length);
|
||||
if (names.length === 0) return;
|
||||
const encoded = names.map((name) => utf8Encoder.encode(name));
|
||||
const encoded = names.map(encodeText);
|
||||
writer.varUint(encoded.reduce((total, bytes) => total + bytes.length + 1, 0));
|
||||
for (const bytes of encoded) {
|
||||
if (bytes.includes(0)) throw new BlkFormatError('a name cannot contain a NUL character');
|
||||
|
||||
@@ -11,11 +11,10 @@ import type { BlkValue } from '../model.ts';
|
||||
import { BlkFormatError } from '../errors.ts';
|
||||
import { isInline, typeById, typeByName } from '../param-types.ts';
|
||||
import type { ByteWriter } from './byte-writer.ts';
|
||||
import { decodeText, encodeText } from './text-bytes.ts';
|
||||
|
||||
export const NAME_TABLE_STRING_FLAG = 0x8000_0000;
|
||||
|
||||
const utf8Decoder = new TextDecoder('utf-8', { fatal: true });
|
||||
const utf8Encoder = new TextEncoder();
|
||||
const bits = new DataView(new ArrayBuffer(4));
|
||||
|
||||
export interface DecodeContext {
|
||||
@@ -68,7 +67,7 @@ export function storesInline(value: BlkValue): boolean {
|
||||
|
||||
/** Append a string to the complex-data area; returns its offset. */
|
||||
export function appendString(complex: ByteWriter, text: string): number {
|
||||
const encoded = utf8Encoder.encode(text);
|
||||
const encoded = encodeText(text);
|
||||
if (encoded.includes(0)) throw new BlkFormatError('a string value cannot contain a NUL character');
|
||||
const offset = complex.length;
|
||||
complex.bytes(encoded);
|
||||
@@ -110,7 +109,7 @@ function decodeString(v: number, { names, complex }: DecodeContext): string {
|
||||
}
|
||||
const end = complex.indexOf(0, v);
|
||||
if (v >= complex.length || end < 0) throw new BlkFormatError(`string at complex offset ${v} is not terminated`);
|
||||
return utf8Decoder.decode(complex.subarray(v, end));
|
||||
return decodeText(complex.subarray(v, end));
|
||||
}
|
||||
|
||||
function decodeInline(type: BlkValue['type'], v: number): BlkValue {
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* Names and string values: bytes in the file, JavaScript strings in memory.
|
||||
*
|
||||
* BLK text is UTF-8, but the engine treats it as plain bytes and never checks it, and some
|
||||
* stock data is not valid UTF-8: the shared name map of 2.61.0.37 mis.vromfs.bin holds 3
|
||||
* such names among 103183 (mission-trigger comments whose first byte is damaged). Rejecting
|
||||
* them would make the whole pack unreadable, and replacing them would change the bytes.
|
||||
*
|
||||
* So invalid bytes are kept with the "surrogateescape" scheme (Python, PEP 383): each byte
|
||||
* that is not part of a valid UTF-8 sequence becomes the lone low surrogate U+DC00 + byte,
|
||||
* and encoding turns such a lone surrogate back into that byte. Valid UTF-8 can never
|
||||
* produce a lone surrogate, so the mapping is unambiguous and a document read and written
|
||||
* back keeps every byte.
|
||||
*/
|
||||
|
||||
const strictUtf8 = new TextDecoder('utf-8', { fatal: true });
|
||||
const utf8Encoder = new TextEncoder();
|
||||
|
||||
const ESCAPE_BASE = 0xdc00;
|
||||
/** Only bytes 0x80..0xFF can be invalid; ASCII always decodes. */
|
||||
const ESCAPE_FIRST = ESCAPE_BASE + 0x80;
|
||||
const ESCAPE_LAST = ESCAPE_BASE + 0xff;
|
||||
|
||||
export function decodeText(bytes: Uint8Array): string {
|
||||
try {
|
||||
return strictUtf8.decode(bytes);
|
||||
} catch {
|
||||
return decodeWithEscapes(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
export function encodeText(text: string): Uint8Array {
|
||||
if (!hasEscapes(text)) return utf8Encoder.encode(text);
|
||||
const parts: Uint8Array[] = [];
|
||||
let runStart = 0;
|
||||
for (let i = 0; i < text.length; i += 1) {
|
||||
const unit = text.charCodeAt(i);
|
||||
if (!isEscape(text, i, unit)) continue;
|
||||
if (i > runStart) parts.push(utf8Encoder.encode(text.slice(runStart, i)));
|
||||
parts.push(Uint8Array.of(unit - ESCAPE_BASE));
|
||||
runStart = i + 1;
|
||||
}
|
||||
if (runStart < text.length) parts.push(utf8Encoder.encode(text.slice(runStart)));
|
||||
return concat(parts);
|
||||
}
|
||||
|
||||
function decodeWithEscapes(bytes: Uint8Array): string {
|
||||
let text = '';
|
||||
let i = 0;
|
||||
while (i < bytes.length) {
|
||||
const length = sequenceLength(bytes[i]!);
|
||||
const sequence = length > 0 && i + length <= bytes.length ? decodeSequence(bytes.subarray(i, i + length)) : undefined;
|
||||
if (sequence === undefined) {
|
||||
text += String.fromCharCode(ESCAPE_BASE + bytes[i]!);
|
||||
i += 1;
|
||||
} else {
|
||||
text += sequence;
|
||||
i += length;
|
||||
}
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
/** Length of the UTF-8 sequence a lead byte announces; 0 when it cannot start one. */
|
||||
function sequenceLength(lead: number): number {
|
||||
if (lead < 0x80) return 1;
|
||||
if (lead >= 0xc2 && lead <= 0xdf) return 2;
|
||||
if (lead >= 0xe0 && lead <= 0xef) return 3;
|
||||
if (lead >= 0xf0 && lead <= 0xf4) return 4;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** The one character a candidate sequence encodes, or undefined when it is not valid UTF-8. */
|
||||
function decodeSequence(candidate: Uint8Array): string | undefined {
|
||||
try {
|
||||
return strictUtf8.decode(candidate);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function hasEscapes(text: string): boolean {
|
||||
for (let i = 0; i < text.length; i += 1) {
|
||||
if (isEscape(text, i, text.charCodeAt(i))) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** A low surrogate in the escape range that does not complete a surrogate pair. */
|
||||
function isEscape(text: string, index: number, unit: number): boolean {
|
||||
if (unit < ESCAPE_FIRST || unit > ESCAPE_LAST) return false;
|
||||
const previous = index > 0 ? text.charCodeAt(index - 1) : 0;
|
||||
return !(previous >= 0xd800 && previous <= 0xdbff);
|
||||
}
|
||||
|
||||
function concat(parts: readonly Uint8Array[]): Uint8Array {
|
||||
const result = new Uint8Array(parts.reduce((total, part) => total + part.length, 0));
|
||||
let offset = 0;
|
||||
for (const part of parts) {
|
||||
result.set(part, offset);
|
||||
offset += part.length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user