|
| 1 | +/** |
| 2 | + * Build-time asset generators — shared between Vite plugin, extension host, |
| 3 | + * and future standalone backends. |
| 4 | + * |
| 5 | + * Reads furniture manifests and asset directories and produces |
| 6 | + * catalog and index structures. |
| 7 | + */ |
| 8 | + |
| 9 | +import * as fs from 'fs'; |
| 10 | +import * as path from 'path'; |
| 11 | + |
| 12 | +import type { CatalogEntry } from './types.js'; |
| 13 | +import type { FurnitureManifest, InheritedProps, ManifestGroup } from './manifestUtils.js'; |
| 14 | +import { flattenManifest } from './manifestUtils.js'; |
| 15 | + |
| 16 | +// ── Furniture catalog ───────────────────────────────────────────────────────── |
| 17 | + |
| 18 | +export function buildFurnitureCatalog(assetsDir: string): CatalogEntry[] { |
| 19 | + const furnitureDir = path.join(assetsDir, 'furniture'); |
| 20 | + if (!fs.existsSync(furnitureDir)) return []; |
| 21 | + |
| 22 | + const catalog: CatalogEntry[] = []; |
| 23 | + const dirs = fs |
| 24 | + .readdirSync(furnitureDir, { withFileTypes: true }) |
| 25 | + .filter((e) => e.isDirectory()) |
| 26 | + .map((e) => e.name) |
| 27 | + .sort(); |
| 28 | + |
| 29 | + for (const folderName of dirs) { |
| 30 | + const manifestPath = path.join(furnitureDir, folderName, 'manifest.json'); |
| 31 | + if (!fs.existsSync(manifestPath)) continue; |
| 32 | + try { |
| 33 | + const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8')) as FurnitureManifest; |
| 34 | + |
| 35 | + if (manifest.type === 'asset') { |
| 36 | + // Single-asset manifest — validate required fields |
| 37 | + if ( |
| 38 | + manifest.width == null || |
| 39 | + manifest.height == null || |
| 40 | + manifest.footprintW == null || |
| 41 | + manifest.footprintH == null |
| 42 | + ) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + const file = manifest.file ?? `${manifest.id}.png`; |
| 46 | + catalog.push({ |
| 47 | + id: manifest.id, |
| 48 | + name: manifest.name, |
| 49 | + label: manifest.name, |
| 50 | + category: manifest.category, |
| 51 | + file, |
| 52 | + furniturePath: `furniture/${folderName}/${file}`, |
| 53 | + width: manifest.width, |
| 54 | + height: manifest.height, |
| 55 | + footprintW: manifest.footprintW, |
| 56 | + footprintH: manifest.footprintH, |
| 57 | + isDesk: manifest.category === 'desks', |
| 58 | + canPlaceOnWalls: manifest.canPlaceOnWalls, |
| 59 | + canPlaceOnSurfaces: manifest.canPlaceOnSurfaces, |
| 60 | + backgroundTiles: manifest.backgroundTiles, |
| 61 | + groupId: manifest.id, |
| 62 | + }); |
| 63 | + } else { |
| 64 | + // Group manifest — flatten into individual assets |
| 65 | + if (!manifest.members) continue; |
| 66 | + const inherited: InheritedProps = { |
| 67 | + groupId: manifest.id, |
| 68 | + name: manifest.name, |
| 69 | + category: manifest.category, |
| 70 | + canPlaceOnWalls: manifest.canPlaceOnWalls, |
| 71 | + canPlaceOnSurfaces: manifest.canPlaceOnSurfaces, |
| 72 | + backgroundTiles: manifest.backgroundTiles, |
| 73 | + ...(manifest.rotationScheme ? { rotationScheme: manifest.rotationScheme } : {}), |
| 74 | + }; |
| 75 | + const rootGroup: ManifestGroup = { |
| 76 | + type: 'group', |
| 77 | + groupType: manifest.groupType as 'rotation' | 'state' | 'animation', |
| 78 | + rotationScheme: manifest.rotationScheme, |
| 79 | + members: manifest.members, |
| 80 | + }; |
| 81 | + const assets = flattenManifest(rootGroup, inherited); |
| 82 | + for (const asset of assets) { |
| 83 | + catalog.push({ |
| 84 | + ...asset, |
| 85 | + furniturePath: `furniture/${folderName}/${asset.file}`, |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + } catch { |
| 90 | + // skip malformed manifests |
| 91 | + } |
| 92 | + } |
| 93 | + return catalog; |
| 94 | +} |
| 95 | + |
| 96 | +// ── Asset index ─────────────────────────────────────────────────────────────── |
| 97 | + |
| 98 | +export function buildAssetIndex(assetsDir: string) { |
| 99 | + function listSorted(subdir: string, pattern: RegExp): string[] { |
| 100 | + const dir = path.join(assetsDir, subdir); |
| 101 | + if (!fs.existsSync(dir)) return []; |
| 102 | + return fs |
| 103 | + .readdirSync(dir) |
| 104 | + .filter((f) => pattern.test(f)) |
| 105 | + .sort((a, b) => { |
| 106 | + const na = parseInt(/(\d+)/.exec(a)?.[1] ?? '0', 10); |
| 107 | + const nb = parseInt(/(\d+)/.exec(b)?.[1] ?? '0', 10); |
| 108 | + return na - nb; |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + let defaultLayout: string | null = null; |
| 113 | + let bestRev = 0; |
| 114 | + if (fs.existsSync(assetsDir)) { |
| 115 | + for (const f of fs.readdirSync(assetsDir)) { |
| 116 | + const m = /^default-layout-(\d+)\.json$/.exec(f); |
| 117 | + if (m) { |
| 118 | + const rev = parseInt(m[1], 10); |
| 119 | + if (rev > bestRev) { |
| 120 | + bestRev = rev; |
| 121 | + defaultLayout = f; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + if (!defaultLayout && fs.existsSync(path.join(assetsDir, 'default-layout.json'))) { |
| 126 | + defaultLayout = 'default-layout.json'; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return { |
| 131 | + floors: listSorted('floors', /^floor_\d+\.png$/i), |
| 132 | + walls: listSorted('walls', /^wall_\d+\.png$/i), |
| 133 | + characters: listSorted('characters', /^char_\d+\.png$/i), |
| 134 | + defaultLayout, |
| 135 | + }; |
| 136 | +} |
0 commit comments