Skip to content

Commit

Permalink
refactor: move registry/dispatcher specific logic to init methods
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxuum committed Jul 31, 2024
1 parent 1fe6b4f commit a179a92
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 59 deletions.
54 changes: 3 additions & 51 deletions packages/core/src/client/core.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { RunService, UserInputService } from "@rbxts/services";
import { CommandShortcut } from "../shared";
import { RunService } from "@rbxts/services";
import { DEFAULT_CONFIG } from "../shared/config";
import { BaseCommand } from "../shared/core/command";
import { getRemotes } from "../shared/network";
import { CenturionLogger } from "../shared/util/log";
import { ClientDispatcher } from "./dispatcher";
import { ClientRegistry } from "./registry";
import { ClientConfig } from "./types";
import { getShortcutKeycodes, isShortcutContext } from "./util";

export class CenturionClient {
private started = false;
private readonly logger: CenturionLogger;
readonly registry: ClientRegistry;
readonly dispatcher: ClientDispatcher;
readonly config: ClientConfig;
Expand Down Expand Up @@ -51,7 +46,6 @@ export class CenturionClient {
};
this.registry = new ClientRegistry(this.config);
this.dispatcher = new ClientDispatcher(this.config, this.registry);
this.logger = new CenturionLogger(this.config.logLevel, "Core");
}

/**
Expand All @@ -60,25 +54,10 @@ export class CenturionClient {
* @param callback A callback that is called after the registry has been initialized.
*/
async start(callback?: (registry: ClientRegistry) => void) {
this.logger.assert(!this.started, "Centurion has already been started");
assert(!this.started, "Centurion has already been started");

this.registry.init();
if (this.config.registerBuiltInCommands) {
const commands =
script.Parent?.WaitForChild("builtin").WaitForChild("commands");
this.logger.assert(
commands !== undefined,
"Failed to locate built-in commands",
);
this.registry.load(commands);
}

if (this.config.shortcutsEnabled) {
this.registry.commandRegistered.Connect((command) =>
this.registerShortcuts(command),
);
}

this.dispatcher.init();
callback?.(this.registry);
await this.registry.sync();
this.started = true;
Expand All @@ -88,31 +67,4 @@ export class CenturionClient {
config: this.config,
});
}

private registerShortcuts(command: BaseCommand) {
if (command.options.shortcuts === undefined) return;

const commandPath = command.getPath();
for (const shortcut of command.options.shortcuts) {
const keys = new Set(getShortcutKeycodes(shortcut as CommandShortcut));
const keyCount = keys.size();
if (keyCount === 0) continue;

const args = isShortcutContext(shortcut) ? shortcut.arguments : undefined;
UserInputService.InputBegan.Connect((input, gameProcessed) => {
if (gameProcessed) return;
if (input.UserInputType !== Enum.UserInputType.Keyboard) return;

const keysPressed = UserInputService.GetKeysPressed();
if (
keysPressed.size() !== keyCount ||
!keysPressed.every((key) => keys.has(key.KeyCode))
) {
return;
}

this.dispatcher.run(commandPath, args);
});
}
}
}
41 changes: 39 additions & 2 deletions packages/core/src/client/dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { Signal } from "@rbxts/beacon";
import { Players } from "@rbxts/services";
import { RegistryPath } from "../shared";
import { Players, UserInputService } from "@rbxts/services";
import { CommandShortcut, RegistryPath } from "../shared";
import { BaseCommand } from "../shared/core/command";
import { BaseDispatcher } from "../shared/core/dispatcher";
import { getInputText } from "../shared/util/string";
import { ClientConfig, HistoryEntry } from "./types";
import { getShortcutKeycodes, isShortcutContext } from "./util";

export class ClientDispatcher extends BaseDispatcher<ClientConfig> {
private readonly history: HistoryEntry[] = [];
readonly historyUpdated = new Signal<[history: HistoryEntry[]]>();

init() {
if (this.config.shortcutsEnabled) {
this.registry.commandRegistered.Connect((command) =>
this.handleShortcuts(command),
);
}
}

/**
* Executes a command.
*
Expand Down Expand Up @@ -89,4 +99,31 @@ export class ClientDispatcher extends BaseDispatcher<ClientConfig> {
this.history.clear();
this.historyUpdated.Fire(this.history);
}

private handleShortcuts(command: BaseCommand) {
if (command.options.shortcuts === undefined) return;

const commandPath = command.getPath();
for (const shortcut of command.options.shortcuts) {
const keys = new Set(getShortcutKeycodes(shortcut as CommandShortcut));
const keyCount = keys.size();
if (keyCount === 0) continue;

const args = isShortcutContext(shortcut) ? shortcut.arguments : undefined;
UserInputService.InputBegan.Connect((input, gameProcessed) => {
if (gameProcessed) return;
if (input.UserInputType !== Enum.UserInputType.Keyboard) return;

const keysPressed = UserInputService.GetKeysPressed();
if (
keysPressed.size() !== keyCount ||
!keysPressed.every((key) => keys.has(key.KeyCode))
) {
return;
}

this.run(commandPath, args);
});
}
}
}
13 changes: 13 additions & 0 deletions packages/core/src/client/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ export class ClientRegistry extends BaseRegistry<ClientConfig> {
private initialSyncReceived = false;
private syncedPaths = new Set<string>();

init() {
super.init();
if (this.config.registerBuiltInCommands) {
const commands =
script.Parent?.WaitForChild("builtin").WaitForChild("commands");
this.logger.assert(
commands !== undefined,
"Failed to locate built-in commands",
);
this.load(commands);
}
}

/**
* Begins registry synchronisation to the server.
*
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/server/core.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { RunService } from "@rbxts/services";
import { DEFAULT_CONFIG } from "../shared/config";
import { getRemotes } from "../shared/network";
import { CenturionLogger } from "../shared/util/log";
import { ServerDispatcher } from "./dispatcher";
import { ServerRegistry } from "./registry";
import { ServerConfig } from "./types";

export class CenturionServer {
private started = false;
private readonly logger: CenturionLogger;
readonly registry: ServerRegistry;
readonly dispatcher: ServerDispatcher;
readonly config: Readonly<ServerConfig>;
Expand Down Expand Up @@ -47,7 +45,6 @@ export class CenturionServer {
});
this.registry = new ServerRegistry(this.config);
this.dispatcher = new ServerDispatcher(this.config, this.registry);
this.logger = new CenturionLogger(this.config.logLevel, "Core");
}

/**
Expand All @@ -56,10 +53,10 @@ export class CenturionServer {
* @param callback A callback that is called after the registry has been initialized.
*/
async start(callback?: (registry: ServerRegistry) => void) {
this.logger.assert(!this.started, "Centurion has already been started");
assert(!this.started, "Centurion has already been started");

this.dispatcher.init();
this.registry.init();
this.dispatcher.init();
callback?.(this.registry);
this.started = true;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/shared/core/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export abstract class BaseDispatcher<C extends SharedConfig> {

constructor(
protected readonly config: C,
private readonly registry: BaseRegistry<C>,
protected readonly registry: BaseRegistry<C>,
) {
this.logger = new CenturionLogger(config.logLevel, "Dispatcher");
}
Expand Down

0 comments on commit a179a92

Please sign in to comment.