Skip to content

Commit 2c68595

Browse files
committed
fix nullpointer
1 parent d5d35e3 commit 2c68595

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/main/java/de/labystudio/spotifyapi/platform/windows/api/spotify/SpotifyProcess.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public SpotifyProcess() {
6161
Psapi.ModuleInfo spotifyExeModule = this.getModuleInfo("Spotify.exe");
6262
Psapi.ModuleInfo libCefModule = this.getModuleInfo("libcef.dll");
6363
long minAddress = spotifyExeModule == null ? 0 : spotifyExeModule.getBaseOfDll();
64-
long maxAddress = spotifyExeModule == null ? this.addressTrackId : libCefModule.getBaseOfDll();
64+
long maxAddress = libCefModule == null ? this.addressTrackId : libCefModule.getBaseOfDll();
6565

6666
// Check if the song is currently playing using the title bar
6767
boolean isPlaying = this.isPlayingUsingTitle();

src/test/java/SpotifyProcessTest.java

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import de.labystudio.spotifyapi.platform.windows.api.WinProcess;
22
import de.labystudio.spotifyapi.platform.windows.api.jna.Psapi;
33
import de.labystudio.spotifyapi.platform.windows.api.playback.PlaybackAccessor;
4+
import de.labystudio.spotifyapi.platform.windows.api.spotify.SpotifyTitle;
5+
6+
import java.util.ArrayList;
7+
import java.util.Comparator;
8+
import java.util.List;
9+
import java.util.Map;
410

511
public class SpotifyProcessTest {
612

@@ -14,16 +20,72 @@ public static void main(String[] args) {
1420
long addressTrackId = process.findAddressOfText(moduleInfo.getBaseOfDll(), "spotify:track:", 0);
1521
System.out.println("Track Id Address: 0x" + Long.toHexString(addressTrackId));
1622

23+
boolean isPlaying = process.getWindowTitle().contains(SpotifyTitle.DELIMITER);
1724
long addressPlayBack = process.findInMemory(
1825
0,
1926
addressTrackId,
2027
PREFIX_CONTEXT,
2128
(address, index) -> {
2229
PlaybackAccessor accessor = new PlaybackAccessor(process, address);
23-
return accessor.isValid();
30+
boolean valid = accessor.isValid() && accessor.isPlaying() == isPlaying; // Check if address is valid
31+
32+
// If valid then pull the data again and check if it is still valid
33+
if (valid) {
34+
accessor.update();
35+
return accessor.isValid();
36+
}
37+
38+
return false;
2439
}
2540
);
41+
if (addressPlayBack == -1) {
42+
System.out.println("Could not find playback address");
43+
return;
44+
}
2645
System.out.println("Playback Address: 0x" + Long.toHexString(addressPlayBack));
46+
47+
printModules(process, addressPlayBack);
48+
}
49+
50+
public static void printModules(WinProcess process, long addressPlayBack) {
51+
List<Module> modules = new ArrayList<>();
52+
for (Map.Entry<String, Psapi.ModuleInfo> entry : process.getModules().entrySet()) {
53+
modules.add(new Module(entry.getKey(), entry.getValue().getBaseOfDll()));
54+
}
55+
modules.sort(Comparator.comparingLong(o -> o.address));
56+
57+
int passed = 0;
58+
for (Module entry : modules) {
59+
long entryPoint = entry.address;
60+
if (entryPoint > addressPlayBack) {
61+
if (passed == 0) {
62+
System.out.println(Long.toHexString(addressPlayBack) + " CONTEXT ------------------------");
63+
}
64+
passed++;
65+
if (passed > 1) {
66+
break;
67+
}
68+
}
69+
System.out.println(Long.toHexString(entryPoint) + " " + entry.name);
70+
}
71+
}
72+
73+
private static class Module {
74+
private final String name;
75+
private final long address;
76+
77+
public Module(String name, long address) {
78+
this.name = name;
79+
this.address = address;
80+
}
81+
82+
public String getName() {
83+
return this.name;
84+
}
85+
86+
public long getAddress() {
87+
return this.address;
88+
}
2789
}
2890

2991
}

0 commit comments

Comments
 (0)