2015-05-14 16:08:49 -04:00
|
|
|
var assert = require("assert");
|
2015-05-13 09:15:16 -04:00
|
|
|
|
2015-05-14 16:08:49 -04:00
|
|
|
module.exports = {readPackets: readPackets};
|
2015-05-13 09:15:16 -04:00
|
|
|
|
2015-05-14 16:08:49 -04:00
|
|
|
function readPackets(packets, states) {
|
|
|
|
var packetFields = {};
|
|
|
|
var packetNames = {};
|
|
|
|
var packetIds = {};
|
|
|
|
var packetStates = {toClient: {}, toServer: {}};
|
|
|
|
for(var stateName in states) {
|
|
|
|
var state = states[stateName];
|
2015-05-13 09:15:16 -04:00
|
|
|
|
2015-05-14 16:08:49 -04:00
|
|
|
packetFields[state] = {toClient: [], toServer: []};
|
|
|
|
packetNames[state] = {toClient: [], toServer: []};
|
|
|
|
packetIds[state] = {toClient: [], toServer: []};
|
2015-05-13 09:15:16 -04:00
|
|
|
|
2015-05-14 16:08:49 -04:00
|
|
|
['toClient', 'toServer'].forEach(function(direction) {
|
|
|
|
for(var name in packets[state][direction]) {
|
|
|
|
var info = packets[state][direction][name];
|
|
|
|
var id = parseInt(info.id);
|
|
|
|
var fields = info.fields;
|
2015-05-13 09:15:16 -04:00
|
|
|
|
2015-05-14 16:08:49 -04:00
|
|
|
assert(id !== undefined, 'missing id for packet ' + name);
|
|
|
|
assert(fields !== undefined, 'missing fields for packet ' + name);
|
|
|
|
assert(!packetNames[state][direction].hasOwnProperty(id), 'duplicate packet id ' + id + ' for ' + name);
|
|
|
|
assert(!packetIds[state][direction].hasOwnProperty(name), 'duplicate packet name ' + name + ' for ' + id);
|
2015-09-19 18:41:03 -04:00
|
|
|
assert(!packetFields[state][direction].hasOwnProperty(name), 'duplicate packet id ' + id + ' for ' + name);
|
2015-05-14 16:08:49 -04:00
|
|
|
assert(!packetStates[direction].hasOwnProperty(name), 'duplicate packet name ' + name + ' for ' + id + ', must be unique across all states');
|
2015-05-13 09:15:16 -04:00
|
|
|
|
2015-05-14 16:08:49 -04:00
|
|
|
packetNames[state][direction][id] = name;
|
|
|
|
packetIds[state][direction][name] = id;
|
2015-09-19 18:41:03 -04:00
|
|
|
packetFields[state][direction][name] = fields;
|
2015-05-14 16:08:49 -04:00
|
|
|
packetStates[direction][name] = state;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
packetFields: packetFields,
|
|
|
|
packetNames: packetNames,
|
|
|
|
packetIds: packetIds,
|
|
|
|
packetStates: packetStates
|
|
|
|
};
|
2015-05-13 09:15:16 -04:00
|
|
|
}
|