Ame
feat(integration): add merged EU4 content and localization
37a34fd
Raw
History Blame Contribute Delete
3.2 kB
// Top-level entry point: wires codec + lexer + ast + node + edit + overlay
// together into the `load({ game, mod })` API. This is the only module that
// constructs Node instances from real files, since it's the one place that
// has both the parsed tree (ast.mjs) and the per-file EditSession (edit.mjs)
// needed to back a writable Node.
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';
/**
* @param {{ game: string, mod: string }} roots
*/
export async function load({ game, mod }) {
const sessions = new Map(); // absolute path -> EditSession
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,
});
}
// loadDir hits the filesystem, so dir() is async: `await dir(relPath)`
// returns a collection with the same get/all/where API for every
// directory, per overlay.mjs's makeCollection.
//
// Cached per relPath: callers legitimately ask for the same directory more
// than once (each registry and each rule pass does), and without the cache
// every call re-walked the filesystem and re-pushed the same ambiguities,
// so a single real collision got reported once per caller.
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;
}
}