From 99d0c4f1e94e1e8096779f24c1b84d801d4d97d3 Mon Sep 17 00:00:00 2001 From: plenarius Date: Mon, 20 Apr 2026 19:11:32 -0400 Subject: [PATCH] feat: add TLK string preview lookup for NWScript editor --- packages/backend/src/nwscript/tlk-index.ts | 32 ++++++++++++++++++++++ packages/backend/src/routes/editor.ts | 16 +++++++++++ 2 files changed, 48 insertions(+) create mode 100644 packages/backend/src/nwscript/tlk-index.ts diff --git a/packages/backend/src/nwscript/tlk-index.ts b/packages/backend/src/nwscript/tlk-index.ts new file mode 100644 index 0000000..cecd7ad --- /dev/null +++ b/packages/backend/src/nwscript/tlk-index.ts @@ -0,0 +1,32 @@ +import fs from "fs/promises"; + +const CUSTOM_TLK_OFFSET = 16777216; +const tlkStrings = new Map(); + +export async function loadTlkIndex(tlkJsonPath: string): Promise { + tlkStrings.clear(); + try { + const raw = await fs.readFile(tlkJsonPath, "utf-8"); + const data = JSON.parse(raw); + if (Array.isArray(data)) { + for (const entry of data) { + if (entry.id !== undefined && entry.value !== undefined) { + tlkStrings.set(Number(entry.id), String(entry.value)); + } + } + } + } catch { + // TLK file not available yet + } +} + +export function lookupTlk(id: number): string | undefined { + if (id >= CUSTOM_TLK_OFFSET) { + return tlkStrings.get(id - CUSTOM_TLK_OFFSET); + } + return tlkStrings.get(id); +} + +export function getTlkCount(): number { + return tlkStrings.size; +} diff --git a/packages/backend/src/routes/editor.ts b/packages/backend/src/routes/editor.ts index cb65c29..30a4eb3 100644 --- a/packages/backend/src/routes/editor.ts +++ b/packages/backend/src/routes/editor.ts @@ -6,6 +6,7 @@ import { deleteFile, } from "../services/editor.service.js"; import { lookupResref, getResrefCount } from "../nwscript/resref-index.js"; +import { lookupTlk, getTlkCount } from "../nwscript/tlk-index.js"; const router = Router(); @@ -62,4 +63,19 @@ router.get("/resref-count", (_req, res) => { res.json({ count: getResrefCount() }); }); +router.get("/tlk/:id", (req, res) => { + const id = parseInt(req.params.id, 10); + if (isNaN(id)) return res.status(400).json({ error: "invalid id" }); + const text = lookupTlk(id); + if (text !== undefined) { + res.json({ id, text }); + } else { + res.status(404).json({ error: "TLK entry not found" }); + } +}); + +router.get("/tlk-count", (_req, res) => { + res.json({ count: getTlkCount() }); +}); + export default router;