Use a mapper for symbolic subpacket names vs discriminator byte values

This commit is contained in:
deathcap 2016-01-23 16:29:15 -08:00
parent 80cc68e480
commit 74c5b74950

View file

@ -62,7 +62,7 @@ proto.addType('FML|HS',
[ [
{ {
"name": "discriminator", "name": "discriminator",
"type": "byte" "type": "fml|hsMapper"
}, },
// ServerHello // ServerHello
@ -73,7 +73,7 @@ proto.addType('FML|HS',
{ {
"compareTo": "discriminator", "compareTo": "discriminator",
"fields": { "fields": {
"0": "byte" "ServerHello": "byte"
}, },
"default": "void" "default": "void"
}, },
@ -86,7 +86,7 @@ proto.addType('FML|HS',
{ {
"compareTo": "discriminator", "compareTo": "discriminator",
"fields": { "fields": {
"0": [ "ServerHello": [
"switch", "switch",
{ {
// "Only sent if protocol version is greater than 1." // "Only sent if protocol version is greater than 1."
@ -112,7 +112,7 @@ proto.addType('FML|HS',
{ {
"compareTo": "discriminator", "compareTo": "discriminator",
"fields": { "fields": {
"1": "byte" "ClientHello": "byte"
}, },
"default": "void" "default": "void"
} }
@ -127,7 +127,7 @@ proto.addType('FML|HS',
{ {
"compareTo": "discriminator", "compareTo": "discriminator",
"fields": { "fields": {
"2": [ "ModList": [
"array", "array",
{ {
"countType": "varint", "countType": "varint",
@ -161,7 +161,7 @@ proto.addType('FML|HS',
{ {
"compareTo": "discriminator", "compareTo": "discriminator",
"fields": { "fields": {
"3": "boolean" "RegistryData": "boolean"
}, },
"default": "void" "default": "void"
}, },
@ -190,7 +190,7 @@ proto.addType('FML|HS',
{ {
"compareTo": "discriminator", "compareTo": "discriminator",
"fields": { "fields": {
"-1": "byte" "HandshakeAck": "byte"
}, },
"default": "void" "default": "void"
}, },
@ -202,7 +202,7 @@ proto.addType('FML|HS',
function writeAck(client, phase) { function writeAck(client, phase) {
var ackData = proto.createPacketBuffer('FML|HS', { var ackData = proto.createPacketBuffer('FML|HS', {
discriminator: -1, // HandshakeAck, discriminator: 'HandshakeAck', // HandshakeAck,
phase: 2 // WAITINGSERVERDATA phase: 2 // WAITINGSERVERDATA
}); });
client.write('custom_payload', { client.write('custom_payload', {
@ -225,7 +225,7 @@ client.on('custom_payload', function(packet) {
console.log('FML|HS',parsed); console.log('FML|HS',parsed);
if (parsed.data.discriminator === 0) { // ServerHello if (parsed.data.discriminator === 'ServerHello') {
if (parsed.data.fmlProtocolVersionServer > 2) { if (parsed.data.fmlProtocolVersionServer > 2) {
// TODO: support higher protocols, if they change // TODO: support higher protocols, if they change
} }
@ -236,7 +236,7 @@ client.on('custom_payload', function(packet) {
}); });
var clientHello = proto.createPacketBuffer('FML|HS', { var clientHello = proto.createPacketBuffer('FML|HS', {
discriminator: 1, // ClientHello discriminator: 'ClientHello',
fmlProtocolVersionClient: parsed.data.fmlProtocolVersionServer fmlProtocolVersionClient: parsed.data.fmlProtocolVersionServer
}); });
@ -246,7 +246,7 @@ client.on('custom_payload', function(packet) {
}); });
var modList = proto.createPacketBuffer('FML|HS', { var modList = proto.createPacketBuffer('FML|HS', {
discriminator: 2, // ModList discriminator: 'ModList',
//mods: [] //mods: []
// TODO: send from ServerListPing packet, allow customizing not hardcoding // TODO: send from ServerListPing packet, allow customizing not hardcoding
mods: [ mods: [
@ -258,18 +258,18 @@ client.on('custom_payload', function(packet) {
data: modList data: modList
}); });
writeAck(client, 2); // WAITINGSERVERDATA writeAck(client, 2); // WAITINGSERVERDATA
} else if (parsed.data.discriminator === 2) { // ModList } else if (parsed.data.discriminator === 'ModList') {
console.log('Server ModList:',parsed.data.mods); console.log('Server ModList:',parsed.data.mods);
// TODO: client/server check if mods compatible // TODO: client/server check if mods compatible
} else if (parsed.data.discriminator === 3) { // RegistryData } else if (parsed.data.discriminator === 'RegistryData') {
console.log('RegistryData',parsed.data); console.log('RegistryData',parsed.data);
if (!parsed.data.hasMore) { if (!parsed.data.hasMore) {
console.log('LAST RegistryData'); console.log('LAST RegistryData');
writeAck(client, 3); // WAITINGSERVERCOMPLETE writeAck(client, 3); // WAITINGSERVERCOMPLETE
} }
} else if (parsed.data.discriminator === -1) { // HandshakeAck } else if (parsed.data.discriminator === 'HandshakeAck') {
if (parsed.data.phase === 2) { // WAITINGCACK if (parsed.data.phase === 2) { // WAITINGCACK
writeAck(client, 4); // PENDINGCOMPLETE writeAck(client, 4); // PENDINGCOMPLETE
} else if (parsed.data.phase === 3) { // COMPLETE } else if (parsed.data.phase === 3) { // COMPLETE