Skip to content

Commit

Permalink
feat: support install script by use function
Browse files Browse the repository at this point in the history
  • Loading branch information
dockfries committed Oct 5, 2022
1 parent d811cc4 commit c4f146c
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "omp-node-lib",
"version": "0.3.6",
"version": "0.3.7",
"description": "Better with omp-node-ts",
"main": "dist/bundle.js",
"types": "dist/bundle.d.ts",
Expand Down
89 changes: 85 additions & 4 deletions src/controllers/gamemode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import {
import * as fns from "@/wrapper/functions";
import { logger } from "@/logger";
import {
NOOP,
OnRconCommand,
OnRconLoginAttempt,
promisifyCallback,
} from "@/utils/helperUtils";
import * as ow from "omp-wrapper";
import { defaultCharset } from "./settings";
import { TCommonCallback } from "@/types";
import { TCommonCallback, TFilterScript } from "@/types";

export abstract class BaseGameMode {
private static preInstallScripts: Array<TFilterScript> = [];
private static installedScripts: Array<TFilterScript> = [];
public static charset = defaultCharset;
private initialized = false;

Expand All @@ -41,9 +44,11 @@ export abstract class BaseGameMode {
"onIncomingConnection"
)
);
OnRconCommand(
promisifyCallback.call(this, this.onRconCommand, "onRconCommand")
);
OnRconCommand((command: string): number => {
const regCmd = command.trim().match(/[^/\s]+/gi);
if (regCmd) this.promiseRconCmd(regCmd);
return 1;
});
OnRconLoginAttempt(
promisifyCallback.call(
this,
Expand All @@ -63,6 +68,19 @@ export abstract class BaseGameMode {
fns.GameModeExit();
}

// like vue.js, support filter script which use omp-node-lib
public use(plugin: TFilterScript, ...options: Array<any>): void | this {
const { preInstallScripts, installedScripts } = BaseGameMode;
if (installedScripts.some((fs) => fs === plugin)) {
logger.warn(`[BaseGameMode]: Script has already been applied`);
return;
}
plugin.load = plugin.load.bind(plugin, this, ...options);
preInstallScripts.push(plugin);
BaseGameMode.loadScript(plugin.name);
return this;
}

public static supportAllNickname() {
/**
* In utf8, different national languages take up different numbers of bytes,
Expand Down Expand Up @@ -228,6 +246,69 @@ export abstract class BaseGameMode {
}
return 1;
}
private async promiseRconCmd(command: RegExpMatchArray): Promise<any> {
const firstLevel = command[0];
let fnRes = this.onRconCommand(command.join(" "));
if (fnRes instanceof Promise) fnRes = await fnRes;
if (!fnRes) return NOOP("OnRconCommandI18n");
const scriptName = command[1];
if (!scriptName) return;
if (firstLevel === "loadfs") {
BaseGameMode.loadScript(scriptName);
return;
}
if (firstLevel === "unloadfs") {
BaseGameMode.unloadScript(scriptName);
return;
}
if (firstLevel === "reloadfs") {
BaseGameMode.unloadScript(scriptName);
BaseGameMode.loadScript(scriptName);
return;
}
}
private static loadScript(scriptName: string): void {
setTimeout(async () => {
try {
const { preInstallScripts, installedScripts } = BaseGameMode;
const fsIdx = preInstallScripts.findIndex(
(fs) => fs.name === scriptName
);
if (fsIdx === -1) return;

const fs = preInstallScripts[fsIdx];
const loadFn = fs.load;
if (loadFn instanceof Promise) await loadFn();
else loadFn();

preInstallScripts.splice(fsIdx, 1);
installedScripts.push(fs);
} catch (err) {
logger.error(`[BaseGameMode]: script ${scriptName} load fail`);
}
});
}
private static unloadScript(scriptName: string): void {
setTimeout(async () => {
try {
const { preInstallScripts, installedScripts } = BaseGameMode;
const fsIdx = installedScripts.findIndex(
(fs) => fs.name === scriptName
);
if (fsIdx === -1) return;

const fs = installedScripts[fsIdx];
const unloadFn = fs.unload;
if (unloadFn instanceof Promise) await unloadFn();
else unloadFn();

installedScripts.splice(fsIdx, 1);
preInstallScripts.push(fs);
} catch (err) {
logger.error(`[BaseGameMode]: script ${scriptName} unload fail`);
}
});
}
public static findModelFileNameFromCRC = fns.FindModelFileNameFromCRC;
public static findTextureFileNameFromCRC = fns.FindTextureFileNameFromCRC;
public static getWeaponName = fns.GetWeaponName;
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/player/playerEvent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
NOOP,
OnPlayerCommandText,
OnPlayerText,
promisifyCallback,
Expand Down Expand Up @@ -366,12 +367,11 @@ export abstract class BasePlayerEvent<P extends BasePlayer> {
player.lastDrunkLevel = nowDrunkLevel;
}, 1000);
private async promiseCommand(p: P, cmd: RegExpMatchArray): Promise<any> {
const NOOP = () => promisifyCallback(() => 0, "OnPlayerCommandTextI18n");
const fullCommand = cmd.join(" ");

let rFnRes = this.onCommandReceived(p, fullCommand);
if (rFnRes instanceof Promise) rFnRes = await rFnRes;
if (!rFnRes) return NOOP();
if (!rFnRes) return NOOP("OnPlayerCommandTextI18n");

/**
* Use eventBus to observe and subscribe to level 1 instructions,
Expand All @@ -382,7 +382,7 @@ export abstract class BasePlayerEvent<P extends BasePlayer> {
if (idx > -1 || (await this.cmdBus.emit(p, idx, cmd.slice(1)))) {
let pFnRes = this.onCommandPerformed(p, fullCommand);
if (pFnRes instanceof Promise) pFnRes = await pFnRes;
if (!pFnRes) return NOOP();
if (!pFnRes) return NOOP("OnPlayerCommandTextI18n");
return;
}

Expand Down
7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ export type TDynamicAreaTypes =
| "polygon";

export type TCommonCallback = number | Promise<number>;

export type TFilterScript = {
name: string;
load: (...args: Array<any>) => any;
unload: () => any;
[propName: string | number | symbol]: any;
};
3 changes: 3 additions & 0 deletions src/utils/helperUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,6 @@ export const promisifyCallback = function (
return retNum;
};
};

export const NOOP = (cbName: string, unhandled = 0) =>
promisifyCallback(() => unhandled, cbName);

0 comments on commit c4f146c

Please sign in to comment.