Switch on packet type through an anonymous container

This commit is contained in:
deathcap 2016-01-23 17:02:07 -08:00
parent 74c5b74950
commit b9bc0e1cb8

View file

@ -58,144 +58,125 @@ proto.addType('fml|hsMapper',
// TODO: refactor to use one big switch like https://github.com/PrismarineJS/node-minecraft-protocol/blob/master/src/transforms/serializer.js#L21 // TODO: refactor to use one big switch like https://github.com/PrismarineJS/node-minecraft-protocol/blob/master/src/transforms/serializer.js#L21
proto.addType('FML|HS', proto.addType('FML|HS',
[ [
'container', "container",
[ [
{ {
"name": "discriminator", "name": "discriminator",
"type": "fml|hsMapper" "type": "fml|hsMapper"
}, },
// ServerHello
{ {
"name": "fmlProtocolVersionServer", "anon": true,
"type": [ "type":
[
"switch", "switch",
{ {
"compareTo": "discriminator", "compareTo": "discriminator",
"fields": { "fields":
"ServerHello": "byte" {
}, "ServerHello":
"default": "void" [
}, "container",
], [
}, {
{ "name": "fmlProtocolVersionServer",
"name": "overrideDimension", "type": "byte"
"type": [
"switch",
{
"compareTo": "discriminator",
"fields": {
"ServerHello": [
"switch",
{
// "Only sent if protocol version is greater than 1."
"compareTo": "fmlProtocolVersion",
"fields": {
"0": "void",
"1": "void"
}, },
"default": "int" {
} "name": "overrideDimension",
] "type":
},
"default": "void"
},
],
},
// ClientHello
{
"name": "fmlProtocolVersionClient", // TODO: merge or fix name collision with fmlProtocolVersionServer?
"type": [
"switch",
{
"compareTo": "discriminator",
"fields": {
"ClientHello": "byte"
},
"default": "void"
}
],
},
// ModList
{
"name": "mods",
"type": [
"switch",
{
"compareTo": "discriminator",
"fields": {
"ModList": [
"array",
{
"countType": "varint",
"type": [
"container",
[ [
"switch",
{ {
"name": "name", // "Only sent if protocol version is greater than 1."
"type": "string" "compareTo": "fmlProtocolVersionServer",
}, "fields":
{ {
"name": "version", "0": "void",
"type": "string" "1": "void"
},
"default": "int"
} }
] ]
], },
}, ],
], ],
"ClientHello":
[
"container",
[
{
"name": "fmlProtocolVersionClient",
"type": "byte"
}
]
],
"ModList":
[
"container",
[
{
"name": "mods",
"type":
[
"array",
{
"countType": "varint",
"type":
[
"container",
[
{
"name": "name",
"type": "string"
},
{
"name": "version",
"type": "string"
}
]
],
},
]
}
],
],
"RegistryData":
[
"container",
[
{
"name": "hasMore",
"type": "boolean"
},
/* TODO: support all fields
{
"name": "registryName",
"type": "string"
},
*/
],
],
"HandshakeAck":
[
"container",
[
{
"name": "phase",
"type": "byte"
},
],
],
}, },
"default": "void"
} }
// TODO: mods array: modname string, modversion string ]
], }
},
// RegistryData
{
"name": "hasMore",
"type": [
"switch",
{
"compareTo": "discriminator",
"fields": {
"RegistryData": "boolean"
},
"default": "void"
},
],
/* TODO: support all fields
"name": "registryName",
"type": [
"switch",
{
"compareTo": "discriminator",
"fields": {
"3": "string"
},
"default": "void"
},
],
*/
},
// HandshakeAck
{
"name": "phase",
"type": [
"switch",
{
"compareTo": "discriminator",
"fields": {
"HandshakeAck": "byte"
},
"default": "void"
},
],
},
] ]
] ]
); );
@ -245,6 +226,7 @@ client.on('custom_payload', function(packet) {
data: clientHello data: clientHello
}); });
console.log('Sending client modlist');
var modList = proto.createPacketBuffer('FML|HS', { var modList = proto.createPacketBuffer('FML|HS', {
discriminator: 'ModList', discriminator: 'ModList',
//mods: [] //mods: []