OptiFine & UUID fix

Allows OptiFine properly install and for forge clients to launch in offline mode. Addresses #84
This commit is contained in:
Pierce 2022-03-01 18:52:06 -08:00
parent 756caa260b
commit 86d42246f1
4 changed files with 169 additions and 148 deletions

View file

@ -1,14 +1,17 @@
const request = require('request') const request = require('request')
const uuid = require('uuid').v1 const { v3 } = require('uuid')
let uuid
let api_url = 'https://authserver.mojang.com' let api_url = 'https://authserver.mojang.com'
module.exports.getAuth = function (username, password, client_token = null) { module.exports.getAuth = function (username, password, client_token = null) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
getUUID(username)
if (!password) { if (!password) {
const user = { const user = {
access_token: uuid(), access_token: uuid,
client_token: client_token || uuid(), client_token: client_token || uuid,
uuid: uuid(), uuid,
name: username, name: username,
user_properties: '{}' user_properties: '{}'
} }
@ -23,9 +26,9 @@ module.exports.getAuth = function (username, password, client_token = null) {
name: 'Minecraft', name: 'Minecraft',
version: 1 version: 1
}, },
username: username, username,
password: password, password,
clientToken: uuid(), clientToken: uuid,
requestUser: true requestUser: true
} }
} }
@ -50,13 +53,13 @@ module.exports.getAuth = function (username, password, client_token = null) {
}) })
} }
module.exports.validate = function (access_token, client_token) { module.exports.validate = function (accessToken, clientToken) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const requestObject = { const requestObject = {
url: api_url + '/validate', url: api_url + '/validate',
json: { json: {
accessToken: access_token, accessToken,
clientToken: client_token clientToken
} }
} }
@ -74,8 +77,8 @@ module.exports.refreshAuth = function (accessToken, clientToken) {
const requestObject = { const requestObject = {
url: api_url + '/refresh', url: api_url + '/refresh',
json: { json: {
accessToken: accessToken, accessToken,
clientToken: clientToken, clientToken,
requestUser: true requestUser: true
} }
} }
@ -88,13 +91,13 @@ module.exports.refreshAuth = function (accessToken, clientToken) {
const userProfile = { const userProfile = {
access_token: body.accessToken, access_token: body.accessToken,
client_token: uuid(), client_token: getUUID(body.selectedProfile.name),
uuid: body.selectedProfile.id, uuid: body.selectedProfile.id,
name: body.selectedProfile.name, name: body.selectedProfile.name,
user_properties: parsePropts(body.user.properties) user_properties: parsePropts(body.user.properties)
} }
resolve(userProfile) return resolve(userProfile)
}) })
}) })
} }
@ -104,16 +107,16 @@ module.exports.invalidate = function (accessToken, clientToken) {
const requestObject = { const requestObject = {
url: api_url + '/invalidate', url: api_url + '/invalidate',
json: { json: {
accessToken: accessToken, accessToken,
clientToken: clientToken clientToken
} }
} }
request.post(requestObject, function (error, response, body) { request.post(requestObject, function (error, response, body) {
if (error) return reject(error) if (error) return reject(error)
if (!body) resolve(true) if (!body) return resolve(true)
else reject(body) else return reject(body)
}) })
}) })
} }
@ -123,16 +126,16 @@ module.exports.signOut = function (username, password) {
const requestObject = { const requestObject = {
url: api_url + '/signout', url: api_url + '/signout',
json: { json: {
username: username, username,
password: password password
} }
} }
request.post(requestObject, function (error, response, body) { request.post(requestObject, function (error, response, body) {
if (error) return reject(error) if (error) return reject(error)
if (!body) resolve(true) if (!body) return resolve(true)
else reject(body) else return reject(body)
}) })
}) })
} }
@ -156,3 +159,10 @@ function parsePropts (array) {
return '{}' return '{}'
} }
} }
function getUUID (value) {
if (!uuid) {
uuid = v3(value, v3.DNS)
}
return uuid
}

View file

@ -458,7 +458,7 @@ class Handler {
runInstaller (path) { runInstaller (path) {
return new Promise(resolve => { return new Promise(resolve => {
const installer = child.exec(path) const installer = child.exec(path)
installer.on('close', (code) => resolve()) installer.on('close', (code) => resolve(code))
}) })
} }

View file

@ -6,6 +6,7 @@ const EventEmitter = require('events').EventEmitter
class MCLCore extends EventEmitter { class MCLCore extends EventEmitter {
async launch (options) { async launch (options) {
try {
this.options = { ...options } this.options = { ...options }
this.options.root = path.resolve(this.options.root) this.options.root = path.resolve(this.options.root)
this.options.overrides = { this.options.overrides = {
@ -51,8 +52,14 @@ class MCLCore extends EventEmitter {
if (this.options.installer) { if (this.options.installer) {
// So installers that create a profile in launcher_profiles.json can run without breaking. // So installers that create a profile in launcher_profiles.json can run without breaking.
const profilePath = path.join(this.options.root, 'launcher_profiles.json') const profilePath = path.join(this.options.root, 'launcher_profiles.json')
if (!fs.existsSync(profilePath)) { fs.writeFileSync(profilePath, JSON.stringify({}, null, 4)) } if (!fs.existsSync(profilePath) || !JSON.parse(fs.readFileSync(profilePath)).profiles) {
await this.handler.runInstaller(this.options.installer) fs.writeFileSync(profilePath, JSON.stringify({ profiles: {} }, null, 4))
}
const code = await this.handler.runInstaller(this.options.installer)
if (!this.options.version.custom && code === 0) {
this.emit('debug', '[MCLC]: Installer successfully ran, but no custom version was provided')
}
this.emit('debug', `[MCLC]: Installer closed with code ${code}`)
} }
const directory = this.options.overrides.directory || path.join(this.options.root, 'versions', this.options.version.custom ? this.options.version.custom : this.options.version.number) const directory = this.options.overrides.directory || path.join(this.options.root, 'versions', this.options.version.custom ? this.options.version.custom : this.options.version.number)
@ -133,6 +140,10 @@ class MCLCore extends EventEmitter {
this.emit('debug', `[MCLC]: Launching with arguments ${launchArguments.join(' ')}`) this.emit('debug', `[MCLC]: Launching with arguments ${launchArguments.join(' ')}`)
return this.startMinecraft(launchArguments) return this.startMinecraft(launchArguments)
} catch (e) {
this.emit('debug', `[MCLC]: Failed to start due to ${e}, closing...`)
return null
}
} }
printVersion () { printVersion () {

View file

@ -1,6 +1,6 @@
{ {
"name": "minecraft-launcher-core", "name": "minecraft-launcher-core",
"version": "3.16.11", "version": "3.16.12",
"description": "Lightweight module that downloads and runs Minecraft using javascript / NodeJS", "description": "Lightweight module that downloads and runs Minecraft using javascript / NodeJS",
"main": "index.js", "main": "index.js",
"dependencies": { "dependencies": {