Skip to content

Commit b21ef5b

Browse files
Merge pull request #186 from rune-js/feature/es6-packets
Working on converting packets to ES6 along with a new portable structure
2 parents 41ed974 + 663a06f commit b21ef5b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+610
-552
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"description": "",
55
"main": "src/game-server.ts",
66
"scripts": {
7-
"start": "npm run build && concurrently \"npm run build:watch\" \"node --max-old-space-size=4096 dist/main.js\"",
8-
"start:server": "npm run build && node --max-old-space-size=4096 dist/main.js",
7+
"start": "rimraf dist && npm run build && concurrently \"npm run build:watch\" \"node --max-old-space-size=4096 dist/main.js\"",
8+
"start:server": "rimraf dist && npm run build && node --max-old-space-size=4096 dist/main.js",
99
"lint": "tslint --project tsconfig.json",
1010
"fake-players": "concurrently \"npm run build:watch\" \"node --max-old-space-size=4096 dist/main.js -fakePlayers\"",
1111
"fake-players-tick": "concurrently \"npm run build:watch\" \"node --max-old-space-size=4096 dist/main.js -fakePlayers -tickTime\"",

src/game-server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { setPlayerInitPlugins } from '@server/world/actor/player/player';
2626
import { setNpcInitPlugins } from '@server/world/actor/npc/npc';
2727
import { setQuestPlugins } from '@server/world/config/quests';
2828
import { setPlayerPlugins } from '@server/world/actor/player/action/player-action';
29+
import { loadPackets } from '@server/net/inbound-packets';
2930

3031

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

131+
await loadPackets();
132+
130133
world = new World();
131134
await injectPlugins();
132135

src/net/client-connection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Player } from '@server/world/actor/player/player';
33
import { world } from '@server/game-server';
44
import { LoginHandshakeParser } from './data-parser/login-handshake-parser';
55
import { ClientLoginParser } from './data-parser/client-login-parser';
6-
import { ClientPacketDataParser } from './data-parser/client-packet-data-parser';
6+
import { InboundPacketDataParser } from './data-parser/inbound-packet-data-parser';
77
import { DataParser } from './data-parser/data-parser';
88
import { VersionHandshakeParser } from '@server/net/data-parser/version-handshake-parser';
99
import { UpdateServerParser } from '@server/net/data-parser/update-server-parser';
@@ -61,7 +61,7 @@ export class ClientConnection {
6161
this.dataParser = new ClientLoginParser(this);
6262
} else if(this.connectionStage === ConnectionStage.LOGIN) {
6363
this.connectionStage = ConnectionStage.LOGGED_IN;
64-
this.dataParser = new ClientPacketDataParser(this);
64+
this.dataParser = new InboundPacketDataParser(this);
6565
}
6666
} catch(err) {
6767
console.error('Error decoding client data');

src/net/data-parser/client-packet-data-parser.ts renamed to src/net/data-parser/inbound-packet-data-parser.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { incomingPacketSizes } from '@server/net/incoming-packet-sizes';
2-
import { handlePacket } from '@server/net/incoming-packet-directory';
1+
import { handlePacket, incomingPackets } from '../inbound-packets';
32
import { DataParser } from './data-parser';
43
import { ByteBuffer } from '@runejs/byte-buffer';
4+
import { logger } from '@runejs/logger';
55

66
/**
7-
* Parses incoming packet data from the game client once the user is fully authenticated.
7+
* Parses inbound packet data from the game client once the user is fully authenticated.
88
*/
9-
export class ClientPacketDataParser extends DataParser {
9+
export class InboundPacketDataParser extends DataParser {
1010

1111
private activePacketId: number = null;
1212
private activePacketSize: number = null;
@@ -40,7 +40,12 @@ export class ClientPacketDataParser extends DataParser {
4040

4141
this.activePacketId = this.activeBuffer.get('BYTE', 'UNSIGNED');
4242
this.activePacketId = (this.activePacketId - inCipher.rand()) & 0xff;
43-
this.activePacketSize = incomingPacketSizes[this.activePacketId];
43+
const incomingPacket = incomingPackets.get(this.activePacketId);
44+
if(incomingPacket) {
45+
this.activePacketSize = incomingPacket.size;
46+
} else {
47+
this.activePacketSize = -3;
48+
}
4449
}
4550

4651
// Packet will provide the size

src/net/inbound-packets.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Player } from '../world/actor/player/player';
2+
import { logger } from '@runejs/logger';
3+
import { getFiles } from '../util/files';
4+
import { ByteBuffer } from '@runejs/byte-buffer';
5+
6+
interface InboundPacket {
7+
opcode: number;
8+
size: number;
9+
handler: (player: Player, packet: { packetId: number, packetSize: number, buffer: ByteBuffer }) => void;
10+
}
11+
12+
export const incomingPackets = new Map<number, InboundPacket>();
13+
14+
export const PACKET_DIRECTORY = './dist/net/inbound-packets';
15+
16+
export async function loadPackets(): Promise<Map<number, InboundPacket>> {
17+
incomingPackets.clear();
18+
19+
for await(const path of getFiles(PACKET_DIRECTORY, [ '.' ])) {
20+
const location = './inbound-packets' + path.substring(PACKET_DIRECTORY.length).replace('.js', '');
21+
const packet = require(location).default;
22+
if(Array.isArray(packet)) {
23+
packet.forEach(p => incomingPackets.set(p.opcode, p));
24+
} else {
25+
incomingPackets.set(packet.opcode, packet);
26+
}
27+
}
28+
29+
return incomingPackets;
30+
}
31+
32+
export function handlePacket(player: Player, packetId: number, packetSize: number, buffer: ByteBuffer): void {
33+
const incomingPacket = incomingPackets.get(packetId);
34+
35+
if(!incomingPacket) {
36+
logger.info(`Unknown packet ${packetId} with size ${packetSize} received.`);
37+
return;
38+
}
39+
40+
new Promise(resolve => {
41+
incomingPacket.handler(player, { packetId, packetSize, buffer });
42+
resolve();
43+
}).catch(error => logger.error(`Error handling inbound packet: ${error}`));
44+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { buttonAction } from '../../world/actor/player/action/button-action';
2+
3+
const ignoreButtons = [
4+
'269:99' // character design accept button
5+
];
6+
7+
const buttonClickPacket = (player, packet) => {
8+
const { buffer } = packet;
9+
const widgetId = buffer.get('SHORT');
10+
const buttonId = buffer.get('SHORT');
11+
12+
if(ignoreButtons.indexOf(`${widgetId}:${buttonId}`) === -1) {
13+
buttonAction(player, widgetId, buttonId);
14+
}
15+
};
16+
17+
export default {
18+
opcode: 64,
19+
size: 4,
20+
handler: buttonClickPacket
21+
};

src/net/incoming-packets/character-design-packet.ts renamed to src/net/inbound-packets/character-design-packet.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
import { incomingPacket } from '../incoming-packet';
2-
import { Player } from '../../world/actor/player/player';
31
import { widgets } from '../../world/config/widget';
4-
import { ByteBuffer } from '@runejs/byte-buffer';
52

6-
export const characterDesignPacket: incomingPacket = (player: Player, packetId: number, packetSize: number, packet: ByteBuffer): void => {
3+
const characterDesignPacket = (player, packet) => {
74
if(!player.activeWidget || player.activeWidget.widgetId !== widgets.characterDesign) {
85
return;
96
}
107

8+
const { buffer } = packet;
9+
1110
// @TODO verify validity of selections
1211

13-
const gender: number = packet.get();
14-
const models: number[] = new Array(7);
15-
const colors: number[] = new Array(5);
12+
const gender = buffer.get();
13+
const models = new Array(7);
14+
const colors = new Array(5);
1615

1716
for(let i = 0; i < models.length; i++) {
18-
models[i] = packet.get();
17+
models[i] = buffer.get();
1918
}
2019

2120
for(let i = 0; i < colors.length; i++) {
22-
colors[i] = packet.get();
21+
colors[i] = buffer.get();
2322
}
2423

2524
player.appearance = {
@@ -41,3 +40,9 @@ export const characterDesignPacket: incomingPacket = (player: Player, packetId:
4140
player.updateFlags.appearanceUpdateRequired = true;
4241
player.closeActiveWidgets();
4342
};
43+
44+
export default {
45+
opcode: 231,
46+
size: 13,
47+
handler: characterDesignPacket
48+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const chatPacket = (player, packet) => {
2+
const { buffer } = packet;
3+
buffer.get();
4+
const color = buffer.get();
5+
const effects = buffer.get();
6+
const data = Buffer.from(buffer.getSlice(buffer.readerIndex, buffer.length - buffer.readerIndex));
7+
player.updateFlags.addChatMessage({ color, effects, data });
8+
};
9+
10+
export default {
11+
opcode: 75,
12+
size: -3,
13+
handler: chatPacket
14+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { inputCommandAction } from '../../world/actor/player/action/input-command-action';
2+
3+
const commandPacket = (player, packet) => {
4+
const input = packet.buffer.getString();
5+
6+
if(!input || input.trim().length === 0) {
7+
return;
8+
}
9+
10+
const isConsole = packet.opcode === 246;
11+
12+
const args = input.trim().split(' ');
13+
const command = args[0];
14+
15+
args.splice(0, 1);
16+
17+
inputCommandAction(player, command, isConsole, args);
18+
};
19+
20+
export default [{
21+
opcode: 246,
22+
size: -3,
23+
handler: commandPacket
24+
},{
25+
opcode: 248,
26+
size: -1,
27+
handler: commandPacket
28+
}];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { itemAction } from '../../world/actor/player/action/item-action';
2+
3+
const dropItemPacket = (player, packet) => {
4+
const { buffer } = packet;
5+
const widgetId = buffer.get('SHORT', 'UNSIGNED', 'LITTLE_ENDIAN');
6+
const containerId = buffer.get('SHORT', 'UNSIGNED', 'LITTLE_ENDIAN');
7+
const slot = buffer.get('SHORT', 'UNSIGNED');
8+
const itemId = buffer.get('SHORT', 'UNSIGNED', 'LITTLE_ENDIAN');
9+
10+
itemAction(player, itemId, slot, widgetId, containerId, 'drop');
11+
};
12+
13+
export default {
14+
opcode: 29,
15+
size: 8,
16+
handler: dropItemPacket
17+
};

0 commit comments

Comments
 (0)