diff --git a/packages/asset-manager/src/asset-manager.library.ts b/packages/asset-manager/src/asset-manager.library.ts index 57d577f3..9ad0d93c 100644 --- a/packages/asset-manager/src/asset-manager.library.ts +++ b/packages/asset-manager/src/asset-manager.library.ts @@ -1,46 +1,19 @@ -import { BaseAssetManagerLibrary, type InitContext } from "@nanoforge/common"; - -import { NfFile } from "./file"; +import { BaseAssetManagerLibrary, type InitContext, NfFile, NfNotFound } from "@nanoforge/common"; export class AssetManagerLibrary extends BaseAssetManagerLibrary { private _assets: Map; - private _wasm: Map; - private _wgsl: Map; get __name(): string { return "AssetManagerLibrary"; } public async __init(context: InitContext): Promise { - this._assets = context.files.assets; - this._wasm = context.files.wasm; - this._wgsl = context.files.wgsl; + this._assets = context.files; } - /** - * @todo Error management - */ - public async getAsset(path: string): Promise { + public getAsset(path: string): NfFile { const res = this._assets.get(this._parsePath(path)); - if (!res) throw new Error("Asset not found."); - return new NfFile(res); - } - - /** - * @todo Error management - */ - public async getWasm(path: string): Promise { - const res = this._wasm.get(this._parsePath(path)); - if (!res) throw new Error("Asset not found."); - return new NfFile(res); - } - - /** - * @todo Error management - */ - public async getWgsl(path: string): Promise { - const res = this._wgsl.get(this._parsePath(path)); - if (!res) throw new Error("Asset not found."); + if (!res) throw new NfNotFound(path, "Asset"); return new NfFile(res); } diff --git a/packages/asset-manager/src/file.ts b/packages/asset-manager/src/file.ts deleted file mode 100644 index 94ebd9ec..00000000 --- a/packages/asset-manager/src/file.ts +++ /dev/null @@ -1,17 +0,0 @@ -export class NfFile { - private readonly _path: string; - - constructor(path: string) { - this._path = path; - } - - get path(): string { - return this._path; - } - - public async getText(): Promise { - const res = await fetch(this._path); - if (!res.ok) throw new Error(`${res.status} ${res.statusText}`); - return await res.text(); - } -} diff --git a/packages/asset-manager/src/index.ts b/packages/asset-manager/src/index.ts index d3725508..b1ecb3a3 100644 --- a/packages/asset-manager/src/index.ts +++ b/packages/asset-manager/src/index.ts @@ -1,2 +1 @@ export { AssetManagerLibrary } from "./asset-manager.library"; -export { NfFile } from "./file"; diff --git a/packages/asset-manager/test/asset-manager.library.spec.ts b/packages/asset-manager/test/asset-manager.library.spec.ts index 8d00c257..9ebad65d 100644 --- a/packages/asset-manager/test/asset-manager.library.spec.ts +++ b/packages/asset-manager/test/asset-manager.library.spec.ts @@ -15,41 +15,35 @@ describe("Asset Manager Library", () => { const context = new InitContext(appContext, libraryManager, configRegistry, { // @ts-ignore canvas: null, - files: { - assets: new Map([["/test.png", "blob:http://localhost:3000/test.png"]]), - wasm: new Map([["/test.wasm", "blob:http://localhost:3000/test.wasm"]]), - wgsl: new Map([["/test.wgsl", "blob:http://localhost:3000/test.wgsl"]]), - }, + files: new Map([ + ["/test.png", "blob:http://localhost:3000/test.png"], + ["/test.wasm", "blob:http://localhost:3000/test.wasm"], + ["/test.wgsl", "blob:http://localhost:3000/test.wgsl"], + ]), }); library.__init(context); - it("Should get asset", async () => { - expect((await library.getAsset("test.png")).path).toEqual( - "blob:http://localhost:3000/test.png", - ); + it("Should get asset", () => { + expect(library.getAsset("test.png").path).toEqual("blob:http://localhost:3000/test.png"); }); - it("Should throw on unknown asset", async () => { - await expect(library.getAsset("test-unknown.png")).rejects.toThrow(); + it("Should throw on unknown asset", () => { + expect(() => library.getAsset("test-unknown.png")).toThrow(); }); - it("Should get wasm", async () => { - expect((await library.getWasm("test.wasm")).path).toEqual( - "blob:http://localhost:3000/test.wasm", - ); + it("Should get wasm", () => { + expect(library.getAsset("test.wasm").path).toEqual("blob:http://localhost:3000/test.wasm"); }); - it("Should throw on unknown wasm", async () => { - await expect(library.getWasm("test-unknown.wasm")).rejects.toThrow(); + it("Should throw on unknown wasm", () => { + expect(() => library.getAsset("test-unknown.wasm")).toThrow(); }); - it("Should get wgsl", async () => { - expect((await library.getWgsl("test.wgsl")).path).toEqual( - "blob:http://localhost:3000/test.wgsl", - ); + it("Should get wgsl", () => { + expect(library.getAsset("test.wgsl").path).toEqual("blob:http://localhost:3000/test.wgsl"); }); - it("Should throw on unknown wgsl", async () => { - await expect(library.getWgsl("test-unknown.wgsl")).rejects.toThrow(); + it("Should throw on unknown wgsl", () => { + expect(() => library.getAsset("test-unknown.wgsl")).toThrow(); }); }); diff --git a/packages/common/src/common/file.ts b/packages/common/src/common/file.ts new file mode 100644 index 00000000..ef1b53ba --- /dev/null +++ b/packages/common/src/common/file.ts @@ -0,0 +1,49 @@ +import { NfFetchException } from "../exception"; + +export class NfFile { + private readonly _path: string; + + constructor(path: string) { + this._path = path; + } + + get path(): string { + return this._path; + } + + public async arrayBuffer(): Promise { + const res = await this._fetch(); + return await res.arrayBuffer(); + } + + public async blob(): Promise { + const res = await this._fetch(); + return await res.blob(); + } + + public async bytes(): Promise> { + const res = await this._fetch(); + return await res.bytes(); + } + + public async formData(): Promise { + const res = await this._fetch(); + return await res.formData(); + } + + public async json(): Promise { + const res = await this._fetch(); + return await res.json(); + } + + public async text(): Promise { + const res = await this._fetch(); + return await res.text(); + } + + private async _fetch(): Promise { + const res = await fetch(this._path); + if (!res.ok) throw new NfFetchException(res.status, res.statusText); + return res; + } +} diff --git a/packages/common/src/common/index.ts b/packages/common/src/common/index.ts new file mode 100644 index 00000000..47a87de9 --- /dev/null +++ b/packages/common/src/common/index.ts @@ -0,0 +1 @@ +export { NfFile } from "./file"; diff --git a/packages/common/src/exception/exceptions/fetch.exception.ts b/packages/common/src/exception/exceptions/fetch.exception.ts new file mode 100644 index 00000000..a37f5eea --- /dev/null +++ b/packages/common/src/exception/exceptions/fetch.exception.ts @@ -0,0 +1,14 @@ +import { NfException } from "../abstracts/exception.abstract"; + +export class NfFetchException extends NfException { + private readonly _code: number; + + get code(): number { + return this._code; + } + + constructor(code: number, text: string) { + super(`Fetch exception : ${code} - ${text}`); + this._code = code; + } +} diff --git a/packages/common/src/exception/index.ts b/packages/common/src/exception/index.ts index 851ccaf0..8f3ee8a1 100644 --- a/packages/common/src/exception/index.ts +++ b/packages/common/src/exception/index.ts @@ -1 +1,2 @@ export { NfNotFound } from "./exceptions/not-found.exception"; +export { NfFetchException } from "./exceptions/fetch.exception"; diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index c88c5209..5adc5c5b 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -1,3 +1,4 @@ +export * from "./common"; export * from "./context"; export * from "./library"; export * from "./options"; diff --git a/packages/common/src/library/libraries/abstracts/asset-manager.library.abstract.ts b/packages/common/src/library/libraries/abstracts/asset-manager.library.abstract.ts index 27f16364..b98d5ff1 100644 --- a/packages/common/src/library/libraries/abstracts/asset-manager.library.abstract.ts +++ b/packages/common/src/library/libraries/abstracts/asset-manager.library.abstract.ts @@ -1,7 +1,10 @@ +import { type NfFile } from "../../../common"; import { type InitContext } from "../../../context"; import { type IAssetManagerLibrary } from "../interfaces"; import { Library } from "../library"; export abstract class BaseAssetManagerLibrary extends Library implements IAssetManagerLibrary { public abstract __init(context: InitContext): Promise; + + public abstract getAsset(path: string): NfFile; } diff --git a/packages/common/src/library/libraries/interfaces/finals/asset-manager.library.type.ts b/packages/common/src/library/libraries/interfaces/finals/asset-manager.library.type.ts index 7348904f..3f4d7a24 100644 --- a/packages/common/src/library/libraries/interfaces/finals/asset-manager.library.type.ts +++ b/packages/common/src/library/libraries/interfaces/finals/asset-manager.library.type.ts @@ -1,3 +1,6 @@ +import { type NfFile } from "../../../../common"; import { type IExposedLibrary } from "../bases/exposed.library.type"; -export interface IAssetManagerLibrary extends IExposedLibrary {} +export interface IAssetManagerLibrary extends IExposedLibrary { + getAsset(path: string): NfFile; +} diff --git a/packages/common/src/options/types/options.type.ts b/packages/common/src/options/types/options.type.ts index b0a02e36..18e4cd79 100644 --- a/packages/common/src/options/types/options.type.ts +++ b/packages/common/src/options/types/options.type.ts @@ -1,8 +1,4 @@ export interface IRunOptions { canvas: HTMLCanvasElement; - files: { - assets: Map; - wasm: Map; - wgsl: Map; - }; + files: Map; } diff --git a/packages/ecs/src/ecs-library.ts b/packages/ecs/src/ecs-library.ts index ed4a5872..da308662 100644 --- a/packages/ecs/src/ecs-library.ts +++ b/packages/ecs/src/ecs-library.ts @@ -1,4 +1,3 @@ -import { type AssetManagerLibrary } from "@nanoforge/asset-manager"; import { ASSET_MANAGER_LIBRARY, BaseComponentSystemLibrary, @@ -7,8 +6,7 @@ import { type InitContext, } from "@nanoforge/common"; -import type { MainModule } from "../lib"; -import { Module } from "../lib"; +import { type MainModule, Module } from "../lib"; import { ECSRegistry } from "./ecs-registry"; export class ECSLibrary extends BaseComponentSystemLibrary { @@ -29,9 +27,7 @@ export class ECSLibrary extends BaseComponentSystemLibrary { } async __init(context: InitContext): Promise { - const wasmFile = await context.libraries - .getAssetManager() - .library.getWasm(this.path); + const wasmFile = context.libraries.getAssetManager().library.getAsset(this.path); this.module = await Module({ locateFile: () => wasmFile.path }); this._registry = new ECSRegistry(new this.module.Registry()); } diff --git a/packages/ecs/src/index.ts b/packages/ecs/src/index.ts index 45e35908..c17bf8b7 100644 --- a/packages/ecs/src/index.ts +++ b/packages/ecs/src/index.ts @@ -1,4 +1,4 @@ -import "../lib/libecs.wasm"; +import "@lib/libecs.wasm"; export { ECSLibrary } from "./ecs-library"; export type { ECSRegistry, Component, System } from "./ecs-registry"; diff --git a/packages/ecs/test/ecs-library.spec.ts b/packages/ecs/test/ecs-library.spec.ts index 58b6b088..8b4302ed 100644 --- a/packages/ecs/test/ecs-library.spec.ts +++ b/packages/ecs/test/ecs-library.spec.ts @@ -30,11 +30,7 @@ describe("ECSLibrary", () => { const initContext = new InitContext(appContext, libraryManager, configRegistry, { // @ts-ignore canvas: null, - files: { - assets: new Map(), - wasm: new Map([["/libecs.wasm", "./lib/libecs.wasm"]]), - wgsl: new Map(), - }, + files: new Map([["/libecs.wasm", "./lib/libecs.wasm"]]), }); const clearContext = new ClearContext(appContext, libraryManager); diff --git a/packages/ecs/tsconfig.build.json b/packages/ecs/tsconfig.build.json index 03fe8683..6af7f97c 100644 --- a/packages/ecs/tsconfig.build.json +++ b/packages/ecs/tsconfig.build.json @@ -5,17 +5,13 @@ "rootDir": ".", "paths": { "@lib": ["./lib"], - "@nanoforge/common": ["../common"], - "@nanoforge/asset-manager": ["../asset-manager"] + "@nanoforge/common": ["../common"] } }, "exclude": ["node_modules", "dist", "test/**/*", "*.spec.ts"], "references": [ { "path": "../common/tsconfig.build.json" - }, - { - "path": "../asset-manager/tsconfig.build.json" } ] } diff --git a/packages/graphics-2d/test/graphics-2d.library.spec.ts b/packages/graphics-2d/test/graphics-2d.library.spec.ts index 0846aa83..515c56e3 100644 --- a/packages/graphics-2d/test/graphics-2d.library.spec.ts +++ b/packages/graphics-2d/test/graphics-2d.library.spec.ts @@ -15,11 +15,7 @@ describe("Graphics 2D Library", () => { const context = new InitContext(appContext, libraryManager, configRegistry, { // @ts-ignore canvas: null, - files: { - assets: new Map([["/test.png", "blob:http://localhost:3000/test.png"]]), - wasm: new Map([["/test.wasm", "blob:http://localhost:3000/test.wasm"]]), - wgsl: new Map([["/test.wgsl", "blob:http://localhost:3000/test.wgsl"]]), - }, + files: new Map(), }); it("Should throw if canvas is undefined", async () => {