Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
Fix for new lunar launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Nilsen84 committed Aug 13, 2023
1 parent 763482d commit e3b9cb1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 69 deletions.
64 changes: 43 additions & 21 deletions gui/inject/main-inject.cjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import renderer from './renderer-inject.js'

module.exports = function() {
const { app, ipcMain, BrowserWindow, webContents, dialog, shell } = require('electron')
const cp = require('child_process'), originalSpawn = cp.spawn
const path = require('path')
const fs = require('fs')
const parse = require('shell-quote/parse')
const semver = require('semver')
const https = require('https')
const { app, webContents, dialog, shell, BrowserWindow, ipcMain } = require('electron')
const semver = require('semver')
const parse = require('shell-quote/parse')

const installDir = path.dirname(__dirname)

Expand Down Expand Up @@ -60,6 +61,18 @@ module.exports = function() {
}
}

ipcMain.on('LCQT_READ_CONFIG', event => {
event.returnValue = readConfigSync()
})

ipcMain.on('LCQT_WRITE_CONFIG', async (event, config) => {
await fs.promises.writeFile(
configPath,
JSON.stringify(config, null, 4),
'utf8'
)
})

ipcMain.on('LCQT_OPEN_WINDOW', event => {
let mainWin = BrowserWindow.fromWebContents(event.sender)

Expand All @@ -73,7 +86,6 @@ module.exports = function() {
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
additionalArguments: [configPath]
},
autoHideMenuBar: true
})
Expand All @@ -85,34 +97,44 @@ module.exports = function() {
window.loadURL(`file://${__dirname}/index.html`)
})

ipcMain.on('LCQT_GET_LAUNCH_OPTIONS', (event) => {
cp.spawn = (cmd, args, opts) => {
if(!['javaw', 'java'].includes(path.basename(cmd, '.exe'))) {
return originalSpawn(cmd, args, opts)
}

let config = readConfigSync()

event.returnValue = {
customJvm: config.customJvmEnabled && config.customJvm,
jvmArgs: [
...config.agents
? config.agents.filter(a => a.enabled).map(a => `-javaagent:${a.path}=${a.option}`)
: [],
`-javaagent:${path.join(installDir, 'agent.jar')}`,
...config.jvmArgsEnabled ? parse(config.jvmArgs) : []
],
minecraftArgs: config.crackedEnabled && config.crackedUsername ? [
'--username', config.crackedUsername
] : []
args = args.filter(e => e !== '-XX:+DisableAttachMechanism');
delete opts.env['_JAVA_OPTIONS'];
delete opts.env['JAVA_TOOL_OPTIONS'];
delete opts.env['JDK_JAVA_OPTIONS'];

args.splice(
Math.max(0, args.indexOf('-cp')),
0,
...config.agents ? config.agents.filter(a => a.enabled).map(a => `-javaagent:${a.path}=${a.option}`) : [],
`-javaagent:${path.join(installDir, 'agent.jar')}`,
...config.jvmArgsEnabled ? parse(config.jvmArgs) : []
)

if(config.crackedEnabled && config.crackedUsername) {
args.push('--username', config.crackedUsername)
}
})

return originalSpawn(
config.customJvmEnabled && config.customJvm || cmd,
args,
opts
)
}

for(const contents of webContents.getAllWebContents()) {
contents.removeAllListeners('devtools-opened')
contents.executeJavaScript(renderer)
}

app.on('web-contents-created', (event, webContents) => {
webContents.on('dom-ready', () => {
if(webContents.getURL().includes(__dirname)) return

webContents.removeAllListeners('devtools-opened')
webContents.executeJavaScript(renderer)
})
})
Expand Down
44 changes: 9 additions & 35 deletions gui/inject/renderer-inject.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,4 @@
import syringe from './syringe.svg'
const { ipcRenderer } = require('electron')
const path = require('path')
const cp = require('child_process'), originalSpawn = cp.spawn

cp.spawn = function (cmd, args, opts) {
if (!['javaw', 'java'].includes(path.basename(cmd, '.exe'))) {
return originalSpawn(cmd, args, opts)
}

args = args.filter(e => e !== '-XX:+DisableAttachMechanism');

delete opts.env['_JAVA_OPTIONS'];
delete opts.env['JAVA_TOOL_OPTIONS'];
delete opts.env['JDK_JAVA_OPTIONS'];

let lcqtOpts = ipcRenderer.sendSync('LCQT_GET_LAUNCH_OPTIONS')

args.splice(
Math.max(0, args.indexOf('-cp')),
0,
...lcqtOpts.jvmArgs
)

return originalSpawn(
lcqtOpts.customJvm || cmd,
[...args, ...lcqtOpts.minecraftArgs],
opts
)
}

function waitForElement(selector) {
return new Promise(resolve => {
Expand All @@ -49,10 +20,13 @@ function waitForElement(selector) {
})
}

waitForElement("#exit-button").then(exitButton => {
let clone = exitButton.cloneNode(false)
waitForElement(".fa-gears").then(faGears => {
let settingsButton = faGears.parentNode

let clone = settingsButton.cloneNode(false)
clone.id = null
clone.innerHTML = `<img src='${syringe}' width="22" height="22"/>`
clone.addEventListener('click', () => ipcRenderer.send('LCQT_OPEN_WINDOW'))
exitButton.parentNode.insertBefore(clone, exitButton)
})
clone.innerHTML = `<img src='${syringe}' width="30" alt="lcqt"/>`
clone.addEventListener('click', () => window.electron.ipcRenderer.sendMessage('LCQT_OPEN_WINDOW'))

settingsButton.parentNode.insertBefore(clone, settingsButton)
})
16 changes: 3 additions & 13 deletions gui/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,15 @@ function createConfig() {
let config: Config = { ...defaultConfig }

if (!dev) {
const configPath: string = window.process.argv.pop()
const fs = require('fs')

try {
config = {...config, ...JSON.parse(fs.readFileSync(configPath, 'utf8'))}
}catch(e) {
console.error(e)
}
const { ipcRenderer } = require('electron')
config = {...config, ...ipcRenderer.sendSync('LCQT_READ_CONFIG')}

if (typeof(config.customJvm) == 'boolean') {
config.customJvm = ''
}

window.onchange = () => {
fs.writeFileSync(
configPath,
JSON.stringify(config, null, 4),
'utf8'
)
ipcRenderer.send('LCQT_WRITE_CONFIG', config)
}
}

Expand Down
2 changes: 2 additions & 0 deletions injector/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ fn run() -> Result<(), Box<dyn Error>> {
serde_json::to_string(env::current_exe()?.parent().unwrap())?
);

println!("[LCQT] Sending payload");

debugger.send("Runtime.evaluate", json!({
"expression": payload,
"includeCommandLineAPI": true
Expand Down

0 comments on commit e3b9cb1

Please sign in to comment.