Created
December 4, 2023 18:16
-
-
Save komelgman/f5fefa1bdd2159cd263d539d4027ab4c to your computer and use it in GitHub Desktop.
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
import java.util.List; | |
import java.util.Optional; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.github.robtimus.os.windows.registry.BinaryValue; | |
import com.github.robtimus.os.windows.registry.DWordValue; | |
import com.github.robtimus.os.windows.registry.QWordValue; | |
import com.github.robtimus.os.windows.registry.RegistryKey; | |
import com.github.robtimus.os.windows.registry.RegistryValue; | |
public class WinGpuInfoLoader { | |
public static final Logger LOG = LoggerFactory.getLogger(WinGpuInfoLoader.class); | |
public static final String DISPLAY_DEVICES_REGISTRY_PATH = | |
"SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}"; | |
public static final String ADAPTER_STRING = "HardwareInformation.AdapterString"; | |
public static final String DRIVER_DESC = "DriverDesc"; | |
public static final String QW_MEMORY_SIZE = "HardwareInformation.qwMemorySize"; | |
public static final String MEMORY_SIZE = "HardwareInformation.MemorySize"; | |
public List<GraphicCard> load() { | |
RegistryKey gpuDevicesRegistryKey = RegistryKey.HKEY_LOCAL_MACHINE.resolve(DISPLAY_DEVICES_REGISTRY_PATH); | |
return gpuDevicesRegistryKey | |
.subKeys() | |
.filter(sk -> sk.name().startsWith("0")) | |
.filter(sk -> sk.findValue(ADAPTER_STRING, RegistryValue.class).isPresent()) | |
.map(sk -> { | |
String name = sk.findStringValue(DRIVER_DESC).orElse("<Unknown>"); | |
long vram = getRegistryValueAsLong(sk.findValue(QW_MEMORY_SIZE, RegistryValue.class)) | |
.orElseGet(() -> getRegistryValueAsLong(sk.findValue(MEMORY_SIZE, RegistryValue.class)).orElse(0L)); | |
return new GraphicCard(name, vram); | |
}).toList(); | |
} | |
private static Optional<Long> getRegistryValueAsLong(Optional<RegistryValue> optionalValue) { | |
if (optionalValue.isEmpty()) { | |
return Optional.empty(); | |
} | |
Long result = null; | |
try { | |
RegistryValue registryValue = optionalValue.get(); | |
if (registryValue instanceof QWordValue qWordValue) { | |
result = qWordValue.value(); | |
} else if (registryValue instanceof DWordValue dWordValue) { | |
result = (long) dWordValue.value(); | |
} else if (registryValue instanceof BinaryValue binaryValue) { | |
result = bytesToLong(binaryValue.data()); | |
} | |
} catch (RuntimeException e) { | |
LOG.debug(e.getMessage(), e); | |
} | |
return Optional.ofNullable(result); | |
} | |
private static long bytesToLong(byte[] bytes) { | |
long result = 0; | |
if (bytes.length > 8) { | |
throw new IllegalArgumentException("Oops! bytes length > 8"); | |
} | |
for (byte i = 0; i < bytes.length; ++i) { | |
result += (long) (bytes[i] & 255) << 8 * i; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment