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

export class AssetManagerLibrary extends BaseAssetManagerLibrary {
private _assets: Map<string, string>;
private _assets?: Map<string, string>;

get __name(): string {
return "AssetManagerLibrary";
Expand All @@ -12,6 +12,7 @@ export class AssetManagerLibrary extends BaseAssetManagerLibrary {
}

public getAsset(path: string): NfFile {
if (!this._assets) this.throwNotInitializedError();
const res = this._assets.get(this._parsePath(path));
if (!res) throw new NfNotFound(path, "Asset");
return new NfFile(res);
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/context/contexts/library.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export enum LibraryStatusEnum {
}

export class LibraryContext {
protected _status: LibraryStatusEnum;
protected _status: LibraryStatusEnum = LibraryStatusEnum.UNLOADED;

get status() {
return this._status;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NfException } from "../abstracts/exception.abstract";

export class NfNotInitializedException extends NfException {
get code(): number {
return 404;
}

constructor(item: string, type?: string) {
super(`${type ? `${type} - ` : ""}${item} not initialized.`);
}
}
3 changes: 2 additions & 1 deletion packages/common/src/exception/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { NfNotFound } from "./exceptions/not-found.exception";
export { NfFetchException } from "./exceptions/fetch.exception";
export { NfNotFound } from "./exceptions/not-found.exception";
export { NfNotInitializedException } from "./exceptions/not-initialized.exception";
5 changes: 5 additions & 0 deletions packages/common/src/library/libraries/library.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type ClearContext, type InitContext } from "../../context";
import { NfNotInitializedException } from "../../exception";
import { RelationshipHandler } from "../relationship/relationship-handler";
import { DEFAULT_LIBRARY_OPTIONS } from "./consts/library-options-default.const";
import { type ILibrary, type ILibraryOptions } from "./library.type";
Expand Down Expand Up @@ -30,4 +31,8 @@ export abstract class Library implements ILibrary {

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public async __clear(_context: ClearContext): Promise<void> {}

protected throwNotInitializedError(): never {
throw new NfNotInitializedException(this.__name, "Library");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { LibraryHandle } from "../handle/library.handle";

export class BaseLibraryManager {
protected _libraries: LibraryHandle[] = [];
private _librariesIndex: {
[sym: symbol]: number;
};
private _librariesIndex: Record<symbol, number> = {};

/**
* @todo Add error management
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/application/nanoforge-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type ILibrary,
type INetworkLibrary,
type IRunOptions,
NfNotInitializedException,
} from "@nanoforge/common";

import { EditableApplicationContext } from "../common/context/contexts/application.editable-context";
Expand All @@ -13,7 +14,7 @@ import type { IApplicationOptions } from "./application-options.type";

export abstract class NanoforgeApplication {
protected applicationConfig: ApplicationConfig;
private _core: Core;
private _core?: Core;
private readonly _options: IApplicationOptions;

constructor(options?: Partial<IApplicationOptions>) {
Expand Down Expand Up @@ -51,6 +52,7 @@ export abstract class NanoforgeApplication {
}

public run() {
return this._core.run();
if (!this._core) throw new NfNotInitializedException("Core");
return this._core?.run();
}
}
8 changes: 6 additions & 2 deletions packages/core/src/common/library/manager/library.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import {
import { EditableLibraryContext } from "../../context/contexts/library.editable-context";
import { Relationship } from "../relationship-functions";

const hasMethod = (obj: any, method: string) => {
return typeof obj[method] === "function";
};

export class EditableLibraryManager extends LibraryManager {
public set(sym: symbol, library: ILibrary) {
this.setNewLibrary(sym, library, new EditableLibraryContext());
Expand Down Expand Up @@ -90,13 +94,13 @@ export class EditableLibraryManager extends LibraryManager {

public getMutableLibraries(): LibraryHandle<IMutableLibrary>[] {
return this._libraries.filter(
(handle) => handle && typeof handle.library["mute"] === "function",
(handle) => handle && hasMethod(handle.library, "mute"),
) as LibraryHandle<IMutableLibrary>[];
}

private _getRunnerLibraries(): LibraryHandle<IRunnerLibrary>[] {
return this._libraries.filter(
(handle) => handle && typeof handle.library["__run"] === "function",
(handle) => handle && hasMethod(handle.library, "__run"),
) as LibraryHandle<IRunnerLibrary>[];
}
}
9 changes: 7 additions & 2 deletions packages/core/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
InitContext,
type LibraryHandle,
LibraryStatusEnum,
NfNotInitializedException,
} from "@nanoforge/common";

import { type ApplicationConfig } from "../application/application-config";
Expand All @@ -19,8 +20,8 @@ import { ConfigRegistry } from "../config/config-registry";
export class Core {
private readonly config: ApplicationConfig;
private readonly context: ApplicationContext;
private options: IApplicationOptions;
private _configRegistry: ConfigRegistry;
private options?: IApplicationOptions;
private _configRegistry?: ConfigRegistry;

constructor(config: ApplicationConfig, context: ApplicationContext) {
this.config = config;
Expand All @@ -34,6 +35,8 @@ export class Core {
}

public async run(): Promise<void> {
if (!this.options) throw new NfNotInitializedException("Core");

const context = this.getExecutionContext();
const clientContext = this.getClientContext();
const libraries = this.config.libraryManager.getExecutionLibraries();
Expand All @@ -58,6 +61,8 @@ export class Core {
}

private getInitContext(options: IRunOptions): InitContext {
if (!this._configRegistry) throw new NfNotInitializedException("Core");

return new InitContext(this.context, this.config.libraryManager, this._configRegistry, options);
}

Expand Down
1 change: 0 additions & 1 deletion packages/ecs/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ package-lock.json
yarn.lock
bun.lock

*.js
*.d.ts
build
23 changes: 0 additions & 23 deletions packages/ecs/public/libecs.d.ts

This file was deleted.

15 changes: 0 additions & 15 deletions packages/ecs/public/libecs.js

This file was deleted.

Binary file removed packages/ecs/public/libecs.wasm
Binary file not shown.
9 changes: 5 additions & 4 deletions packages/ecs/src/ecs-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import {
type InitContext,
} from "@nanoforge/common";

import type { MainModule, Registry } from "../lib";
import { Module } from "../lib";
import { type MainModule, Module, type Registry } from "../lib";

export class ECSLibrary extends BaseComponentSystemLibrary {
private module: MainModule;
private _registry: Registry;
private module?: MainModule;
private _registry?: Registry;

private readonly path: string = "libecs.wasm";

Expand All @@ -33,10 +32,12 @@ export class ECSLibrary extends BaseComponentSystemLibrary {
}

async __run(ctx: Context): Promise<void> {
if (!this._registry) this.throwNotInitializedError();
this._registry.runSystems(ctx);
}

get registry(): Registry {
if (!this._registry) this.throwNotInitializedError();
return this._registry;
}
}
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 { Component, System, Registry } from "../lib";
1 change: 0 additions & 1 deletion packages/ecs/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"outDir": ".",
"rootDir": ".",
"paths": {
"@lib": ["./lib"],
"@nanoforge/common": ["../common"]
}
},
Expand Down
3 changes: 2 additions & 1 deletion packages/graphics-2d/src/graphics-2d.library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { BaseGraphicsLibrary, type InitContext } from "@nanoforge/common";
import { Graphics } from ".";

export class Graphics2DLibrary extends BaseGraphicsLibrary {
private _stage: Graphics.Stage;
private _stage?: Graphics.Stage;

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

get stage(): Graphics.Stage {
if (!this._stage) this.throwNotInitializedError();
return this._stage;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/input/src/input.library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { InputHandler } from "./input-handler";
import { type InputEnum } from "./input.enum";

export class InputLibrary extends BaseInputLibrary {
private _inputHandler: InputHandler;
private _inputHandler?: InputHandler;

get __name(): string {
return "InputLibrary";
Expand All @@ -15,10 +15,12 @@ export class InputLibrary extends BaseInputLibrary {
}

public isKeyPressed(key: InputEnum): boolean {
if (!this._inputHandler) this.throwNotInitializedError();
return this._inputHandler.getKeyStatus(key);
}

public getPressedKeys(): InputEnum[] {
if (!this._inputHandler) this.throwNotInitializedError();
const res: InputEnum[] = [];
for (const rawKey in this._inputHandler.inputs) {
const key = rawKey as InputEnum;
Expand Down
11 changes: 6 additions & 5 deletions packages/music/src/music.library.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BaseMusicLibrary, NfNotFound } from "@nanoforge/common";

export class MusicLibrary extends BaseMusicLibrary {
private muted: boolean;
private musics: Map<string, HTMLAudioElement>;
private muted: boolean = true;
private musics?: Map<string, HTMLAudioElement>;
private current: HTMLAudioElement | null = null;

get __name(): string {
Expand All @@ -15,17 +15,17 @@ export class MusicLibrary extends BaseMusicLibrary {
}

public mute(): void {
if (!this.musics) this.throwNotInitializedError();
this.muted = !this.muted;
for (const key in this.musics) {
const element = this.musics[key];

for (const [, element] of this.musics) {
if (element) {
element.muted = this.muted;
}
}
}

public play(music: string): void {
if (!this.musics) this.throwNotInitializedError();
const musicElement = this.musics.get(music);

if (musicElement) {
Expand All @@ -44,6 +44,7 @@ export class MusicLibrary extends BaseMusicLibrary {
}

public load(music: string, file: string) {
if (!this.musics) this.throwNotInitializedError();
const element = new Audio(file);
if (element) {
element.muted = this.muted;
Expand Down
11 changes: 6 additions & 5 deletions packages/sound/src/sound.library.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BaseSoundLibrary, NfNotFound } from "@nanoforge/common";

export class SoundLibrary extends BaseSoundLibrary {
private muted: boolean;
private sounds: Map<string, HTMLAudioElement>;
private muted: boolean = true;
private sounds?: Map<string, HTMLAudioElement>;

get __name(): string {
return "NfSound";
Expand All @@ -14,17 +14,17 @@ export class SoundLibrary extends BaseSoundLibrary {
}

public mute(): void {
if (!this.sounds) this.throwNotInitializedError();
this.muted = !this.muted;
for (const key in this.sounds) {
const element = this.sounds[key];

for (const [, element] of this.sounds) {
if (element) {
element.muted = this.muted;
}
}
}

public play(sound: string): void {
if (!this.sounds) this.throwNotInitializedError();
const soundElement = this.sounds.get(sound);

if (soundElement) {
Expand All @@ -40,6 +40,7 @@ export class SoundLibrary extends BaseSoundLibrary {
}

public load(sound: string, file: string) {
if (!this.sounds) this.throwNotInitializedError();
const element = new Audio(file);
if (element) {
element.muted = this.muted;
Expand Down
2 changes: 0 additions & 2 deletions packages/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"composite": true,
"module": "commonjs",
"declaration": true,
"noImplicitAny": false,
"skipLibCheck": true,
"noUnusedLocals": false,
"importHelpers": true,
Expand All @@ -18,7 +17,6 @@
"strict": true,
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"strictPropertyInitialization": false,
"types": ["node"]
}
}
Loading