Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

10.7 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file  protoRuntime.ts — the wire format the generated protobuf code is written against.
 *
 * Generated code imports one small file, `_runtime.ts`, written beside it, so a game that uses Protobuf
 * carries no engine dependency for it and can read the whole codec in one screen. The text lives here as a
 * string because the editor ships without this source tree to copy from.
 *
 * 64-bit integers are JavaScript numbers: encoding or decoding one outside ±2^53 throws instead of rounding.
 */
export const PROTO_RUNTIME = `// Generated by Estella. Do not edit: it is rewritten on every run.
/* eslint-disable */

const TWO32 = 4294967296;
const MAX_SAFE = Number.MAX_SAFE_INTEGER;
const utf8Encoder = new TextEncoder();
const utf8Decoder = new TextDecoder('utf-8', { fatal: true });

const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

/** The JSON form of the debugging wire: bytes are base64 text. */
export function b64(bytes: Uint8Array): string {
    let out = '';
    for (let i = 0; i < bytes.length; i += 3) {
        const n = (bytes[i]! << 16) | ((bytes[i + 1] ?? 0) << 8) | (bytes[i + 2] ?? 0);
        out += B64[(n >> 18) & 63]! + B64[(n >> 12) & 63]! + (i + 1 < bytes.length ? B64[(n >> 6) & 63]! : '=') + (i + 2 < bytes.length ? B64[n & 63]! : '=');
    }
    return out;
}

export function jObj(v: unknown, path: string): Record<string, unknown> {
    if (typeof v !== 'object' || v === null || Array.isArray(v)) throw new TypeError(path + ': expected an object');
    return v as Record<string, unknown>;
}
export function jArr(v: unknown, path: string): unknown[] {
    if (!Array.isArray(v)) throw new TypeError(path + ': expected a list');
    return v;
}
export function jStr(v: unknown, path: string): string {
    if (typeof v !== 'string') throw new TypeError(path + ': expected text');
    return v;
}
export function jBool(v: unknown, path: string): boolean {
    if (typeof v !== 'boolean') throw new TypeError(path + ': expected true or false');
    return v;
}
export function jNum(v: unknown, path: string): number {
    if (typeof v !== 'number') throw new TypeError(path + ': expected a number');
    return v;
}
export function jInt(v: unknown, path: string, min: number, max: number): number {
    if (typeof v !== 'number' || !Number.isInteger(v)) throw new TypeError(path + ': expected an integer');
    if (v < min || v > max) throw new RangeError(path + ': outside ' + min + '..' + max);
    return v;
}
export function jBytes(v: unknown, path: string): Uint8Array {
    const text = jStr(v, path);
    if (text.length % 4 !== 0 || !/^[A-Za-z0-9+/]*={0,2}$/.test(text)) throw new TypeError(path + ': expected base64 text');
    const pad = text.endsWith('==') ? 2 : text.endsWith('=') ? 1 : 0;
    const out = new Uint8Array((text.length / 4) * 3 - pad);
    let o = 0;
    for (let i = 0; i < text.length; i += 4) {
        const n = (B64.indexOf(text[i]!) << 18) | (B64.indexOf(text[i + 1]!) << 12) | ((text[i + 2] === '=' ? 0 : B64.indexOf(text[i + 2]!)) << 6) | (text[i + 3] === '=' ? 0 : B64.indexOf(text[i + 3]!));
        if (o < out.length) out[o++] = (n >> 16) & 255;
        if (o < out.length) out[o++] = (n >> 8) & 255;
        if (o < out.length) out[o++] = n & 255;
    }
    return out;
}
export function jMapInt(key: string, path: string, min: number, max: number): number {
    return jInt(/^-?\d+$/.test(key) ? Number(key) : NaN, path + '[' + key + ']', min, max);
}
export function jMapBool(key: string, path: string): boolean {
    if (key !== 'true' && key !== 'false') throw new TypeError(path + '[' + key + ']: expected true or false');
    return key === 'true';
}

export class Writer {
    private buf = new Uint8Array(64);
    private len = 0;
    private view = new DataView(this.buf.buffer);

    private room(n: number): void {
        if (this.len + n <= this.buf.length) return;
        let size = this.buf.length * 2;
        while (size < this.len + n) size *= 2;
        const next = new Uint8Array(size);
        next.set(this.buf.subarray(0, this.len));
        this.buf = next;
        this.view = new DataView(next.buffer);
    }

    finish(): Uint8Array { return this.buf.slice(0, this.len); }

    private varint64(lo: number, hi: number): void {
        this.room(10);
        while (hi > 0 || lo >= 128) {
            this.buf[this.len++] = (lo & 127) | 128;
            lo = ((lo >>> 7) | (hi << 25)) >>> 0;
            hi >>>= 7;
        }
        this.buf[this.len++] = lo;
    }

    tag(field: number, wireType: number): void { this.varint64(((field << 3) | wireType) >>> 0, 0); }

    uint32(v: number): void { this.varint64(v >>> 0, 0); }
    int32(v: number): void {
        if (v >= 0) this.varint64(v >>> 0, 0);
        else this.varint64(v >>> 0, 0xffffffff);
    }
    sint32(v: number): void { this.varint64(((v << 1) ^ (v >> 31)) >>> 0, 0); }
    bool(v: boolean): void { this.varint64(v ? 1 : 0, 0); }

    uint64(v: number): void {
        if (!Number.isSafeInteger(v) || v < 0) throw new RangeError('uint64 out of range: ' + v);
        this.varint64(v % TWO32, Math.floor(v / TWO32));
    }
    int64(v: number): void {
        if (!Number.isSafeInteger(v)) throw new RangeError('int64 out of range: ' + v);
        if (v >= 0) { this.varint64(v % TWO32, Math.floor(v / TWO32)); return; }
        const m = -v;
        let lo = ((~(m % TWO32)) + 1) >>> 0;
        let hi = (~Math.floor(m / TWO32)) >>> 0;
        if (lo === 0) hi = (hi + 1) >>> 0;
        this.varint64(lo, hi);
    }
    sint64(v: number): void {
        if (!Number.isSafeInteger(v) || Math.abs(v) > MAX_SAFE / 2) throw new RangeError('sint64 out of range: ' + v);
        this.uint64(v >= 0 ? v * 2 : -v * 2 - 1);
    }

    fixed32(v: number): void { this.room(4); this.view.setUint32(this.len, v >>> 0, true); this.len += 4; }
    sfixed32(v: number): void { this.room(4); this.view.setInt32(this.len, v | 0, true); this.len += 4; }
    fixed64(v: number): void {
        if (!Number.isSafeInteger(v) || v < 0) throw new RangeError('fixed64 out of range: ' + v);
        this.room(8);
        this.view.setUint32(this.len, v % TWO32, true);
        this.view.setUint32(this.len + 4, Math.floor(v / TWO32), true);
        this.len += 8;
    }
    sfixed64(v: number): void {
        if (!Number.isSafeInteger(v)) throw new RangeError('sfixed64 out of range: ' + v);
        this.room(8);
        const lo = ((v % TWO32) + TWO32) % TWO32;
        this.view.setUint32(this.len, lo, true);
        this.view.setInt32(this.len + 4, Math.floor(v / TWO32), true);
        this.len += 8;
    }
    float(v: number): void { this.room(4); this.view.setFloat32(this.len, v, true); this.len += 4; }
    double(v: number): void { this.room(8); this.view.setFloat64(this.len, v, true); this.len += 8; }

    bytes(v: Uint8Array): void {
        this.uint32(v.length);
        this.room(v.length);
        this.buf.set(v, this.len);
        this.len += v.length;
    }
    string(v: string): void { this.bytes(utf8Encoder.encode(v)); }
}

export class Reader {
    pos = 0;
    private view: DataView;
    constructor(private readonly buf: Uint8Array) {
        this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
    }

    get length(): number { return this.buf.length; }

    private need(n: number): void {
        if (this.pos + n > this.buf.length) throw new RangeError('protobuf message is truncated');
    }

    private varint64(): [number, number] {
        let lo = 0;
        let hi = 0;
        for (let i = 0; i < 10; i++) {
            this.need(1);
            const b = this.buf[this.pos++]!;
            if (i < 4) lo |= (b & 127) << (7 * i);
            else if (i === 4) { lo |= (b & 15) << 28; hi = (b & 127) >> 4; }
            else hi |= (b & 127) << (7 * i - 32);
            if (!(b & 128)) return [lo >>> 0, hi >>> 0];
        }
        throw new RangeError('protobuf varint is too long');
    }

    tag(): number { return this.uint32(); }
    uint32(): number { return this.varint64()[0]; }
    int32(): number { return this.varint64()[0] | 0; }
    sint32(): number { const v = this.uint32(); return (v >>> 1) ^ -(v & 1); }
    bool(): boolean { const [lo, hi] = this.varint64(); return (lo | hi) !== 0; }

    uint64(): number {
        const [lo, hi] = this.varint64();
        if (hi > 0x1fffff) throw new RangeError('uint64 does not fit in a number');
        return hi * TWO32 + lo;
    }
    int64(): number {
        const [lo, hi] = this.varint64();
        if (hi & 0x80000000) {
            const nlo = (~lo) >>> 0;
            const nhi = (~hi) >>> 0;
            if (nhi > 0x1fffff) throw new RangeError('int64 does not fit in a number');
            return -(nhi * TWO32 + nlo + 1);
        }
        if (hi > 0x1fffff) throw new RangeError('int64 does not fit in a number');
        return hi * TWO32 + lo;
    }
    sint64(): number { const v = this.uint64(); return v % 2 === 0 ? v / 2 : -(v + 1) / 2; }

    fixed32(): number { this.need(4); const v = this.view.getUint32(this.pos, true); this.pos += 4; return v; }
    sfixed32(): number { this.need(4); const v = this.view.getInt32(this.pos, true); this.pos += 4; return v; }
    fixed64(): number {
        this.need(8);
        const lo = this.view.getUint32(this.pos, true);
        const hi = this.view.getUint32(this.pos + 4, true);
        this.pos += 8;
        if (hi > 0x1fffff) throw new RangeError('fixed64 does not fit in a number');
        return hi * TWO32 + lo;
    }
    sfixed64(): number {
        this.need(8);
        const lo = this.view.getUint32(this.pos, true);
        const hi = this.view.getInt32(this.pos + 4, true);
        this.pos += 8;
        const v = hi * TWO32 + lo;
        if (!Number.isSafeInteger(v)) throw new RangeError('sfixed64 does not fit in a number');
        return v;
    }
    float(): number { this.need(4); const v = this.view.getFloat32(this.pos, true); this.pos += 4; return v; }
    double(): number { this.need(8); const v = this.view.getFloat64(this.pos, true); this.pos += 8; return v; }

    /** Reads a length and returns where that many bytes end. */
    end(): number {
        const n = this.uint32();
        this.need(n);
        return this.pos + n;
    }
    bytes(): Uint8Array { const end = this.end(); const v = this.buf.slice(this.pos, end); this.pos = end; return v; }
    string(): string { const end = this.end(); const v = utf8Decoder.decode(this.buf.subarray(this.pos, end)); this.pos = end; return v; }

    skip(wireType: number): void {
        switch (wireType) {
            case 0: this.varint64(); break;
            case 1: this.need(8); this.pos += 8; break;
            case 2: this.pos = this.end(); break;
            case 5: this.need(4); this.pos += 4; break;
            default: throw new RangeError('unsupported protobuf wire type ' + wireType);
        }
    }
}
`;