Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ DiscoveryClient.prototype.close = function close (cb) {

module.exports = {
DiscoveryClient: DiscoveryClient,
PipDB: require('./lib/pipdb'),
PipDecode: require('./lib/pipdecode'),
FALLOUT_UDP_PORT: FALLOUT_UDP_PORT,
FALLOUT_TCP_PORT: FALLOUT_TCP_PORT
}
142 changes: 142 additions & 0 deletions lib/pipdb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
require('buffertools').extend();
var EventEmitter = require('events').EventEmitter;
var util = require('util');

// protocol spec taken from https://github.com/NimVek/pipboy/blob/master/PROTOCOL.md
// thanks to NimVek for reverse engineering the protocol and publishing his efforts!

var Types = {
Bool: 0,
Int8: 1,
UInt8: 2,
Int32: 3,
UInt32: 4,
Float: 5,
String: 6,
List: 7,
Dictionary: 8
};

function PipDB() {
EventEmitter.call(this);

this.properties = {}
this.propertyTypes = {};
}

util.inherits(PipDB, EventEmitter);

module.exports = function() {
var eventEmitter = new PipDB();
eventEmitter.properties = {};
eventEmitter.propertyTypes = {};

eventEmitter.on('data', function(buffer) {
var cursor = 0;

while(cursor <= buffer.length - 5) {
var type = buffer.readUInt8(cursor);
var id = buffer.readUInt32LE(cursor + 1);
this.propertyTypes[id] = type;

cursor += 5;

if(type <= 2) { // 8 bit number
if(type == Types.Bool) {
this.properties[id] = (buffer.readUInt8(cursor) === 0) ? false : true;
} else if(type == Types.Int8) {
this.properties[id] = buffer.readInt8(cursor);
} else if(type == Types.UInt8) {
this.properties[id] = buffer.readUInt8(cursor);
}

cursor += 1;
} else if(type <= 5) { // 32 bit number
if(type == Types.Int32) {
this.properties[id] = buffer.readInt32LE(cursor);
} else if(type == Types.UInt32) {
this.properties[id] = buffer.readUInt32LE(cursor);
} else if(type == Types.Float) {
this.properties[id] = buffer.readFloatLE(cursor);
}

cursor += 4;
} else if(type == Types.String) { // string
var s = '';
for(var i = cursor; i < buffer.length; i++) {
if(buffer[i] != 0) {
s += String.fromCharCode(buffer[i]);
} else {
break;
}
}

this.properties[id] = s;
cursor += s.length + 1;
} else if(type == Types.List) { // list
var count = buffer.readUInt16LE(cursor);
cursor += 2;

var refs = [];
for(var i = 0; i < count; i++) {
var refId = buffer.readUInt32LE(cursor);
refs.push(refId);
cursor += 4;
}

this.properties[id] = refs;
} else if(type == Types.Dictionary) { // dict
var count = buffer.readUInt16LE(cursor);
cursor += 2;

var dict = {};

for(var i = 0; i < count; i++) {
var refId = buffer.readUInt32LE(cursor);
cursor += 4;

var name = '';
for(var q = cursor; q < buffer.length; q++) {
if(buffer[q] == 0) {
break;
} else {
name += String.fromCharCode(buffer[q]);
}
}

cursor += name.length + 1;
dict[name] = refId;
}

this.properties[id] = dict;
cursor += 2; // skip dummy uint16
}
}

this.emit('db_update', this.normalizeDBEntry(0));
});

eventEmitter.normalizeDBEntry = function(index) {
if(this.propertyTypes[index] == Types.Dictionary) {
var dict = {};

for(var i in this.properties[index]) {
dict[i] = this.normalizeDBEntry(this.properties[index][i]);
}

return dict;
} else if(this.propertyTypes[index] == Types.List) {
var list = [];

for(var i in this.properties[index]) {
list.push(this.normalizeDBEntry(this.properties[index][i]));
}

return list;
} else {
return this.properties[index];
}
}

return eventEmitter;
}
69 changes: 69 additions & 0 deletions lib/pipdecode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require('buffertools').extend();
var EventEmitter = require('events').EventEmitter;
var util = require('util');

// protocol spec taken from https://github.com/NimVek/pipboy/blob/master/PROTOCOL.md
// thanks to NimVek for reverse engineering the protocol and publishing his efforts!

var Channels = {
Heartbeat: 0,
Info: 1,
GameBusy: 2,
DatabaseUpdate: 3,
LocalMapUpdate: 4,
CommandRequest: 5,
CommandResponse: 6
};

function PipDecode() {
EventEmitter.call(this);

this.buffer = new Buffer(0);
this.expectedSize = null;
}

util.inherits(PipDecode, EventEmitter);

module.exports = function() {
var emitter = new PipDecode();

emitter.on('data', function(data) {
this.buffer = this.buffer.concat(data);

if(!this.expectedSize) {
this.expectedSize = this.buffer.readUInt32LE(0) + 5;
}

if(this.buffer.length >= this.expectedSize) {
var size = this.buffer.readUInt32LE(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed this is defined here but not used. Should we verify that size still === this.expectedSize?

var channel = this.buffer.readUInt8(4);
var data = this.buffer.slice(5, this.expectedSize);

switch(channel) {
case Channels.Heartbeat:
this.emit('heartbeat');
break;
case Channels.Info:
this.emit('info', JSON.parse(data.toString()));
break;
case Channels.GameBusy:
this.emit('game_busy');
break;
case Channels.DatabaseUpdate:
this.emit('db_update', data);
break;
case Channels.LocalMapUpdate:
this.emit('localmap_update', data);
break;
case Channels.CommandResponse:
this.emit('command_response', JSOn.parse(data.toString()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a capital N here in JSON.parse

break;
}

this.buffer = this.buffer.slice(this.expectedSize);
this.expectedSize = null;
}
});

return emitter;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"url": "https://github.com/rgbkrk/pipboylib.git"
},
"dependencies": {
"buffertools": "^2.1.3",
"concentrate": "0.2.3",
"dissolve": "0.3.3",
"lodash": "3.10.1"
Expand Down