Files
OpenOnyx/src/openonyx/ipc/DiscordIpcSocket.java
T

152 lines
4.6 KiB
Java

package openonyx.ipc;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.StandardProtocolFamily;
import java.net.UnixDomainSocketAddress;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Locale;
import openonyx.core.OnyxClient;
public final class DiscordIpcSocket {
private SocketChannel socketChannel;
private RandomAccessFile randomAccessFile;
public static final int d = 2;
public static final int i = 1;
public static final int f = 0;
private volatile boolean H;
private static final boolean e = System.getProperty("os.name", "").toLowerCase(Locale.ROOT).contains("win");
private boolean L(int i) {
DiscordIpcSocket discordIpcSocket1 = this;
try {
if (e) {
discordIpcSocket1.randomAccessFile = new RandomAccessFile(new StringBuilder().insert(0, "\\\\.\\pipe\\discord-ipc-").append(ix).toString(), "rw");
} else {
String[] strings1 = new String[3];
boolean bl = true;
strings1[0] = System.getenv("XDG_RUNTIME_DIR");
strings1[1] = System.getenv("TMPDIR");
strings1[2] = "/tmp";
String string = L(strings1);
discordIpcSocket1.socketChannel = SocketChannel.open(StandardProtocolFamily.UNIX);
SocketChannel socketChannelx = discordIpcSocket1.socketChannel;
String[] strings = new String[1];
boolean bl1 = true;
strings[0] = new StringBuilder().insert(0, "discord-ipc-").append(ix).toString();
socketChannelx.connect(UnixDomainSocketAddress.of(Path.of(string, strings)));
}
return true;
} catch (Exception exception) {
discordIpcSocket1.d();
return false;
}
}
public boolean L() {
return this.H;
}
private void d() {
DiscordIpcSocket discordIpcSocket1 = this;
discordIpcSocket1.H = false;
try {
if (discordIpcSocket1.randomAccessFile != null) {
discordIpcSocket1.randomAccessFile.close();
}
} catch (IOException iOException1) {
OnyxClient.LOGGER.debug("Failed to close Discord IPC pipe", iOException1);
}
try {
if (discordIpcSocket1.socketChannel != null) {
discordIpcSocket1.socketChannel.close();
}
} catch (IOException iOException) {
OnyxClient.LOGGER.debug("Failed to close Discord IPC socket", iOException);
}
discordIpcSocket1.socketChannel = discordIpcSocket1.randomAccessFile = null;
}
public void L() {
DiscordIpcSocket discordIpcSocket1 = this;
boolean bl = discordIpcSocket1.H;
discordIpcSocket1.H = false;
try {
if (bl) {
discordIpcSocket1.L(2, "{}");
}
} catch (IOException iOException) {
}
discordIpcSocket1.d();
}
private static String L(String... strings) {
String[] strings2 = strings;
String[] strings1 = strings2;
int ix = strings2.length;
for (int k = j = 0; k < ix; k = ++j) {
String string;
if ((string = strings1[j]) != null) {
return string;
}
}
return "/tmp";
}
public boolean L(String string) {
DiscordIpcSocket discordIpcSocket1 = this;
for (int j = ix = 0; j < 10; j = ++ix) {
if (discordIpcSocket1.L(ix)) {
try {
boolean bl = false;
discordIpcSocket1.L(0, "{\"v\":1,\"client_id\":\"" + string + "\"}");
discordIpcSocket1.H = true;
return true;
} catch (IOException iOException) {
discordIpcSocket1.d();
}
}
}
return false;
}
public void L(int i, String string) throws IOException {
DiscordIpcSocket discordIpcSocket1 = this;
byte[] b = string.getBytes(StandardCharsets.UTF_8);
ByteBuffer byteBuffer = ByteBuffer.allocate(8 + b.length).order(ByteOrder.LITTLE_ENDIAN);
byteBuffer.putInt(ix);
byteBuffer.putInt(b.length);
byteBuffer.put(b);
byteBuffer.flip();
if (discordIpcSocket1.randomAccessFile != null) {
discordIpcSocket1.randomAccessFile.write(byteBuffer.array());
} else {
if (discordIpcSocket1.socketChannel == null) {
throw new IOException("Discord IPC is not connected");
}
ByteBuffer byteBuffer1 = byteBuffer;
while (byteBuffer1.hasRemaining()) {
byteBuffer1 = byteBuffer;
discordIpcSocket1.socketChannel.write(byteBuffer);
}
}
}
}