Last active
October 22, 2023 15:17
-
-
Save rom1504/e98047e95c09c9472094 to your computer and use it in GitHub Desktop.
mcpe json conv
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 fs = require('fs'); | |
var xml2js = require('xml2js'); | |
var parser = new xml2js.Parser(); | |
fs.readFile(__dirname + '/protocol.xml', function(err, data) { | |
parser.parseString(data, function (err, result) { | |
fs.writeFileSync('output.json', JSON.stringify(result, null, 2)); | |
var protocol = JSON.parse(fs.readFileSync(__dirname + '/output.json')); | |
var mappings={}; | |
var fields_switch={}; | |
var _main = { | |
"types":{ | |
"packet": [ | |
"container", | |
[ | |
{ | |
"name": "name", | |
"type": [ | |
"mapper", | |
{ | |
"type": "u8", | |
"mappings": mappings | |
} | |
] | |
}, | |
{ | |
"name": "params", | |
"type": [ | |
"switch", | |
{ | |
"compareTo": "name", | |
"fields": fields_switch | |
} | |
] | |
} | |
] | |
] | |
} | |
} | |
protocol.protocol.pdu.forEach(function(packet) { | |
var packet_name = packet.$.name.toLowerCase().split(' ').join('_'); | |
var packet_id = packet.$.id; | |
var packet_online = packet.$.online; | |
var packet_type="packet_"+packet_name; | |
mappings[packet_id]=packet_name; | |
fields_switch[packet_name]=packet_type; | |
var fields=[]; | |
if(packet.field != null) { | |
packet.field.forEach(function(field) { | |
var _field = { | |
name: field.$.name.toLowerCase().split(' ').join('_'), | |
type: field.$.type.toLowerCase().split(' ').join('_') | |
}; | |
fields.push(_field); | |
}); | |
} | |
_main.types[packet_type]=["container",fields]; | |
}); | |
fs.unlinkSync('output.json'); | |
fs.writeFileSync('protocol.json', JSON.stringify(_main, null, 2)); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment