Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 4 additions & 31 deletions packages/asset-manager/src/asset-manager.library.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>;
private _wasm: Map<string, string>;
private _wgsl: Map<string, string>;

get __name(): string {
return "AssetManagerLibrary";
}

public async __init(context: InitContext): Promise<void> {
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<NfFile> {
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<NfFile> {
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<NfFile> {
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);
}

Expand Down
17 changes: 0 additions & 17 deletions packages/asset-manager/src/file.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/asset-manager/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { AssetManagerLibrary } from "./asset-manager.library";
export { NfFile } from "./file";
40 changes: 17 additions & 23 deletions packages/asset-manager/test/asset-manager.library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
49 changes: 49 additions & 0 deletions packages/common/src/common/file.ts
Original file line number Diff line number Diff line change
@@ -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<ArrayBuffer> {
const res = await this._fetch();
return await res.arrayBuffer();
}

public async blob(): Promise<Blob> {
const res = await this._fetch();
return await res.blob();
}

public async bytes(): Promise<Uint8Array<ArrayBuffer>> {
const res = await this._fetch();
return await res.bytes();
}

public async formData(): Promise<FormData> {
const res = await this._fetch();
return await res.formData();
}

public async json(): Promise<any> {
const res = await this._fetch();
return await res.json();
}

public async text(): Promise<string> {
const res = await this._fetch();
return await res.text();
}

private async _fetch(): Promise<Response> {
const res = await fetch(this._path);
if (!res.ok) throw new NfFetchException(res.status, res.statusText);
return res;
}
}
1 change: 1 addition & 0 deletions packages/common/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { NfFile } from "./file";
14 changes: 14 additions & 0 deletions packages/common/src/exception/exceptions/fetch.exception.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions packages/common/src/exception/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { NfNotFound } from "./exceptions/not-found.exception";
export { NfFetchException } from "./exceptions/fetch.exception";
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./common";
export * from "./context";
export * from "./library";
export * from "./options";
Expand Down
Original file line number Diff line number Diff line change
@@ -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<void>;

public abstract getAsset(path: string): NfFile;
}
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 1 addition & 5 deletions packages/common/src/options/types/options.type.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
export interface IRunOptions {
canvas: HTMLCanvasElement;
files: {
assets: Map<string, string>;
wasm: Map<string, string>;
wgsl: Map<string, string>;
};
files: Map<string, string>;
}
8 changes: 2 additions & 6 deletions packages/ecs/src/ecs-library.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { type AssetManagerLibrary } from "@nanoforge/asset-manager";
import {
ASSET_MANAGER_LIBRARY,
BaseComponentSystemLibrary,
Expand All @@ -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 {
Expand All @@ -29,9 +27,7 @@ export class ECSLibrary extends BaseComponentSystemLibrary {
}

async __init(context: InitContext): Promise<void> {
const wasmFile = await context.libraries
.getAssetManager<AssetManagerLibrary>()
.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());
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ecs/src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
6 changes: 1 addition & 5 deletions packages/ecs/test/ecs-library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 1 addition & 5 deletions packages/ecs/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
6 changes: 1 addition & 5 deletions packages/graphics-2d/test/graphics-2d.library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Loading