Skip to content

Commit

Permalink
[Fix] Fix IPC not looping on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
CDAGaming committed Dec 26, 2019
1 parent 0df3bc1 commit c052ff0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.
24 changes: 12 additions & 12 deletions src/main/java/com/jagrosh/discordipc/IPCClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public IPCClient(long clientId) {
* Constructs a new IPCClient using the provided {@code clientId}.<br>
* This is initially unconnected to Discord.
*
* @param clientId The Rich Presence application's client ID, which can be found
* <a href=https://discordapp.com/developers/applications/me>here</a>
* @param clientId The Rich Presence application's client ID, which can be found
* <a href=https://discordapp.com/developers/applications/me>here</a>
* @param debugMode Whether Debug Logging should be shown for this client
*/
public IPCClient(long clientId, boolean debugMode) {
Expand Down Expand Up @@ -113,6 +113,16 @@ public void setListener(IPCListener listener) {
pipe.setListener(listener);
}

/**
* Gets encoding to send packets in.<p>
* Default: UTF-8
*
* @return encoding
*/
public String getEncoding() {
return this.encoding;
}

/**
* Sets the encoding to send packets in.<p>
* <p>
Expand All @@ -127,16 +137,6 @@ public void setEncoding(final String encoding) {
this.encoding = encoding;
}

/**
* Gets encoding to send packets in.<p>
* Default: UTF-8
*
* @return encoding
*/
public String getEncoding() {
return this.encoding;
}

/**
* Gets the client ID associated with this IPCClient
*
Expand Down
59 changes: 31 additions & 28 deletions src/main/java/com/jagrosh/discordipc/entities/pipe/Pipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,42 +64,44 @@ public static Pipe openPipe(IPCClient ipcClient, long clientId, HashMap<String,
}
pipe = createPipe(ipcClient, callbacks, location);

pipe.send(Packet.OpCode.HANDSHAKE, new JSONObject().put("v", VERSION).put("client_id", Long.toString(clientId)), null);
if (pipe != null) {
pipe.send(Packet.OpCode.HANDSHAKE, new JSONObject().put("v", VERSION).put("client_id", Long.toString(clientId)), null);

Packet p = pipe.read(); // this is a valid client at this point
Packet p = pipe.read(); // this is a valid client at this point

final JSONObject data = p.getJson().getJSONObject("data");
final JSONObject userData = data.getJSONObject("user");
final JSONObject data = p.getJson().getJSONObject("data");
final JSONObject userData = data.getJSONObject("user");

pipe.build = DiscordBuild.from(data
.getJSONObject("config")
.getString("api_endpoint"));
pipe.build = DiscordBuild.from(data
.getJSONObject("config")
.getString("api_endpoint"));

pipe.currentUser = new User(
userData.getString("username"),
userData.getString("discriminator"),
Long.parseLong(userData.getString("id")),
userData.has("avatar") ? userData.getString("avatar") : null
);
pipe.currentUser = new User(
userData.getString("username"),
userData.getString("discriminator"),
Long.parseLong(userData.getString("id")),
userData.has("avatar") ? userData.getString("avatar") : null
);

if (ipcClient.isDebugMode()) {
System.out.println(String.format("Found a valid client (%s) with packet: %s", pipe.build.name(), p.toString()));
System.out.println(String.format("Found a valid user (%s) with id: %s", pipe.currentUser.getName(), pipe.currentUser.getId()));
}

// we're done if we found our first choice
if (pipe.build == preferredOrder[0] || DiscordBuild.ANY == preferredOrder[0]) {
if (ipcClient.isDebugMode()) {
System.out.println(String.format("Found preferred client: %s", pipe.build.name()));
System.out.println(String.format("Found a valid client (%s) with packet: %s", pipe.build.name(), p.toString()));
System.out.println(String.format("Found a valid user (%s) with id: %s", pipe.currentUser.getName(), pipe.currentUser.getId()));
}
break;
}

open[pipe.build.ordinal()] = pipe; // didn't find first choice yet, so store what we have
open[DiscordBuild.ANY.ordinal()] = pipe; // also store in 'any' for use later
// we're done if we found our first choice
if (pipe.build == preferredOrder[0] || DiscordBuild.ANY == preferredOrder[0]) {
if (ipcClient.isDebugMode()) {
System.out.println(String.format("Found preferred client: %s", pipe.build.name()));
}
break;
}

pipe.build = null;
pipe = null;
open[pipe.build.ordinal()] = pipe; // didn't find first choice yet, so store what we have
open[DiscordBuild.ANY.ordinal()] = pipe; // also store in 'any' for use later

pipe.build = null;
pipe = null;
}
} catch (IOException | JSONException ex) {
pipe = null;
}
Expand Down Expand Up @@ -163,7 +165,8 @@ private static Pipe createPipe(IPCClient ipcClient, HashMap<String, Callback> ca
String osName = System.getProperty("os.name").toLowerCase();

if (osName.contains("win")) {
return new WindowsPipe(ipcClient, callbacks, location);
WindowsPipe attemptedPipe = new WindowsPipe(ipcClient, callbacks, location);
return attemptedPipe.file != null ? attemptedPipe : null;
} else if (osName.contains("linux") || osName.contains("mac")) {
try {
return new UnixPipe(ipcClient, callbacks, location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
import java.util.HashMap;

public class WindowsPipe extends Pipe {
private final RandomAccessFile file;
public RandomAccessFile file;

WindowsPipe(IPCClient ipcClient, HashMap<String, Callback> callbacks, String location) {
super(ipcClient, callbacks);
try {
this.file = new RandomAccessFile(location, "rw");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
this.file = null;
}
}

Expand Down

0 comments on commit c052ff0

Please sign in to comment.