-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f8f3bc
commit e6d2330
Showing
2 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...n/java/de/labystudio/spotifyapi/platform/windows/api/playback/MemoryPlaybackAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package de.labystudio.spotifyapi.platform.windows.api.playback; | ||
|
||
import de.labystudio.spotifyapi.platform.windows.api.WinProcess; | ||
|
||
/** | ||
* Accessor to read the duration, position and playing state from the Spotify process. | ||
* | ||
* @author LabyStudio | ||
*/ | ||
public class MemoryPlaybackAccessor implements PlaybackAccessor { | ||
|
||
private static final long MIN_TRACK_DURATION = 1000; // 1 second | ||
private static final long MAX_TRACK_DURATION = 1000 * 60 * 10; // 10 minutes | ||
|
||
private final WinProcess process; | ||
private final PointerRegistry pointerRegistry; | ||
|
||
private int length; | ||
private int position; | ||
private boolean isPlaying; | ||
|
||
/** | ||
* Creates a new instance of the PlaybackAccessor. | ||
* | ||
* @param process The Spotify process to read from. | ||
* @param address The reference address of the playback section | ||
*/ | ||
public MemoryPlaybackAccessor(WinProcess process, long address) { | ||
this.process = process; | ||
|
||
// Create pointer registry to calculate the absolute addresses using the relative offsets | ||
this.pointerRegistry = new PointerRegistry(0x0CFF4498, address); | ||
this.pointerRegistry.register("position", 0x0CFF4810); | ||
this.pointerRegistry.register("length", 0x0CFF4820); | ||
this.pointerRegistry.register("is_playing", 0x0CFF4850); // 1=true, 0=false | ||
|
||
this.update(); | ||
} | ||
|
||
/** | ||
* Read the current length, position and playing state from the Spotify process. | ||
* | ||
* @return true if the new values are valid, false otherwise | ||
*/ | ||
@Override | ||
public boolean update() { | ||
this.position = this.process.readInteger(this.pointerRegistry.getAddress("position")); | ||
this.length = this.process.readInteger(this.pointerRegistry.getAddress("length")); | ||
this.isPlaying = this.process.readBoolean(this.pointerRegistry.getAddress("is_playing")); | ||
return this.isValid(); | ||
} | ||
|
||
/** | ||
* Checks if the current values are valid. | ||
* <p> | ||
* To make sure that we have correct values from the memory address, | ||
* we have to set some rules what kind of duration is correct. | ||
* <p> | ||
* The values are correct if:<br> | ||
* - position {@literal <}= length<br> | ||
* - length {@literal >} 0<br> | ||
* - length {@literal <}= 10 minutes<br> | ||
* - position {@literal >}= 1 second<br> | ||
* - the parity bits are correct<br> | ||
* | ||
* @return true if the current values are valid, false otherwise | ||
*/ | ||
@Override | ||
public boolean isValid() { | ||
return this.position <= this.length | ||
&& this.position >= 0 | ||
&& this.length <= MAX_TRACK_DURATION | ||
&& this.length >= MIN_TRACK_DURATION; | ||
} | ||
|
||
@Override | ||
public int getLength() { | ||
return this.length; | ||
} | ||
|
||
@Override | ||
public int getPosition() { | ||
return this.position; | ||
} | ||
|
||
@Override | ||
public boolean isPlaying() { | ||
return this.isPlaying; | ||
} | ||
|
||
} |