Skip to content

Commit

Permalink
fix(WinGPUDectector): registry value can be binary
Browse files Browse the repository at this point in the history
Signed-off-by: axel7083 <[email protected]>
  • Loading branch information
axel7083 committed May 31, 2024
1 parent ea4ec94 commit e2f2f99
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
10 changes: 7 additions & 3 deletions packages/backend/src/workers/gpu/WinGPUDetector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { WinGPUDetector } from './WinGPUDetector';
import { env } from '@podman-desktop/api';
import WinReg from 'winreg';

/* eslint-disable @typescript-eslint/no-explicit-any */

type WinRegKeys = (err: unknown, registries: WinReg.Registry[] | undefined) => void;
type WinRegValues = (err: unknown, registries: WinReg.RegistryItem[] | undefined) => void;

Expand All @@ -16,9 +18,11 @@ vi.mock('@podman-desktop/api', () => {

// Mock winreg module
vi.mock('winreg', () => {
const Registry = vi.fn();
const Registry: any = vi.fn();
Registry.prototype.keys = vi.fn();
Registry.prototype.values = vi.fn();
Registry.REG_BINARY = 'REG_BINARY';
Registry.REG_SZ = 'REG_SZ';
return { default: Registry };
});

Expand Down Expand Up @@ -64,7 +68,7 @@ test('getValues should return a list of values for a given subkey', async () =>

test('extractGpuInfo should return correct IGPUInfo object', () => {
const mockValues = [
{ name: 'HardwareInformation.AdapterString', value: 'GPU Model' },
{ name: 'HardwareInformation.AdapterString', value: 'GPU Model', type: 'REG_SZ' },
{ name: 'HardwareInformation.qwMemorySize', value: '40000000' },
] as unknown as WinReg.RegistryItem[];

Expand All @@ -86,7 +90,7 @@ test('extractGpuInfo should return undefined if necessary information is missing
test('getGpuInfoFromSubkey should return IGPUInfo object', async () => {
const mockSubkey = new WinReg({ hive: WinReg.HKLM, key: '\\Subkey1' });
const mockValues = [
{ name: 'HardwareInformation.AdapterString', value: 'GPU Model' },
{ name: 'HardwareInformation.AdapterString', value: 'GPU Model', type: 'REG_SZ' },
{ name: 'HardwareInformation.qwMemorySize', value: '40000000' },
] as unknown as WinReg.RegistryItem[];

Expand Down
21 changes: 20 additions & 1 deletion packages/backend/src/workers/gpu/WinGPUDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,32 @@ export class WinGPUDetector extends WindowsWorker<void, IGPUInfo[]> {
return listValues();
}

/**
* This method transform the value to a string, it will convert if required
* @param item
* @private
*/
private getString(item: WinReg.RegistryItem): string {
switch (item.type) {
case WinReg.REG_BINARY:
return Buffer.from(item.value, 'hex').toString();
case WinReg.REG_SZ:
return item.value;
default:
throw new Error(`registry item type not supported (${item.type})`);
}
}

/**
* Extracts GPU information from registry values.
* @param values - An array of WinReg.RegistryItem objects representing the registry values.
* @returns An IGPUInfo object if the necessary information is found; otherwise, undefined.
*/
private extractGpuInfo(values: WinReg.RegistryItem[]): IGPUInfo | undefined {
const adapterString = values.find(item => item.name === 'HardwareInformation.AdapterString')?.value;
const adapterItem = values.find(item => item.name === 'HardwareInformation.AdapterString');
if (!adapterItem) return undefined;

const adapterString = this.getString(adapterItem);
const memorySize = values.find(item => item.name === 'HardwareInformation.qwMemorySize')?.value;

if (adapterString) {
Expand Down

0 comments on commit e2f2f99

Please sign in to comment.