Skip to content

Commit 200ca52

Browse files
HerXayahLabyStudio
andauthored
version 1.1.16 - Fix Song detection and update Offsets (#8)
* Update to latest Offsets * Better detection of what is an Invalid and Valid TrackID * Revert my left in Debug * Checking if letter is a Digit or a real Letter * Fixed mixing of true and false + improve detection * implement dynamic track address scanning as fallback, version 1.1.16 --------- Co-authored-by: LabyStudio <[email protected]>
1 parent 134188d commit 200ca52

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
out
77
.gradle
88
*.iml
9-
build
9+
build
10+
run/

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'de.labystudio'
7-
version '1.1.15'
7+
version '1.1.16'
88

99
compileJava {
1010
sourceCompatibility = '1.8'

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

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ public class SpotifyProcess extends WinProcess {
1818
// Spotify track id
1919
private static final String PREFIX_SPOTIFY_TRACK = "spotify:track:";
2020
private static final long[] OFFSETS_TRACK_ID = {
21-
0x14C9F0, // 64-Bit
22-
0x102178, // 32-Bit
21+
0x14FA30, // 64-Bit (1.2.21.1104.g42cf0a50)
22+
0x106198, // 32-Bit (1.2.21.1104.g42cf0a50)
23+
0x14C9F0, // 64-Bit (Old)
24+
0x102178, // 32-Bit (Old)
2325
0x1499F0, // 64-Bit (Old)
2426
0xFEFE8 // 32-Bit (Old)
2527
};
@@ -62,14 +64,36 @@ private long findTrackIdAddress() {
6264

6365
// Check all offsets for valid track id
6466
long addressTrackId = -1;
67+
long minTrackIdOffset = Long.MAX_VALUE;
68+
long maxTrackIdOffset = Long.MIN_VALUE;
6569
for (long trackIdOffset : OFFSETS_TRACK_ID) {
66-
addressTrackId = chromeElfAddress + trackIdOffset;
67-
if (addressTrackId != -1 && this.isTrackIdValid(this.readTrackId(addressTrackId))) {
70+
// Get min and max of hardcoded offset
71+
minTrackIdOffset = Math.min(minTrackIdOffset, trackIdOffset);
72+
maxTrackIdOffset = Math.max(maxTrackIdOffset, trackIdOffset);
73+
74+
// Check if the hardcoded offset is valid
75+
long targetAddressTrackId = chromeElfAddress + trackIdOffset;
76+
if (this.isTrackIdValid(this.readTrackId(targetAddressTrackId))) {
6877
// If the offset works, exit the loop
78+
addressTrackId = targetAddressTrackId;
6979
break;
7080
}
7181
}
7282

83+
// If the hardcoded offsets are not valid, try to find it dynamically
84+
if (addressTrackId == -1) {
85+
if (DEBUG) {
86+
System.out.println("Could not find track id with hardcoded offsets. Trying to find it dynamically...");
87+
}
88+
89+
long threshold = (maxTrackIdOffset - minTrackIdOffset) * 3;
90+
long scanAddressFrom = chromeElfAddress + minTrackIdOffset - threshold;
91+
long scanAddressTo = chromeElfAddress + maxTrackIdOffset + threshold;
92+
addressTrackId = this.findAddressOfText(scanAddressFrom, scanAddressTo, "spotify:track:", (address, index) -> {
93+
return this.isTrackIdValid(this.readTrackId(address));
94+
});
95+
}
96+
7397
if (addressTrackId == -1) {
7498
throw new IllegalStateException("Could not find track id in memory");
7599
}
@@ -87,10 +111,22 @@ private long findTrackIdAddress() {
87111
}
88112

89113
private PlaybackAccessor findPlaybackAccessor() {
90-
// Find addresses of playback states
114+
// Find addresses of playback states when playing a playlist
91115
long addressPlayBack = this.findAddressOfText(0, 0x0FFFFFFF, "playlist", (address, index) -> {
92-
return this.hasText(address + 408, "context", "autoplay");
116+
return this.hasText(address + 408, "context", "autoplay")
117+
&& this.hasText(address + 128, "your_library", "home")
118+
&& new MemoryPlaybackAccessor(this, address).isValid();
93119
});
120+
121+
if (addressPlayBack == -1) {
122+
// Find addresses of playback states when playing an album
123+
addressPlayBack = this.findAddressOfText(0, 0x0FFFFFFF, "album", (address, index) -> {
124+
return this.hasText(address + 408, "context", "autoplay")
125+
&& this.hasText(address + 128, "your_library", "home")
126+
&& new MemoryPlaybackAccessor(this, address).isValid();
127+
});
128+
}
129+
94130
if (addressPlayBack == -1) {
95131
if (DEBUG) {
96132
System.out.println("Could not find playback address in memory");
@@ -175,7 +211,7 @@ public long getAddressTrackId() {
175211
*/
176212
public boolean isTrackIdValid(String trackId) {
177213
for (char c : trackId.toCharArray()) {
178-
if (c == 0) {
214+
if (!Character.isLetterOrDigit(c)) {
179215
return false;
180216
}
181217
}

src/test/java/SpotifyProcessTest.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import de.labystudio.spotifyapi.platform.windows.api.WinProcess;
22
import de.labystudio.spotifyapi.platform.windows.api.jna.Psapi;
3+
import de.labystudio.spotifyapi.platform.windows.api.playback.MemoryPlaybackAccessor;
34

45
import java.util.ArrayList;
56
import java.util.Comparator;
@@ -17,12 +18,22 @@ public static void main(String[] args) {
1718
System.out.println("Track Id Address: 0x" + Long.toHexString(addressTrackId));
1819

1920
long addressOfPlayback = process.findAddressOfText(0, 0x0FFFFFFF, "playlist", (address, index) -> {
20-
return process.hasText(address + 408, "context", "autoplay");
21-
22-
// process.hasText(address + 128, "your_library", "home")
21+
return process.hasText(address + 408, "context", "autoplay")
22+
&& process.hasText(address + 128, "your_library", "home")
23+
&& new MemoryPlaybackAccessor(process, address).isValid();
2324
});
2425

25-
System.out.println("Playback Address: 0x" + Long.toHexString(addressOfPlayback));
26+
if (addressOfPlayback == -1) {
27+
addressOfPlayback = process.findAddressOfText(0, 0x0FFFFFFF, "album", (address, index) -> {
28+
return process.hasText(address + 408, "context", "autoplay")
29+
&& process.hasText(address + 128, "your_library", "home")
30+
&& new MemoryPlaybackAccessor(process, address).isValid();
31+
});
32+
}
33+
34+
MemoryPlaybackAccessor accessor = new MemoryPlaybackAccessor(process, addressOfPlayback);
35+
System.out.println("Playback Address: 0x" + Long.toHexString(addressOfPlayback) + " (" + (accessor.isValid() ? "valid" : "invalid") + ")");
36+
System.out.println("Position: " + accessor.getPosition());
2637
}
2738

2839
public static void printModules(WinProcess process, long targetAddress) {

0 commit comments

Comments
 (0)