Skip to content

Commit

Permalink
chore(spine): ensure spine tag is used
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyie committed Apr 29, 2024
1 parent 414e0e8 commit 5e01f03
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
17 changes: 12 additions & 5 deletions packages/spine/src/spineAtlasCacheBuster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { removeSync, writeFileSync } from 'fs-extra';
import { AtlasView } from './AtlasView';
import { type AssetPipe, checkExt, findAssets } from '@play-co/assetpack-core';

import type { Asset } from '@play-co/assetpack-core';
import type { Asset, PluginOptions } from '@play-co/assetpack-core';

export type SpineAtlasCacheBustOptions = PluginOptions<'spine'>;

/**
* This should be used after the cache buster plugin in the pipes.
Expand All @@ -18,19 +20,24 @@ import type { Asset } from '@play-co/assetpack-core';
* @param _options
* @returns
*/
export function spineAtlasCacheBuster(): AssetPipe
export function spineAtlasCacheBuster(_options: SpineAtlasCacheBustOptions = {}): AssetPipe
{
const defaultOptions = {};
const defaultOptions = {
tags: {
spine: 'spine',
..._options.tags,
},
};

const atlasFileToFix: Asset[] = [];

return {
folder: false,
name: 'spine-cache-buster',
defaultOptions,
test(asset: Asset, _options)
test(asset: Asset, options)
{
return checkExt(asset.path, '.atlas');
return checkExt(asset.path, '.atlas') && asset.metaData[options.tags.spine];
},

async transform(asset: Asset, _options)
Expand Down
8 changes: 5 additions & 3 deletions packages/spine/src/spineAtlasCompress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { checkExt, createNewAssetAt, swapExt } from '@play-co/assetpack-core';
import type { Asset, AssetPipe, PluginOptions } from '@play-co/assetpack-core';
import type { CompressOptions } from '@play-co/assetpack-plugin-image';

export type SpineAtlasCompressOptions = PluginOptions<'nc'> & CompressOptions;
export type SpineAtlasCompressOptions = PluginOptions<'nc' | 'spine'> & CompressOptions;

export function spineAtlasCompress(_options?: SpineAtlasCompressOptions): AssetPipe<SpineAtlasCompressOptions>
{
const defaultOptions = {
..._options,
tags: {
tps: 'nc',
nc: 'nc',
spine: 'spine',
..._options?.tags
}
};
Expand All @@ -22,7 +23,8 @@ export function spineAtlasCompress(_options?: SpineAtlasCompressOptions): AssetP
test(asset: Asset, options)
{
return !asset.allMetaData[options.tags.nc]
&& checkExt(asset.path, '.atlas');
&& checkExt(asset.path, '.atlas')
&& asset.metaData[options.tags.spine];
},
async transform(asset: Asset, options)
{
Expand Down
39 changes: 26 additions & 13 deletions packages/spine/src/spineAtlasManifestMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { readJsonSync, writeJSONSync } from 'fs-extra';
import { AtlasView } from './AtlasView';
import { type AssetPipe, findAssets, path } from '@play-co/assetpack-core';

import type { Asset } from '@play-co/assetpack-core';
import type { Asset, PluginOptions } from '@play-co/assetpack-core';

export interface SpineManifestOptions
{
export type SpineManifestOptions = PluginOptions<'spine'> & {
output?: string;
}
};

/**
* This pipe will modify the manifest generated by 'pixiManifest'. It will look for any images found in the atlas
Expand All @@ -32,7 +31,11 @@ export function spineAtlasManifestMod(_options: SpineManifestOptions = {}): Asse
{
const defaultOptions = {
output: 'manifest.json',
..._options
..._options,
tags: {
spine: 'spine',
..._options.tags,
},
};

return {
Expand All @@ -42,13 +45,21 @@ export function spineAtlasManifestMod(_options: SpineManifestOptions = {}): Asse

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

const manifestLocation = options.output;

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

const manifest = readJsonSync(newFileName);

Expand All @@ -59,14 +70,17 @@ export function spineAtlasManifestMod(_options: SpineManifestOptions = {}): Asse
atlasView.getTextures().forEach((texture) =>
{
// relative path to the output folder
const texturePath = path.relative(pipeSystem.outputPath, path.joinSafe(atlasAsset.directory, texture));
const texturePath = path.relative(
pipeSystem.outputPath,
path.joinSafe(atlasAsset.directory, texture)
);

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

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

Expand All @@ -76,8 +90,7 @@ function findAndRemoveManifestAsset(manifest: any, assetPath: string)
{
const assets = manifest.bundles[i].assets;

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

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

Expand Down
7 changes: 5 additions & 2 deletions packages/spine/src/spineAtlasMipmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type AssetPipe, checkExt, createNewAssetAt } from '@play-co/assetpack-c
import type { Asset, PluginOptions } from '@play-co/assetpack-core';
import type { MipmapOptions } from '@play-co/assetpack-plugin-image';

export type SpineOptions = PluginOptions<'fix'> & MipmapOptions;
export type SpineOptions = PluginOptions<'fix' | 'spine'> & MipmapOptions;

export function spineAtlasMipmap(_options?: SpineOptions): AssetPipe<SpineOptions>
{
Expand All @@ -14,6 +14,7 @@ export function spineAtlasMipmap(_options?: SpineOptions): AssetPipe<SpineOption
..._options,
tags: {
fix: 'fix',
spine: 'spine',
..._options?.tags
},
};
Expand All @@ -24,7 +25,9 @@ export function spineAtlasMipmap(_options?: SpineOptions): AssetPipe<SpineOption
defaultOptions,
test(asset: Asset, options)
{
return !asset.allMetaData[options.tags.fix as any] && checkExt(asset.path, '.atlas');
return !asset.allMetaData[options.tags.fix]
&& checkExt(asset.path, '.atlas')
&& asset.metaData[options.tags.spine];
},
async transform(asset: Asset, options)
{
Expand Down

0 comments on commit 5e01f03

Please sign in to comment.