| |
| |
| |
| |
| |
|
|
| import { tokenize } from './lexer.mjs'; |
| import { parse } from './ast.mjs'; |
| import { readGameText } from './codec.mjs'; |
| import { Node } from './node.mjs'; |
| import { EditSession } from './edit.mjs'; |
| import { loadDir, makeCollection } from './overlay.mjs'; |
|
|
| |
| |
| |
| export async function load({ game, mod }) { |
| const sessions = new Map(); |
| const ambiguities = []; |
|
|
| function getOrCreateSession(path, text, tokens, meta) { |
| let session = sessions.get(path); |
| if (!session) { |
| session = new EditSession(path, text, tokens, meta); |
| sessions.set(path, session); |
| } |
| return session; |
| } |
|
|
| async function buildNode(entry) { |
| const { text, bom, encoding } = await readGameText(entry.path); |
| const tokens = tokenize(text); |
| const tree = parse(tokens); |
| const session = getOrCreateSession(entry.path, text, tokens, { bom, encoding }); |
| return new Node({ |
| items: tree.items, |
| tokens, |
| text, |
| path: entry.path, |
| layer: entry.layer, |
| session, |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| const dirCache = new Map(); |
|
|
| async function dir(relPath) { |
| let pending = dirCache.get(relPath); |
| if (!pending) { |
| pending = loadDir({ gameRoot: game, modRoot: mod, relPath }).then((result) => { |
| ambiguities.push(...result.ambiguities); |
| return makeCollection(result.entries, buildNode); |
| }); |
| dirCache.set(relPath, pending); |
| } |
| return pending; |
| } |
|
|
| async function file(relPath) { |
| const modPath = joinPath(mod, relPath); |
| const gamePath = joinPath(game, relPath); |
| const path = await pathExists(modPath) ? modPath : gamePath; |
| const layer = path === modPath ? 'mod' : 'vanilla'; |
| return buildNode({ path, layer }); |
| } |
|
|
| async function save() { |
| for (const session of sessions.values()) { |
| if (session.dirty) { |
| await session.save(); |
| } |
| } |
| } |
|
|
| return { |
| dir, |
| file, |
| save, |
| get ambiguities() { |
| return ambiguities; |
| }, |
| }; |
| } |
|
|
| function joinPath(root, relPath) { |
| const normalized = relPath.replace(/\\/g, '/'); |
| return `${root.replace(/\\/g, '/').replace(/\/+$/, '')}/${normalized}`; |
| } |
|
|
| async function pathExists(path) { |
| try { |
| const { stat } = await import('node:fs/promises'); |
| await stat(path); |
| return true; |
| } catch { |
| return false; |
| } |
| } |
|
|