Skip to content

Commit

Permalink
Merge pull request #70 from rune-js/plugins
Browse files Browse the repository at this point in the history
Reworking the plugin a system a bit to be less rigid.
  • Loading branch information
TheBlackParade authored Feb 20, 2020
2 parents 9148d9a + a8d7e72 commit eb4cb1a
Show file tree
Hide file tree
Showing 26 changed files with 148 additions and 90 deletions.
35 changes: 19 additions & 16 deletions src/game-server.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
import * as net from 'net';
import yargs from 'yargs';
import { watch } from 'chokidar';

import { RsBuffer } from './net/rs-buffer';
import { World } from './world/world';
import { ClientConnection } from './net/client-connection';
import { logger } from '@runejs/logger';
import { GameCache } from '@runejs/cache-parser';
import { NpcActionPlugin, setNpcPlugins } from '@server/world/mob/player/action/npc-action';
import { ObjectActionPlugin, setObjectPlugins } from '@server/world/mob/player/action/object-action';
import { BASE_PLUGIN_DIRECTORY, loadPlugins } from '@server/plugins/plugin-loader';
import { ItemOnItemActionPlugin, setItemOnItemPlugins } from '@server/world/mob/player/action/item-on-item-action';
import { ButtonActionPlugin, setButtonPlugins } from '@server/world/mob/player/action/button-action';
import { setNpcPlugins } from '@server/world/mob/player/action/npc-action';
import { setObjectPlugins } from '@server/world/mob/player/action/object-action';
import { loadPlugins } from '@server/plugins/plugin-loader';
import { setItemOnItemPlugins } from '@server/world/mob/player/action/item-on-item-action';
import { setButtonPlugins } from '@server/world/mob/player/action/button-action';
import { parseServerConfig, ServerConfig } from '@server/world/config/server-config';
import { ActionPlugin, ActionType } from '@server/plugins/plugin';

export let serverConfig: ServerConfig;
export let gameCache: GameCache;
export let world: World;

export async function injectPlugins(): Promise<void> {
async function inject<T>(path: string): Promise<T[]> {
return await loadPlugins('.' + BASE_PLUGIN_DIRECTORY + '/' + path);
}
const actionTypes: { [key: string]: ActionPlugin[] } = {};
const plugins = await loadPlugins();

plugins.map(plugin => plugin.actions).reduce((a, b) => a.concat(b)).forEach(action => {
if(!actionTypes.hasOwnProperty(action.type)) {
actionTypes[action.type] = [];
}

const promises = [
inject<NpcActionPlugin>('npc').then(setNpcPlugins),
inject<ObjectActionPlugin>('object').then(setObjectPlugins),
inject<ItemOnItemActionPlugin>('item-on-item').then(setItemOnItemPlugins),
inject<ButtonActionPlugin>('buttons').then(setButtonPlugins)
];
actionTypes[action.type].push(action);
});

await Promise.all(promises);
setButtonPlugins(actionTypes[ActionType.BUTTON]);
setNpcPlugins(actionTypes[ActionType.NPC_ACTION]);
setObjectPlugins(actionTypes[ActionType.OBJECT_ACTION]);
setItemOnItemPlugins(actionTypes[ActionType.ITEM_ON_ITEM]);
}

export function runGameServer(): void {
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/buttons/dialogue-action-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buttonAction, ButtonActionPlugin } from '@server/world/mob/player/action/button-action';
import { buttonAction } from '@server/world/mob/player/action/button-action';
import { ActionType, RunePlugin } from '@server/plugins/plugin';

const dialogueActions: { [key: number]: number } = {
2494: 1, 2495: 2, 2496: 3, 2497: 4, 2498: 5,
Expand All @@ -14,4 +15,4 @@ export const action: buttonAction = (details) => {
player.dialogueInteractionEvent.next(dialogueActions[buttonId]);
};

export default { buttonIds, action } as ButtonActionPlugin;
export default new RunePlugin({ type: ActionType.BUTTON, buttonIds, action });
5 changes: 3 additions & 2 deletions src/plugins/buttons/logout-button-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { buttonAction, ButtonActionPlugin } from '@server/world/mob/player/action/button-action';
import { buttonAction } from '@server/world/mob/player/action/button-action';
import { ActionType, RunePlugin } from '@server/plugins/plugin';

export const action: buttonAction = (details) => {
const { player } = details;
player.logout();
};

export default { buttonIds: 2458, action } as ButtonActionPlugin;
export default new RunePlugin({ type: ActionType.BUTTON, buttonIds: 2458, action });
5 changes: 3 additions & 2 deletions src/plugins/buttons/player-setting-button-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buttonAction, ButtonActionPlugin } from '@server/world/mob/player/action/button-action';
import { buttonAction } from '@server/world/mob/player/action/button-action';
import { ActionType, RunePlugin } from '@server/plugins/plugin';

