Files
minecraft-data/tools/js/convertMcpcProtocolJsonToYaml.js
extremeheat a748994fe2 Implement protodef-yaml for mcpc (#886)
* Add yaml protocol data for mcpc

* update bedrock protocol data for latest protodef-yaml changes
Changes only to sorting

* add pc support to incrementVersion script

* update build

* update pc proto.yml to add class name comments

* Update package.json

* update incrementVersion script and add to doc

* Update incrementVersion.js

Fix not updating protocol/version in dataPath for newest version

* add doc for protocol

* convert old pc protocols to yaml

* lint

* Use hex in protocol.json packet mappers

* fix test

* update generated protocols
Fix protodef-yaml setting switch defaults to void
2024-07-18 08:10:55 +02:00

66 lines
1.9 KiB
JavaScript

const { genYAML } = require('protodef-yaml')
const { join } = require('path')
const fs = require('fs')
function addDataPath (version, key, value) {
const dataPaths = require('../../data/dataPaths.json')
if (dataPaths.pc[version][key]) return
dataPaths.pc[version][key] = value
console.log('Updating datapath for', version, key, value)
fs.writeFileSync(join(__dirname, '../../data/dataPaths.json'), JSON.stringify(dataPaths, null, 2))
}
function convert (version) {
let text = '!version: ' + version + '\n\n'
const protocol = require(`../../data/pc/${version}/protocol.json`)
for (const state in protocol) {
const data = protocol[state]
if (state === 'types') {
text += '^types:\n'
text += genYAML(data, 1)
}
// 0.30c (stateless)
if (state === 'toClient') {
text += '\n^toClient.types:\n'
text += genYAML(data.types, 1)
}
if (state === 'toServer') {
text += '\n^toServer.types:\n'
text += genYAML(data.types, 1)
}
// Rest of versions have states
if (data.toClient) {
text += `\n^${state}.toClient.types:\n`
text += genYAML(data.toClient.types, 1)
}
if (data.toServer) {
text += `\n^${state}.toServer.types:\n`
text += genYAML(data.toServer.types, 1)
}
}
const outFile = `../../data/pc/${version}/proto.yml`
const absPath = fs.realpathSync(`../../data/pc/${version}/`) + '/proto.yml'
console.log(`Wrote to ${absPath}`)
fs.writeFileSync(outFile, text)
addDataPath(version, 'proto', 'pc/' + version)
}
if (process.argv.length < 3) {
console.log('Usage: node convertMcpcProtocolJsonToYaml.js <version>')
process.exit(1)
}
const version = process.argv[2]
if (version === 'all') {
const dataPaths = require('../../data/dataPaths.json')
for (const version in dataPaths.pc) {
try {
convert(version)
} catch (e) {
console.log('Failed for', version, e)
}
}
} else {
convert(version)
}