feat: add TLK string preview lookup for NWScript editor
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
import fs from "fs/promises";
|
||||||
|
|
||||||
|
const CUSTOM_TLK_OFFSET = 16777216;
|
||||||
|
const tlkStrings = new Map<number, string>();
|
||||||
|
|
||||||
|
export async function loadTlkIndex(tlkJsonPath: string): Promise<void> {
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
deleteFile,
|
deleteFile,
|
||||||
} from "../services/editor.service.js";
|
} from "../services/editor.service.js";
|
||||||
import { lookupResref, getResrefCount } from "../nwscript/resref-index.js";
|
import { lookupResref, getResrefCount } from "../nwscript/resref-index.js";
|
||||||
|
import { lookupTlk, getTlkCount } from "../nwscript/tlk-index.js";
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
@@ -62,4 +63,19 @@ router.get("/resref-count", (_req, res) => {
|
|||||||
res.json({ count: getResrefCount() });
|
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;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user