Skip to content

Commit d5d35e3

Browse files
committed
improve performance by reducing the range to search for the playback information in memory on windows, add reconnect timeout for windows, version 1.1.9
1 parent 049cc61 commit d5d35e3

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
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.8:all'
26+
implementation 'com.github.LabyStudio:java-spotify-api:1.1.9: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.8'
7+
version '1.1.9'
88

99
compileJava {
1010
sourceCompatibility = '1.8'

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
public class WinSpotifyAPI extends AbstractTickSpotifyAPI {
2222

23+
private static final long RECONNECT_TIMEOUT = 1000 * 10L;
24+
2325
private SpotifyProcess process;
2426

2527
private Track currentTrack;
@@ -31,13 +33,22 @@ public class WinSpotifyAPI extends AbstractTickSpotifyAPI {
3133
private long lastAccessorPosition = -1;
3234
private long lastTimeSynced;
3335

36+
private long lastTimeDisconnected = -1;
37+
3438
/**
3539
* Updates the current track, position and playback state.
3640
* If the process is not connected, it will try to connect to the Spotify process.
3741
*/
3842
protected void onTick() {
3943
try {
4044
if (!this.isConnected()) {
45+
// Check if we passed the reconnect timeout
46+
long timePassedSinceLastDisconnect = System.currentTimeMillis() - this.lastTimeDisconnected;
47+
if (timePassedSinceLastDisconnect < RECONNECT_TIMEOUT) {
48+
return;
49+
}
50+
51+
// Connect
4152
this.process = new SpotifyProcess();
4253

4354
// Fire on connect
@@ -104,6 +115,7 @@ protected void onTick() {
104115
// Fire on disconnect
105116
this.listeners.forEach(listener -> listener.onDisconnect(exception));
106117
this.process = null;
118+
this.lastTimeDisconnected = System.currentTimeMillis();
107119
}
108120
}
109121

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,11 @@ public Map<String, Psapi.ModuleInfo> getModules() {
294294
public long getFirstModuleAddress() {
295295
long minAddress = Long.MAX_VALUE;
296296
for (Map.Entry<String, Psapi.ModuleInfo> module : this.getModules().entrySet()) {
297-
minAddress = Math.min(minAddress, module.getValue().getBaseOfDll());
297+
long baseOfDll = module.getValue().getBaseOfDll();
298+
if (baseOfDll <= 0) {
299+
continue;
300+
}
301+
minAddress = Math.min(minAddress, baseOfDll);
298302
}
299303
return minAddress;
300304
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ public SpotifyProcess() {
4141

4242
long timeScanStart = System.currentTimeMillis();
4343

44-
Psapi.ModuleInfo chromeElf = this.getModuleInfo("chrome_elf.dll");
45-
if (chromeElf == null) {
44+
Psapi.ModuleInfo chromeElfModule = this.getModuleInfo("chrome_elf.dll");
45+
if (chromeElfModule == null) {
4646
throw new IllegalStateException("Could not find chrome_elf.dll module");
4747
}
4848

4949
// Find address of track id (Located in the chrome_elf.dll module)
50-
long chromeElfAddress = chromeElf.getBaseOfDll();
50+
long chromeElfAddress = chromeElfModule.getBaseOfDll();
5151
this.addressTrackId = this.findAddressOfText(chromeElfAddress, PREFIX_SPOTIFY_TRACK, 0);
5252

5353
if (this.addressTrackId == -1 || !this.isTrackIdValid(this.getTrackId())) {
@@ -57,13 +57,19 @@ public SpotifyProcess() {
5757
System.out.println("Found track id address: " + Long.toHexString(this.addressTrackId));
5858
}
5959

60+
// Get address range to search for playback
61+
Psapi.ModuleInfo spotifyExeModule = this.getModuleInfo("Spotify.exe");
62+
Psapi.ModuleInfo libCefModule = this.getModuleInfo("libcef.dll");
63+
long minAddress = spotifyExeModule == null ? 0 : spotifyExeModule.getBaseOfDll();
64+
long maxAddress = spotifyExeModule == null ? this.addressTrackId : libCefModule.getBaseOfDll();
65+
6066
// Check if the song is currently playing using the title bar
6167
boolean isPlaying = this.isPlayingUsingTitle();
6268

6369
// Find addresses of playback states
6470
this.addressPlayBack = this.findInMemory(
65-
0,
66-
this.addressTrackId,
71+
minAddress,
72+
maxAddress,
6773
PREFIX_CONTEXT,
6874
(address, index) -> {
6975
PlaybackAccessor accessor = new PlaybackAccessor(this, address);

0 commit comments

Comments
 (0)