bedrock-protocol/data/provider.js
extremeheat a55eaddc98
Add packet dumper, new server example, internal client/server test (#4)
* Add packet dumper, configuable vanilla server, client events

* Fix server/client closing

* Add internal server test

* protocol: use WindowID types

* Add internal client/server test

* test timeout fixes

* client example updates

* update server example, use protocol updates
Server example with bedrock-provider
Use 64-bit varints for entity runtime ids

* fix internal test packet path
2021-03-26 05:19:08 -04:00

44 lines
1.3 KiB
JavaScript

const { Versions } = require('../src/options')
const { getFiles } = require('../src/datatypes/util')
const { join } = require('path')
let 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) => {
fileMap = {}
loadVersions()
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)
}
}
}