Skip to content

Commit

Permalink
version 1.1.16 - Fix Song detection and update Offsets (#8)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
HerXayah and LabyStudio authored Sep 29, 2023
1 parent 134188d commit 200ca52
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
out
.gradle
*.iml
build
build
run/
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'de.labystudio'
version '1.1.15'
version '1.1.16'

compileJava {
sourceCompatibility = '1.8'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public class SpotifyProcess extends WinProcess {
// Spotify track id
private static final String PREFIX_SPOTIFY_TRACK = "spotify:track:";
private static final long[] OFFSETS_TRACK_ID = {
0x14C9F0, // 64-Bit
0x102178, // 32-Bit
0x14FA30, // 64-Bit (1.2.21.1104.g42cf0a50)
0x106198, // 32-Bit (1.2.21.1104.g42cf0a50)
0x14C9F0, // 64-Bit (Old)
0x102178, // 32-Bit (Old)
0x1499F0, // 64-Bit (Old)
0xFEFE8 // 32-Bit (Old)
};
Expand Down Expand Up @@ -62,14 +64,36 @@ private long findTrackIdAddress() {

// Check all offsets for valid track id
long addressTrackId = -1;
long minTrackIdOffset = Long.MAX_VALUE;
long maxTrackIdOffset = Long.MIN_VALUE;
for (long trackIdOffset : OFFSETS_TRACK_ID) {
addressTrackId = chromeElfAddress + trackIdOffset;
if (addressTrackId != -1 && this.isTrackIdValid(this.readTrackId(addressTrackId))) {
// Get min and max of hardcoded offset
minTrackIdOffset = Math.min(minTrackIdOffset, trackIdOffset);
maxTrackIdOffset = Math.max(maxTrackIdOffset, trackIdOffset);

// Check if the hardcoded offset is valid
long targetAddressTrackId = chromeElfAddress + trackIdOffset;
if (this.isTrackIdValid(this.readTrackId(targetAddressTrackId))) {
// If the offset works, exit the loop
addressTrackId = targetAddressTrackId;
break;
}
}

// If the hardcoded offsets are not valid, try to find it dynamically
if (addressTrackId == -1) {
if (DEBUG) {
System.out.println("Could not find track id with hardcoded offsets. Trying to find it dynamically...");
}

long threshold = (maxTrackIdOffset - minTrackIdOffset) * 3;
long scanAddressFrom = chromeElfAddress + minTrackIdOffset - threshold;
long scanAddressTo = chromeElfAddress + maxTrackIdOffset + threshold;
addressTrackId = this.findAddressOfText(scanAddressFrom, scanAddressTo, "spotify:track:", (address, index) -> {
return this.isTrackIdValid(this.readTrackId(address));
});
}

if (addressTrackId == -1) {
throw new IllegalStateException("Could not find track id in memory");
}
Expand All @@ -87,10 +111,22 @@ private long findTrackIdAddress() {
}

private PlaybackAccessor findPlaybackAccessor() {
// Find addresses of playback states
// Find addresses of playback states when playing a playlist
long addressPlayBack = this.findAddressOfText(0, 0x0FFFFFFF, "playlist", (address, index) -> {
return this.hasText(address + 408, "context", "autoplay");
return this.hasText(address + 408, "context", "autoplay")
&& this.hasText(address + 128, "your_library", "home")
&& new MemoryPlaybackAccessor(this, address).isValid();
});

if (addressPlayBack == -1) {
// Find addresses of playback states when playing an album
addressPlayBack = this.findAddressOfText(0, 0x0FFFFFFF, "album", (address, index) -> {
return this.hasText(address + 408, "context", "autoplay")
&& this.hasText(address + 128, "your_library", "home")
&& new MemoryPlaybackAccessor(this, address).isValid();
});
}

if (addressPlayBack == -1) {
if (DEBUG) {
System.out.println("Could not find playback address in memory");
Expand Down Expand Up @@ -175,7 +211,7 @@ public long getAddressTrackId() {
*/
public boolean isTrackIdValid(String trackId) {
for (char c : trackId.toCharArray()) {
if (c == 0) {
if (!Character.isLetterOrDigit(c)) {
return false;
}
}
Expand Down
19 changes: 15 additions & 4 deletions src/test/java/SpotifyProcessTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import de.labystudio.spotifyapi.platform.windows.api.WinProcess;
import de.labystudio.spotifyapi.platform.windows.api.jna.Psapi;
import de.labystudio.spotifyapi.platform.windows.api.playback.MemoryPlaybackAccessor;

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

long addressOfPlayback = process.findAddressOfText(0, 0x0FFFFFFF, "playlist", (address, index) -> {
return process.hasText(address + 408, "context", "autoplay");

// process.hasText(address + 128, "your_library", "home")
return process.hasText(address + 408, "context", "autoplay")
&& process.hasText(address + 128, "your_library", "home")
&& new MemoryPlaybackAccessor(process, address).isValid();
});

System.out.println("Playback Address: 0x" + Long.toHexString(addressOfPlayback));
if (addressOfPlayback == -1) {
addressOfPlayback = process.findAddressOfText(0, 0x0FFFFFFF, "album", (address, index) -> {
return process.hasText(address + 408, "context", "autoplay")
&& process.hasText(address + 128, "your_library", "home")
&& new MemoryPlaybackAccessor(process, address).isValid();
});
}

MemoryPlaybackAccessor accessor = new MemoryPlaybackAccessor(process, addressOfPlayback);
System.out.println("Playback Address: 0x" + Long.toHexString(addressOfPlayback) + " (" + (accessor.isValid() ? "valid" : "invalid") + ")");
System.out.println("Position: " + accessor.getPosition());
}

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

0 comments on commit 200ca52

Please sign in to comment.