Use minecraft-data for protocol data (#126)

* use minecraft-data protocol data

* use minecraft-data for extra data

* Update .npmignore

* update skin data

* fix example

* remove .gitattr

* fix mcdata skin import, disable install script
This commit is contained in:
extremeheat 2021-09-25 16:57:29 -04:00 committed by GitHub
commit f8ea6c01f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 39 additions and 52032 deletions

View file

@ -1,56 +1,19 @@
/**
* This is a utility script that converts the YAML here into ProtoDef schema code and (soon) docs/typescript definitions.
* It also pre-compiles JS code from the schema for easier development.
*
* Pre-compiles JS code from the schema for easier development.
* You can run this with `npm run build`
*
*/
const fs = require('fs')
const { ProtoDefCompiler } = require('protodef').Compiler
const { Versions } = require('../src/options')
const { convert } = require('minecraft-data/minecraft-data/tools/js/compileProtocol')
const mcData = require('minecraft-data')
const { join } = require('path')
function getJSON (path) {
return JSON.parse(fs.readFileSync(path, 'utf-8'))
}
// Parse the YML files and turn to JSON
function genProtoSchema () {
const { parse, compile } = require('protodef-yaml/compiler')
// Create the packet_map.yml from proto.yml
const parsed = parse('./proto.yml')
const version = parsed['!version']
const packets = []
for (const key in parsed) {
if (key.startsWith('%container')) {
const [, name] = key.split(',')
if (name.startsWith('packet_')) {
const children = parsed[key]
const packetName = name.replace('packet_', '')
const packetID = children['!id']
packets.push([packetID, packetName, name])
}
}
}
let l1 = ''
let l2 = ''
for (const [id, name, fname] of packets) {
l1 += ` 0x${id.toString(16).padStart(2, '0')}: ${name}\n`
l2 += ` if ${name}: ${fname}\n`
}
// TODO: skip creating packet_map.yml and just generate the ProtoDef map JSON directly
const t = `#Auto-generated from proto.yml, do not modify\n!import: types.yaml\nmcpe_packet:\n name: varint =>\n${l1}\n params: name ?\n${l2}`
fs.writeFileSync('./packet_map.yml', t)
compile('./proto.yml', 'proto.json')
return version
}
// Filter versions we support
const versions = mcData.versions.bedrock.filter(e => e.releaseType === 'release').map(e => e.minecraftVersion)
// Compile the ProtoDef JSON into JS
function createProtocol () {
function createProtocol (version) {
const compiler = new ProtoDefCompiler()
const protocol = getJSON('./protocol.json').types
const protocol = mcData('bedrock_' + version).protocol.types
compiler.addTypes(require('../src/datatypes/compiler-minecraft'))
compiler.addTypes(require('prismarine-nbt/compiler-zigzag'))
compiler.addTypesToCompile(protocol)
@ -63,27 +26,19 @@ function createProtocol () {
return compiledProto
}
function copyLatest () {
process.chdir(join(__dirname, '/../data/latest'))
const version = genProtoSchema()
try { fs.mkdirSync(`../${version}`) } catch {}
fs.writeFileSync(`../${version}/protocol.json`, JSON.stringify({ types: getJSON('./proto.json') }, null, 2))
fs.unlinkSync('./proto.json') // remove temp file
fs.unlinkSync('./packet_map.yml') // remove temp file
return version
}
function main (ver = 'latest') {
if (ver === 'latest') ver = copyLatest()
process.chdir(join(__dirname, '/../data/', ver))
// Put the .js files into the data/ dir, we also use the data dir when dumping packets for tests
const dir = join(__dirname, '/../data/', ver)
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true })
process.chdir(dir)
console.log('Generating JS...', ver)
createProtocol(ver)
}
// If no argument, build everything
if (!process.argv[2]) {
copyLatest()
for (const version in Versions) {
convert('latest')
for (const version of versions) {
main(version)
}
} else { // build the specified version