Skip to content

Commit

Permalink
Fix version check (#3259)
Browse files Browse the repository at this point in the history
* Fix version check

* improve variable naming

* fix common test server close to handle already closed server

* handle too old protocol versions

* fix syntax

* Update version.js lint

* remove `supportedVersions`

* expose latestSupportedVersion/oldestSupportedVersion

* Update loader.js
  • Loading branch information
extremeheat authored Dec 30, 2023
1 parent 79cd7bd commit 88d361f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,8 @@ export class Particle {
);
}

export let supportedVersions: string[]
export let testedVersions: string[]
export let latestSupportedVersion: string
export let oldestSupportedVersion: string

export function supportFeature (feature: string, version: string): boolean
34 changes: 17 additions & 17 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const plugins = {
particle: require('./plugins/particle')
}

const supportedVersions = require('./version').supportedVersions
const testedVersions = require('./version').testedVersions
const minecraftData = require('minecraft-data')
const { testedVersions, latestSupportedVersion, oldestSupportedVersion } = require('./version')

module.exports = {
createBot,
Expand All @@ -55,9 +55,10 @@ module.exports = {
ScoreBoard: require('./scoreboard'),
BossBar: require('./bossbar'),
Particle: require('./particle'),
supportedVersions,
latestSupportedVersion,
oldestSupportedVersion,
testedVersions,
supportFeature: (feature, version) => require('prismarine-registry')(version).supportFeature(feature)
supportFeature: (feature, version) => minecraftData(version).supportFeature(feature)
}

function createBot (options = {}) {
Expand Down Expand Up @@ -107,22 +108,21 @@ function createBot (options = {}) {
if (!bot._client.wait_connect) next()
else bot._client.once('connect_allowed', next)
function next () {
bot.registry = require('prismarine-registry')(bot._client.version)
const version = bot.registry.version
if (supportedVersions.indexOf(version.majorVersion) === -1) {
throw new Error(`Version ${version.minecraftVersion} is not supported.`)
}
const serverPingVersion = bot._client.version
bot.registry = require('prismarine-registry')(serverPingVersion)
if (!bot.registry?.version) throw new Error(`Server version '${serverPingVersion}' is not supported, no data for version`)

const latestTestedVersion = testedVersions[testedVersions.length - 1]
const latestProtocolVersion = require('prismarine-registry')(latestTestedVersion).protocolVersion
if (version.protocolVersion > latestProtocolVersion) {
throw new Error(`Version ${version.minecraftVersion} is not supported. Latest supported version is ${latestTestedVersion}.`)
const versionData = bot.registry.version
if (versionData['>'](latestSupportedVersion)) {
throw new Error(`Server version '${serverPingVersion}' is not supported. Latest supported version is '${latestSupportedVersion}'.`)
} else if (versionData['<'](oldestSupportedVersion)) {
throw new Error(`Server version '${serverPingVersion}' is not supported. Oldest supported version is '${oldestSupportedVersion}'.`)
}

bot.protocolVersion = version.version
bot.majorVersion = version.majorVersion
bot.version = version.minecraftVersion
options.version = version.minecraftVersion
bot.protocolVersion = versionData.version
bot.majorVersion = versionData.majorVersion
bot.version = versionData.minecraftVersion
options.version = versionData.minecraftVersion
bot.supportFeature = bot.registry.supportFeature
setTimeout(() => bot.emit('inject_allowed'), 0)
}
Expand Down
6 changes: 4 additions & 2 deletions lib/version.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const testedVersions = ['1.8.8', '1.9.4', '1.10.2', '1.11.2', '1.12.2', '1.13.2', '1.14.4', '1.15.2', '1.16.5', '1.17.1', '1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20.1']
module.exports = {
supportedVersions: ['1.8', '1.9', '1.10', '1.11', '1.12', '1.13', '1.14', '1.15', '1.16', '1.17', '1.18', '1.19', '1.20'],
testedVersions: ['1.8.8', '1.9.4', '1.10.2', '1.11.2', '1.12.2', '1.13.2', '1.14.4', '1.15.2', '1.16.5', '1.17.1', '1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20.1']
testedVersions,
latestSupportedVersion: testedVersions[testedVersions.length - 1],
oldestSupportedVersion: testedVersions[0]
}
12 changes: 8 additions & 4 deletions test/externalTests/plugins/testCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,14 @@ function inject (bot) {
const closeExample = async (err) => {
console.log('kill process ' + child.pid)

process.kill(child.pid, 'SIGTERM')

const [code] = await once(child, 'close')
console.log('close requested', code)
try {
process.kill(child.pid, 'SIGTERM')
const [code] = await onceWithCleanup(child, 'close', { timeout: 1000 })
console.log('close requested', code)
} catch (e) {
console.log(e)
console.log('process termination failed, process may already be closed')
}

if (err) {
throw err
Expand Down

0 comments on commit 88d361f

Please sign in to comment.