Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions editor/build-scripts/unused-assets.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -278,6 +286,26 @@ function analyze(root, options = {}) {
else decide(rel, ruled(rel, keyOf(rel).slice('effects/'.length)));
}

// BGM plans (AudioManager.playBgmPlan): audio/<folder>/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;
Expand Down
49 changes: 49 additions & 0 deletions editor/tests/unused-assets.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading