Upload files to "src/openonyx/music"
This commit is contained in:
1 parent
35040eaade
commit
43d62845c7
3 files changed
+220
No files matched your search
@@ -0,0 +1,202 @@
|
||||
package openonyx.music;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
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.Base64;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import openonyx.core.OnyxClient;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
final class WindowsNowPlaying implements NowPlayingSource {
|
||||
private boolean I;
|
||||
private static final String d = "$ErrorActionPreference = 'Stop'\n$outPath = '__OUTPUT__'\n\nfunction Write-Result($obj) {\n [System.IO.File]::WriteAllText($outPath, ($obj | ConvertTo-Json -Compress), [System.Text.Encoding]::UTF8)\n}\n\ntry {\n Add-Type -AssemblyName System.Runtime.WindowsRuntime\n\n $asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object {\n $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1'\n })[0]\n\n function Await($WinRtTask, $ResultType) {\n $asTask = $asTaskGeneric.MakeGenericMethod($ResultType)\n $netTask = $asTask.Invoke($null, @($WinRtTask))\n $netTask.Wait(-1) | Out-Null\n $netTask.Result\n }\n\n [Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager,Windows.Media.Control,ContentType=WindowsRuntime] | Out-Null\n\n $manager = Await ([Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager]::RequestAsync()) ([Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager])\n $session = $manager.GetCurrentSession()\n\n if ($null -eq $session) {\n Write-Result ([PSCustomObject]@{ empty = $true })\n exit 0\n }\n\n $props = Await ($session.TryGetMediaPropertiesAsync()) ([Windows.Media.Control.GlobalSystemMediaTransportControlsSessionMediaProperties])\n $playback = $session.GetPlaybackInfo()\n $timeline = $session.GetTimelineProperties()\n\n # SMTC hands the cover over as a stream reference rather than a URL, so it has to be\n # read out whole and carried back as base64. Its own try/catch: a player that\n # publishes no thumbnail, or hands back a stream that will not open, must still\n # leave the title and the progress bar working.\n #\n # The stream comes back from the reflection-driven await as a bare COM object with no\n # usable WinRT projection: its .Size reads as empty (so the old size-gated read never\n # ran) and DataReader refuses to wrap it. AsStreamForRead projects it to an ordinary\n # .NET stream, which drains cleanly regardless — that is the only path that works here.\n $art = $null\n\n try {\n $reference = $props.Thumbnail\n\n if ($null -ne $reference) {\n $stream = Await ($reference.OpenReadAsync()) ([Windows.Storage.Streams.IRandomAccessStreamWithContentType])\n\n $asStreamForRead = [System.IO.WindowsRuntimeStreamExtensions].GetMethods() | Where-Object {\n $_.Name -eq 'AsStreamForRead' -and $_.GetParameters().Count -eq 1\n } | Select-Object -First 1\n\n $netStream = $asStreamForRead.Invoke($null, @($stream))\n\n try {\n $buffer = New-Object System.IO.MemoryStream\n $netStream.CopyTo($buffer)\n $bytes = $buffer.ToArray()\n\n if ($bytes.Length -gt 0) {\n $art = [Convert]::ToBase64String($bytes)\n }\n\n $buffer.Dispose()\n } finally {\n $netStream.Dispose()\n }\n }\n } catch {\n $art = $null\n }\n\n Write-Result ([PSCustomObject]@{\n title = $props.Title\n artist = $props.Artist\n playing = ($playback.PlaybackStatus.ToString() -eq 'Playing')\n positionMs = [math]::Round($timeline.Position.TotalMilliseconds)\n durationMs = [math]::Round(($timeline.EndTime - $timeline.StartTime).TotalMilliseconds)\n art = $art\n })\n} catch {\n Write-Result ([PSCustomObject]@{ error = $_.Exception.Message })\n}\n";
|
||||
private static final long i = 6L;
|
||||
private Path path;
|
||||
private boolean H;
|
||||
private Path path2;
|
||||
|
||||
private byte[] L(String string) {
|
||||
WindowsNowPlaying windowsNowPlaying1 = this;
|
||||
if (string != null && !string.isBlank()) {
|
||||
try {
|
||||
byte[] b = Base64.getDecoder().decode(string);
|
||||
if (!windowsNowPlaying1.I) {
|
||||
windowsNowPlaying1.I = true;
|
||||
Logger logger = OnyxClient.LOGGER;
|
||||
Object[] objects = new Object[1];
|
||||
boolean bl = true;
|
||||
objects[0] = b.length;
|
||||
logger.info("Now-playing (Windows): cover art is {} bytes", objects);
|
||||
}
|
||||
|
||||
return b;
|
||||
} catch (IllegalArgumentException illegalArgumentException) {
|
||||
windowsNowPlaying1.L("Malformed thumbnail from PowerShell (" + string.length() + " chars)");
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
if (!windowsNowPlaying1.I) {
|
||||
windowsNowPlaying1.I = true;
|
||||
OnyxClient.LOGGER.info("Now-playing (Windows): session published no cover art");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private NowPlayingSourceData L(String string) {
|
||||
WindowsNowPlaying windowsNowPlaying1 = this;
|
||||
|
||||
JsonElement jsonElement;
|
||||
try {
|
||||
jsonElement = new JsonParser().parse(string);
|
||||
} catch (RuntimeException runtimeException) {
|
||||
windowsNowPlaying1.L("Malformed JSON from PowerShell: " + string);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (jsonElement != null && jsonElement.isJsonObject()) {
|
||||
JsonObject jsonObject;
|
||||
if ((jsonObject = jsonElement.getAsJsonObject()).has("error")) {
|
||||
windowsNowPlaying1.L(new StringBuilder().insert(0, "PowerShell script error: ").append(L(jsonObject, "error")).toString());
|
||||
return null;
|
||||
}
|
||||
|
||||
if ((string = L(jsonObject, "title")) != null && !string.isBlank()) {
|
||||
String string1 = L(jsonObject, "artist");
|
||||
boolean bl = jsonObject.has("playing") && jsonObject.get("playing").isJsonPrimitive() && jsonObject.get("playing").getAsBoolean();
|
||||
int ix = L(jsonObject, "positionMs");
|
||||
int j = L(jsonObject, "durationMs");
|
||||
NowPlayingSourceData nowPlayingSourceData = new NowPlayingSourceData;
|
||||
String string2;
|
||||
boolean bl1;
|
||||
if (string1 == null) {
|
||||
string2 = "";
|
||||
bl1 = bl;
|
||||
} else {
|
||||
string2 = string1;
|
||||
bl1 = bl;
|
||||
}
|
||||
|
||||
nowPlayingSourceData./* $VF: Unable to resugar constructor */<init>(string, string2, bl1, ix, j, null, windowsNowPlaying1.L(L(jsonObject, "art")));
|
||||
return nowPlayingSourceData;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
windowsNowPlaying1.L(new StringBuilder().insert(0, "Unexpected output from PowerShell: ").append(string).toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String L(JsonObject jsonObject, String string) {
|
||||
JsonElement jsonElement;
|
||||
return (jsonElement = jsonObject.get(string)) != null && jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isString()
|
||||
? jsonElement.getAsString()
|
||||
: null;
|
||||
}
|
||||
|
||||
private void L(String string) {
|
||||
if (!this.H) {
|
||||
this.H = true;
|
||||
Logger logger = OnyxClient.LOGGER;
|
||||
Object[] objects = new Object[1];
|
||||
boolean bl = true;
|
||||
objects[0] = string;
|
||||
logger.warn("Now-playing (Windows): {}", objects);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public NowPlayingSourceData L() {
|
||||
WindowsNowPlaying windowsNowPlaying1 = this;
|
||||
|
||||
try {
|
||||
windowsNowPlaying1.L();
|
||||
Files.deleteIfExists(windowsNowPlaying1.path2);
|
||||
String[] strings = new String[7];
|
||||
boolean bl1 = true;
|
||||
strings[0] = "powershell.exe";
|
||||
strings[1] = "-NoProfile";
|
||||
strings[2] = "-NonInteractive";
|
||||
strings[3] = "-ExecutionPolicy";
|
||||
strings[4] = "Bypass";
|
||||
strings[5] = "-File";
|
||||
strings[6] = windowsNowPlaying1.path.toString();
|
||||
Process process = new ProcessBuilder(strings).start();
|
||||
String string = new String(process.getErrorStream().readAllBytes(), StandardCharsets.UTF_8);
|
||||
if (!process.waitFor(6L, TimeUnit.SECONDS)) {
|
||||
process.destroyForcibly();
|
||||
windowsNowPlaying1.L("powershell.exe timed out after 6s");
|
||||
return null;
|
||||
} else {
|
||||
Path pathx = windowsNowPlaying1.path2;
|
||||
LinkOption[] linkOptions = new LinkOption[0];
|
||||
boolean bl = true;
|
||||
if (!Files.isRegularFile(pathx, linkOptions)) {
|
||||
windowsNowPlaying1.L(
|
||||
"powershell.exe produced no output (exit "
|
||||
+ process.exitValue()
|
||||
+ "). stderr: "
|
||||
+ (string.isBlank() ? "(none)" : string.strip())
|
||||
+ ". Script left at: "
|
||||
+ windowsNowPlaying1.path
|
||||
);
|
||||
return null;
|
||||
} else {
|
||||
NowPlayingSourceData nowPlayingSourceData = windowsNowPlaying1.L(Files.readString(windowsNowPlaying1.path2, StandardCharsets.UTF_8));
|
||||
windowsNowPlaying1.H = false;
|
||||
return nowPlayingSourceData;
|
||||
}
|
||||
}
|
||||
} catch (IOException | InterruptedException iOException) {
|
||||
windowsNowPlaying1.L(new StringBuilder().insert(0, "Failed to run powershell.exe: ").append(iOException.getMessage()).toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void L() throws IOException {
|
||||
WindowsNowPlaying windowsNowPlaying1 = this;
|
||||
if (windowsNowPlaying1.path != null) {
|
||||
Path path1 = windowsNowPlaying1.path;
|
||||
LinkOption[] linkOptions = new LinkOption[0];
|
||||
boolean bl = true;
|
||||
if (Files.isRegularFile(path1, linkOptions)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
FileAttribute[] fileAttributes = new FileAttribute[0];
|
||||
boolean bl1 = true;
|
||||
windowsNowPlaying1.path = Files.createTempFile("onyx-nowplaying", ".ps1", fileAttributes);
|
||||
fileAttributes = new FileAttribute[0];
|
||||
bl1 = true;
|
||||
windowsNowPlaying1.path2 = Files.createTempFile("onyx-nowplaying-out", ".json", fileAttributes);
|
||||
windowsNowPlaying1.path.toFile().deleteOnExit();
|
||||
windowsNowPlaying1.path2.toFile().deleteOnExit();
|
||||
String string = windowsNowPlaying1.path2.toString().replace("\\", "\\\\").replace("'", "''");
|
||||
Path pathx = windowsNowPlaying1.path;
|
||||
String string1 = "$ErrorActionPreference = 'Stop'\n$outPath = '__OUTPUT__'\n\nfunction Write-Result($obj) {\n [System.IO.File]::WriteAllText($outPath, ($obj | ConvertTo-Json -Compress), [System.Text.Encoding]::UTF8)\n}\n\ntry {\n Add-Type -AssemblyName System.Runtime.WindowsRuntime\n\n $asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object {\n $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1'\n })[0]\n\n function Await($WinRtTask, $ResultType) {\n $asTask = $asTaskGeneric.MakeGenericMethod($ResultType)\n $netTask = $asTask.Invoke($null, @($WinRtTask))\n $netTask.Wait(-1) | Out-Null\n $netTask.Result\n }\n\n [Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager,Windows.Media.Control,ContentType=WindowsRuntime] | Out-Null\n\n $manager = Await ([Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager]::RequestAsync()) ([Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager])\n $session = $manager.GetCurrentSession()\n\n if ($null -eq $session) {\n Write-Result ([PSCustomObject]@{ empty = $true })\n exit 0\n }\n\n $props = Await ($session.TryGetMediaPropertiesAsync()) ([Windows.Media.Control.GlobalSystemMediaTransportControlsSessionMediaProperties])\n $playback = $session.GetPlaybackInfo()\n $timeline = $session.GetTimelineProperties()\n\n # SMTC hands the cover over as a stream reference rather than a URL, so it has to be\n # read out whole and carried back as base64. Its own try/catch: a player that\n # publishes no thumbnail, or hands back a stream that will not open, must still\n # leave the title and the progress bar working.\n #\n # The stream comes back from the reflection-driven await as a bare COM object with no\n # usable WinRT projection: its .Size reads as empty (so the old size-gated read never\n # ran) and DataReader refuses to wrap it. AsStreamForRead projects it to an ordinary\n # .NET stream, which drains cleanly regardless — that is the only path that works here.\n $art = $null\n\n try {\n $reference = $props.Thumbnail\n\n if ($null -ne $reference) {\n $stream = Await ($reference.OpenReadAsync()) ([Windows.Storage.Streams.IRandomAccessStreamWithContentType])\n\n $asStreamForRead = [System.IO.WindowsRuntimeStreamExtensions].GetMethods() | Where-Object {\n $_.Name -eq 'AsStreamForRead' -and $_.GetParameters().Count -eq 1\n } | Select-Object -First 1\n\n $netStream = $asStreamForRead.Invoke($null, @($stream))\n\n try {\n $buffer = New-Object System.IO.MemoryStream\n $netStream.CopyTo($buffer)\n $bytes = $buffer.ToArray()\n\n if ($bytes.Length -gt 0) {\n $art = [Convert]::ToBase64String($bytes)\n }\n\n $buffer.Dispose()\n } finally {\n $netStream.Dispose()\n }\n }\n } catch {\n $art = $null\n }\n\n Write-Result ([PSCustomObject]@{\n title = $props.Title\n artist = $props.Artist\n playing = ($playback.PlaybackStatus.ToString() -eq 'Playing')\n positionMs = [math]::Round($timeline.Position.TotalMilliseconds)\n durationMs = [math]::Round(($timeline.EndTime - $timeline.StartTime).TotalMilliseconds)\n art = $art\n })\n} catch {\n Write-Result ([PSCustomObject]@{ error = $_.Exception.Message })\n}\n"
|
||||
.replace("__OUTPUT__", string);
|
||||
Charset charset = StandardCharsets.UTF_8;
|
||||
OpenOption[] openOptions = new OpenOption[0];
|
||||
bl1 = true;
|
||||
Files.writeString(pathx, string1, charset, openOptions);
|
||||
}
|
||||
|
||||
private static int L(JsonObject jsonObject, String string) {
|
||||
JsonElement jsonElement;
|
||||
return (jsonElement = jsonObject.get(string)) != null && jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isNumber()
|
||||
? jsonElement.getAsInt()
|
||||
: 0;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user