Skip to content

Commit

Permalink
feat(manifest): update manifest plugin to remove specific logic (#16)
Browse files Browse the repository at this point in the history
* - add spine cache buster
- add sprite sheet cache buster
- tests

* cache id is now takes into account all pipe options
add test

* docs

* Update packages/spine/test/spineAtlasAll.test.ts

* remove only

* fix test and merge bits

* rename

* - update manifest plugin to remove specific logic
- add a manifest modifier for texture packer
- add a manifest modifier for spine atlas

* fix options

* fix options

* fin name

* fix tests

---------

Co-authored-by: Zyie <[email protected]>
  • Loading branch information
GoodBoyDigital and Zyie authored Apr 29, 2024
1 parent eece9ff commit ef71c06
Show file tree
Hide file tree
Showing 16 changed files with 9,119 additions and 4,307 deletions.
12,764 changes: 8,547 additions & 4,217 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ export * from './utils/merge';
export * from './utils/path';
export * from './utils/stripTags';
export * from './utils/swapExt';
export * from './utils/findAssetsWithFileName';
export * from './utils/findAssets';
5 changes: 5 additions & 0 deletions packages/core/src/pipes/PipeSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,10 @@ export class PipeSystem
}
}
}

getPipe(name: string): AssetPipe
{
return this.pipeHash[name];
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Asset } from '../Asset';

export function findAssetsWithFileName(
export function findAssets(
test: (asset: Asset) => boolean,
asset: Asset,
searchTransform: boolean,
Expand All @@ -16,14 +16,14 @@ export function findAssetsWithFileName(
{
const child = asset.children[i];

findAssetsWithFileName(test, child, searchTransform, out);
findAssets(test, child, searchTransform, out);
}

for (let i = 0; i < asset.transformChildren.length; i++)
{
const transformChild = asset.transformChildren[i];

findAssetsWithFileName(test, transformChild, searchTransform, out);
findAssets(test, transformChild, searchTransform, out);
}

return out;
Expand Down
87 changes: 10 additions & 77 deletions packages/manifest/src/pixiManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {
type Asset,
type AssetPipe,
type PipeSystem,
path,
findAssetsWithFileName
path
} from '@play-co/assetpack-core';

import fs from 'fs-extra';
Expand Down Expand Up @@ -78,8 +77,6 @@ export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe<Pixi
defaultOptions,
finish: async (asset: Asset, options, pipeSystem: PipeSystem) =>
{
removeAtlasTextures(asset);

const newFileName = path.dirname(options.output) === '.'
? path.joinSafe(pipeSystem.outputPath, options.output) : options.output;

Expand Down Expand Up @@ -128,34 +125,15 @@ function collectAssets(

if (asset.transformChildren.length > 0)
{
if (asset.metaData.tps)
{
// do some special think for textures packed sprite sheet pages..
getTexturePackedAssets(finalAssets).forEach((pages, pageIndex) =>
{
bundleAssets.push({
alias: getShortNames(stripTags(path.relative(entryPath, `${asset.path}-${pageIndex}`)), options),
src: pages
.map((finalAsset) => path.relative(outputPath, finalAsset.path))
.sort((a, b) => b.localeCompare(a)),
data: options.includeMetaData ? {
tags: asset.allMetaData
} : undefined
});
});
}
else
{
bundleAssets.push({
alias: getShortNames(stripTags(path.relative(entryPath, asset.path)), options),
src: finalAssets
.map((finalAsset) => path.relative(outputPath, finalAsset.path))
.sort((a, b) => b.localeCompare(a)),
data: options.includeMetaData ? {
tags: asset.allMetaData
} : undefined
});
}
bundleAssets.push({
alias: getShortNames(stripTags(path.relative(entryPath, asset.path)), options),
src: finalAssets
.map((finalAsset) => path.relative(outputPath, finalAsset.path))
.sort((a, b) => b.localeCompare(a)),
data: options.includeMetaData ? {
tags: asset.allMetaData
} : undefined
});
}

asset.children.forEach((child) =>
Expand All @@ -166,51 +144,6 @@ function collectAssets(
// for all assets.. check for atlas and remove them from the bundle..
}

function removeAtlasTextures(asset: Asset)
{
// do a pass to remove what we don't want..

const atlasAssets = findAssetsWithFileName((asset) =>
asset.extension === '.atlas' && asset.transformChildren.length === 0, asset, true);

atlasAssets.forEach((atlasAsset) =>
{
const view = new AtlasView(atlasAsset.buffer);

const textureNames = view.getTextures();

textureNames.forEach((texture) =>
{
const textureAssets = findAssetsWithFileName((asset) =>
asset.filename === texture, asset, true);

textureAssets.forEach((textureAsset) =>
{
textureAsset.skip = true;
});
});
});
}

function getTexturePackedAssets(assets: Asset[])
{
// first get the jsons..
const jsonAssets = assets.filter((asset) => asset.extension === '.json');

const groupAssets: Asset[][] = [];

for (let i = 0; i < jsonAssets.length; i++)
{
const jsonAsset = jsonAssets[i];

groupAssets[jsonAsset.allMetaData.page] ??= [];

groupAssets[jsonAsset.allMetaData.page].push(jsonAsset);
}

return groupAssets;
}

function getShortNames(name: string, options: PixiManifestOptions)
{
const createShortcuts = options.createShortcuts;
Expand Down
14 changes: 12 additions & 2 deletions packages/manifest/test/Manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { AssetPack } from '@play-co/assetpack-core';
import { audio } from '@play-co/assetpack-plugin-ffmpeg';
import { compress, mipmap } from '@play-co/assetpack-plugin-image';
import { pixiManifest } from '@play-co/assetpack-plugin-manifest';
import { spineAtlasMipmap } from '@play-co/assetpack-plugin-spine';
import { texturePacker } from '@play-co/assetpack-plugin-texture-packer';
import { spineAtlasManifestMod, spineAtlasMipmap } from '@play-co/assetpack-plugin-spine';
import { texturePacker, texturePackerManifestMod } from '@play-co/assetpack-plugin-texture-packer';
import { existsSync, readJSONSync } from 'fs-extra';
import type { File } from '../../../shared/test';
import {
Expand Down Expand Up @@ -131,6 +131,8 @@ describe('Manifest', () =>
avif: false,
}),
pixiManifest(),
spineAtlasManifestMod(),
texturePackerManifestMod(),
]
});

Expand Down Expand Up @@ -338,6 +340,8 @@ describe('Manifest', () =>
trimExtensions: true,
includeMetaData: false
}),
spineAtlasManifestMod(),
texturePackerManifestMod(),
],
});

