From 871c6a0d00c954283bf163fade159a5e846e3205 Mon Sep 17 00:00:00 2001 From: dgali Date: Sun, 29 Nov 2026 22:18:09 +0000 Subject: [PATCH] Exclude unused files: a BGM plan ships with the files it names A plan (audio//plan.json, played by AudioManager.playBgmPlan) is the only thing in a project that names its pieces, and the pruner never read it, so a deployment with Exclude unused files left every piece out. The plan itself usually stayed, because "plan" is a word somewhere in any project: a build that ships a plan without its files and stops on Retry when it plays. analyze() now reads each audio/**/plan.json shaped like a plan. The plan ships when its folder would (named in the project, Always include, a plugin listing it, a playtest loading the plan or any of its files), and then every file it names ships too; a path out of the plan's folder keeps nothing. A plan nothing keeps is left out whole. Reasons reuse the existing ones, so no new strings. Co-Authored-By: Claude Opus 5.5 --- editor/build-scripts/unused-assets.cjs | 28 +++++++++++++++ editor/tests/unused-assets.test.cjs | 49 ++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/editor/build-scripts/unused-assets.cjs b/editor/build-scripts/unused-assets.cjs index f3ad685d0..8281aa9c8 100644 --- a/editor/build-scripts/unused-assets.cjs +++ b/editor/build-scripts/unused-assets.cjs @@ -99,6 +99,14 @@ function effectResources(file) { return found; } +/** The file paths a BGM plan (plan.json) plays, relative to its folder; null when the file is not a plan. */ +function planFiles(file) { + let plan; + try { plan = JSON.parse(fs.readFileSync(file, 'utf8')); } catch (_) { return null; } + if (!plan || typeof plan.files !== 'object' || !plan.files || !Array.isArray(plan.clips)) return null; + return Object.values(plan.files).filter(info => info && typeof info.path === 'string' && info.path).map(info => info.path.replace(/\\/g, '/')); +} + /** * Every fixed start code builds names from: "Face_" + id, 'Battler' + n, `Face_${id}`, "Face_%1", "Face_{id}". * At least three characters, so a short start ("rr", "c") doesn't keep half a folder. @@ -278,6 +286,26 @@ function analyze(root, options = {}) { else decide(rel, ruled(rel, keyOf(rel).slice('effects/'.length))); } + // BGM plans (AudioManager.playBgmPlan): audio//plan.json names the files it plays, relative to + // itself, and nothing else in the project does. A plan is played by its folder, so it ships when its folder + // would: named in the project (the script call that plays it), on the Always include list, listed by a + // plugin, or loaded in a playtest (the plan or any of its files). Then every file it names ships with it, + // and the files beside it that it does not name (a reference mix, say) are judged as usual. A plan + // nothing keeps is left out whole, never shipped without the files it needs. + for (const rel of files.filter(r => /^audio\/(.+\/)?plan\.json$/i.test(r))) { + const pieces = planFiles(path.join(root, rel)); + if (!pieces) continue; + const folder = path.posix.dirname(rel); + const named = pieces.map(p => path.posix.normalize(folder + '/' + p)).filter(p => p.startsWith(folder + '/')); + // By the folder's name: the file's own, "plan", is a word any text holds. + let reason = ruled(rel, folder.slice('audio/'.length).toLowerCase()); + if (!reason && named.some(p => played.has(keyOf(p)))) reason = 'loaded in a playtest'; + decide(rel, reason); + if (!reason) continue; + const wanted = new Set(named.map(keyOf)); + for (const other of files) if (other.startsWith(folder + '/') && wanted.has(keyOf(other))) decide(other, 'named in the project'); + } + // Everything else, by name: folder/name for a picture in a subfolder of its folder. for (const rel of files) { if (verdict.has(rel)) continue; diff --git a/editor/tests/unused-assets.test.cjs b/editor/tests/unused-assets.test.cjs index 782285e00..edafa1b02 100644 --- a/editor/tests/unused-assets.test.cjs +++ b/editor/tests/unused-assets.test.cjs @@ -138,6 +138,55 @@ test('a plugin that lists a folder keeps the folder whole', t => { assert.match(a.files.find(f => f.path === 'audio/bgm/Bonus.ogg').reason, /lists/); }); +test('a BGM plan ships with the files it names when anything keeps its folder, and is left out whole when nothing does', t => { + const { root, put } = project(t); + const plan = files => JSON.stringify({ format: 'reactor/music-plan', version: 1, rate: 48000, length: 10, loop: null, clips: [], + files: Object.fromEntries(files.map((p, i) => ['f' + i, { path: p, frames: 10, channels: 2, rate: 48000, loop: null }])) }); + // Played by a script call; the plan's files are named only inside it. + put('data/CommonEvents.json', JSON.stringify([null, { list: [{ code: 355, parameters: ['AudioManager.playBgmPlan("plans/forest");'] }] }])); + put('audio/plans/forest/plan.json', plan(['pieces/Opening.wav', 'pieces\\Loop A.wav', '../../bgm/Bonus.ogg'])); + put('audio/plans/forest/pieces/Opening.wav'); + put('audio/plans/forest/pieces/Loop A.wav'); + put('audio/plans/forest/pieces/Spare.wav'); + put('audio/plans/forest/reference.wav'); + // Nothing plays it. + put('audio/plans/cave/plan.json', plan(['pieces/Drip.wav'])); + put('audio/plans/cave/pieces/Drip.wav'); + // Only a playtest reached it, through one of its files. + put('audio/plans/hall/plan.json', plan(['Echo.wav'])); + put('audio/plans/hall/Echo.wav'); + put(Unused.USAGE_FILE, JSON.stringify({ files: ['audio/plans/hall/Echo.wav'] })); + // A plan.json that is not a plan is judged like any other file. + put('audio/bgm/plan.json', '{"not": "a plan"}'); + + const a = Unused.analyze(root); + assert.equal(used(a, 'audio/plans/forest/plan.json'), true); + assert.equal(used(a, 'audio/plans/forest/pieces/Opening.wav'), true); + assert.equal(used(a, 'audio/plans/forest/pieces/Loop A.wav'), true, 'a Windows separator in the plan'); + assert.equal(used(a, 'audio/plans/forest/pieces/Spare.wav'), false, 'beside the plan but not in it'); + assert.equal(used(a, 'audio/plans/forest/reference.wav'), false); + assert.equal(used(a, 'audio/bgm/Bonus.ogg'), false, 'a path out of the plan\'s folder keeps nothing'); + assert.equal(used(a, 'audio/plans/cave/plan.json'), false, 'a plan nothing plays'); + assert.equal(used(a, 'audio/plans/cave/pieces/Drip.wav'), false, 'and its files with it'); + assert.equal(used(a, 'audio/plans/hall/plan.json'), true, 'a playtest played one of its files'); + assert.equal(used(a, 'audio/plans/hall/Echo.wav'), true); + assert.match(a.files.find(f => f.path === 'audio/bgm/plan.json').reason, /nothing/, 'not a plan: judged by name like any other file'); + + Unused.writeSettings(root, { keep: ['audio/plans/cave'] }); + const kept = Unused.analyze(root); + assert.equal(used(kept, 'audio/plans/cave/pieces/Drip.wav'), true, 'Always include keeps a plan\'s folder'); + + // An encrypted build: the plan's files are .wav_; the plan itself stays plain. + const copy = fs.mkdtempSync(path.join(os.tmpdir(), 'rr-unused-plan-')); + t.after(() => fs.rmSync(copy, { recursive: true, force: true })); + fs.cpSync(root, copy, { recursive: true }); + fs.renameSync(path.join(copy, 'audio/plans/forest/pieces/Opening.wav'), path.join(copy, 'audio/plans/forest/pieces/Opening.wav_')); + Unused.prune(copy, a); + assert.equal(fs.existsSync(path.join(copy, 'audio/plans/forest/pieces/Opening.wav_')), true); + assert.equal(fs.existsSync(path.join(copy, 'audio/plans/forest/plan.json')), true); + assert.equal(fs.existsSync(path.join(copy, 'audio/plans/cave')), false, 'an unplayed plan goes, folder and all'); +}); + test('pruning a copy removes the same files encrypted or converted, and the folders it empties; data and code stay', t => { const { root } = project(t); const a = Unused.analyze(root);