import { resolveGamedataFile } from "@/lib/services/import/core/asset-paths"; import { readGamedataJson, withGamedataLock, writeGamedataJsonAtomic, } from "@/lib/services/import/core/gamedata-json"; import { siteSettings } from "@/lib/services/site-settings"; interface FdSet { id: number; [k: string]: unknown; } interface FdSetType { type: string; paletteId: number; sets: FdSet[]; [k: string]: unknown; } interface FdPalette { id: number; colors: unknown[]; } interface FigureDataFile { palettes: FdPalette[]; setTypes: FdSetType[]; } let pathOverride: string | null = null; export function __setFigureDataPathForTests(p: string | null): void { pathOverride = p; } export async function getFigureDataPath(): Promise { if (pathOverride) return pathOverride; const url = await siteSettings.get("figuredata_url", "/nitro-assets/gamedata/FigureData.json"); return resolveGamedataFile(url ?? "/nitro-assets/gamedata/FigureData.json", "FigureData.json"); } export async function readFigureData(): Promise { const data = await readGamedataJson>(await getFigureDataPath()); return { palettes: data.palettes ?? [], setTypes: data.setTypes ?? [] }; } export async function listLocalSetIds(setType: string): Promise> { const fd = await readFigureData(); const st = fd.setTypes.find((s) => s.type === setType); return new Set((st?.sets ?? []).map((s) => s.id)); } export async function mergeSet(setType: string, paletteId: number, set: FdSet): Promise { const file = await getFigureDataPath(); await withGamedataLock(file, async () => { const fd = await readGamedataJson(file); fd.setTypes ??= []; let st = fd.setTypes.find((s) => s.type === setType); if (!st) { st = { type: setType, paletteId, sets: [] }; fd.setTypes.push(st); } st.sets ??= []; const idx = st.sets.findIndex((s) => s.id === set.id); if (idx >= 0) { st.sets[idx] = set; } else { st.sets.push(set); } await writeGamedataJsonAtomic(file, fd); }); } export async function mergePalette(palette: FdPalette): Promise { const file = await getFigureDataPath(); await withGamedataLock(file, async () => { const fd = await readGamedataJson(file); fd.palettes ??= []; if (!fd.palettes.some((p) => p.id === palette.id)) { fd.palettes.push(palette); await writeGamedataJsonAtomic(file, fd); } }); }