2019-01-07 07:52:29 +01:00
|
|
|
const Core = require('./core');
|
2019-01-06 21:10:46 +01:00
|
|
|
|
|
|
|
class Samp extends Core {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.encoding = 'win1252';
|
2019-02-04 06:40:47 +01:00
|
|
|
this.magicHeader = 'SAMP';
|
|
|
|
this.responseMagicHeader = null;
|
|
|
|
this.isVcmp = false;
|
2019-01-06 21:10:46 +01:00
|
|
|
}
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2019-01-07 07:52:29 +01:00
|
|
|
async run(state) {
|
|
|
|
// read info
|
|
|
|
{
|
|
|
|
const reader = await this.sendPacket('i');
|
2019-02-04 06:40:47 +01:00
|
|
|
if (this.isVcmp) {
|
2019-02-04 08:16:30 +01:00
|
|
|
const consumed = reader.part(12);
|
|
|
|
state.raw.version = this.reader(consumed).string();
|
2019-02-04 06:40:47 +01:00
|
|
|
}
|
2019-01-07 07:52:29 +01:00
|
|
|
state.password = !!reader.uint(1);
|
|
|
|
state.raw.numplayers = reader.uint(2);
|
|
|
|
state.maxplayers = reader.uint(2);
|
2019-02-05 02:58:28 +01:00
|
|
|
state.name = reader.pascalString(4);
|
|
|
|
state.raw.gamemode = reader.pascalString(4);
|
|
|
|
state.raw.map = reader.pascalString(4);
|
2019-01-07 07:52:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// read rules
|
2019-02-04 06:40:47 +01:00
|
|
|
if (!this.isVcmp) {
|
2019-01-07 07:52:29 +01:00
|
|
|
const reader = await this.sendPacket('r');
|
|
|
|
const ruleCount = reader.uint(2);
|
|
|
|
state.raw.rules = {};
|
|
|
|
for(let i = 0; i < ruleCount; i++) {
|
2019-02-05 02:58:28 +01:00
|
|
|
const key = reader.pascalString(1);
|
|
|
|
const value = reader.pascalString(1);
|
2019-01-07 07:52:29 +01:00
|
|
|
state.raw.rules[key] = value;
|
2017-08-09 12:32:09 +02:00
|
|
|
}
|
2019-01-07 07:52:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// read players
|
2019-02-04 06:40:47 +01:00
|
|
|
// don't even bother if > 100 players, because the server won't respond
|
|
|
|
let gotPlayerData = false;
|
|
|
|
if (state.raw.numplayers < 100) {
|
|
|
|
if (this.isVcmp) {
|
|
|
|
const reader = await this.sendPacket('c', true);
|
|
|
|
if (reader !== null) {
|
|
|
|
gotPlayerData = true;
|
|
|
|
const playerCount = reader.uint(2);
|
|
|
|
for(let i = 0; i < playerCount; i++) {
|
|
|
|
const player = {};
|
2019-02-05 02:58:28 +01:00
|
|
|
player.name = reader.pascalString(1);
|
2019-02-04 06:40:47 +01:00
|
|
|
state.players.push(player);
|
|
|
|
}
|
2019-01-07 07:52:29 +01:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-04 06:40:47 +01:00
|
|
|
const reader = await this.sendPacket('d', true);
|
|
|
|
if (reader !== null) {
|
|
|
|
gotPlayerData = true;
|
|
|
|
const playerCount = reader.uint(2);
|
|
|
|
for(let i = 0; i < playerCount; i++) {
|
|
|
|
const player = {};
|
|
|
|
player.id = reader.uint(1);
|
2019-02-05 02:58:28 +01:00
|
|
|
player.name = reader.pascalString(1);
|
2019-02-04 06:40:47 +01:00
|
|
|
player.score = reader.int(4);
|
|
|
|
player.ping = reader.uint(4);
|
|
|
|
state.players.push(player);
|
|
|
|
}
|
2019-01-07 07:52:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-04 06:40:47 +01:00
|
|
|
if (!gotPlayerData) {
|
2019-02-04 08:11:28 +01:00
|
|
|
state.players = state.raw.numplayers;
|
2019-02-04 06:40:47 +01:00
|
|
|
}
|
2017-08-09 12:32:09 +02:00
|
|
|
}
|
2019-01-07 07:52:29 +01:00
|
|
|
async sendPacket(type,allowTimeout) {
|
|
|
|
const outBuffer = Buffer.alloc(11);
|
2019-02-04 06:40:47 +01:00
|
|
|
outBuffer.write(this.magicHeader,0, 4);
|
2017-08-09 11:05:55 +02:00
|
|
|
const ipSplit = this.options.address.split('.');
|
2019-01-07 07:52:29 +01:00
|
|
|
outBuffer.writeUInt8(parseInt(ipSplit[0]),4);
|
|
|
|
outBuffer.writeUInt8(parseInt(ipSplit[1]),5);
|
|
|
|
outBuffer.writeUInt8(parseInt(ipSplit[2]),6);
|
|
|
|
outBuffer.writeUInt8(parseInt(ipSplit[3]),7);
|
|
|
|
outBuffer.writeUInt16LE(this.options.port,8);
|
|
|
|
outBuffer.writeUInt8(type.charCodeAt(0),10);
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2019-02-04 06:40:47 +01:00
|
|
|
const checkBuffer = Buffer.from(outBuffer);
|
|
|
|
if (this.responseMagicHeader) {
|
|
|
|
checkBuffer.write(this.responseMagicHeader, 0, 4);
|
|
|
|
}
|
|
|
|
|
2019-01-07 07:52:29 +01:00
|
|
|
return await this.udpSend(
|
|
|
|
outBuffer,
|
|
|
|
(buffer) => {
|
|
|
|
const reader = this.reader(buffer);
|
2019-02-04 06:40:47 +01:00
|
|
|
for(let i = 0; i < checkBuffer.length; i++) {
|
|
|
|
if(checkBuffer.readUInt8(i) !== reader.uint(1)) return;
|
2019-01-07 07:52:29 +01:00
|
|
|
}
|
|
|
|
return reader;
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
if(allowTimeout) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-08-09 12:32:09 +02:00
|
|
|
}
|
2019-01-07 07:52:29 +01:00
|
|
|
);
|
2017-08-09 12:32:09 +02:00
|
|
|
}
|
2017-08-09 11:05:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Samp;
|