Expand Down Expand Up @@ -483,6 +487,8 @@ describe('Manifest', () =>
trimExtensions: false,
includeMetaData: false
}),
spineAtlasManifestMod(),
texturePackerManifestMod(),
],
});

Expand Down Expand Up @@ -605,6 +611,8 @@ describe('Manifest', () =>
trimExtensions: false,
includeMetaData: false
}),
spineAtlasManifestMod(),
texturePackerManifestMod(),
],
});

Expand Down Expand Up @@ -749,6 +757,8 @@ describe('Manifest', () =>
trimExtensions: true,
includeMetaData: false
}),
spineAtlasManifestMod(),
texturePackerManifestMod(),
],
});

Expand Down
3 changes: 2 additions & 1 deletion packages/spine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
},
"devDependencies": {
"@play-co/assetpack-core": "1.3.0",
"@play-co/assetpack-plugin-image": "1.3.0"
"@play-co/assetpack-plugin-image": "1.3.0",
"@play-co/assetpack-plugin-manifest": "1.3.0"
},
"peerDependencies": {
"@play-co/assetpack-core": ">=0.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/spine/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './spineAtlasMipmap';
export * from './spineAtlasCompress';
export * from './spineAtlasCacheBuster';
export * from './spineAtlasManifestMod';
4 changes: 2 additions & 2 deletions packages/spine/src/spineAtlasCacheBuster.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Asset } from '@play-co/assetpack-core';
import { checkExt, type AssetPipe, findAssetsWithFileName } from '@play-co/assetpack-core';
import { checkExt, type AssetPipe, findAssets } from '@play-co/assetpack-core';
import { AtlasView } from './AtlasView';
import { removeSync, writeFileSync } from 'fs-extra';

Expand Down Expand Up @@ -58,7 +58,7 @@ export function spineAtlasCacheBuster(): AssetPipe

atlasView.getTextures().forEach((texture) =>
{
const textureAssets = findAssetsWithFileName((asset) =>
const textureAssets = findAssets((asset) =>
asset.filename === texture, asset, true);

// last transformed child is the renamed texture
Expand Down
89 changes: 89 additions & 0 deletions packages/spine/src/spineAtlasManifestMod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import type { Asset } from '@play-co/assetpack-core';
import { type AssetPipe, path, findAssets } from '@play-co/assetpack-core';
import { AtlasView } from './AtlasView';
import { readJsonSync, writeJSONSync } from 'fs-extra';

export interface SpineManifestOptions
{
output?: string;
}

/**
* This pipe will modify the manifest generated by 'pixiManifest'. It will look for any images found in the atlas
* files and remove them from the manifest. As the atlas files will be responsible for loading the textures.
*
* Once done, it rewrites the manifest.
*
* This should be added after the `pixiManifest` pipe.
*
* ensure that the same output path is passed to the pipe as the `pixiManifest` pipe. Otherwise
* the manifest will not be found.
*
* As this pipe needs to know about all the textures in the texture files most of the work is done
* in the finish method.
*
* Kind of like applying a patch at the end of the manifest process.
*
* @param _options
* @returns
*/
export function spineAtlasManifestMod(_options: SpineManifestOptions = {}): AssetPipe<SpineManifestOptions>
{
const defaultOptions = {
output: 'manifest.json',
..._options
};

return {
folder: false,
name: 'spine-atlas-manifest',
defaultOptions,

async finish(asset: Asset, options, pipeSystem)
{
const atlasAssets = findAssets((asset) =>
asset.extension === '.atlas' && asset.transformChildren.length === 0, asset, true);

const manifestLocation = options.output;

const newFileName = path.dirname(manifestLocation) === '.'
? path.joinSafe(pipeSystem.outputPath, manifestLocation) : manifestLocation;

const manifest = readJsonSync(newFileName);

atlasAssets.forEach((atlasAsset) =>
{
const atlasView = new AtlasView(atlasAsset.buffer);

atlasView.getTextures().forEach((texture) =>
{
// relative path to the output folder
const texturePath = path.relative(pipeSystem.outputPath, path.joinSafe(atlasAsset.directory, texture));

findAndRemoveManifestAsset(manifest, texturePath);
});
});

writeJSONSync(newFileName, manifest, { spaces: 2 });
}
};
}

function findAndRemoveManifestAsset(manifest: any, assetPath: string)
{
for (let i = 0; i < manifest.bundles.length; i++)
{
const assets = manifest.bundles[i].assets;

const manifestAsset = assets.find((asset: {src: string[]}) =>

asset.src.includes(assetPath)
);

if (manifestAsset)
{
assets.splice(assets.indexOf(manifestAsset), 1);
break;
}
}
}
Loading

0 comments on commit ef71c06

Please sign in to comment.