Skip to content

Commit 4c18d88

Browse files
authored
Check ZED SDK compatibility version before loading (#4)
1 parent 22ceec7 commit 4c18d88

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

src/main/java/us/ihmc/zed/library/ZEDJavaAPINativeLibrary.java

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44
import us.ihmc.tools.nativelibraries.NativeLibraryLoader;
55
import us.ihmc.tools.nativelibraries.NativeLibraryWithDependencies;
66

7+
import java.io.BufferedReader;
8+
import java.io.FileReader;
9+
import java.io.IOException;
10+
import java.util.regex.Matcher;
11+
import java.util.regex.Pattern;
12+
713
public class ZEDJavaAPINativeLibrary implements NativeLibraryDescription {
14+
private static final int ZED_SDK_COMPATIBILITY_MAJOR_VERSION = 4;
15+
private static final int ZED_SDK_COMPATIBILITY_MINOR_VERSION = 2;
16+
817
@Override
918
public String getPackage(OperatingSystem os, Architecture arch) {
1019
String archPackage = "";
@@ -41,10 +50,60 @@ public NativeLibraryWithDependencies getLibraryWithDependencies(OperatingSystem
4150
private static boolean loaded = false;
4251

4352
public static boolean load() {
44-
if (!loaded) {
45-
ZEDJavaAPINativeLibrary lib = new ZEDJavaAPINativeLibrary();
46-
loaded = NativeLibraryLoader.loadLibrary(lib);
53+
String installedZEDSDKVersion = getZEDSDKCMakePackageVersion();
54+
55+
int installedZEDSDKMajorVersion = 0;
56+
int installedZEDSDKMinorVersion = 0;
57+
58+
if (installedZEDSDKVersion != null) {
59+
String[] installedZEDSDKVersionParts = installedZEDSDKVersion.split("\\.");
60+
61+
try {
62+
installedZEDSDKMajorVersion = Integer.parseInt(installedZEDSDKVersionParts[0]);
63+
installedZEDSDKMinorVersion = Integer.parseInt(installedZEDSDKVersionParts[1]);
64+
} catch (NumberFormatException e) {
65+
// Ignore
66+
}
4767
}
68+
69+
if (installedZEDSDKMajorVersion > ZED_SDK_COMPATIBILITY_MAJOR_VERSION ||
70+
(installedZEDSDKMajorVersion == ZED_SDK_COMPATIBILITY_MAJOR_VERSION && installedZEDSDKMinorVersion >= ZED_SDK_COMPATIBILITY_MINOR_VERSION)) {
71+
if (!loaded) {
72+
ZEDJavaAPINativeLibrary lib = new ZEDJavaAPINativeLibrary();
73+
loaded = NativeLibraryLoader.loadLibrary(lib);
74+
}
75+
} else {
76+
System.err.println("Incompatible ZED SDK installation ("
77+
+ installedZEDSDKVersion + ") found. Please install "
78+
+ ZED_SDK_COMPATIBILITY_MAJOR_VERSION + "." + ZED_SDK_COMPATIBILITY_MINOR_VERSION + " or higher.");
79+
}
80+
4881
return loaded;
4982
}
83+
84+
public static String getZEDSDKCMakePackageVersion() {
85+
String cmakePackageFile;
86+
87+
if (System.getProperty("os.name").contains("Windows")) {
88+
cmakePackageFile = "C:/Program Files (x86)/ZED SDK/zed-config-version.cmake";
89+
} else {
90+
cmakePackageFile = "/usr/local/zed/zed-config-version.cmake";
91+
}
92+
93+
try (BufferedReader br = new BufferedReader(new FileReader(cmakePackageFile))) {
94+
String line;
95+
Pattern pattern = Pattern.compile("set\\(PACKAGE_VERSION \"(\\d+\\.\\d+\\.\\d+)\"\\)");
96+
97+
while ((line = br.readLine()) != null) {
98+
Matcher matcher = pattern.matcher(line);
99+
if (matcher.find()) {
100+
return matcher.group(1).trim();
101+
}
102+
}
103+
} catch (IOException e) {
104+
e.printStackTrace();
105+
}
106+
107+
return null;
108+
}
50109
}

0 commit comments

Comments
 (0)