Skip to content

Commit

Permalink
add rule to detect unused textures in model files
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAfroOfDoom committed Jan 11, 2024
1 parent 55c2840 commit 4e0699a
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
89 changes: 89 additions & 0 deletions package-scripts/linting-rules/no-unused-textures-in-models.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const { readFileSync } = require('fs');
const { differenceBy, uniq } = require('lodash');

const name = 'no-unused-textures-in-models';
const applicableExtensions = ['.ajmodel'];

/**
* Iterates through each face of each cube (the model's default variant)
* and returns a (deduplicated) list of found textures
*/
const getDefaultVariantTextures = (ajmodel) => {
const defaultVariantTextureIdxs = [];
const cubes = ajmodel.elements.filter(({ type }) => type === 'cube');
for (const element of cubes) {
// side = 'north', 'south', etc.
for (const side of Object.keys(element.faces)) {
const textureIdx = element.faces[side].texture;
if (typeof textureIdx !== 'undefined') {
defaultVariantTextureIdxs.push(textureIdx);
}
}
}
// Deduplicate and convert from texture-index to texture object
const defaultTextures = uniq(defaultVariantTextureIdxs).map(
(idx) => ajmodel.textures[idx],
);

return defaultTextures;
};

/**
* Takes in an allowlist of texture UUIDs (ones that are used in the default variant),
* iterates through a model's variants, and returns a list of (target) textures from
* each variant's texture map whose source texture was found in the allowlist
*/
const getAllowedVariantTextures = (ajmodel, allowlist) => {
const allowedVariantTextures = [];
for (const variant of ajmodel.animated_java.variants) {
// Skip the default variant since that's what our allowlist consists of
if (variant.default) {
continue;
}
for (const [source, target] of Object.entries(variant.textureMap)) {
if (allowlist.includes(source)) {
allowedVariantTextures.push(
ajmodel.textures.find(({ uuid }) => uuid === target),
);
}
}
}
return uniq(allowedVariantTextures);
};

/** Errors for textures defined in a model file that aren't actually used by any cube's face (in any variant) */
const noUnusedTexturesInModels = (file) => {
// Return early if file does not match any applicable extension
if (applicableExtensions.every((extension) => !file.endsWith(extension))) {
return [];
}

const errors = [];

const ajmodel = JSON.parse(readFileSync(file, 'utf8'));

const defaultTextures = getDefaultVariantTextures(ajmodel);
const defaultTextureUuids = defaultTextures.map(({ uuid }) => uuid);
const variantTextures = getAllowedVariantTextures(
ajmodel,
defaultTextureUuids,
);

const allSeenTextures = uniq(defaultTextures.concat(variantTextures));
const unusedTextures = differenceBy(
ajmodel.textures,
allSeenTextures,
'uuid',
);
if (unusedTextures.length > 0) {
const unusedTextureNames = unusedTextures.map(({ name }) => name);
errors.push(`delete unused textures: [ ${unusedTextureNames.join(', ')} ]`);
}

return errors;
};

module.exports = {
function: noUnusedTexturesInModels,
name,
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"eslint": "8.56.0",
"glob": "10.3.10",
"image-size": "1.1.1",
"lodash": "4.17.21",
"minimatch": "9.0.3",
"minimist": "1.2.8",
"nps": "5.10.0",
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1840,7 +1840,7 @@ __metadata:
languageName: node
linkType: hard

"lodash@npm:^4.17.10, lodash@npm:^4.17.14, lodash@npm:^4.17.4, lodash@npm:^4.5.1, lodash@npm:^4.8.0":
"lodash@npm:4.17.21, lodash@npm:^4.17.10, lodash@npm:^4.17.14, lodash@npm:^4.17.4, lodash@npm:^4.5.1, lodash@npm:^4.8.0":
version: 4.17.21
resolution: "lodash@npm:4.17.21"
checksum: eb835a2e51d381e561e508ce932ea50a8e5a68f4ebdd771ea240d3048244a8d13658acbd502cd4829768c56f2e16bdd4340b9ea141297d472517b83868e677f7
Expand Down Expand Up @@ -2154,6 +2154,7 @@ __metadata:
eslint: 8.56.0
glob: 10.3.10
image-size: 1.1.1
lodash: 4.17.21
minimatch: 9.0.3
minimist: 1.2.8
nps: 5.10.0
Expand Down

0 comments on commit 4e0699a

Please sign in to comment.