Created
August 25, 2024 15:32
-
-
Save anatawa12/708430f892dd910bc72557e4655df02f to your computer and use it in GitHub Desktop.
This script removes all entries in the unity asset file that are not needed internally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/* | |
* This script removes all entries in the asset file that are not needed. | |
* Please note that this doesn't consider external references. | |
*/ | |
const fs = require('fs'); | |
const path = process.argv[2]; | |
const data = fs.readFileSync(path, 'utf8'); | |
const split = data.split(/(?<=\n)(?=---)/g); | |
const heading = split[0]; | |
const body = split.slice(1).map(parseBody); | |
const map = new Map(body.map(x => [x.id, x])); | |
const queue = body.filter(x => x.need).map(x => x.id); | |
while (queue.length) { | |
const id = queue.shift(); | |
const entry = map.get(id); | |
entry.dependencies.forEach(dep => { | |
const depEntry = map.get(dep); | |
if (!depEntry.need) { | |
depEntry.need = true; | |
queue.push(dep); | |
} | |
}); | |
} | |
const result = heading + body.filter(x => x.need).map(x => x.body).join(''); | |
fs.writeFileSync(path, result, 'utf8'); | |
/** | |
* | |
* @param {string} part | |
*/ | |
function parseBody(body) { | |
const matched = body.match(/^--- .*&(-?\d+)/); | |
const dependenciesList = [...body.matchAll(/\{fileID: (-?\d+)\}/g)].map(match => match[1]); | |
const dependencies = [...new Set(dependenciesList.filter(x => x != '0'))]; | |
const need = /m_ObjectHideFlags: 0/.test(body); | |
return { | |
id: matched[1], | |
body, | |
dependencies, | |
need, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment