UNPKG

pocket-minecraft-protocol

Version:

Parse and serialize Minecraft Bedrock Edition packets

45 lines (40 loc) 1.36 kB
const { Versions } = require('../src/options') const { getFiles } = require('../src/datatypes/util') const { join } = require('path') const fileMap = {} // Walks all the directories for each of the supported versions in options.js // then builds a file map for each version // { 'protocol.json': { '1.16.200': '1.16.200/protocol.json', '1.16.210': '1.16.210/...' } } function loadVersions () { for (const version in Versions) { let files = [] try { files = getFiles(join(__dirname, '/', version)) } catch {} for (const file of files) { const rfile = file.replace(join(__dirname, '/', version) + '/', '') fileMap[rfile] = fileMap[rfile] ?? [] fileMap[rfile].push([Versions[version], file]) fileMap[rfile].sort().reverse() } } } module.exports = (protocolVersion) => { return { // Returns the most recent file based on the specified protocolVersion // e.g. if `version` is 1.16 and a file for 1.16 doesn't exist, load from 1.15 file getPath (file) { if (!fileMap[file]) { throw Error('Unknown file ' + file) } for (const [pver, path] of fileMap[file]) { if (pver <= protocolVersion) { // console.debug('for', file, 'returining', path) return path } } throw Error('unknown file ' + file) } } } loadVersions()