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
15 changes: 9 additions & 6 deletions e2e/game/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import { exampleSystem } from "./systems/example.system";
export async function main(options: IRunOptions) {
const app = NanoforgeFactory.createClient({
tickRate: 60,
environment: {
serverAddress: "127.0.0.1",
serverTcpPort: "4445",
serverUdpPort: "4444",
},
});

const assetManager = new AssetManagerLibrary();
Expand All @@ -37,7 +32,15 @@ export async function main(options: IRunOptions) {
app.useNetwork(network);
app.use(Symbol("music"), music);

await app.init(options);
await app.init({
...options,
env: {
...options.env,
SERVER_ADDRESS: "127.0.0.1",
SERVER_TCP_PORT: "4445",
SERVER_UDP_PORT: "4444",
},
});

const registry = ecs.registry;

Expand Down
11 changes: 9 additions & 2 deletions example/pong-network/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { controlPlayer, draw, move, packetHandler } from "./systems";

export const app = NanoforgeFactory.createClient({
tickRate: 60,
environment: { serverTcpPort: "4445", serverUdpPort: "4444", serverAddress: "127.0.0.1" },
});

export const layer = new Layer();
Expand All @@ -30,7 +29,15 @@ export const main = async (options: IRunOptions) => {
app.useAssetManager(assetManager);
app.useInput(input);

await app.init(options);
await app.init({
...options,
env: {
...options.env,
SERVER_TCP_PORT: "4445",
SERVER_UDP_PORT: "4444",
SERVER_ADDRESS: "127.0.0.1",
},
});

const registry = ecsLibrary.registry;

Expand Down
6 changes: 4 additions & 2 deletions example/pong-network/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { bounce, move, packetHandler } from "./systems";

export const app = NanoforgeFactory.createServer({
tickRate: 60,
environment: { listeningTcpPort: "4445", listeningUdpPort: "4444" },
});

export const main = async (options: IRunOptions) => {
Expand All @@ -21,7 +20,10 @@ export const main = async (options: IRunOptions) => {
app.useNetwork(network);
app.useAssetManager(assetManager);

await app.init(options);
await app.init({
...options,
env: { ...options.env, LISTENING_TCP_PORT: "4445", LISTENING_UDP_PORT: "4444" },
});

const registry = ecsLibrary.registry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BaseContext } from "./base.context";
export class InitContext extends BaseContext {
private readonly _canvas: IRunClientOptions["canvas"] | undefined;
private readonly _files: IRunOptions["files"];
private readonly _env: Record<string, string | undefined>;
private readonly _config: IConfigRegistry;

constructor(
Expand All @@ -18,6 +19,7 @@ export class InitContext extends BaseContext {

this._canvas = (options as IRunClientOptions)["canvas"];
this._files = options.files;
this._env = options.env;
this._config = configRegistry;
}

Expand All @@ -29,6 +31,10 @@ export class InitContext extends BaseContext {
return this._files;
}

get env(): Record<string, string | undefined> {
return this._env;
}

get config(): IConfigRegistry {
return this._config;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/options/types/options.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ export type IRunOptions = IRunClientOptions | IRunServerOptions;
export interface IRunClientOptions {
canvas: HTMLCanvasElement;
files: Map<string, string>;
env: Record<string, string | undefined>;
}

export interface IRunServerOptions {
files: Map<string, string>;
env: Record<string, string | undefined>;
}
46 changes: 33 additions & 13 deletions packages/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,47 @@ bun add @nanoforge-dev/config

## Warning

This library is of exclusive usage for other libraries. To put variables in the environment to allow libraries to use it through this config library, you must put it in factory options :
This library is of exclusive usage for other libraries. To put variables in the environment to allow libraries to use it through this config library, you must put it in `.env` file at the root of your project (or in your environment) :

```dotenv
NANOFORGE_CLIENT_SERVER_TCP_PORT=4445
NANOFORGE_CLIENT_SERVER_UDP_PORT=4444
NANOFORGE_CLIENT_SERVER_ADDRESS=127.0.0.1
```

You must prefix your variables according to this table :

| Prefix | Availability |
| :------------------ | :----------------------- |
| `NANOFORGE_CLIENT_` | Available in client only |
| `NANOFORGE_SERVER_` | Available in server only |
| `NANOFORGE_` | Available in both apps |

⚠️ Prefixs are removed in libs

You can also put it in init options (every value must be string or undefined) :

```ts
import { type IRunOptions } from "@nanoforge-dev/common";
import { NanoforgeFactory } from "@nanoforge-dev/core";
import { NetworkLibrary } from "@nanoforge-dev/network";

export async function main(options: IRunOptions) {
const app = NanoforgeFactory.createClient({
environment: {
serverTcpPort: "4445",
serverUdpPort: "4444",
serverAddress: "127.0.0.1",
},
});
const app = NanoforgeFactory.createClient();

const network = new NetworkLibrary();

app.useNetwork(network);

await app.init(options);
await app.init({
...options,
env: {
...options.env,
SERVER_TCP_PORT: "4445",
SERVER_UDP_PORT: "4444",
SERVER_ADDRESS: "127.0.0.1",
},
});

await app.run();
}
Expand Down Expand Up @@ -92,23 +112,23 @@ export class ClientConfigNetwork {
@Expose()
@IsOptional()
@IsPort()
serverTcpPort?: string;
SERVER_TCP_PORT?: string;

@Expose()
@IsOptional()
@IsPort()
serverUdpPort?: string;
SERVER_UDP_PORT?: string;

// This var must be ip address or fqdn (it cannot be undefined)
@Expose()
@IsIpOrFQDN()
serverAddress?: string;
SERVER_ADDRESS?: string;

// This var must be a byte length between 2 and 64. It can be undefined as it as a default value.
@Expose()
@Default("PACKET_END")
@IsByteLength(2, 64)
magicValue!: string;
MAGIC_VALUE!: string;
}
```

Expand Down
1 change: 1 addition & 0 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./exports";
export * from "./default";
export * from "./transformers";
export * from "./validators";
9 changes: 9 additions & 0 deletions packages/config/src/transformers/boolean.transformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Transform } from "class-transformer";

const transformStringToBoolean = ({ value }: { value: string }) => {
if (value === "true") return true;
if (value === "false") return false;
return undefined;
};

export const TransformToBoolean = () => Transform(transformStringToBoolean);
1 change: 1 addition & 0 deletions packages/config/src/transformers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { TransformToBoolean } from "./boolean.transformer";
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export interface IApplicationOptions {
tickRate: number;
environment: Record<string, unknown>;
}
1 change: 0 additions & 1 deletion packages/core/src/application/nanoforge-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export abstract class NanoforgeApplication {

this._options = {
tickRate: 60,
environment: {},
...(options ?? {}),
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Core {

public async init(options: IRunOptions, appOptions: IApplicationOptions): Promise<void> {
this.options = appOptions;
this._configRegistry = new ConfigRegistry(appOptions.environment);
this._configRegistry = new ConfigRegistry(options.env);
await this.runInit(this.getInitContext(options));
}

Expand Down
22 changes: 11 additions & 11 deletions packages/network-client/src/client.network.library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ export class NetworkClientLibrary extends BaseNetworkLibrary {
public override async __init(context: InitContext): Promise<void> {
const config: ClientConfigNetwork = await context.config.registerConfig(ClientConfigNetwork);

if (config.serverTcpPort === undefined && config.serverUdpPort === undefined) {
if (config.SERVER_TCP_PORT === undefined && config.SERVER_UDP_PORT === undefined) {
throw new NfConfigException("No server port specified to connect", this.__name);
}

if (config.serverTcpPort !== undefined) {
if (config.SERVER_TCP_PORT !== undefined) {
this.tcp = new TCPClient(
+config.serverTcpPort,
config.serverAddress,
config.magicValue,
config.wss,
+config.SERVER_TCP_PORT,
config.SERVER_ADDRESS,
config.MAGIC_VALUE,
config.WSS,
);
await this.tcp.connect();
}

if (config.serverUdpPort !== undefined) {
if (config.SERVER_UDP_PORT !== undefined) {
this.udp = new UDPClient(
+config.serverUdpPort,
config.serverAddress,
config.magicValue,
config.wss,
+config.SERVER_UDP_PORT,
config.SERVER_ADDRESS,
config.MAGIC_VALUE,
config.WSS,
);
await this.udp.connect();
}
Expand Down
12 changes: 7 additions & 5 deletions packages/network-client/src/config.client.network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,32 @@ import {
IsIpOrFQDN,
IsOptional,
IsPort,
TransformToBoolean,
} from "@nanoforge-dev/config";

export class ClientConfigNetwork {
@Expose()
@IsOptional()
@IsPort()
serverTcpPort?: string;
SERVER_TCP_PORT?: string;

@Expose()
@IsOptional()
@IsPort()
serverUdpPort?: string;
SERVER_UDP_PORT?: string;

@Expose()
@IsIpOrFQDN()
serverAddress!: string;
SERVER_ADDRESS!: string;

@Expose()
@Default("PACKET_END")
@IsByteLength(2, 64)
magicValue!: string;
MAGIC_VALUE!: string;

@Expose()
@TransformToBoolean()
@IsBoolean()
@Default(false)
wss!: boolean;
WSS!: boolean;
}
26 changes: 13 additions & 13 deletions packages/network-client/test/client.network.library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,29 @@ describe("NetworkClientLibrary", () => {

describe("config validation", () => {
it("should throw NfConfigException when neither TCP nor UDP port is provided", async () => {
const ctx = makeContext({ serverAddress: "127.0.0.1", magicValue: "END" });
const ctx = makeContext({ SERVER_ADDRESS: "127.0.0.1", MAGIC_VALUE: "END" });
await expect(new NetworkClientLibrary().__init(ctx)).rejects.toThrow(NfConfigException);
});
});

describe("initialization", () => {
it("should initialize a TCP client when only serverTcpPort is provided", async () => {
it("should initialize a TCP client when only SERVER_TCP_PORT is provided", async () => {
const ctx = makeContext({
serverTcpPort: "8080",
serverAddress: "127.0.0.1",
magicValue: "END",
SERVER_TCP_PORT: "8080",
SERVER_ADDRESS: "127.0.0.1",
MAGIC_VALUE: "END",
});
const lib = new NetworkClientLibrary();
await lib.__init(ctx);
expect(lib.tcp).toBeDefined();
expect(lib.udp).toBeUndefined();
});

it("should initialize a UDP client when only serverUdpPort is provided", async () => {
it("should initialize a UDP client when only SERVER_UDP_PORT is provided", async () => {
const ctx = makeContext({
serverUdpPort: "8081",
serverAddress: "127.0.0.1",
magicValue: "END",
SERVER_UDP_PORT: "8081",
SERVER_ADDRESS: "127.0.0.1",
MAGIC_VALUE: "END",
});
const lib = new NetworkClientLibrary();
await lib.__init(ctx);
Expand All @@ -92,10 +92,10 @@ describe("NetworkClientLibrary", () => {

it("should initialize both TCP and UDP clients when both ports are provided", async () => {
const ctx = makeContext({
serverTcpPort: "8080",
serverUdpPort: "8081",
serverAddress: "127.0.0.1",
magicValue: "END",
SERVER_TCP_PORT: "8080",
SERVER_UDP_PORT: "8081",
SERVER_ADDRESS: "127.0.0.1",
MAGIC_VALUE: "END",
});
const lib = new NetworkClientLibrary();
await lib.__init(ctx);
Expand Down
Loading