Skip to content

Commit

Permalink
feat(spine): Adds a Spine Package (#9)
Browse files Browse the repository at this point in the history
* - add skip property and skipChildren
- fixed json and test
- fixed texture packer still creating individual images
- ensure output is removed if the cache key changes

* -test tweak
- fix compressed pngs not working

* fix rotation on texture packer

* - modify cache-buster to use hash
- remove spineAtlasMipmap from mipmap-compress
- fix png compression
- add spine package

* pr feedback

* fix install

* fix tests

* fix test

* note

* feedback

* fix fs

* update spine

* fix mip

---------

Co-authored-by: Zyie <[email protected]>
  • Loading branch information
GoodBoyDigital and Zyie authored Apr 22, 2024
1 parent 973ef7d commit 469fb68
Show file tree
Hide file tree
Showing 44 changed files with 3,437 additions and 15,649 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ jobs:
- run: npm run lint
- run: npx nx affected --target=test:types --parallel=3
- uses: FedericoCarboni/setup-ffmpeg@v2
- run: npx nx affected --target=test --parallel=3 --ci
- run: npx nx affected --target=build --parallel=3
- run: npx nx affected --target=test --parallel=3 --ci
16,632 changes: 1,586 additions & 15,046 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion packages/cache-buster/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"test:types": "tsc --noEmit"
},
"dependencies": {
"@node-rs/crc32": "^1.10.0",
"fs-extra": "^11.1.0"
},
"devDependencies": {
Expand Down
19 changes: 2 additions & 17 deletions packages/cache-buster/src/cacheBuster.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import type { AssetPipe, Asset } from '@play-co/assetpack-core';
import { createNewAssetAt, swapExt } from '@play-co/assetpack-core';
import fs from 'fs-extra';

import nodeCrc32 from '@node-rs/crc32';

/**
* Cache buster asset pipe. This pipe will add a hash to the end of the filename
Expand All @@ -28,26 +25,14 @@ export function cacheBuster(): AssetPipe
},
async transform(asset: Asset)
{
const buffer = asset.buffer ?? fs.readFileSync(asset.path);

const hash = crc32(buffer);
const hash = asset.hash;
const newFileName = swapExt(asset.filename, `-${hash}${asset.extension}`);

const newAsset = createNewAssetAt(asset, newFileName);

// by attaching the buffer - we can avoid reading the file again
// and the final copy op will use the buffer, rather than the file path!
newAsset.buffer = buffer;
newAsset.buffer = asset.buffer;

return [newAsset];
}
};
}

/** Calculate a CRC32 checksum. */
export function crc32(input: string | Buffer): string
{
const checksumHex = nodeCrc32.crc32(input).toString(16);

return Buffer.from(checksumHex, 'hex').toString('base64url');
}
14 changes: 7 additions & 7 deletions packages/cache-buster/test/cacheBuster.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AssetPack, path } from '@play-co/assetpack-core';
import { existsSync, readFileSync } from 'fs-extra';
import { Asset, AssetPack, path } from '@play-co/assetpack-core';
import { existsSync } from 'fs-extra';
import { assetPath, createFolder, getInputDir, getOutputDir } from '../../../shared/test';
import { cacheBuster, crc32 } from '../src';
import { cacheBuster } from '../src';

const pkg = 'cache-buster';

Expand Down Expand Up @@ -40,10 +40,10 @@ describe('CacheBuster', () =>

const originalPath = path.joinSafe('.testInput', testName, 'ttf.ttf');

const buffer = readFileSync(originalPath);

const hash = crc32(buffer);
const asset = new Asset({
path: originalPath,
});

expect(existsSync(path.joinSafe('.testOutput', testName, `ttf-${hash}.ttf`))).toBe(true);
expect(existsSync(path.joinSafe('.testOutput', testName, `ttf-${asset.hash}.ttf`))).toBe(true);
});
});
2 changes: 1 addition & 1 deletion packages/core/src/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class Asset
Logger.warn('[Assetpack] folders should not have hashes. Contact the developer of the Assetpack');
}

this._hash ??= getHash(this.buffer ?? this.path);
this._hash ??= getHash(this.buffer);

return this._hash;
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export * from './utils/stripTags';
export * from './utils/merge';
export * from './utils/path';
export * from './utils/swapExt';
export * from './utils/findAssetsWithFileName';

30 changes: 30 additions & 0 deletions packages/core/src/utils/findAssetsWithFileName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { Asset } from '../Asset';

export function findAssetsWithFileName(
test: (asset: Asset) => boolean,
asset: Asset,
searchTransform: boolean,
out: Asset[] = []
): Asset[]
{
if (test(asset))
{
out.push(asset);
}

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

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

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

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

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

import fs from 'fs-extra';
Expand Down Expand Up @@ -31,6 +32,36 @@ export interface PixiManifestOptions
trimExtensions?: boolean;
}

// TODO EXPORT this out! But don't want to create a dependency on the atlas plugin just yet..
export class AtlasView
{
public rawAtlas: string;

constructor(buffer: Buffer)
{
this.rawAtlas = buffer.toString();
}

getTextures(): string[]
{
const regex = /^.+?(?:\.png|\.jpg|\.jpeg|\.webp|\.avif)$/gm;

const matches = this.rawAtlas.match(regex);

return matches as string[];
}

replaceTexture(filename: string, newFilename: string)
{
this.rawAtlas = this.rawAtlas.replace(filename, newFilename);
}

get buffer()
{
return Buffer.from(this.rawAtlas);
}
}

export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe<PixiManifestOptions>
{
const defaultOptions = {
Expand All @@ -45,6 +76,8 @@ 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 @@ -100,15 +133,19 @@ function collectAssets(
{
bundleAssets.push({
alias: getShortNames(stripTags(path.relative(entryPath, `${asset.path}-${pageIndex}`)), options),
src: pages.map((finalAsset) => path.relative(outputPath, finalAsset.path))
src: pages
.map((finalAsset) => path.relative(outputPath, finalAsset.path))
.sort((a, b) => b.localeCompare(a))
});
});
}
else
{
bundleAssets.push({
alias: getShortNames(stripTags(path.relative(entryPath, asset.path)), options),
src: finalAssets.map((finalAsset) => path.relative(outputPath, finalAsset.path))
src: finalAssets
.map((finalAsset) => path.relative(outputPath, finalAsset.path))
.sort((a, b) => b.localeCompare(a))
});
}
}
Expand All @@ -117,6 +154,34 @@ function collectAssets(
{
collectAssets(child, options, outputPath, entryPath, bundles, localBundle);
});

// 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[])
Expand Down
Loading

0 comments on commit 469fb68

Please sign in to comment.