Skip to content

Commit

Permalink
fix: Errors resulting from identical TPS file names in different dir (#…
Browse files Browse the repository at this point in the history
…111)

* fix: Errors resulting from identical TPS file names in different dir

* check dir with rootTransformAsset

* add test : samename tps in different directories

---------

Co-authored-by: h1ve2 <[email protected]>
  • Loading branch information
h1ve2 and h1ve2 authored Feb 5, 2025
1 parent 39e511b commit d936c10
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ export function texturePackerCacheBuster(): AssetPipe<any, 'tps'>

const texture = json.meta.image;

const textureAssets = findAssets((asset) =>
asset.filename === texture, asset, true);
const textureAssets = findAssets((assetObj) =>
assetObj.filename === texture && jsonAsset.rootTransformAsset.directory === assetObj.rootTransformAsset.directory,
asset, true);

// last transformed child is the renamed texture
const cacheBustedTexture = textureAssets[0].getFinalTransformedChildren()[0];
Expand All @@ -72,7 +73,8 @@ export function texturePackerCacheBuster(): AssetPipe<any, 'tps'>
json.meta.related_multi_packs = (json.meta.related_multi_packs as string[]).map((pack) =>
{
const foundAssets = findAssets((asset) =>
asset.filename === pack, asset, true);
asset.filename === pack && jsonAsset.rootTransformAsset.directory === asset.rootTransformAsset.directory,
asset, true);

return foundAssets[0].getFinalTransformedChildren()[0].filename;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs-extra';
import { glob } from 'glob';
import path from 'path';
import { describe, expect, it } from 'vitest';
import { cacheBuster } from '../../src/cache-buster/index.js';
import { AssetPack } from '../../src/core/index.js';
Expand All @@ -8,7 +9,9 @@ import { texturePacker } from '../../src/texture-packer/texturePacker.js';
import { texturePackerCacheBuster } from '../../src/texture-packer/texturePackerCacheBuster.js';
import { texturePackerCompress } from '../../src/texture-packer/texturePackerCompress.js';
import { createTPSFolder } from '../utils/createTPSFolder.js';
import { getCacheDir, getInputDir, getOutputDir } from '../utils/index.js';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../utils/index.js';

import type { File } from '../utils/index.js';

const pkg = 'texture-packer';

Expand Down Expand Up @@ -133,4 +136,164 @@ describe('Texture Packer Cache Buster', () =>
}
});
});

it('should create same name sprites sheet in different directories and correctly update json', async () =>
{
const testName = 'tp-cache-bust-samename';
const inputDir = getInputDir(pkg, testName);
const outputDir = getOutputDir(pkg, testName);

const spritesSingleA: File[] = [];
const spritesSingleB: File[] = [];
const spritesSplitA: File[] = [];
const spritesSplitB: File[] = [];

for (let i = 0; i < 5; i++)
{
spritesSingleA.push({
name: `sprite${i}.png`,
content: assetPath(`image/sp-${i + 1}.png`),
});
}
for (let i = 5; i < 10; i++)
{
spritesSingleB.push({
name: `sprite${i}.png`,
content: assetPath(`image/sp-${i + 1}.png`),
});
}

for (let i = 0; i < 9; i++)
{
spritesSplitA.push({
name: `sprite${i}.png`,
content: assetPath(`image/sp-${i + 1}.png`),
});
}
for (let i = 1; i < 10; i++)
{
spritesSplitB.push({
name: `sprite${i}.png`,
content: assetPath(`image/sp-${i + 1}.png`),
});
}

createFolder(
pkg,
{
name: testName,
files: [],
folders: [
{
name: 'a',
files: [],
folders: [
{
name: 'single{tps}',
files: spritesSingleA,
folders: [],
},
{
name: 'split{tps}',
files: spritesSplitA,
folders: [],
},
],
},
{
name: 'b',
files: [],
folders: [
{
name: 'single{tps}',
files: spritesSingleB,
folders: [],
},
{
name: 'split{tps}',
files: spritesSplitB,
folders: [],
},
],
},
],
});

const assetpack = new AssetPack({
entry: inputDir,
cacheLocation: getCacheDir(pkg, testName),
output: outputDir,
cache: false,
pipes: [
texturePacker({
resolutionOptions: {
resolutions: { default: 1 },
maximumTextureSize: 512,
},
}),
cacheBuster(),
texturePackerCacheBuster(),
],
});

await assetpack.run();

const aPath = path.join(outputDir, 'a', `*.{json,png}`)
.replaceAll('\\', '/');
const aFiles = await glob(aPath);

const bPath = path.join(outputDir, 'b', `*.{json,png}`)
.replaceAll('\\', '/');
const bFiles = await glob(bPath);

expect(aFiles.length)
.toBe(6);
expect(aFiles.filter((file) => file.endsWith('.json')).length)
.toBe(3);
expect(aFiles.filter((file) => file.endsWith('.png')).length)
.toBe(3);

expect(bFiles.length)
.toBe(6);
expect(bFiles.filter((file) => file.endsWith('.json')).length)
.toBe(3);
expect(bFiles.filter((file) => file.endsWith('.png')).length)
.toBe(3);

// check that the files are correct
checkMutiPacks(aFiles, path.join(outputDir, 'a'));
checkMutiPacks(bFiles, path.join(outputDir, 'b'));
});
});

/**
* check json image&relatedMultiPacks
* @param files
* @param outputDir
*/
function checkMutiPacks(files: string[], outputDir: string): boolean
{
const jsonFiles = files.filter((file) => file.endsWith('.json'));
const pngFiles = files.filter((file) => file.endsWith('.png'));

jsonFiles.forEach((jsonFile) =>
{
const rawJson = fs.readJSONSync(jsonFile);

expect(pngFiles.includes(path.join(outputDir, rawJson.meta.image)))
.toBe(true);

// check if json has related_multi_packs
if (rawJson.meta.related_multi_packs && rawJson.meta.related_multi_packs.length > 0)
{
const relatedMultiPacks = rawJson.meta.related_multi_packs as string[];

expect(relatedMultiPacks.length)
.toBe(1);
expect(jsonFiles.includes(path.join(outputDir, relatedMultiPacks[0])))
.toBe(true);
}
});

return false;
}

0 comments on commit d936c10

Please sign in to comment.