Skip to content

Commit

Permalink
fix: Try to assign the gpu setting to optimize mc running
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Oct 28, 2023
1 parent c86bdd7 commit 7f0cff9
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 17 deletions.
3 changes: 2 additions & 1 deletion xmcl-electron-app/main/ElectronLauncherApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { pluginAutoUpdate } from './pluginAutoUpdate'
import { isDirectory } from './utils/fs'
import { ElectronUpdater } from './utils/updater'
import { getWindowsUtils } from './utils/windowsUtils'
import { pluginPowerMonitor } from './pluginPowerMonitor'

class ElectronShell implements Shell {
showItemInFolder = shell.showItemInFolder
Expand Down Expand Up @@ -100,7 +101,7 @@ export default class ElectronLauncherApp extends LauncherApp {
defaultApp,
getEnv(),
definedServices,
[pluginAutoUpdate],
[pluginAutoUpdate, pluginPowerMonitor],
)
}

Expand Down
67 changes: 67 additions & 0 deletions xmcl-electron-app/main/pluginPowerMonitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { LaunchService, LauncherApp, LauncherAppPlugin } from '@xmcl/runtime'
import { exec } from 'child_process'
import { powerMonitor, app as elec } from 'electron'
import { ensureElevateExe } from './utils/elevate'

const enum PerformanceType {
AUTO = 0,
POWER_SAVING = 1,
HIGH_PERFORMANCE = 2,
}

export const pluginPowerMonitor: LauncherAppPlugin = async (app) => {
if (app.platform.os !== 'windows') return

const info = await elec.getGPUInfo('basic') as any
if (info?.gpuDevice?.filter((v: any) => v?.vendorId !== 5140)?.length < 2) return
const { log, warn } = app.getLogger('GPUOptifimizer')

log('Detected multiple GPUs. Trying to assign Minecraft JVM to high performance GPU')

app.registry.get(LaunchService).then((servi) => {
servi.registerPlugin({
async onBeforeLaunch(input, output) {
const javaPath = output.javaPath
try {
const result = await queryGPUStatus(javaPath)
log(`JVM GPU setting: ${result}`)
} catch (e) {
// No GPU status
warn(`No GPU assignment: ${(e as any).message}`)
try {
await addRegistryKey(app, javaPath, powerMonitor.onBatteryPower ? PerformanceType.POWER_SAVING : PerformanceType.HIGH_PERFORMANCE)
log('Assigned Minecraft JVM to high performance GPU')
} catch (e) {
warn(`Failed to assign Minecraft JVM to high performance GPU: ${(e as any).message}`)
}
}
},
})
})
}

function queryGPUStatus(javaPath: string) {
return new Promise<string>((resolve, reject) => {
exec(`@chcp 65001 >nul & cmd /d/s/c REG QUERY HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\DirectX\\UserGpuPreferences /v "${javaPath}"`, (err, out, stderr) => {
if (err) {
reject(new Error(stderr))
} else {
resolve(out)
}
})
})
}

async function addRegistryKey(app: LauncherApp, javaPath: string, type: PerformanceType) {
const elevate = await ensureElevateExe(app.appDataPath)
return new Promise<string>((resolve, reject) => {
const val = `GpuPreference=${type};`
exec(`@chcp 65001 >nul & "${elevate}" cmd /d/s/c REG ADD HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\DirectX\\UserGpuPreferences /v "${javaPath}" /t REG_SZ /d ${val} /f`, (err, out, stderr) => {
if (err) {
reject(new Error(stderr))
} else {
resolve(out)
}
})
})
}
19 changes: 19 additions & 0 deletions xmcl-electron-app/main/utils/elevate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AZURE_CDN, AZURE_MS_CDN } from '@/constant'
import { download } from '@xmcl/file-transfer'
import { join } from 'path'

export async function ensureElevateExe(appDataPath: string) {
const elevate = join(appDataPath, 'elevate.exe')
await download({
url: [
`${AZURE_CDN}/elevate.exe`,
`${AZURE_MS_CDN}/elevate.exe`,
],
validator: {
algorithm: 'sha1',
hash: 'd8d449b92de20a57df722df46435ba4553ecc802',
},
destination: elevate,
})
return elevate
}
17 changes: 1 addition & 16 deletions xmcl-electron-app/main/utils/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import ElectronLauncherApp from '../ElectronLauncherApp'
import { DownloadAppInstallerTask } from './appinstaller'
import { checksum } from './fs'
import { GFW } from '@xmcl/runtime/lib/entities/gfw'
import { ensureElevateExe } from './elevate'

/**
* Only download asar file update.
Expand Down Expand Up @@ -293,20 +294,4 @@ export class ElectronUpdater implements LauncherAppUpdater {
}
}

async function ensureElevateExe(appDataPath: string) {
const elevate = join(appDataPath, 'elevate.exe')
await download({
url: [
`${AZURE_CDN}/elevate.exe`,
`${AZURE_MS_CDN}/elevate.exe`,
],
validator: {
algorithm: 'sha1',
hash: 'd8d449b92de20a57df722df46435ba4553ecc802',
},
destination: elevate,
})
return elevate
}

let injectedUpdate = false

0 comments on commit 7f0cff9

Please sign in to comment.