@antha/asset 0.4.3 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -26,7 +26,7 @@ type GameState = AnthaAssetModState & {
26
26
  };
27
27
 
28
28
  const titleAsset = defineAsset({
29
- name: 'title',
29
+ assetName: 'title',
30
30
  maxProgress: 1,
31
31
  load({incrementProgressCallback}) {
32
32
  incrementProgressCallback();
@@ -0,0 +1,41 @@
1
+ import { type AnthaEngine, type AnthaMod } from '@antha/engine';
2
+ import { type AnyObject, type MaybePromise, type PartialWithUndefined } from '@augment-vir/common';
3
+ import { type AnthaAssetModState } from './antha-asset.mod.js';
4
+ import { type AssetLoader, type AssetLoadSession } from './asset-loader.js';
5
+ /** State used internally by {@link createAnthaBootstrapMod}. */
6
+ export type AnthaBootstrapModState = {
7
+ hasStartedBootstrap: boolean;
8
+ };
9
+ /** Parameters passed to {@link AnthaBootstrapModOptions.bootstrap}. */
10
+ export type AnthaBootstrapParams<State extends AnyObject, Module> = {
11
+ assetLoader: AssetLoader;
12
+ engine: AnthaEngine;
13
+ loadSession: AssetLoadSession;
14
+ module: Module;
15
+ state: Partial<State & AnthaAssetModState & AnthaBootstrapModState>;
16
+ };
17
+ /** Result returned by {@link AnthaBootstrapModOptions.bootstrap}. */
18
+ export type AnthaBootstrapResult = {
19
+ /** Mods that Antha installs after bootstrapping finishes. */
20
+ mods: ReadonlyArray<AnthaMod>;
21
+ };
22
+ /** Options for {@link createAnthaBootstrapMod}. */
23
+ export type AnthaBootstrapModOptions<State extends AnyObject, Module> = {
24
+ /**
25
+ * Bootstraps the loaded module and returns the mods that should be installed for it. Await any
26
+ * initial asset loading here, using the provided load session, before returning.
27
+ */
28
+ bootstrap: (this: void, params: Readonly<AnthaBootstrapParams<State, Module>>) => MaybePromise<AnthaBootstrapResult>;
29
+ /** Lazily imports the code required to bootstrap the game. */
30
+ loadModule: (this: void) => MaybePromise<Module>;
31
+ } & PartialWithUndefined<{
32
+ /** Label shown for the code-loading task. */
33
+ assetName: string;
34
+ }>;
35
+ /**
36
+ * Creates a mod that tracks a lazy game-module import on Antha's loading screen, then installs the
37
+ * mods returned by the module's bootstrap callback.
38
+ *
39
+ * @category Pre-Built Mods
40
+ */
41
+ export declare function createAnthaBootstrapMod<State extends AnyObject = AnyObject>(): <Module>(options: Readonly<AnthaBootstrapModOptions<NoInfer<State>, Module>>) => AnthaMod<NoInfer<NoInfer<State> & AnthaAssetModState & AnthaBootstrapModState>>;
@@ -0,0 +1,48 @@
1
+ import { defineAnthaMod, SkipExecution } from '@antha/engine';
2
+ import { ensureErrorAndPrependMessage, } from '@augment-vir/common';
3
+ /**
4
+ * Creates a mod that tracks a lazy game-module import on Antha's loading screen, then installs the
5
+ * mods returned by the module's bootstrap callback.
6
+ *
7
+ * @category Pre-Built Mods
8
+ */
9
+ export function createAnthaBootstrapMod() {
10
+ return function createBootstrapMod(options) {
11
+ return defineAnthaMod({
12
+ modName: 'antha-bootstrap',
13
+ execute({ engine, state }) {
14
+ const assetLoader = state.assetLoader;
15
+ if (!assetLoader || state.hasStartedBootstrap) {
16
+ return SkipExecution;
17
+ }
18
+ state.hasStartedBootstrap = true;
19
+ const loadSession = assetLoader.createLoadSession();
20
+ loadSession.reportProgress({
21
+ current: 0,
22
+ currentResourceName: options.assetName || 'Game code',
23
+ total: 0,
24
+ });
25
+ void Promise.resolve()
26
+ .then(() => {
27
+ return options.loadModule();
28
+ })
29
+ .then(async (module) => {
30
+ const bootstrapResult = await options.bootstrap({
31
+ assetLoader,
32
+ engine,
33
+ loadSession,
34
+ module,
35
+ state,
36
+ });
37
+ engine.currentMods.push(...bootstrapResult.mods);
38
+ loadSession.complete();
39
+ })
40
+ .catch((error) => {
41
+ loadSession.complete();
42
+ engine.log.error(ensureErrorAndPrependMessage(error, 'Failed to bootstrap game.'));
43
+ });
44
+ return SkipExecution;
45
+ },
46
+ });
47
+ };
48
+ }
@@ -50,7 +50,7 @@ export type AssetValue<SpecificAsset extends Pick<Asset, 'load'>> = Awaited<Retu
50
50
  * @category Asset
51
51
  */
52
52
  export type Asset<AssetValue = any> = {
53
- name: string;
53
+ assetName: string;
54
54
  maxProgress: number;
55
55
  load: AssetLoaderCallback<AssetValue>;
56
56
  };
@@ -173,7 +173,7 @@ export class AssetLoader {
173
173
  this.assetCache.set(asset, deferredLoadPromise.promise);
174
174
  loadSession?.reportProgress({
175
175
  current: 0,
176
- currentResourceName: asset.name,
176
+ currentResourceName: asset.assetName,
177
177
  total: asset.maxProgress,
178
178
  });
179
179
  const loadedAsset = await asset.load({
@@ -181,7 +181,7 @@ export class AssetLoader {
181
181
  incrementProgressCallback?.(progressParams);
182
182
  loadSession?.incrementProgress({
183
183
  amount: progressParams,
184
- currentResourceName: asset.name,
184
+ currentResourceName: asset.assetName,
185
185
  });
186
186
  },
187
187
  });
@@ -197,7 +197,7 @@ export class AssetLoader {
197
197
  await entry.cleanup();
198
198
  }
199
199
  catch (error) {
200
- this.log.error(ensureErrorAndPrependMessage(error, `Failed to cleanup asset: ${asset.name}`));
200
+ this.log.error(ensureErrorAndPrependMessage(error, `Failed to cleanup asset: ${asset.assetName}`));
201
201
  }
202
202
  }
203
203
  this.assetCache.delete(asset);
@@ -233,7 +233,7 @@ export class AssetLoader {
233
233
  progress: {
234
234
  current: currentProgress,
235
235
  total: maxProgress,
236
- currentResourceName: assetsToLoad[0]?.name,
236
+ currentResourceName: assetsToLoad[0]?.assetName,
237
237
  },
238
238
  });
239
239
  }
@@ -247,7 +247,7 @@ export class AssetLoader {
247
247
  progress: {
248
248
  current: currentProgress,
249
249
  total: maxProgress,
250
- currentResourceName: assetsToLoad[0]?.name,
250
+ currentResourceName: assetsToLoad[0]?.assetName,
251
251
  },
252
252
  });
