|
| 1 | +package de.labystudio.spotifyapi.platform.windows.api.playback; |
| 2 | + |
| 3 | +import de.labystudio.spotifyapi.platform.windows.api.WinProcess; |
| 4 | + |
| 5 | +/** |
| 6 | + * Accessor to read the duration, position and playing state from the Spotify process. |
| 7 | + * |
| 8 | + * @author LabyStudio |
| 9 | + */ |
| 10 | +public class MemoryPlaybackAccessor implements PlaybackAccessor { |
| 11 | + |
| 12 | + private static final long MIN_TRACK_DURATION = 1000; // 1 second |
| 13 | + private static final long MAX_TRACK_DURATION = 1000 * 60 * 10; // 10 minutes |
| 14 | + |
| 15 | + private final WinProcess process; |
| 16 | + private final PointerRegistry pointerRegistry; |
| 17 | + |
| 18 | + private int length; |
| 19 | + private int position; |
| 20 | + private boolean isPlaying; |
| 21 | + |
| 22 | + /** |
| 23 | + * Creates a new instance of the PlaybackAccessor. |
| 24 | + * |
| 25 | + * @param process The Spotify process to read from. |
| 26 | + * @param address The reference address of the playback section |
| 27 | + */ |
| 28 | + public MemoryPlaybackAccessor(WinProcess process, long address) { |
| 29 | + this.process = process; |
| 30 | + |
| 31 | + // Create pointer registry to calculate the absolute addresses using the relative offsets |
| 32 | + this.pointerRegistry = new PointerRegistry(0x0CFF4498, address); |
| 33 | + this.pointerRegistry.register("position", 0x0CFF4810); |
| 34 | + this.pointerRegistry.register("length", 0x0CFF4820); |
| 35 | + this.pointerRegistry.register("is_playing", 0x0CFF4850); // 1=true, 0=false |
| 36 | + |
| 37 | + this.update(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Read the current length, position and playing state from the Spotify process. |
| 42 | + * |
| 43 | + * @return true if the new values are valid, false otherwise |
| 44 | + */ |
| 45 | + @Override |
| 46 | + public boolean update() { |
| 47 | + this.position = this.process.readInteger(this.pointerRegistry.getAddress("position")); |
| 48 | + this.length = this.process.readInteger(this.pointerRegistry.getAddress("length")); |
| 49 | + this.isPlaying = this.process.readBoolean(this.pointerRegistry.getAddress("is_playing")); |
| 50 | + return this.isValid(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Checks if the current values are valid. |
| 55 | + * <p> |
| 56 | + * To make sure that we have correct values from the memory address, |
| 57 | + * we have to set some rules what kind of duration is correct. |
| 58 | + * <p> |
| 59 | + * The values are correct if:<br> |
| 60 | + * - position {@literal <}= length<br> |
| 61 | + * - length {@literal >} 0<br> |
| 62 | + * - length {@literal <}= 10 minutes<br> |
| 63 | + * - position {@literal >}= 1 second<br> |
| 64 | + * - the parity bits are correct<br> |
| 65 | + * |
| 66 | + * @return true if the current values are valid, false otherwise |
| 67 | + */ |
| 68 | + @Override |
| 69 | + public boolean isValid() { |
| 70 | + return this.position <= this.length |
| 71 | + && this.position >= 0 |
| 72 | + && this.length <= MAX_TRACK_DURATION |
| 73 | + && this.length >= MIN_TRACK_DURATION; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public int getLength() { |
| 78 | + return this.length; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public int getPosition() { |
| 83 | + return this.position; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean isPlaying() { |
| 88 | + return this.isPlaying; |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments