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
27 changes: 20 additions & 7 deletions packages/asset-manager/src/asset-manager.library.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
import { BaseAssetManagerLibrary, type InitContext } from "@nanoforge/common";

import { NfFile } from "./file";

export class AssetManagerLibrary extends BaseAssetManagerLibrary {
private _assets: Map<string, string>;
private _scripts: 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._scripts = context.files.scripts;
this._wasm = context.files.wasm;
this._wgsl = context.files.wgsl;
}

/**
* @todo Error management
*/
public async getAsset(path: string): Promise<string> {
public async getAsset(path: string): Promise<NfFile> {
const res = this._assets.get(this._parsePath(path));
if (!res) throw new Error("Asset not found.");
return res;
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 getScript(path: string): Promise<string> {
const res = this._scripts.get(this._parsePath(path));
public async getWgsl(path: string): Promise<NfFile> {
const res = this._wgsl.get(this._parsePath(path));
if (!res) throw new Error("Asset not found.");
return res;
return new NfFile(res);
}

private _parsePath(path: string): string {
Expand Down
19 changes: 19 additions & 0 deletions packages/asset-manager/src/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { type IFile } from "@nanoforge/common";

export class NfFile implements IFile {
private readonly _path: string;

constructor(path: string) {
this._path = path;
}

get path(): string {
return this._path;
}

public async getText(): Promise<string> {
const res = await fetch(this._path);
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`);
return await res.text();
}
}
1 change: 1 addition & 0 deletions packages/asset-manager/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { AssetManagerLibrary } from "./asset-manager.library";
export { NfFile } from "./file";
25 changes: 18 additions & 7 deletions packages/asset-manager/test/asset-manager.library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,39 @@ describe("Asset Manager Library", () => {
canvas: null,
files: {
assets: new Map([["/test.png", "blob:http://localhost:3000/test.png"]]),
scripts: new Map([["/test.wasm", "blob:http://localhost:3000/test.wasm"]]),
wasm: new Map([["/test.wasm", "blob:http://localhost:3000/test.wasm"]]),
wgsl: new Map([["/test.wgsl", "blob:http://localhost:3000/test.wgsl"]]),
},
});
library.init(context);

it("Test get asset", async () => {
it("Should get asset", async () => {
await expect(library.getAsset("test.png")).resolves.toEqual(
"blob:http://localhost:3000/test.png",
);
});

it("Test get unknown asset", async () => {
it("Should throw on unknown asset", async () => {
await expect(library.getAsset("test-unknown.png")).rejects.toThrow();
});

it("Test get script", async () => {
await expect(library.getScript("test.wasm")).resolves.toEqual(
it("Should get wasm", async () => {
await expect(library.getWasm("test.wasm")).resolves.toEqual(
"blob:http://localhost:3000/test.wasm",
);
});

it("Test get unknown script", async () => {
await expect(library.getScript("test-unknown.wasm")).rejects.toThrow();
it("Should throw on unknown wasm", async () => {
await expect(library.getWasm("test-unknown.wasm")).rejects.toThrow();
});

it("Should get wgsl", async () => {
await expect(library.getWgsl("test.wgsl")).resolves.toEqual(
"blob:http://localhost:3000/test.wgsl",
);
});

it("Should throw on unknown wgsl", async () => {
await expect(library.getWgsl("test-unknown.wgsl")).rejects.toThrow();
});
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { type InitContext } from "../../../context";
import type { IAssetManagerLibrary } from "../interfaces";
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): Promise<string>;

public abstract getScript(path: string): Promise<string>;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { type IExposedLibrary } from "../bases/exposed.library.type";

export interface IAssetManagerLibrary extends IExposedLibrary {
getAsset(path: string): Promise<string>;

getScript(path: string): Promise<string>;
}
export interface IAssetManagerLibrary extends IExposedLibrary {}
2 changes: 1 addition & 1 deletion packages/common/src/library/libraries/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { IExposedLibrary } from "./bases/exposed.library.type";
export { IRunnerLibrary } from "./bases/runner.library.type";
export { IAssetManagerLibrary } from "./finals/asset-manager.library.type";
export { IAssetManagerLibrary, IFile } from "./finals/asset-manager.library.type";
export { IComponentSystemLibrary } from "./finals/component-system.library.type";
export { IGraphicsLibrary } from "./finals/graphics.library.type";
export { INetworkLibrary } from "./finals/network.library.type";
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type LibraryContext } from "../../../context";
import { type ILibrary } from "../../libraries/library.type";
import { type ILibrary } from "../../libraries";
import { LibraryHandle } from "../handle/library.handle";

export class BaseLibraryManager {
Expand All @@ -11,7 +11,7 @@ export class BaseLibraryManager {
/**
* @todo Add error management
*/
public get(sym: symbol): LibraryHandle {
public get<T extends ILibrary = ILibrary>(sym: symbol): LibraryHandle<T> {
const index = this._librariesIndex[sym];
if (!index) throw new Error(`Library not found: ${Symbol.keyFor(sym)}`);
return this._get(index, sym);
Expand Down
20 changes: 12 additions & 8 deletions packages/common/src/library/manager/managers/library.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,23 @@ export class LibraryManager extends BaseLibraryManager {
}
}

public getComponentSystem(): LibraryHandle<IComponentSystemLibrary> {
return this._get<IComponentSystemLibrary>(DefaultLibrariesEnum.COMPONENT_SYSTEM);
public getComponentSystem<
T extends IComponentSystemLibrary = IComponentSystemLibrary,
>(): LibraryHandle<T> {
return this._get<T>(DefaultLibrariesEnum.COMPONENT_SYSTEM);
}

public getGraphics(): LibraryHandle<IGraphicsLibrary> {
return this._get<IGraphicsLibrary>(DefaultLibrariesEnum.GRAPHICS);
public getGraphics<T extends IGraphicsLibrary = IGraphicsLibrary>(): LibraryHandle<T> {
return this._get<T>(DefaultLibrariesEnum.GRAPHICS);
}

public getNetwork(): LibraryHandle<INetworkLibrary> {
return this._get<INetworkLibrary>(DefaultLibrariesEnum.NETWORK);
public getNetwork<T extends INetworkLibrary = INetworkLibrary>(): LibraryHandle<T> {
return this._get<T>(DefaultLibrariesEnum.NETWORK);
}

public getAssetManager(): LibraryHandle<IAssetManagerLibrary> {
return this._get<IAssetManagerLibrary>(DefaultLibrariesEnum.ASSET_MANAGER);
public getAssetManager<
T extends IAssetManagerLibrary = IAssetManagerLibrary,
>(): LibraryHandle<T> {
return this._get<T>(DefaultLibrariesEnum.ASSET_MANAGER);
}
}
3 changes: 2 additions & 1 deletion packages/common/src/options/types/options.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface IRunOptions {
canvas: HTMLCanvasElement;
files: {
assets: Map<string, string>;
scripts: Map<string, string>;
wasm: Map<string, string>;
wgsl: Map<string, string>;
};
}
Loading
Loading