NativeModuleLister .java

net.minecraft.util.NativeModuleLister (class, 155 lines): members: NativeModuleVersion, tryGetModuleVersion, DEFAULT_CODEPAGE, NativeModuleInfo, LANG_MASK.

net.minecraft.utilclass155 linescommon5.6 KB

API Surface (7)

List<NativeModuleLister.NativeModuleInfo> listModules()
Optional<NativeModuleLister.NativeModuleVersion> tryGetModuleVersion(final String path)
void addCrashSection(final CrashReportCategory category)
record NativeModuleInfo(String name, Optional<NativeModuleLister.NativeModuleVersion> version)
String toString()
record NativeModuleVersion(String description, String version, String company)
String toString()

Fields & Constants (5)

LOGGER
LANG_MASK
DEFAULT_LANG
CODEPAGE_MASK
DEFAULT_CODEPAGE

String literals (6)

"\\VarFileInfo\\Translation"
"FileDescription"
"CompanyName"
"FileVersion"
"Can't get version value "
"Modules"

Code Graph: Dependencies (1)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

NativeModuleLister.java155 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util;
2
3
import com.google.common.collect.ImmutableList;
4
import com.google.common.collect.ImmutableList.Builder;
5
import com.mojang.logging.LogUtils;
6
import com.sun.jna.Memory;
7
import com.sun.jna.Native;
8
import com.sun.jna.Platform;
9
import com.sun.jna.Pointer;
10
import com.sun.jna.platform.win32.Kernel32;
11
import com.sun.jna.platform.win32.Kernel32Util;
12
import com.sun.jna.platform.win32.Version;
13
import com.sun.jna.platform.win32.Win32Exception;
14
import com.sun.jna.platform.win32.Tlhelp32.MODULEENTRY32W;
15
import com.sun.jna.ptr.IntByReference;
16
import com.sun.jna.ptr.PointerByReference;
17
import java.nio.charset.StandardCharsets;
18
import java.util.Comparator;
19
import java.util.List;
20
import java.util.Locale;
21
import java.util.Optional;
22
import java.util.OptionalInt;
23
import java.util.stream.Collectors;
24
import net.minecraft.CrashReportCategory;
25
import org.slf4j.Logger;
26
27
public class NativeModuleLister {
28
	private static final Logger LOGGER = LogUtils.getLogger();
29
	private static final int LANG_MASK = 65535;
30
	private static final int DEFAULT_LANG = 1033;
31
	private static final int CODEPAGE_MASK = -65536;
32
	private static final int DEFAULT_CODEPAGE = 78643200;
33
34
	public static List<NativeModuleLister.NativeModuleInfo> listModules() {
35
		if (!Platform.isWindows()) {
36
			return ImmutableList.of();
37
		}
38
39
		int selfHandle = Kernel32.INSTANCE.GetCurrentProcessId();
40
		Builder<NativeModuleLister.NativeModuleInfo> result = ImmutableList.builder();
41
42
		for (MODULEENTRY32W module : Kernel32Util.getModules(selfHandle)) {
43
			String name = module.szModule();
44
			Optional<NativeModuleLister.NativeModuleVersion> versionInfo = tryGetModuleVersion(module.szExePath());
45
			result.add(new NativeModuleLister.NativeModuleInfo(name, versionInfo));
46
		}
47
48
		return result.build();
49
	}
50
51
	public static Optional<NativeModuleLister.NativeModuleVersion> tryGetModuleVersion(final String path) {
52
		if (!Platform.isWindows()) {
53
			return Optional.empty();
54
		}
55
56
		try {
57
			IntByReference dwDummy = new IntByReference();
58
			int versionLength = Version.INSTANCE.GetFileVersionInfoSize(path, dwDummy);
59
			if (versionLength == 0) {
60
				int lastError = Native.getLastError();
61
				if (lastError != 1813 && lastError != 1812) {
62
					throw new Win32Exception(lastError);
63
				} else {
64
					return Optional.empty();
65
				}
66
			} else {
67
				Pointer lpData = new Memory(versionLength);
68
				if (!Version.INSTANCE.GetFileVersionInfo(path, 0, versionLength, lpData)) {
69
					throw new Win32Exception(Native.getLastError());
70
				}
71
72
				IntByReference size = new IntByReference();
73
				Pointer translationsBuffer = queryVersionValue(lpData, "\\VarFileInfo\\Translation", size);
74
				int[] langsAndCodepages = translationsBuffer.getIntArray(0L, size.getValue() / 4);
75
				OptionalInt maybeLangAndCodepage = findLangAndCodepage(langsAndCodepages);
76
				if (maybeLangAndCodepage.isEmpty()) {
77
					return Optional.empty();
78
				}
79
80
				int langAndCodepage = maybeLangAndCodepage.getAsInt();
81
				int lang = langAndCodepage & 65535;
82
				int codepage = (langAndCodepage & -65536) >> 16;
83
				String description = queryVersionString(lpData, langTableKey("FileDescription", lang, codepage), size);
84
				String companyName = queryVersionString(lpData, langTableKey("CompanyName", lang, codepage), size);
85
				String fileVersion = queryVersionString(lpData, langTableKey("FileVersion", lang, codepage), size);
86
				return Optional.of(new NativeModuleLister.NativeModuleVersion(description, fileVersion, companyName));
87
			}
88
		} catch (Exception e) {
89
			LOGGER.info("Failed to find module info for {}", path, e);
90
			return Optional.empty();
91
		}
92
	}
93
94
	private static String langTableKey(final String key, final int lang, final int codepage) {
95
		return String.format(Locale.ROOT, "\\StringFileInfo\\%04x%04x\\%s", lang, codepage, key);
96
	}
97
98
	private static OptionalInt findLangAndCodepage(final int[] langsAndCodepages) {
99
		OptionalInt bestSoFar = OptionalInt.empty();
100
101
		for (int langAndCodepage : langsAndCodepages) {
102
			if ((langAndCodepage & -65536) == 78643200 && (langAndCodepage & 65535) == 1033) {
103
				return OptionalInt.of(langAndCodepage);
104
			}
105
106
			bestSoFar = OptionalInt.of(langAndCodepage);
107
		}
108
109
		return bestSoFar;
110
	}
111
112
	private static Pointer queryVersionValue(final Pointer lpData, final String key, final IntByReference outSize) {
113
		PointerByReference lplpBuffer = new PointerByReference();
114
		if (!Version.INSTANCE.VerQueryValue(lpData, key, lplpBuffer, outSize)) {
115
			throw new UnsupportedOperationException("Can't get version value " + key);
116
		} else {
117
			return lplpBuffer.getValue();
118
		}
119
	}
120
121
	private static String queryVersionString(final Pointer lpData, final String key, final IntByReference outSize) {
122
		try {
123
			Pointer ptr = queryVersionValue(lpData, key, outSize);
124
			byte[] result = ptr.getByteArray(0L, (outSize.getValue() - 1) * 2);
125
			return sanitize(new String(result, StandardCharsets.UTF_16LE));
126
		} catch (Exception e) {
127
			return "";
128
		}
129
	}
130
131
	private static String sanitize(final String input) {
132
		return Util.CONTROL_CHARACTER_ESCAPER.escape(input);
133
	}
134
135
	public static void addCrashSection(final CrashReportCategory category) {
136
		category.setDetail(
137
			"Modules", () -> listModules().stream().sorted(Comparator.comparing(module -> module.name)).map(e -> "\n\t\t" + e).collect(Collectors.joining())
138
		);
139
	}
140
141
	public record NativeModuleInfo(String name, Optional<NativeModuleLister.NativeModuleVersion> version) {
142
		@Override
143
		public String toString() {
144
			return this.version.<String>map(v -> this.name + ":" + v).orElse(this.name);
145
		}
146
	}
147
148
	public record NativeModuleVersion(String description, String version, String company) {
149
		@Override
150
		public String toString() {
151
			return this.description + ":" + this.version + ":" + this.company;
152
		}
153
	}
154
}
155

Decompiled Minecraft 26.3 source of NativeModuleLister (net/minecraft/util/NativeModuleLister.java) with Mojang official mappings, stored verbatim on the site.