-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Try to assign the gpu setting to optimize mc running
- Loading branch information
Showing
4 changed files
with
94 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
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 | ||
const gpus = info?.gpuDevice?.filter((v: any) => v?.vendorId !== 5140) | ||
.map((v: any) => v.vendorId) || [] | ||
const { log, warn } = app.getLogger('GPUOptifimizer') | ||
if (gpus.length < 2) { | ||
log(`Detected GPUs: [${gpus.join(', ')}]`) | ||
return | ||
} | ||
|
||
log(`Detected GPUs [${gpus.join(', ')}]. 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) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters