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,40 +58,40 @@ 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
proto.addType('FML|HS',
[
'container',
"container",
[
{
"name": "discriminator",
"type": "fml|hsMapper"
},
// ServerHello
{
"name": "fmlProtocolVersionServer",
"type": [
"anon": true,
"type":
[
"switch",
{
"compareTo": "discriminator",
"fields": {
"ServerHello": "byte"
},
"default": "void"
},
],
"fields":
{
"ServerHello":
[
"container",
[
{
"name": "fmlProtocolVersionServer",
"type": "byte"
},
{
"name": "overrideDimension",
"type": [
"switch",
{
"compareTo": "discriminator",
"fields": {
"ServerHello": [
"type":
[
"switch",
{
// "Only sent if protocol version is greater than 1."
"compareTo": "fmlProtocolVersion",
"fields": {
"compareTo": "fmlProtocolVersionServer",
"fields":
{
"0": "void",
"1": "void"
},
@ -99,39 +99,33 @@ proto.addType('FML|HS',
}
]
},
"default": "void"
},
],
},
],
// ClientHello
"ClientHello":
[
"container",
[
{
"name": "fmlProtocolVersionClient", // TODO: merge or fix name collision with fmlProtocolVersionServer?
"type": [
"switch",
{
"compareTo": "discriminator",
"fields": {
"ClientHello": "byte"
},
"default": "void"
"name": "fmlProtocolVersionClient",
"type": "byte"
}
]
],
},
// ModList
"ModList":
[
"container",
[
{
"name": "mods",
"type": [
"switch",
{
"compareTo": "discriminator",
"fields": {
"ModList": [
"type":
[
"array",
{
"countType": "varint",
"type": [
"type":
[
"container",
[
{
@ -145,57 +139,44 @@ proto.addType('FML|HS',
]
],
},
],
},
"default": "void"
]
}
// TODO: mods array: modname string, modversion string
],
},
],
// RegistryData
"RegistryData":
[
"container",
[
{
"name": "hasMore",
"type": [
"switch",
{
"compareTo": "discriminator",
"fields": {
"RegistryData": "boolean"
"type": "boolean"
},
"default": "void"
},
],
/* TODO: support all fields
"name": "registryName",
"type": [
"switch",
{
"compareTo": "discriminator",
"fields": {
"3": "string"
"name": "registryName",
"type": "string"
},
"default": "void"
},
],
*/
},
],
],
// HandshakeAck
"HandshakeAck":
[
"container",
[
{
"name": "phase",
"type": [
"switch",
{
"compareTo": "discriminator",
"fields": {
"HandshakeAck": "byte"
},
"default": "void"
"type": "byte"
},
],
],
},
}
]
}
]
]
);
@ -245,6 +226,7 @@ client.on('custom_payload', function(packet) {
data: clientHello
});
console.log('Sending client modlist');
var modList = proto.createPacketBuffer('FML|HS', {
discriminator: 'ModList',
//mods: []