More async conversion

This commit is contained in:
mmorrison 2019-01-10 06:03:07 -06:00
parent 484e99b29c
commit efe12a00aa
25 changed files with 774 additions and 858 deletions

View file

@ -1,91 +1,79 @@
const async = require('async'),
Core = require('./core');
const Core = require('./core');
class Unreal2 extends Core {
constructor() {
super();
this.encoding = 'latin1';
}
run(state) {
async.series([
(c) => {
this.sendPacket(0,true,(b) => {
const reader = this.reader(b);
state.raw.serverid = reader.uint(4);
state.raw.ip = this.readUnrealString(reader);
state.raw.port = reader.uint(4);
state.raw.queryport = reader.uint(4);
state.name = this.readUnrealString(reader,true);
state.map = this.readUnrealString(reader,true);
state.raw.gametype = this.readUnrealString(reader,true);
state.raw.numplayers = reader.uint(4);
state.maxplayers = reader.uint(4);
this.readExtraInfo(reader,state);
async run(state) {
{
const b = await this.sendPacket(0, true);
const reader = this.reader(b);
state.raw.serverid = reader.uint(4);
state.raw.ip = this.readUnrealString(reader);
state.raw.port = reader.uint(4);
state.raw.queryport = reader.uint(4);
state.name = this.readUnrealString(reader, true);
state.map = this.readUnrealString(reader, true);
state.raw.gametype = this.readUnrealString(reader, true);
state.raw.numplayers = reader.uint(4);
state.maxplayers = reader.uint(4);
this.readExtraInfo(reader, state);
}
c();
});
},
(c) => {
this.sendPacket(1,true,(b) => {
const reader = this.reader(b);
state.raw.mutators = [];
state.raw.rules = {};
while(!reader.done()) {
{
const b = await this.sendPacket(1,true);
const reader = this.reader(b);
state.raw.mutators = [];
state.raw.rules = {};
while(!reader.done()) {
const key = this.readUnrealString(reader,true);
const value = this.readUnrealString(reader,true);
if(key === 'Mutator') state.raw.mutators.push(value);
else state.raw.rules[key] = value;
}
if('GamePassword' in state.raw.rules)
state.password = state.raw.rules.GamePassword !== 'True';
}
{
const b = await this.sendPacket(2,false);
const reader = this.reader(b);
while(!reader.done()) {
const player = {};
player.id = reader.uint(4);
if(!player.id) break;
if(player.id === 0) {
// Unreal2XMP Player (ID is always 0)
reader.skip(4);
}
player.name = this.readUnrealString(reader,true);
player.ping = reader.uint(4);
player.score = reader.int(4);
reader.skip(4); // stats ID
// Extra data for Unreal2XMP players
if(player.id === 0) {
const count = reader.uint(1);
for(let iField = 0; iField < count; iField++) {
const key = this.readUnrealString(reader,true);
const value = this.readUnrealString(reader,true);
if(key === 'Mutator') state.raw.mutators.push(value);
else state.raw.rules[key] = value;
player[key] = value;
}
}
if('GamePassword' in state.raw.rules)
state.password = state.raw.rules.GamePassword !== 'True';
if(player.id === 0 && player.name === 'Player') {
// these show up in ut2004 queries, but aren't real
// not even really sure why they're there
continue;
}
c();
});
},
(c) => {
this.sendPacket(2,false,(b) => {
const reader = this.reader(b);
while(!reader.done()) {
const player = {};
player.id = reader.uint(4);
if(!player.id) break;
if(player.id === 0) {
// Unreal2XMP Player (ID is always 0)
reader.skip(4);
}
player.name = this.readUnrealString(reader,true);
player.ping = reader.uint(4);
player.score = reader.int(4);
reader.skip(4); // stats ID
// Extra data for Unreal2XMP players
if(player.id === 0) {
const count = reader.uint(1);
for(let iField = 0; iField < count; iField++) {
const key = this.readUnrealString(reader,true);
const value = this.readUnrealString(reader,true);
player[key] = value;
}
}
if(player.id === 0 && player.name === 'Player') {
// these show up in ut2004 queries, but aren't real
// not even really sure why they're there
continue;
}
(player.ping ? state.players : state.bots).push(player);
}
c();
});
},
(c) => {
this.finish(state);
(player.ping ? state.players : state.bots).push(player);
}
]);
}
}
readExtraInfo(reader,state) {
this.debugLog(log => {
log("UNREAL2 EXTRA INFO:");
@ -96,6 +84,7 @@ class Unreal2 extends Core {
log(reader.buffer.slice(reader.i));
});
}
readUnrealString(reader, stripColor) {
let length = reader.uint(1);
let out;
@ -120,11 +109,12 @@ class Unreal2 extends Core {
return out;
}
sendPacket(type,required,callback) {
async sendPacket(type,required) {
const outbuffer = Buffer.from([0x79,0,0,0,type]);
const packets = [];
this.udpSend(outbuffer,(buffer) => {
return await this.udpSend(outbuffer,(buffer) => {
const reader = this.reader(buffer);
const header = reader.uint(4);
const iType = reader.uint(1);
@ -132,8 +122,7 @@ class Unreal2 extends Core {
packets.push(reader.rest());
}, () => {
if(!packets.length && required) return;
callback(Buffer.concat(packets));
return true;
return Buffer.concat(packets);
});
}
}