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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "",
"main": "src/game-server.ts",
"scripts": {
"start": "npm run build && concurrently \"npm run build:watch\" \"node --max-old-space-size=4096 dist/main.js\"",
"start:server": "npm run build && node --max-old-space-size=4096 dist/main.js",
"start": "rimraf dist && npm run build && concurrently \"npm run build:watch\" \"node --max-old-space-size=4096 dist/main.js\"",
"start:server": "rimraf dist && npm run build && node --max-old-space-size=4096 dist/main.js",
"lint": "tslint --project tsconfig.json",
"fake-players": "concurrently \"npm run build:watch\" \"node --max-old-space-size=4096 dist/main.js -fakePlayers\"",
"fake-players-tick": "concurrently \"npm run build:watch\" \"node --max-old-space-size=4096 dist/main.js -fakePlayers -tickTime\"",
Expand Down
3 changes: 3 additions & 0 deletions src/game-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { setPlayerInitPlugins } from '@server/world/actor/player/player';
import { setNpcInitPlugins } from '@server/world/actor/npc/npc';
import { setQuestPlugins } from '@server/world/config/quests';
import { setPlayerPlugins } from '@server/world/actor/player/action/player-action';
import { loadPackets } from '@server/net/inbound-packets';


export let serverConfig: ServerConfig;
Expand Down Expand Up @@ -127,6 +128,8 @@ export async function runGameServer(): Promise<void> {
// delete cache.indexChannels;
// delete cache.indices;

await loadPackets();

world = new World();
await injectPlugins();

Expand Down
4 changes: 2 additions & 2 deletions src/net/client-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Player } from '@server/world/actor/player/player';
import { world } from '@server/game-server';
import { LoginHandshakeParser } from './data-parser/login-handshake-parser';
import { ClientLoginParser } from './data-parser/client-login-parser';
import { ClientPacketDataParser } from './data-parser/client-packet-data-parser';
import { InboundPacketDataParser } from './data-parser/inbound-packet-data-parser';
import { DataParser } from './data-parser/data-parser';
import { VersionHandshakeParser } from '@server/net/data-parser/version-handshake-parser';
import { UpdateServerParser } from '@server/net/data-parser/update-server-parser';
Expand Down Expand Up @@ -61,7 +61,7 @@ export class ClientConnection {
this.dataParser = new ClientLoginParser(this);
} else if(this.connectionStage === ConnectionStage.LOGIN) {
this.connectionStage = ConnectionStage.LOGGED_IN;
this.dataParser = new ClientPacketDataParser(this);
this.dataParser = new InboundPacketDataParser(this);
}
} catch(err) {
console.error('Error decoding client data');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { incomingPacketSizes } from '@server/net/incoming-packet-sizes';
import { handlePacket } from '@server/net/incoming-packet-directory';
import { handlePacket, incomingPackets } from '../inbound-packets';
import { DataParser } from './data-parser';
import { ByteBuffer } from '@runejs/byte-buffer';
import { logger } from '@runejs/logger';

/**
* Parses incoming packet data from the game client once the user is fully authenticated.
* Parses inbound packet data from the game client once the user is fully authenticated.
*/
export class ClientPacketDataParser extends DataParser {
export class InboundPacketDataParser extends DataParser {

private activePacketId: number = null;
private activePacketSize: number = null;
Expand Down Expand Up @@ -40,7 +40,12 @@ export class ClientPacketDataParser extends DataParser {

this.activePacketId = this.activeBuffer.get('BYTE', 'UNSIGNED');
this.activePacketId = (this.activePacketId - inCipher.rand()) & 0xff;
this.activePacketSize = incomingPacketSizes[this.activePacketId];
const incomingPacket = incomingPackets.get(this.activePacketId);
if(incomingPacket) {
this.activePacketSize = incomingPacket.size;
} else {
this.activePacketSize = -3;
}
}

// Packet will provide the size
Expand Down
44 changes: 44 additions & 0 deletions src/net/inbound-packets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Player } from '../world/actor/player/player';
import { logger } from '@runejs/logger';
import { getFiles } from '../util/files';
import { ByteBuffer } from '@runejs/byte-buffer';

interface InboundPacket {
opcode: number;
size: number;
handler: (player: Player, packet: { packetId: number, packetSize: number, buffer: ByteBuffer }) => void;
}

export const incomingPackets = new Map<number, InboundPacket>();

export const PACKET_DIRECTORY = './dist/net/inbound-packets';

export async function loadPackets(): Promise<Map<number, InboundPacket>> {
incomingPackets.clear();

for await(const path of getFiles(PACKET_DIRECTORY, [ '.' ])) {
const location = './inbound-packets' + path.substring(PACKET_DIRECTORY.length).replace('.js', '');
const packet = require(location).default;
if(Array.isArray(packet)) {
packet.forEach(p => incomingPackets.set(p.opcode, p));
} else {
incomingPackets.set(packet.opcode, packet);
}
}

return incomingPackets;
}

export function handlePacket(player: Player, packetId: number, packetSize: number, buffer: ByteBuffer): void {
const incomingPacket = incomingPackets.get(packetId);

if(!incomingPacket) {
logger.info(`Unknown packet ${packetId} with size ${packetSize} received.`);
return;
}

new Promise(resolve => {
incomingPacket.handler(player, { packetId, packetSize, buffer });
resolve();
}).catch(error => logger.error(`Error handling inbound packet: ${error}`));
}
21 changes: 21 additions & 0 deletions src/net/inbound-packets/button-click-packet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { buttonAction } from '../../world/actor/player/action/button-action';

const ignoreButtons = [
'269:99' // character design accept button
];

const buttonClickPacket = (player, packet) => {
const { buffer } = packet;
const widgetId = buffer.get('SHORT');
const buttonId = buffer.get('SHORT');

if(ignoreButtons.indexOf(`${widgetId}:${buttonId}`) === -1) {
buttonAction(player, widgetId, buttonId);
}
};

export default {
opcode: 64,
size: 4,
handler: buttonClickPacket
};
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { incomingPacket } from '../incoming-packet';
import { Player } from '../../world/actor/player/player';
import { widgets } from '../../world/config/widget';
import { ByteBuffer } from '@runejs/byte-buffer';

export const characterDesignPacket: incomingPacket = (player: Player, packetId: number, packetSize: number, packet: ByteBuffer): void => {
const characterDesignPacket = (player, packet) => {
if(!player.activeWidget || player.activeWidget.widgetId !== widgets.characterDesign) {
return;
}

const { buffer } = packet;

// @TODO verify validity of selections

const gender: number = packet.get();
const models: number[] = new Array(7);
const colors: number[] = new Array(5);
const gender = buffer.get();
const models = new Array(7);
const colors = new Array(5);

for(let i = 0; i < models.length; i++) {
models[i] = packet.get();
models[i] = buffer.get();
}

for(let i = 0; i < colors.length; i++) {
colors[i] = packet.get();
colors[i] = buffer.get();
}

player.appearance = {
Expand All @@ -41,3 +40,9 @@ export const characterDesignPacket: incomingPacket = (player: Player, packetId:
player.updateFlags.appearanceUpdateRequired = true;
player.closeActiveWidgets();
};

export default {
opcode: 231,
size: 13,
handler: characterDesignPacket
};
14 changes: 14 additions & 0 deletions src/net/inbound-packets/chat-packet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const chatPacket = (player, packet) => {
const { buffer } = packet;
buffer.get();
const color = buffer.get();
const effects = buffer.get();
const data = Buffer.from(buffer.getSlice(buffer.readerIndex, buffer.length - buffer.readerIndex));
player.updateFlags.addChatMessage({ color, effects, data });
};

export default {
opcode: 75,
size: -3,
handler: chatPacket
};
28 changes: 28 additions & 0 deletions src/net/inbound-packets/command-packet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { inputCommandAction } from '../../world/actor/player/action/input-command-action';

const commandPacket = (player, packet) => {
const input = packet.buffer.getString();

if(!input || input.trim().length === 0) {
return;
}

const isConsole = packet.opcode === 246;

const args = input.trim().split(' ');
const command = args[0];

args.splice(0, 1);

inputCommandAction(player, command, isConsole, args);
};

export default [{
opcode: 246,
size: -3,
handler: commandPacket
},{
opcode: 248,
size: -1,
handler: commandPacket
}];
17 changes: 17 additions & 0 deletions src/net/inbound-packets/drop-item-packet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { itemAction } from '../../world/actor/player/action/item-action';

const dropItemPacket = (player, packet) => {
const { buffer } = packet;
const widgetId = buffer.get('SHORT', 'UNSIGNED', 'LITTLE_ENDIAN');
const containerId = buffer.get('SHORT', 'UNSIGNED', 'LITTLE_ENDIAN');
const slot = buffer.get('SHORT', 'UNSIGNED');
const itemId = buffer.get('SHORT', 'UNSIGNED', 'LITTLE_ENDIAN');

itemAction(player, itemId, slot, widgetId, containerId, 'drop');
};

export default {
opcode: 29,
size: 8,
handler: dropItemPacket
};
35 changes: 35 additions & 0 deletions src/net/inbound-packets/examine-packet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { world } from '../../game-server';

const examinePacket = (player, packet) => {
const { packetId, buffer } = packet;
const id = buffer.get('SHORT', 'SIGNED', 'LITTLE_ENDIAN');

let message;

if (packetId === 151) {
message = world.examine.getItem(id);
} else if (packetId === 148) {
message = world.examine.getObject(id);
} else if (packetId === 247) {
message = world.examine.getNpc(id);
}

if (message) {
player.sendMessage(message);
}
};

export default [{
opcode: 148,
size: 2,
handler: examinePacket
},{
opcode: 151,
size: 2,
handler: examinePacket
},{
opcode: 247,
size: 2,
handler: examinePacket
}];

17 changes: 17 additions & 0 deletions src/net/inbound-packets/item-equip-packet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { itemAction } from '../../world/actor/player/action/item-action';

const itemEquipPacket = (player, packet) => {
const { buffer } = packet;
const containerId = buffer.get('SHORT', 'SIGNED', 'LITTLE_ENDIAN');
const widgetId = buffer.get('SHORT', 'SIGNED', 'LITTLE_ENDIAN');
const slot = buffer.get('SHORT', 'UNSIGNED', 'LITTLE_ENDIAN');
const itemId = buffer.get('SHORT', 'UNSIGNED');

itemAction(player, itemId, slot, widgetId, containerId, 'equip');
};

export default {
opcode: 102,
size: 8,
handler: itemEquipPacket
}
95 changes: 95 additions & 0 deletions src/net/inbound-packets/item-interaction-packet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { itemAction } from '../../world/actor/player/action/item-action';
import { getItemOption } from '../../world/items/item';

const option1 = buffer => {
const itemId = buffer.get('SHORT', 'UNSIGNED');
const slot = buffer.get('SHORT', 'UNSIGNED', 'LITTLE_ENDIAN');
const widgetId = buffer.get('SHORT', 'SIGNED', 'LITTLE_ENDIAN');
const containerId = buffer.get('SHORT', 'SIGNED', 'LITTLE_ENDIAN');
return { widgetId, containerId, itemId, slot };
};

const option2 = buffer => {
const itemId = buffer.get('SHORT', 'UNSIGNED', 'LITTLE_ENDIAN');
const containerId = buffer.get('SHORT', 'SIGNED', 'LITTLE_ENDIAN');
const widgetId = buffer.get('SHORT', 'SIGNED', 'LITTLE_ENDIAN');
const slot = buffer.get('SHORT', 'UNSIGNED', 'LITTLE_ENDIAN');
return { widgetId, containerId, itemId, slot };
};

const option3 = buffer => {
const slot = buffer.get('SHORT', 'UNSIGNED');
const containerId = buffer.get('SHORT', 'SIGNED', 'LITTLE_ENDIAN');
const widgetId = buffer.get('SHORT', 'SIGNED', 'LITTLE_ENDIAN');
const itemId = buffer.get('SHORT', 'UNSIGNED');
return { widgetId, containerId, itemId, slot };
};

const option4 = buffer => {
const itemId = buffer.get('SHORT', 'UNSIGNED');
const slot = buffer.get('SHORT', 'UNSIGNED', 'LITTLE_ENDIAN');
const containerId = buffer.get('SHORT', 'SIGNED', 'LITTLE_ENDIAN');
const widgetId = buffer.get('SHORT', 'SIGNED', 'LITTLE_ENDIAN');
return { widgetId, containerId, itemId, slot };
};

const inventoryOption1 = buffer => {
const slot = buffer.get('SHORT', 'UNSIGNED', 'LITTLE_ENDIAN');
const itemId = buffer.get('SHORT', 'SIGNED', 'LITTLE_ENDIAN');
const containerId = buffer.get('SHORT', 'SIGNED', 'LITTLE_ENDIAN');
const widgetId = buffer.get('SHORT', 'UNSIGNED');
return { widgetId, containerId, itemId, slot };
};

const inventoryOption4 = buffer => {
const slot = buffer.get('SHORT', 'UNSIGNED');
const widgetId = buffer.get('SHORT', 'SIGNED', 'LITTLE_ENDIAN');
const containerId = buffer.get('SHORT', 'SIGNED', 'LITTLE_ENDIAN');
const itemId = buffer.get('SHORT', 'UNSIGNED');
return { widgetId, containerId, itemId, slot };
};

const itemInteractionPacket = (player, packet) => {
const { packetId } = packet;
const packets = {
38: { packetDef: option1, optionNumber: 1 },
228: { packetDef: option2, optionNumber: 2 },
26: { packetDef: option3, optionNumber: 3 },
147: { packetDef: option4, optionNumber: 4 },
98: { packetDef: inventoryOption4, optionNumber: 4 },
240: { packetDef: inventoryOption1, optionNumber: 1 },
};

const packetDetails = packets[packetId];
const { widgetId, containerId, itemId, slot } = packetDetails.packetDef(packet);

const option = getItemOption(itemId, packetDetails.optionNumber, { widgetId, containerId });

itemAction(player, itemId, slot, widgetId, containerId, option);
};

export default [{
opcode: 38,
size: 8,
handler: itemInteractionPacket
},{
opcode: 98,
size: 8,
handler: itemInteractionPacket
},{
opcode: 228,
size: 8,
handler: itemInteractionPacket
},{
opcode: 26,
size: 8,
handler: itemInteractionPacket
},{
opcode: 147,
size: 8,
handler: itemInteractionPacket
},{
opcode: 240,
size: 8,
handler: itemInteractionPacket
}];
Loading