const buttonIds: number[] = [
152, 153, // walk/run
Expand All @@ -17,4 +18,4 @@ export const action: buttonAction = (details) => {
player.settingChanged(buttonId);
};

export default { buttonIds, action } as ButtonActionPlugin;
export default new RunePlugin({ type: ActionType.BUTTON, buttonIds, action });
9 changes: 0 additions & 9 deletions src/plugins/npc/lumbridge/bobs-axes/bob-plugin.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/plugins/npc/lumbridge/general-store/shopkeeper-plugin.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/plugins/npcs/bob-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { npcAction } from '@server/world/mob/player/action/npc-action';
import { openShop } from '@server/world/mob/player/action/shop-action';
import { ActionType, RunePlugin } from '@server/plugins/plugin';

const action: npcAction = (details) => {
const { player, npc } = details;
openShop(details.player, 'BOBS_AXES');
};

export default new RunePlugin({ type: ActionType.NPC_ACTION, npcIds: 519, options: 'trade', walkTo: true, action });
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { npcAction, NpcActionPlugin } from '@server/world/mob/player/action/npc-action';
import { npcAction } from '@server/world/mob/player/action/npc-action';
import { dialogueAction, DialogueEmote } from '@server/world/mob/player/action/dialogue-action';
import { ActionType, RunePlugin } from '@server/plugins/plugin';

const action: npcAction = (details) => {
const { player, npc } = details;
Expand Down Expand Up @@ -48,4 +49,4 @@ const action: npcAction = (details) => {
});
};

export default { npcIds: 0, options: 'talk-to', walkTo: true, action } as NpcActionPlugin;
export default new RunePlugin({ type: ActionType.NPC_ACTION, npcIds: 0, options: 'talk-to', walkTo: true, action });
11 changes: 11 additions & 0 deletions src/plugins/npcs/shopkeeper-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { npcAction } from '@server/world/mob/player/action/npc-action';
import { openShop } from '@server/world/mob/player/action/shop-action';
import { ActionType, RunePlugin } from '@server/plugins/plugin';


const action: npcAction = (details) => {
const { player, npc } = details;
openShop(details.player, 'LUMBRIDGE_GENERAL_STORE');
};

export default new RunePlugin({ type: ActionType.NPC_ACTION, npcIds: 520, options: 'trade', walkTo: true, action });
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { objectAction, ObjectActionPlugin } from '@server/world/mob/player/action/object-action';
import { objectAction } from '@server/world/mob/player/action/object-action';
import { gameCache } from "@server/game-server";
import { Position } from '@server/world/position';
import { ActionType, RunePlugin } from '@server/plugins/plugin';


export const action: objectAction = (details) => {
Expand All @@ -19,4 +20,4 @@ export const action: objectAction = (details) => {
}
};

export default {objectIds: [8689], options: ['milk'], walkTo: true, action} as ObjectActionPlugin;
export default new RunePlugin({ type: ActionType.OBJECT_ACTION, objectIds: [8689], options: ['milk'], walkTo: true, action });
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { directionData, WNES } from '@server/world/direction';
import { world } from '@server/game-server';
import { Chunk } from '@server/world/map/chunk';
import { objectAction, ObjectActionPlugin } from '@server/world/mob/player/action/object-action';
import { objectAction } from '@server/world/mob/player/action/object-action';
import { ActionType, RunePlugin } from '@server/plugins/plugin';

// @TODO move to yaml config
const doors = [
Expand Down Expand Up @@ -85,5 +86,5 @@ export const action: objectAction = (details): void => {
player.packetSender.playSound(opening ? 318 : 326, 7);
};

export default { objectIds: [1530, 4465, 4467, 3014, 3017, 3018, 3019, 1536, 1537, 1533, 1531, 1534, 12348], options: [ 'open', 'close' ],
walkTo: true, action } as ObjectActionPlugin;
export default new RunePlugin({ type: ActionType.OBJECT_ACTION, objectIds: [1530, 4465, 4467, 3014, 3017, 3018,
3019, 1536, 1537, 1533, 1531, 1534, 12348], options: [ 'open', 'close' ], walkTo: true, action });
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Position } from '@server/world/position';
import { WNES } from '@server/world/direction';
import { logger } from '@runejs/logger/dist/logger';
import { world } from '@server/game-server';
import { action as doorAction } from '@server/plugins/object/doors/door-plugin';
import { objectAction, ObjectActionPlugin } from '@server/world/mob/player/action/object-action';
import { action as doorAction } from '@server/plugins/objects/doors/door-plugin';
import { objectAction } from '@server/world/mob/player/action/object-action';
import { ActionType, RunePlugin } from '@server/plugins/plugin';

