Support options.version==false in createClient() to auto-detect version

This commit is contained in:
deathcap 2016-01-30 22:50:24 -08:00
parent 163e2157fc
commit 9a68fd6c34
4 changed files with 19 additions and 15 deletions

View file

@ -5,7 +5,7 @@ if(process.argv.length < 4 || process.argv.length > 6) {
process.exit(1);
}
var client = mc.createClientAuto({
var client = mc.createClient({version: false,
host: process.argv[2],
port: parseInt(process.argv[3]),
username: process.argv[4] ? process.argv[4] : "echo",

View file

@ -1,9 +1,9 @@
'use strict';
var ping = require('./ping');
var ping = require('../ping');
var debug = require('../debug');
var states = require('../states');
var assert = require('assert');
var debug = require('./debug');
var createClient = require('./createClient');
var minecraft_data = require('minecraft-data');
// Get the minecraft-data version string for a protocol version
@ -14,19 +14,18 @@ function protocolVersion2MinecraftVersion(n) {
var version = minecraft_data.versions[i];
if (version.version === Math.abs(n) && version.usesNetty === usesNetty) {
console.log(version);
return version.minecraftVersion;
return [version.minecraftVersion, version.majorVersion];
}
}
throw new Error(`unsupported/unknown protocol version: ${n}, update minecraft-data`);
}
function createClientAuto(options) {
assert.ok(options, 'options is required');
module.exports = function(client) {
var options = client.options;
debug('creating client');
options.wait_connect = true; // don't let createClient / src/client/setProtocol proceed on socket 'connect' until 'connect_allowed'
var client = createClient(options); // vanilla
options.wait_connect = true; // don't let src/client/setProtocol proceed on socket 'connect' until 'connect_allowed'
debug('pinging',options.host);
// TODO: detect ping timeout, https://github.com/PrismarineJS/node-minecraft-protocol/issues/329
ping(options, function(err, response) {
@ -46,12 +45,17 @@ function createClientAuto(options) {
// Note that versionName is a descriptive version stirng like '1.8.9' on vailla, but other
// servers add their own name (Spigot 1.8.8, Glowstone++ 1.8.9) so we cannot use it directly,
// even though it is in a format accepted by minecraft-data. Instead, translate the protocol.
client.options.version = protocolVersion2MinecraftVersion(protocolVersion);
var [minecraftVersion, majorVersion] = protocolVersion2MinecraftVersion(protocolVersion);
client.options.version = minecraftVersion;
// Use the exact same protocol version
// Requires https://github.com/PrismarineJS/node-minecraft-protocol/pull/330
client.options.protocolVersion = protocolVersion;
// reinitialize client object with new version TODO: move out of its constructor
client.version = majorVersion;
client.setSerializer(states.HANDSHAKING);
if (response.modinfo && response.modinfo.type === 'FML') {
// Use the list of Forge mods from the server ping, so client will match server
var forgeMods = response.modinfo.modList;
@ -66,5 +70,3 @@ function createClientAuto(options) {
});
return client;
}
module.exports = createClientAuto;

View file

@ -8,6 +8,7 @@ var caseCorrect = require('./client/caseCorrect');
var setProtocol = require('./client/setProtocol');
var play = require('./client/play');
var tcp_dns = require('./client/tcp_dns');
var autoVersion = require('./client/autoVersion');
module.exports=createClient;
@ -15,8 +16,10 @@ function createClient(options) {
assert.ok(options, "options is required");
assert.ok(options.username, "username is required");
// TODO: avoid setting default version if autoVersion is enabled
var optVersion = options.version || require("./version").defaultVersion;
var mcData=require("minecraft-data")(optVersion);
if (!mcData) throw new Error(`unsupported protocol version: ${optVersion}`);
var version = mcData.version;
options.majorVersion = version.majorVersion;
options.protocolVersion = version.version;
@ -25,12 +28,13 @@ function createClient(options) {
client.options = options;
tcp_dns(client);
caseCorrect(client);
if (options.version === false) autoVersion(client);
setProtocol(client);
keepalive(client);
encrypt(client);
play(client);
compress(client);
caseCorrect(client);
return client;
}

View file

@ -2,12 +2,10 @@ var Client = require('./client');
var Server = require('./server');
var serializer = require("./transforms/serializer");
var createClient = require("./createClient");
var createClientAuto = require("./createClientAuto");
var createServer = require("./createServer");
module.exports = {
createClient: createClient,
createClientAuto: createClientAuto,
createServer: createServer,
Client: Client,
Server: Server,