Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working on converting packets to ES6 along with a new portable structure #186

Merged
merged 6 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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,14 @@ 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) {
// throw new Error(`Unknown packet ${this.activePacketId} received - aborting session.`);
logger.warn(`Unknown packet ${this.activePacketId} received.`);
this.activePacketSize = 0;
} else {
this.activePacketSize = incomingPacket.size;
}
}

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

const ignore = [ 234, 160, 216, 13, 58 /* camera move */ ];

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 {
if(ignore.indexOf(packetId) !== -1) {
return;
}

// const packetFunction = packets[packetId];
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: -1,
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
}
Loading