const doubleDoors = [
{
Expand Down Expand Up @@ -106,4 +107,5 @@ const action: objectAction = (details) => {
});
};

export default { objectIds: [1519, 1516, 1517, 1520], options: [ 'open', 'close' ], walkTo: true, action } as ObjectActionPlugin;
export default new RunePlugin({ type: ActionType.OBJECT_ACTION, objectIds: [1519, 1516, 1517, 1520],
options: [ 'open', 'close' ], walkTo: true, action });
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { directionData, WNES } from '@server/world/direction';
import { logger } from '@runejs/logger/dist/logger';
import { world } from '@server/game-server';
import { ModifiedLandscapeObject } from '@server/world/map/landscape-object';
import { objectAction, ObjectActionPlugin } from '@server/world/mob/player/action/object-action';
import { objectAction } from '@server/world/mob/player/action/object-action';
import { ActionType, RunePlugin } from '@server/plugins/plugin';

const gates = [
{
Expand Down Expand Up @@ -208,4 +209,5 @@ const action: objectAction = (details) => {
}
};

export default { objectIds: [1551, 1552, 1553, 1556], options: [ 'open', 'close' ], walkTo: true, action } as ObjectActionPlugin;
export default new RunePlugin({ type: ActionType.OBJECT_ACTION, objectIds: [1551, 1552, 1553, 1556],
options: [ 'open', 'close' ], walkTo: true, action });
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { objectAction, ObjectActionPlugin } from '@server/world/mob/player/action/object-action';
import { objectAction } from '@server/world/mob/player/action/object-action';
import { dialogueAction } from '@server/world/mob/player/action/dialogue-action';
import { World } from '@server/world/world';
import { Position } from '@server/world/position';
import { ActionType, RunePlugin } from '@server/plugins/plugin';

const planes = {min: 0, max: 3};
const validate: (level: number) => boolean = (level) => {
Expand Down Expand Up @@ -47,9 +48,10 @@ export const action: objectAction = (details) => {

};

export default {
export default new RunePlugin({
type: ActionType.OBJECT_ACTION,
objectIds: [1738, 1739, 1740, 1746, 1747, 1748, 12964, 12965, 12966],
options: ['climb', 'climb-up', 'climb-down'],
walkTo: true,
action
} as ObjectActionPlugin;
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { objectAction, ObjectActionPlugin } from '@server/world/mob/player/action/object-action';
import { objectAction } from '@server/world/mob/player/action/object-action';
import { World } from '@server/world/world';
import { world } from '@server/game-server';
import { Position } from '@server/world/position';
import { LandscapeObject } from '@runejs/cache-parser';
import { ActionType, RunePlugin } from '@server/plugins/plugin';


export const action: objectAction = (details) => {
Expand Down Expand Up @@ -38,4 +39,4 @@ export const action: objectAction = (details) => {

};

export default {objectIds: [2718], options: ['operate'], walkTo: true, action} as ObjectActionPlugin;
export default new RunePlugin({ type: ActionType.OBJECT_ACTION, objectIds: [2718], options: ['operate'], walkTo: true, action });
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { objectAction, ObjectActionPlugin } from '@server/world/mob/player/action/object-action';
import { objectAction } from '@server/world/mob/player/action/object-action';
import { gameCache, world } from '@server/game-server';
import { World } from '@server/world/world';
import { ActionType, RunePlugin } from '@server/plugins/plugin';


export const action: objectAction = (details) => {
Expand Down Expand Up @@ -42,4 +43,5 @@ export const action: objectAction = (details) => {
}, World.TICK_LENGTH);
};

export default {objectIds: [313, 5583, 5584, 5585, 1161, 3366, 312, 2646], options: ['pick'], walkTo: true, action} as ObjectActionPlugin;
export default new RunePlugin({ type: ActionType.OBJECT_ACTION, objectIds: [313, 5583, 5584, 5585, 1161, 3366,
312, 2646], options: ['pick'], walkTo: true, action });
23 changes: 12 additions & 11 deletions src/plugins/plugin-loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'fs';
import * as util from 'util';
import { RunePlugin } from '@server/plugins/plugin';

export const pluginFilter = (pluginIds: number | number[], searchId: number, pluginOptions?: string | string[], searchOption?: string): boolean => {
if(Array.isArray(pluginIds)) {
Expand All @@ -25,22 +26,22 @@ export const pluginFilter = (pluginIds: number | number[], searchId: number, plu

const readdir = util.promisify(fs.readdir);
const stat = util.promisify(fs.stat);
const blacklist = ['.map', 'plugin-loader.js'];
const blacklist = ['plugin-loader.js', 'plugin.js'];

async function* getFiles(directory: string): AsyncGenerator<string> {
const files = await readdir(directory);

for (const file of files) {
const invalid = blacklist.some(component => file.endsWith(component));
for(const file of files) {
const invalid = blacklist.some(component => file === component || file.endsWith('.map'));

if (invalid) {
if(invalid) {
continue;
}

const path = directory + '/' + file;
const statistics = await stat(path);

if (statistics.isDirectory()) {
if(statistics.isDirectory()) {
for await (const child of getFiles(path)) {
yield child;
}
Expand All @@ -50,15 +51,15 @@ async function* getFiles(directory: string): AsyncGenerator<string> {
}
}

export const BASE_PLUGIN_DIRECTORY = '/dist/plugins';
export const BASE_PLUGIN_DIRECTORY = './dist/plugins';

export async function loadPlugins<T>(directory: string): Promise<T[]> {
const plugins: T[] = [];
export async function loadPlugins(): Promise<RunePlugin[]> {
const plugins: RunePlugin[] = [];

for await (const path of getFiles(directory)) {
const location = '.' + path.substring(directory.indexOf(BASE_PLUGIN_DIRECTORY) + BASE_PLUGIN_DIRECTORY.length).replace('.js', '');
for await(const path of getFiles(BASE_PLUGIN_DIRECTORY)) {
const location = '.' + path.substring(BASE_PLUGIN_DIRECTORY.length).replace('.js', '');
const plugin = await import(location);
plugins.push(plugin.default as T);
plugins.push(plugin.default as RunePlugin);
}

return plugins;
Expand Down
31 changes: 31 additions & 0 deletions src/plugins/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { NpcActionPlugin } from '@server/world/mob/player/action/npc-action';
import { ObjectActionPlugin } from '@server/world/mob/player/action/object-action';
import { ButtonActionPlugin } from '@server/world/mob/player/action/button-action';
import { ItemOnItemActionPlugin } from '@server/world/mob/player/action/item-on-item-action';

export enum ActionType {
BUTTON = 'button',
ITEM_ON_ITEM = 'item_on_item',
NPC_ACTION = 'npc_action',
OBJECT_ACTION = 'object_action',
COMMAND = 'command'
}

export interface ActionPlugin {
type: ActionType;
}

export class RunePlugin {

public actions: (NpcActionPlugin | ObjectActionPlugin | ButtonActionPlugin | ItemOnItemActionPlugin)[];

public constructor(actions: NpcActionPlugin | ObjectActionPlugin | ButtonActionPlugin | ItemOnItemActionPlugin |
(NpcActionPlugin | ObjectActionPlugin | ButtonActionPlugin | ItemOnItemActionPlugin)[]) {
if(!Array.isArray(actions)) {
this.actions = [actions];
} else {
this.actions = actions;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { itemOnItemAction, ItemOnItemActionPlugin } from '@server/world/mob/player/action/item-on-item-action';
import { itemOnItemAction } from '@server/world/mob/player/action/item-on-item-action';
import { world } from '@server/game-server';
import { Skill } from '@server/world/mob/skills';
import { loopingAction } from '@server/world/mob/player/action/action';
Expand All @@ -7,6 +7,7 @@ import { Player } from '@server/world/mob/player/player';
import { WorldItem } from '@server/world/items/world-item';
import { Position } from '@server/world/position';
import { randomBetween } from '@server/util/num';
import { ActionType, RunePlugin } from '@server/plugins/plugin';

const logs = [
{
Expand Down Expand Up @@ -123,4 +124,4 @@ const action: itemOnItemAction = (details) => {
}
};

export default { items: [ { item1: 590, item2: 1511 } ], action } as ItemOnItemActionPlugin;
export default new RunePlugin({ type: ActionType.ITEM_ON_ITEM, items: [ { item1: 590, item2: 1511 } ], action });
Loading

0 comments on commit eb4cb1a

Please sign in to comment.