Add support for mafia 2 online

This commit is contained in:
mmorrison 2019-02-04 20:20:51 -06:00
parent fc5975bf0c
commit 481a8d86c8
5 changed files with 57 additions and 40 deletions

View file

@ -1,39 +0,0 @@
const Core = require('./core');
class M2mp extends Core {
constructor() {
super();
this.encoding = 'latin1';
}
async run(state) {
const body = await this.udpSend('M2MP',(buffer) => {
const reader = this.reader(buffer);
const header = reader.string(4);
if (header !== 'M2MP') return;
return reader.rest();
});
const reader = this.reader(body);
state.name = this.readString(reader);
state.raw.numplayers = this.readString(reader);
state.maxplayers = this.readString(reader);
state.raw.gamemode = this.readString(reader);
state.password = !!reader.uint(1);
state.gamePort = this.options.port - 1;
while(!reader.done()) {
const name = this.readString(reader);
if(!name) break;
state.players.push({
name:name
});
}
}
readString(reader) {
return reader.pascalString(1,-1);
}
}
module.exports = M2mp;

43
protocols/mafia2mp.js Normal file
View file

@ -0,0 +1,43 @@
const Core = require('./core');
class Mafia2Multiplayer extends Core {
constructor() {
super();
this.encoding = 'latin1';
this.header = 'M2MP';
this.isMafia2Online = false;
}
async run(state) {
const body = await this.udpSend(this.header,(buffer) => {
const reader = this.reader(buffer);
const header = reader.string(this.header.length);
if (header !== this.header) return;
return reader.rest();
});
const reader = this.reader(body);
state.name = this.readString(reader);
state.raw.numplayers = this.readString(reader);
state.maxplayers = parseInt(this.readString(reader));
state.raw.gamemode = this.readString(reader);
state.password = !!reader.uint(1);
state.gamePort = this.options.port - 1;
while(!reader.done()) {
const player = {};
player.name = this.readString(reader);
if(!player.name) break;
if (this.isMafia2Online) {
player.ping = parseInt(this.readString(reader));
}
state.players.push(player);
}
}
readString(reader) {
return reader.pascalString(1,-1);
}
}
module.exports = Mafia2Multiplayer;

11
protocols/mafia2online.js Normal file
View file

@ -0,0 +1,11 @@
const Mafia2Multiplayer = require('./mafia2mp');
class Mafia2Online extends Mafia2Multiplayer {
constructor() {
super();
this.header = 'M2Online';
this.isMafia2Online = true;
}
}
module.exports = Mafia2Online;