Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spine): Adds a Spine Package #9

Merged
merged 16 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16,631 changes: 1,585 additions & 15,046 deletions package-lock.json

Large diffs are not rendered by default.

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);
});
});
6 changes: 3 additions & 3 deletions packages/core/src/Asset.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { extractTagsFromFileName } from './utils/extractTagsFromFileName';
import { getHash } from './utils/getHash';
import fs from 'fs-extra';
import { readFileSync } from 'fs-extra';
Zyie marked this conversation as resolved.
Show resolved Hide resolved
import { Logger } from './logger/Logger';
import { path } from './utils/path';

Expand Down Expand Up @@ -112,7 +112,7 @@ export class Asset

if (!this._buffer)
{
this._buffer = fs.readFileSync(this.path);
this._buffer = readFileSync(this.path);
}

return this._buffer;
Expand All @@ -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!
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't seem like we are adding data anymore?

we used to add all the tags to the manifest

.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
Loading