Created
August 24, 2015 21:08
-
-
Save roblabla/c1cdb842fd6a9e1f9978 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var proto=require("./../../enums/protocol"); | |
var fs=require("fs"); | |
function getFieldInfo(field) { | |
if (Array.isArray(field)) | |
return { type: field[0], typeArgs: field[1] }; | |
else if (field.type && field.typeArgs) | |
return field; | |
else if (typeof field === "string") | |
return { type: field } | |
else | |
throw new Error("Not a fieldInfo : " + JSON.stringify(field, null, 4)); | |
} | |
function transformProtocol(protocol) | |
{ | |
var transformedProtocol = Object | |
.keys(protocol) | |
.reduce(function(transformedProtocol, state) { | |
transformedProtocol[state] = Object | |
.keys(protocol[state]) | |
.reduce(function(stateO, direction) { | |
stateO[direction] = Object | |
.keys(protocol[state][direction]) | |
.reduce(function(packetsO, packetName) { | |
packetsO[packetName] = transformPacket(protocol[state][direction][packetName]); | |
return packetsO; | |
}, {}); | |
return stateO; | |
}, {}); | |
return transformedProtocol; | |
}, {}); | |
transformedProtocol=reorder(["handshaking","status","login","play"],transformedProtocol); | |
return transformedProtocol; | |
} | |
function reorder (order, obj) { | |
return order.reduce (function (rslt, prop) { | |
rslt[prop] = obj[prop]; | |
return rslt; | |
}, {}); | |
} | |
function write(protocol){ | |
fs.writeFile("../../enums/protocol.json", JSON.stringify(protocol,null,2)); | |
} | |
function transformPacket(packet) | |
{ | |
return { | |
"id":packet.id, | |
"fields":transformFields(packet["fields"]) | |
}; | |
} | |
function transformFields(fields) | |
{ | |
return fields.map(function(field){try { return transformField(field);} catch (e) { console.log(field); throw e }}); | |
} | |
function transformField(field) | |
{ | |
console.log("Transforming", field.name); | |
return { | |
"name":field["name"], | |
"type":transformType(field.type) | |
} | |
} | |
function transformType(_field) { | |
var field = getFieldInfo(_field); | |
if(field.type == "container") | |
try { return ["container", transformFields(field.typeArgs)]; } catch (e) { console.log(_field, field); throw e } | |
if(field.type == "buffer") | |
return ["buffer", {"countType": field.typeArgs["countType"]}]; | |
if(field.type == "condition") { | |
return ["switch", { | |
"compareTo": (field.typeArgs.this ? "this." : "") + field.typeArgs.field, | |
"fields": field.typeArgs.values.reduce(function(result, value) { | |
if (field.typeArgs.different) | |
result[value] = "void"; | |
else | |
result[value] = transformType(field.typeArgs.type); | |
return result; | |
}, {}), | |
"default": transformType(field.typeArgs.different ? field.typeArgs.type : "void") | |
}]; | |
} | |
if(field.type=="array") | |
return ["array", field.typeArgs["count"] ? field.typeArgs : { | |
"countType":field.typeArgs["countType"], | |
"type":transformType(field.typeArgs.type), | |
}]; | |
if (typeof field.typeArgs !== "undefined") | |
return [field.type, field.typeArgs]; | |
else | |
return field.type; | |
} | |
write(transformProtocol(proto)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment