Verify battle map asset references

This commit is contained in:
2026-07-05 08:02:49 +09:00
parent cc28f56da6
commit 21d5ad397f
3 changed files with 210 additions and 0 deletions

View File

@@ -0,0 +1,208 @@
import { readdirSync, readFileSync, statSync } from 'node:fs';
import { join, sep } from 'node:path';
import { createServer } from 'vite';
const battleMapDir = join('src', 'assets', 'images', 'battle');
const battleMapAssetsSourcePath = join('src', 'game', 'data', 'battleMapAssets.ts');
const server = await createServer({
logLevel: 'error',
server: { middlewareMode: true },
appType: 'custom'
});
try {
const { battleScenarios } = await server.ssrLoadModule('/src/game/data/battles.ts');
const { battleMapAssets } = await server.ssrLoadModule('/src/game/data/battleMapAssets.ts');
const errors = [];
const staticBattleMapAssets = parseBattleMapAssetSource(readFileSync(battleMapAssetsSourcePath, 'utf8'), errors);
validateRuntimeMatchesSource(errors, battleMapAssets, staticBattleMapAssets);
validateScenarioMapKeys(errors, battleScenarios, battleMapAssets);
validateMapAssetFiles(errors, battleMapAssets, staticBattleMapAssets);
validateBattleDirectoryCoverage(errors, staticBattleMapAssets);
if (errors.length) {
console.error(`Battle map asset verification failed with ${errors.length} issue(s):`);
errors.slice(0, 100).forEach((error) => console.error(`- ${error}`));
if (errors.length > 100) {
console.error(`- ...and ${errors.length - 100} more`);
}
process.exitCode = 1;
} else {
console.log(
`Verified ${Object.keys(battleScenarios).length} scenario map keys and ` +
`${Object.keys(battleMapAssets).length} battle map assets.`
);
}
} finally {
await server.close();
}
function validateScenarioMapKeys(errors, battleScenarios, battleMapAssets) {
const scenarioMapKeys = new Set();
Object.entries(battleScenarios)
.sort(([left], [right]) => left.localeCompare(right))
.forEach(([scenarioId, scenario]) => {
assertNonEmptyString(errors, scenario.mapTextureKey, `${scenarioId}.mapTextureKey`);
scenarioMapKeys.add(scenario.mapTextureKey);
if (!battleMapAssets[scenario.mapTextureKey]) {
errors.push(`${scenarioId}: mapTextureKey "${scenario.mapTextureKey}" is not registered in battleMapAssets`);
}
});
Object.keys(battleMapAssets)
.sort()
.forEach((mapKey) => {
if (!scenarioMapKeys.has(mapKey)) {
errors.push(`battleMapAssets contains unused map key "${mapKey}"`);
}
});
}
function validateRuntimeMatchesSource(errors, battleMapAssets, staticBattleMapAssets) {
Object.keys(battleMapAssets)
.sort()
.forEach((mapKey) => {
if (!staticBattleMapAssets[mapKey]) {
errors.push(`runtime battleMapAssets contains "${mapKey}" but the source manifest has no matching import`);
}
});
Object.keys(staticBattleMapAssets)
.sort()
.forEach((mapKey) => {
if (!battleMapAssets[mapKey]) {
errors.push(`source battleMapAssets contains "${mapKey}" but runtime export is missing it`);
}
});
}
function validateMapAssetFiles(errors, battleMapAssets, staticBattleMapAssets) {
Object.entries(staticBattleMapAssets)
.sort(([left], [right]) => left.localeCompare(right))
.forEach(([mapKey, path]) => {
validateMapFile(errors, mapKey, path);
const url = battleMapAssets[mapKey];
assertNonEmptyString(errors, url, `${mapKey} runtime asset URL`);
if (typeof url !== 'string') {
return;
}
if (url.startsWith('data:')) {
if (!path.toLowerCase().endsWith('.svg') || !url.startsWith('data:image/svg+xml')) {
errors.push(`${mapKey}: runtime data URL does not match source asset type for "${path}"`);
}
return;
}
const runtimePath = assetUrlToPath(url);
if (!runtimePath) {
errors.push(`${mapKey}: URL "${url}" does not point at src/assets/images/battle`);
} else if (runtimePath !== path) {
errors.push(`${mapKey}: runtime URL points at "${runtimePath}" but source imports "${path}"`);
}
});
}
function validateBattleDirectoryCoverage(errors, staticBattleMapAssets) {
const registeredPaths = new Set(Object.values(staticBattleMapAssets));
readdirSync(battleMapDir, { withFileTypes: true })
.filter((entry) => entry.isFile() && /\.(svg|webp)$/i.test(entry.name))
.map((entry) => join(battleMapDir, entry.name))
.sort()
.forEach((path) => {
if (!registeredPaths.has(path)) {
errors.push(`battle map file "${path}" exists but is not registered in battleMapAssets`);
}
});
}
function parseBattleMapAssetSource(source, errors) {
const importsByName = new Map();
const importPattern = /import\s+(\w+)\s+from\s+['"]\.\.\/\.\.\/assets\/images\/battle\/([^'"]+)['"];/g;
for (const match of source.matchAll(importPattern)) {
importsByName.set(match[1], join(battleMapDir, match[2]));
}
const manifestMatch = source.match(/export\s+const\s+battleMapAssets:\s*Record<string,\s*string>\s*=\s*{([\s\S]*?)\n};/);
if (!manifestMatch) {
errors.push(`${battleMapAssetsSourcePath}: cannot find battleMapAssets manifest object`);
return {};
}
const assets = {};
const entryPattern = /['"]([^'"]+)['"]\s*:\s*(\w+)/g;
for (const match of manifestMatch[1].matchAll(entryPattern)) {
const [, mapKey, importName] = match;
const path = importsByName.get(importName);
if (!path) {
errors.push(`${mapKey}: source manifest references "${importName}" without a matching battle map import`);
continue;
}
assets[mapKey] = path;
}
importsByName.forEach((path, importName) => {
if (!Object.values(assets).includes(path)) {
errors.push(`${battleMapAssetsSourcePath}: import "${importName}" from "${path}" is not used in battleMapAssets`);
}
});
return assets;
}
function validateMapFile(errors, context, path) {
let stats;
let bytes;
try {
stats = statSync(path);
bytes = readFileSync(path);
} catch (error) {
errors.push(`${context}: cannot read "${path}": ${error.message}`);
return;
}
if (stats.size < 1024) {
errors.push(`${context}: "${path}" is too small to be a complete battle map asset`);
}
if (path.toLowerCase().endsWith('.svg')) {
const text = bytes.toString('utf8', 0, Math.min(bytes.length, 2048));
if (!text.includes('<svg')) {
errors.push(`${context}: "${path}" does not look like an SVG`);
}
if (!/viewBox=["'][^"']+["']/.test(text) && !/width=["'][^"']+["']/.test(text)) {
errors.push(`${context}: "${path}" should declare SVG dimensions or a viewBox`);
}
return;
}
if (path.toLowerCase().endsWith('.webp')) {
if (
bytes.subarray(0, 4).toString('ascii') !== 'RIFF' ||
bytes.subarray(8, 12).toString('ascii') !== 'WEBP'
) {
errors.push(`${context}: "${path}" does not have a WebP RIFF header`);
}
return;
}
errors.push(`${context}: "${path}" uses an unsupported map asset extension`);
}
function assetUrlToPath(url) {
const marker = 'src/assets/images/battle/';
const index = url.indexOf(marker);
if (index < 0) {
return undefined;
}
return url.slice(index).replaceAll('/', sep);
}
function assertNonEmptyString(errors, value, context) {
if (typeof value !== 'string' || value.trim().length === 0) {
errors.push(`${context} must be a non-empty string`);
}
}