bedrock-protocol/data/provider.js
extremeheat df8612e355
Start work on multi-version support, test cleanup (#43)
* some cleanup

* start work on multi-version support

* undelete some old examples, can update them later

* move old examples
2021-03-12 20:20:25 +01:00

45 lines
No EOL
1.4 KiB
JavaScript

const { Versions } = require('../src/options')
const { getFiles } = require('../src/datatypes/util')
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(__dirname + '/' + version)
} catch {}
for (let file of files) {
const rfile = file.replace(__dirname + '/' + version + '/', '')
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()
// console.log('file map', fileMap)
// module.exports(Versions['1.16.210']).open('creativeitems.json')