Upload files to "src/openonyx/music"
This commit is contained in:
1 parent
14fbc538c4
commit
35040eaade
5 files changed
+434
No files matched your search
@@ -0,0 +1,127 @@
|
||||
package openonyx.music;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.LinkOption;
|
||||
import java.nio.file.OpenOption;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.FileAttribute;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import openonyx.core.OnyxClient;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
final class MacNowPlaying implements NowPlayingSource {
|
||||
private static final String d = "on readSpotify()\n if application \"Spotify\" is running then\n tell application \"Spotify\"\n if player state is playing or player state is paused then\n set trackName to name of current track\n set trackArtist to artist of current track\n set isPlaying to (player state is playing)\n set pos to player position\n set dur to duration of current track\n set artUrl to artwork url of current track\n return trackName & \"@@@\" & trackArtist & \"@@@\" & isPlaying & \"@@@\" & pos & \"@@@\" & dur & \"@@@\" & artUrl\n end if\n end tell\n end if\n return \"\"\nend readSpotify\n\non readMusic()\n if application \"Music\" is running then\n tell application \"Music\"\n if player state is playing or player state is paused then\n set trackName to name of current track\n set trackArtist to artist of current track\n set isPlaying to (player state is playing)\n set pos to player position\n set dur to duration of current track\n -- Music.app's artwork is embedded binary data, not a URL — no cover here.\n return trackName & \"@@@\" & trackArtist & \"@@@\" & isPlaying & \"@@@\" & pos & \"@@@\" & dur & \"@@@\"\n end if\n end tell\n end if\n return \"\"\nend readMusic\n\nset nowPlayingResult to readSpotify()\nif nowPlayingResult is \"\" then set nowPlayingResult to readMusic()\nreturn nowPlayingResult\n";
|
||||
private boolean i;
|
||||
private static final String f = "@@@";
|
||||
private static final long H = 6L;
|
||||
private Path path;
|
||||
|
||||
private void L(String string) {
|
||||
if (!this.i) {
|
||||
this.i = true;
|
||||
Logger logger = OnyxClient.LOGGER;
|
||||
Object[] objects = new Object[1];
|
||||
boolean bl = true;
|
||||
objects[0] = string;
|
||||
logger.warn("Now-playing (macOS): {}", objects);
|
||||
}
|
||||
}
|
||||
|
||||
private void L() throws IOException {
|
||||
if (this.path != null) {
|
||||
Path path1 = this.path;
|
||||
LinkOption[] linkOptions = new LinkOption[0];
|
||||
boolean bl = true;
|
||||
if (Files.isRegularFile(path1, linkOptions)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
FileAttribute[] fileAttributes = new FileAttribute[0];
|
||||
boolean bl1 = true;
|
||||
this.path = Files.createTempFile("onyx-nowplaying", ".applescript", fileAttributes);
|
||||
this.path.toFile().deleteOnExit();
|
||||
Path pathx = this.path;
|
||||
Charset charset = StandardCharsets.UTF_8;
|
||||
OpenOption[] openOptions = new OpenOption[0];
|
||||
bl1 = true;
|
||||
Files.writeString(
|
||||
pathx,
|
||||
"on readSpotify()\n if application \"Spotify\" is running then\n tell application \"Spotify\"\n if player state is playing or player state is paused then\n set trackName to name of current track\n set trackArtist to artist of current track\n set isPlaying to (player state is playing)\n set pos to player position\n set dur to duration of current track\n set artUrl to artwork url of current track\n return trackName & \"@@@\" & trackArtist & \"@@@\" & isPlaying & \"@@@\" & pos & \"@@@\" & dur & \"@@@\" & artUrl\n end if\n end tell\n end if\n return \"\"\nend readSpotify\n\non readMusic()\n if application \"Music\" is running then\n tell application \"Music\"\n if player state is playing or player state is paused then\n set trackName to name of current track\n set trackArtist to artist of current track\n set isPlaying to (player state is playing)\n set pos to player position\n set dur to duration of current track\n -- Music.app's artwork is embedded binary data, not a URL — no cover here.\n return trackName & \"@@@\" & trackArtist & \"@@@\" & isPlaying & \"@@@\" & pos & \"@@@\" & dur & \"@@@\"\n end if\n end tell\n end if\n return \"\"\nend readMusic\n\nset nowPlayingResult to readSpotify()\nif nowPlayingResult is \"\" then set nowPlayingResult to readMusic()\nreturn nowPlayingResult\n",
|
||||
charset,
|
||||
openOptions
|
||||
);
|
||||
}
|
||||
|
||||
private static NowPlayingSourceData L(String string) {
|
||||
String string1 = string;
|
||||
if (string1.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String[] strings;
|
||||
if ((strings = string1.split("@@@", -1)).length < 5) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String string3;
|
||||
if ((string3 = strings[0].trim()).isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String string2 = strings[1].trim();
|
||||
boolean bl = Boolean.parseBoolean(strings[2].trim());
|
||||
string = strings.length > 5 ? strings[5].trim() : "";
|
||||
if (string.isEmpty()) {
|
||||
string = null;
|
||||
}
|
||||
|
||||
try {
|
||||
double dx = Double.parseDouble(L(strings[3]));
|
||||
double e;
|
||||
double fx = (e = Double.parseDouble(L(strings[4]))) > 1000.0 ? e / 1000.0 : e;
|
||||
return new NowPlayingSourceData(string3, string2, bl, (int)Math.round(dx * 1000.0), (int)Math.round(fx * 1000.0), string, null);
|
||||
} catch (NumberFormatException numberFormatException) {
|
||||
return new NowPlayingSourceData(string3, string2, bl, 0, 0, string, null);
|
||||
}
|
||||
}
|
||||
|
||||
private static String L(String string) {
|
||||
return string.trim().replace(',', '.');
|
||||
}
|
||||
|
||||
@Override
|
||||
public NowPlayingSourceData L() {
|
||||
MacNowPlaying macNowPlaying1 = this;
|
||||
|
||||
try {
|
||||
macNowPlaying1.L();
|
||||
String[] strings = new String[2];
|
||||
boolean bl = true;
|
||||
strings[0] = "osascript";
|
||||
strings[1] = macNowPlaying1.path.toString();
|
||||
Process process = new ProcessBuilder(strings).start();
|
||||
String string1 = new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8).strip();
|
||||
String string = new String(process.getErrorStream().readAllBytes(), StandardCharsets.UTF_8).strip();
|
||||
if (!process.waitFor(6L, TimeUnit.SECONDS)) {
|
||||
process.destroyForcibly();
|
||||
macNowPlaying1.L("osascript timed out after 6s");
|
||||
return null;
|
||||
} else if (process.exitValue() != 0) {
|
||||
macNowPlaying1.L(
|
||||
"osascript exited " + process.exitValue() + ": " + (string.isEmpty() ? "(no stderr)" : string) + ". Script left at: " + macNowPlaying1.path
|
||||
);
|
||||
return null;
|
||||
} else {
|
||||
macNowPlaying1.i = false;
|
||||
return L(string1);
|
||||
}
|
||||
} catch (IOException | InterruptedException iOException) {
|
||||
macNowPlaying1.L(new StringBuilder().insert(0, "Failed to run osascript: ").append(iOException.getMessage()).toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
package openonyx.music;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.net.http.HttpResponse.BodyHandlers;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import javax.imageio.ImageIO;
|
||||
import net.minecraft.client.renderer.texture.DynamicTexture;
|
||||
import net.minecraft.client.renderer.texture.ITextureObject;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.Util;
|
||||
import openonyx.core.MinecraftAccess;
|
||||
import openonyx.core.OnyxClient;
|
||||
import openonyx.render.TextureFilter;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public final class NowPlayingService implements MinecraftAccess {
|
||||
private final AtomicReference<TrackInfo> trackInfoAtomicReference;
|
||||
private Thread thread;
|
||||
private int m;
|
||||
private boolean h;
|
||||
private boolean l;
|
||||
private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(8L)).build();
|
||||
private final NowPlayingSource nowPlayingSource = L();
|
||||
private final AtomicReference<NowPlayingServiceData> nowPlayingServiceDataAtomicReference;
|
||||
private final AtomicBoolean atomicBoolean;
|
||||
private String H;
|
||||
private static final long e = 800L;
|
||||
|
||||
public NowPlayingService() {
|
||||
this.trackInfoAtomicReference = new AtomicReference<>();
|
||||
this.nowPlayingServiceDataAtomicReference = new AtomicReference<>();
|
||||
this.atomicBoolean = new AtomicBoolean(false);
|
||||
}
|
||||
|
||||
private static String L(NowPlayingSourceData nowPlayingSourceData) {
|
||||
if (nowPlayingSourceData.artUrl() != null && !nowPlayingSourceData.artUrl().isEmpty()) {
|
||||
return nowPlayingSourceData.artUrl();
|
||||
} else {
|
||||
return nowPlayingSourceData.artData() != null && nowPlayingSourceData.artData().length != 0
|
||||
? new StringBuilder().insert(0, "track:").append(nowPlayingSourceData.title()).append(" - ").append(nowPlayingSourceData.artist()).toString()
|
||||
: null;
|
||||
}
|
||||
}
|
||||
|
||||
private BufferedImage L(String string) {
|
||||
try {
|
||||
HttpRequest httpRequest = HttpRequest.newBuilder(URI.create(string)).timeout(Duration.ofSeconds(8L)).GET().build();
|
||||
HttpResponse httpResponse;
|
||||
if ((httpResponse = HTTP_CLIENT.send(httpRequest, BodyHandlers.ofByteArray())).statusCode() != 200) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream((byte[])httpResponse.body())) {
|
||||
return ImageIO.read(byteArrayInputStream);
|
||||
}
|
||||
} catch (IOException | InterruptedException iOException) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static BufferedImage L(BufferedImage bufferedImage) {
|
||||
BufferedImage bufferedImage1 = bufferedImage;
|
||||
int i;
|
||||
return (i = Math.min(bufferedImage1.getWidth(), bufferedImage1.getHeight())) == bufferedImage1.getWidth() && i == bufferedImage1.getHeight()
|
||||
? bufferedImage1
|
||||
: bufferedImage1.getSubimage((bufferedImage1.getWidth() - i) / 2, (bufferedImage1.getHeight() - i) / 2, i, i);
|
||||
}
|
||||
|
||||
private void i() {
|
||||
NowPlayingService nowPlayingService1 = this;
|
||||
NowPlayingSourceData nowPlayingSourceData;
|
||||
if ((nowPlayingSourceData = nowPlayingService1.nowPlayingSource.L()) != null
|
||||
&& nowPlayingSourceData.title() != null
|
||||
&& !nowPlayingSourceData.title().isBlank()) {
|
||||
if (!nowPlayingService1.h) {
|
||||
nowPlayingService1.h = true;
|
||||
Logger logger = OnyxClient.LOGGER;
|
||||
Object[] objects = new Object[2];
|
||||
boolean bl = true;
|
||||
objects[0] = nowPlayingSourceData.title();
|
||||
objects[1] = nowPlayingSourceData.artist();
|
||||
logger.info("Now-playing: detected \"{}\" by \"{}\"", objects);
|
||||
}
|
||||
|
||||
String string = nowPlayingSourceData.title();
|
||||
ResourceLocation resourceLocation = nowPlayingService1.L(
|
||||
L(nowPlayingSourceData), nowPlayingSourceData.artUrl(), nowPlayingSourceData.artData(), string
|
||||
);
|
||||
AtomicReference atomicReference = nowPlayingService1.trackInfoAtomicReference;
|
||||
TrackInfo trackInfo = new TrackInfo;
|
||||
String string1;
|
||||
ResourceLocation resourceLocation1;
|
||||
if (nowPlayingSourceData.artist() == null) {
|
||||
string1 = "";
|
||||
resourceLocation1 = resourceLocation;
|
||||
} else {
|
||||
string1 = nowPlayingSourceData.artist();
|
||||
resourceLocation1 = resourceLocation;
|
||||
}
|
||||
|
||||
trackInfo./* $VF: Unable to resugar constructor */<init>(
|
||||
string,
|
||||
string1,
|
||||
resourceLocation1,
|
||||
nowPlayingSourceData.playing(),
|
||||
nowPlayingSourceData.positionMs(),
|
||||
nowPlayingSourceData.durationMs(),
|
||||
System.nanoTime()
|
||||
);
|
||||
atomicReference.set(trackInfo);
|
||||
} else {
|
||||
nowPlayingService1.trackInfoAtomicReference.set(null);
|
||||
}
|
||||
}
|
||||
|
||||
private ResourceLocation L(String string, String string1, byte[] b, String string2) {
|
||||
NowPlayingService nowPlayingService1 = this;
|
||||
if (string != null && !string.isEmpty()) {
|
||||
NowPlayingServiceData nowPlayingServiceData;
|
||||
if ((nowPlayingServiceData = nowPlayingService1.nowPlayingServiceDataAtomicReference.get()) != null && string.equals(nowPlayingServiceData.key())) {
|
||||
return nowPlayingServiceData.texture();
|
||||
}
|
||||
|
||||
if (string.equals(nowPlayingService1.H)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
nowPlayingService1.H = string;
|
||||
BufferedImage bufferedImage = string1 != null && !string1.isEmpty() ? nowPlayingService1.L(string1) : nowPlayingService1.L(b);
|
||||
if (bufferedImage == null) {
|
||||
return nowPlayingService1.H = null;
|
||||
}
|
||||
|
||||
if (!nowPlayingService1.l) {
|
||||
nowPlayingService1.l = true;
|
||||
Logger logger = OnyxClient.LOGGER;
|
||||
Object[] objects = new Object[2];
|
||||
boolean bl = true;
|
||||
objects[0] = bufferedImage.getWidth();
|
||||
objects[1] = bufferedImage.getHeight();
|
||||
logger.info("Now-playing: cover art source is {}x{} pixels", objects);
|
||||
}
|
||||
|
||||
BufferedImage bufferedImage1 = L(bufferedImage);
|
||||
ResourceLocation resourceLocation = new ResourceLocation(
|
||||
"onyx", new StringBuilder().insert(0, "nowplaying/art-").append(++nowPlayingService1.m).toString()
|
||||
);
|
||||
NowPlayingServiceData nowPlayingServiceData1 = nowPlayingService1.nowPlayingServiceDataAtomicReference.get();
|
||||
mc.addScheduledTask(
|
||||
() -> {
|
||||
mc.getTextureManager().loadTexture(resourceLocation, new DynamicTexture(bufferedImage1));
|
||||
this.nowPlayingServiceDataAtomicReference.set(new NowPlayingServiceData(string, resourceLocation));
|
||||
if (nowPlayingServiceData1 != null) {
|
||||
ITextureObject iTextureObject;
|
||||
if ((iTextureObject = mc.getTextureManager().getTexture(nowPlayingServiceData1.texture())) != null) {
|
||||
TextureFilter.d(iTextureObject.getGlTextureId());
|
||||
}
|
||||
|
||||
mc.getTextureManager().deleteTexture(nowPlayingServiceData1.texture());
|
||||
}
|
||||
|
||||
TrackInfo trackInfo;
|
||||
if ((trackInfo = this.trackInfoAtomicReference.get()) != null && trackInfo.art() == null && string2.equals(trackInfo.title())) {
|
||||
this.trackInfoAtomicReference
|
||||
.set(
|
||||
new TrackInfo(
|
||||
trackInfo.title(),
|
||||
trackInfo.artist(),
|
||||
resourceLocation,
|
||||
trackInfo.playing(),
|
||||
trackInfo.progressMs(),
|
||||
trackInfo.durationMs(),
|
||||
trackInfo.capturedAtNanos()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
return null;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private BufferedImage L(byte[] b) {
|
||||
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(b)) {
|
||||
BufferedImage bufferedImage;
|
||||
if ((bufferedImage = ImageIO.read(byteArrayInputStream)) == null) {
|
||||
Logger logger = OnyxClient.LOGGER;
|
||||
Object[] objects = new Object[1];
|
||||
boolean bl = true;
|
||||
objects[0] = b.length;
|
||||
logger.warn("Now-playing: cover art ({} bytes) is in no format ImageIO reads", objects);
|
||||
}
|
||||
|
||||
return bufferedImage;
|
||||
} catch (IOException iOException) {
|
||||
OnyxClient.LOGGER.warn("Now-playing: failed to decode cover art", iOException);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public TrackInfo L() {
|
||||
return this.trackInfoAtomicReference.get();
|
||||
}
|
||||
|
||||
public boolean L() {
|
||||
return this.nowPlayingSource != null;
|
||||
}
|
||||
|
||||
public synchronized void D() {
|
||||
this.atomicBoolean.set(false);
|
||||
if (this.thread != null) {
|
||||
this.thread.interrupt();
|
||||
}
|
||||
|
||||
this.thread = null;
|
||||
this.trackInfoAtomicReference.set(null);
|
||||
}
|
||||
|
||||
public synchronized void d() {
|
||||
if (this.nowPlayingSource == null) {
|
||||
Logger logger = OnyxClient.LOGGER;
|
||||
Object[] objects = new Object[1];
|
||||
boolean bl = true;
|
||||
objects[0] = Util.getOSType();
|
||||
logger.warn("Now-playing: no media source for OS type {}, feature disabled", objects);
|
||||
} else if (!this.atomicBoolean.getAndSet(true)) {
|
||||
Logger logger1 = OnyxClient.LOGGER;
|
||||
Object[] objects1 = new Object[1];
|
||||
boolean bl1 = true;
|
||||
objects1[0] = this.nowPlayingSource.getClass().getSimpleName();
|
||||
logger1.info("Now-playing: starting {} polling", objects1);
|
||||
this.thread = new Thread(() -> {
|
||||
NowPlayingService nowPlayingService2 = this;
|
||||
|
||||
for (NowPlayingService nowPlayingService3 = nowPlayingService2; nowPlayingService3.atomicBoolean.get(); nowPlayingService3 = nowPlayingService2) {
|
||||
try {
|
||||
nowPlayingService2.i();
|
||||
} catch (RuntimeException runtimeException) {
|
||||
OnyxClient.LOGGER.warn("Now-playing poll failed", runtimeException);
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(800L);
|
||||
} catch (InterruptedException interruptedException) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}, "onyx-nowplaying");
|
||||
this.thread.setDaemon(true);
|
||||
this.thread.start();
|
||||
}
|
||||
}
|
||||
|
||||
private static NowPlayingSource L() {
|
||||
switch (Util.getOSType()) {
|
||||
case WINDOWS:
|
||||
return new WindowsNowPlaying();
|
||||
case OSX:
|
||||
return new MacNowPlaying();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package openonyx.music;
|
||||
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
record NowPlayingServiceData(String key, ResourceLocation texture) {
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package openonyx.music;
|
||||
|
||||
import net.minecraft.util.Util.EnumOS;
|
||||
|
||||
// $VF: synthetic class
|
||||
class NowPlayingServicePart {
|
||||
static {
|
||||
int[] i = new int[EnumOS.values().length];
|
||||
boolean bl = true;
|
||||
e = i;
|
||||
|
||||
try {
|
||||
e[EnumOS.WINDOWS.ordinal()] = 1;
|
||||
} catch (NoSuchFieldError noSuchFieldError1) {
|
||||
}
|
||||
|
||||
try {
|
||||
e[EnumOS.OSX.ordinal()] = 2;
|
||||
} catch (NoSuchFieldError noSuchFieldError) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package openonyx.music;
|
||||
|
||||
interface NowPlayingSource {
|
||||
NowPlayingSourceData L();
|
||||
}
|
||||
Reference in new issue
Block a user