Skip to content

Commit 282309e

Browse files
committed
fix position desync on track change, improve address search, check playback address twice, add SPOTIFY_API_DEBUG property, version 1.1.2
1 parent 64267b2 commit 282309e

File tree

5 files changed

+59
-19
lines changed

5 files changed

+59
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ repositories {
2323
}
2424
2525
dependencies {
26-
implementation 'com.github.LabyStudio:java-spotify-api:1.1.1:all'
26+
implementation 'com.github.LabyStudio:java-spotify-api:1.1.2:all'
2727
}
2828
```
2929

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.1'
7+
version '1.1.2'
88

99
compileJava {
1010
sourceCompatibility = '1.8'

src/main/java/de/labystudio/spotifyapi/platform/windows/WinSpotifyAPI.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,17 @@ private void updatePosition(int position) {
103103
return;
104104
}
105105

106-
// Update position known state
106+
// The position is known if the song is currently paused
107+
// or if the position has changed during the runtime of the program.
108+
// Because the position is updated when the track initializes, it could be that the position is not known yet.
107109
this.positionKnown = this.currentPosition != -1 || !this.isPlaying;
110+
111+
// Update position
108112
this.currentPosition = position;
113+
this.lastTimePositionUpdated = System.currentTimeMillis();
109114

115+
// Fire on position changed if the position is known
110116
if (this.positionKnown) {
111-
this.lastTimePositionUpdated = System.currentTimeMillis();
112-
113-
// Fire on position changed
114117
this.listeners.forEach(listener -> listener.onPositionChanged(position));
115118
}
116119
}

src/main/java/de/labystudio/spotifyapi/platform/windows/api/WinProcess.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public long findInMemory(long minAddress, long maxAddress, byte[] searchBytes, S
154154
int index = 0;
155155
while (cursor < maxAddress) {
156156
long target = this.findInMemory(cursor, maxAddress, searchBytes);
157-
if (condition.matches(target, index)) {
157+
if (target == -1 || condition.matches(target, index)) {
158158
return target;
159159
}
160160
cursor = target + 1;
@@ -180,6 +180,34 @@ public boolean hasBytes(long address, int... bytes) {
180180
return true;
181181
}
182182

183+
/**
184+
* Check if the given bytes are at the given address.
185+
*
186+
* @param address The address to check.
187+
* @param bytes The bytes to check.
188+
* @return True if the bytes are at the given address.
189+
*/
190+
public boolean hasBytes(long address, byte[] bytes) {
191+
byte[] chunk = this.readBytes(address, bytes.length);
192+
for (int i = 0; i < chunk.length; i++) {
193+
if (chunk[i] != bytes[i]) {
194+
return false;
195+
}
196+
}
197+
return true;
198+
}
199+
200+
/**
201+
* Check if the given text is at the given address.
202+
*
203+
* @param address The address to check.
204+
* @param text The text to check.
205+
* @return True if the text is at the given address.
206+
*/
207+
public boolean hasText(long address, String text) {
208+
return this.hasBytes(address, text.getBytes());
209+
}
210+
183211
/**
184212
* Find the address of a text inside the memory.
185213
* If there are multiple matches of the text, the given index will be used to select the correct one.

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@
1010
*/
1111
public class SpotifyProcess extends WinProcess {
1212

13-
private static final boolean DEBUG = false;
13+
private static final boolean DEBUG = System.getProperty("SPOTIFY_API_DEBUG") != null;
14+
15+
// Spotify track id
16+
private static final String CHROME_ELF_DLL = "chrome_elf.dll";
17+
private static final String PREFIX_SPOTIFY_TRACK = "spotify:track:";
18+
private static final long OFFSET_CHROME_ELF_2 = 4871;
19+
private static final long OFFSET_TRACK_ID = 105700;
20+
21+
// Spotify playback
1422
private static final byte[] PREFIX_CONTEXT = new byte[]{0x63, 0x6F, 0x6E, 0x74, 0x65, 0x78, 0x74};
1523

1624
private final long addressTrackId;
@@ -36,14 +44,11 @@ public SpotifyProcess() {
3644
long timeScanStart = System.currentTimeMillis();
3745

3846
// Find address of track id (Located in the chrome_elf.dll module)
39-
this.addressTrackId = this.findAddressOfText(
40-
this.maxContentAddress / 2,
41-
"spotify:track:",
42-
(address, index) -> this.hasBytes(
43-
address + 37,
44-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
45-
)
46-
);
47+
this.addressTrackId = this.findAddressUsingRules(new SearchRule(CHROME_ELF_DLL, (address, index)
48+
-> this.hasText(address + OFFSET_CHROME_ELF_2, CHROME_ELF_DLL)
49+
&& this.hasText(address + OFFSET_TRACK_ID, PREFIX_SPOTIFY_TRACK)
50+
)) + OFFSET_TRACK_ID;
51+
4752
if (this.addressTrackId == -1 || !this.isTrackIdValid(this.getTrackId())) {
4853
throw new IllegalStateException("Could not find track id in memory");
4954
}
@@ -67,13 +72,17 @@ public SpotifyProcess() {
6772
if (this.addressPlayBack == -1) {
6873
throw new IllegalStateException("Could not find playback in memory");
6974
}
75+
76+
// Create the playback accessor with the found address
77+
this.playbackAccessor = new PlaybackAccessor(this, this.addressPlayBack);
78+
if (!this.playbackAccessor.isValid()) {
79+
throw new IllegalStateException("Could not create playback accessor");
80+
}
81+
7082
if (DEBUG) {
7183
System.out.println("Found playback address at: " + Long.toHexString(this.addressPlayBack));
7284
System.out.println("Scanning took " + (System.currentTimeMillis() - timeScanStart) + "ms");
7385
}
74-
75-
// Create the playback accessor with the found address
76-
this.playbackAccessor = new PlaybackAccessor(this, this.addressPlayBack);
7786
}
7887

7988
/**

0 commit comments

Comments
 (0)