-
Notifications
You must be signed in to change notification settings - Fork 217
/
atlasLoader.ts
110 lines (86 loc) · 4.31 KB
/
atlasLoader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { TextureAtlas } from '@pixi-spine/base';
import { type AssetExtension, LoaderParserPriority, LoadAsset, Loader, checkExtension } from '@pixi/assets';
import { BaseTexture, extensions, ExtensionType, settings, Texture, utils } from '@pixi/core';
import type { ISpineMetadata } from './SpineLoaderAbstract';
type RawAtlas = string;
const spineTextureAtlasLoader: AssetExtension<RawAtlas | TextureAtlas, ISpineMetadata> = {
extension: ExtensionType.Asset,
// cache: {
// test: (asset: RawAtlas | TextureAtlas) => asset instanceof TextureAtlas,
// getCacheableAssets: (keys: string[], asset: RawAtlas | TextureAtlas) => getCacheableAssets(keys, asset),
// },
loader: {
extension: {
type: ExtensionType.LoadParser,
priority: LoaderParserPriority.Normal,
},
test(url: string): boolean {
return checkExtension(url, '.atlas');
},
async load(url: string): Promise<RawAtlas> {
const response = await settings.ADAPTER.fetch(url);
const txt = await response.text();
return txt as RawAtlas;
},
testParse(asset: unknown, options: LoadAsset): Promise<boolean> {
const isExtensionRight = checkExtension(options.src, '.atlas');
const isString = typeof asset === 'string';
return Promise.resolve(isExtensionRight && isString);
},
async parse(asset: RawAtlas, options: LoadAsset, loader: Loader): Promise<TextureAtlas> {
const metadata: ISpineMetadata = options.data;
let basePath = utils.path.dirname(options.src);
if (basePath && basePath.lastIndexOf('/') !== basePath.length - 1) {
basePath += '/';
}
let resolve = null;
let reject = null;
const retPromise = new Promise<TextureAtlas>((res, rej) => {
resolve = res;
reject = rej;
});
// Retval is going to be a texture atlas. However we need to wait for it's callback to resolve this promise.
let retval;
const resolveCallback = (newAtlas: TextureAtlas): void => {
if (!newAtlas) {
reject('Something went terribly wrong loading a spine .atlas file\nMost likely your texture failed to load.');
}
resolve(retval);
};
// if we have an already loaded pixi image in the image field, use that.
if (metadata.image || metadata.images) {
// merge the objects
const pages = Object.assign(metadata.image ? { default: metadata.image } : {}, metadata.images);
retval = new TextureAtlas(
asset as RawAtlas,
(line: any, callback: any) => {
const page = pages[line] || (pages.default as any);
if (page && page.baseTexture) callback(page.baseTexture);
else callback(page);
},
resolveCallback
);
} else {
// We don't have ready to use pixi textures, we need to load them now!
retval = new TextureAtlas(asset as RawAtlas, makeSpineTextureAtlasLoaderFunctionFromPixiLoaderObject(loader, basePath, metadata.imageMetadata), resolveCallback);
}
return (await retPromise) as TextureAtlas;
},
unload(atlas: TextureAtlas) {
atlas.dispose();
},
},
} as AssetExtension<RawAtlas | TextureAtlas, ISpineMetadata>;
/**
* Ugly function to promisify the spine texture atlas loader function.
* @public
*/
export const makeSpineTextureAtlasLoaderFunctionFromPixiLoaderObject = (loader: Loader, atlasBasePath: string, imageMetadata: any) => {
return async (pageName: string, textureLoadedCallback: (tex: BaseTexture) => any): Promise<void> => {
// const url = utils.path.join(...atlasBasePath.split(utils.path.sep), pageName); // Broken in upstream
const url = utils.path.normalize([...atlasBasePath.split(utils.path.sep), pageName].join(utils.path.sep));
const texture = await loader.load<Texture>({ src: url, data: imageMetadata });
textureLoadedCallback(texture.baseTexture);
};
};
extensions.add(spineTextureAtlasLoader);