253
253
  const chunkedAssets = options.maxParallelism
@@ -263,7 +263,7 @@ export class AssetLoader {
263
263
  progress: {
264
264
  current: currentProgress,
265
265
  total: maxProgress,
266
- currentResourceName: asset.name,
266
+ currentResourceName: asset.assetName,
267
267
  },
268
268
  });
269
269
  };
@@ -278,7 +278,7 @@ export class AssetLoader {
278
278
  progress: {
279
279
  current: currentProgress,
280
280
  total: maxProgress,
281
- currentResourceName: asset.name,
281
+ currentResourceName: asset.assetName,
282
282
  },
283
283
  });
284
284
  return await this.loadIndividualAsset({
@@ -293,7 +293,7 @@ export class AssetLoader {
293
293
  currentProgress,
294
294
  maxProgress,
295
295
  assetCount: assetsToLoad.length,
296
- assetNames: assetsToLoad.map((entry) => entry.name).filter(Boolean),
296
+ assetNames: assetsToLoad.map((entry) => entry.assetName).filter(Boolean),
297
297
  },
298
298
  tags: {
299
299
  mod: '@antha/asset',
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './antha-asset.mod.js';
2
+ export * from './antha-bootstrap.mod.js';
2
3
  export * from './asset-loader.js';
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './antha-asset.mod.js';
2
+ export * from './antha-bootstrap.mod.js';
2
3
  export * from './asset-loader.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antha/asset",
3
- "version": "0.4.3",
3
+ "version": "0.6.0",
4
4
  "description": "An Antha mod for handling asset loading.",
5
5
  "keywords": [
6
6
  "vir",
@@ -45,10 +45,12 @@
45
45
  "@web/test-runner": "^1.0.0",
46
46
  "@web/test-runner-playwright": "^1.0.0",
47
47
  "istanbul-smart-text-reporter": "^1.1.5",
48
- "pixi.js": "^8.19.0"
48
+ "markdown-code-example-inserter": "^3.0.6",
49
+ "pixi.js": "^8.19.0",
50
+ "typedoc": "^0.28.20"
49
51
  },
50
52
  "peerDependencies": {
51
- "@antha/engine": "^0.4.3",
53
+ "@antha/engine": "^0.6.0",
52
54
  "element-vir": ">=26"
53
55
  },
54
56
  "engines": {