Skip to content

Commit

Permalink
[Change] Convert to Google GSON
Browse files Browse the repository at this point in the history
  • Loading branch information
CDAGaming committed Dec 26, 2019
1 parent c052ff0 commit 5d56373
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 160 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.kohlschutter.junixsocket</groupId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void onReady(IPCClient client) {
RichPresence.Builder builder = new RichPresence.Builder();
builder.setState("Testing RPC Data...")
.setDetails("$DETAILS_HERE")
.setStartTimestamp(OffsetDateTime.now())
.setStartTimestamp(OffsetDateTime.now().toEpochSecond())
.setLargeImage("success", "Test Successful");
client.sendRichPresence(builder.build());
}
Expand Down
172 changes: 95 additions & 77 deletions src/main/java/com/jagrosh/discordipc/IPCClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
package com.jagrosh.discordipc;

import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.jagrosh.discordipc.entities.*;
import com.jagrosh.discordipc.entities.Packet.OpCode;
import com.jagrosh.discordipc.entities.pipe.Pipe;
import com.jagrosh.discordipc.entities.pipe.PipeStatus;
import com.jagrosh.discordipc.exceptions.NoDiscordClientException;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -222,11 +222,17 @@ public void sendRichPresence(RichPresence presence, Callback callback) {
System.out.println("Sending RichPresence to discord: " + (presence == null ? null : presence.toJson().toString()));
}

pipe.send(OpCode.FRAME, new JSONObject()
.put("cmd", "SET_ACTIVITY")
.put("args", new JSONObject()
.put("pid", getPID())
.put("activity", presence == null ? null : presence.toJson())), callback);
// Setup and Send JsonObject Data Representing an RPC Update
JsonObject finalObject = new JsonObject(),
args = new JsonObject();

finalObject.addProperty("cmd", "SET_ACTIVITY");

args.addProperty("pid", getPID());
args.add("activity", presence == null ? new JsonObject() : presence.toJson());

finalObject.add("args", args);
pipe.send(OpCode.FRAME, finalObject, callback);
}

/**
Expand Down Expand Up @@ -267,9 +273,11 @@ public void subscribe(Event sub, Callback callback) {
System.out.println(String.format("Subscribing to Event: %s", sub.name()));
}

pipe.send(OpCode.FRAME, new JSONObject()
.put("cmd", "SUBSCRIBE")
.put("evt", sub.name()), callback);
JsonObject pipeData = new JsonObject();
pipeData.addProperty("cmd", "SUBSCRIBE");
pipeData.addProperty("evt", sub.name());

pipe.send(OpCode.FRAME, pipeData, callback);
}

public void respondToJoinRequest(User user, ApprovalMode approvalMode, Callback callback) {
Expand All @@ -280,10 +288,15 @@ public void respondToJoinRequest(User user, ApprovalMode approvalMode, Callback
System.out.println(String.format("Sending response to %s as %s", user.getName(), approvalMode.name()));
}

pipe.send(OpCode.FRAME, new JSONObject()
.put("cmd", approvalMode == ApprovalMode.ACCEPT ? "SEND_ACTIVITY_JOIN_INVITE" : "CLOSE_ACTIVITY_REQUEST")
.put("args", new JSONObject()
.put("user_id", user.getId())), callback);
JsonObject pipeData = new JsonObject();
pipeData.addProperty("cmd", approvalMode == ApprovalMode.ACCEPT ? "SEND_ACTIVITY_JOIN_INVITE" : "CLOSE_ACTIVITY_REQUEST");

JsonObject args = new JsonObject();
args.addProperty("user_id", user.getId());

pipeData.add("args", args);

pipe.send(OpCode.FRAME, pipeData, callback);
}
}

Expand Down Expand Up @@ -383,77 +396,82 @@ private void startReading() {
try {
Packet p;
while ((p = pipe.read()).getOp() != OpCode.CLOSE) {
JSONObject json = p.getJson();
Event event = Event.of(json.optString("evt", null));
String nonce = json.optString("nonce", null);
switch (event) {
case NULL:
if (nonce != null && callbacks.containsKey(nonce))
callbacks.remove(nonce).succeed(p);
break;

case ERROR:
if (nonce != null && callbacks.containsKey(nonce))
callbacks.remove(nonce).fail(json.getJSONObject("data").optString("message", null));
break;

case ACTIVITY_JOIN:
if (debugMode) {
System.out.println("Reading thread received a 'join' event.");
}
break;

case ACTIVITY_SPECTATE:
if (debugMode) {
System.out.println("Reading thread received a 'spectate' event.");
}
break;

case ACTIVITY_JOIN_REQUEST:
if (debugMode) {
System.out.println("Reading thread received a 'join request' event.");
}
break;
JsonObject json = p.getJson();

if (json != null) {
Event event = Event.of(json.has("evt") && !json.get("evt").isJsonNull() ? json.getAsJsonPrimitive("evt").getAsString() : null);
String nonce = json.has("nonce") && !json.get("nonce").isJsonNull() ? json.getAsJsonPrimitive("nonce").getAsString() : null;

switch (event) {
case NULL:
if (nonce != null && callbacks.containsKey(nonce))
callbacks.remove(nonce).succeed(p);
break;

case ERROR:
if (nonce != null && callbacks.containsKey(nonce))
callbacks.remove(nonce).fail(json.has("data") && json.getAsJsonObject("data").has("message") ? json.getAsJsonObject("data").getAsJsonObject("message").getAsString() : null);
break;

case ACTIVITY_JOIN:
if (debugMode) {
System.out.println("Reading thread received a 'join' event.");
}
break;

case ACTIVITY_SPECTATE:
if (debugMode) {
System.out.println("Reading thread received a 'spectate' event.");
}
break;

case ACTIVITY_JOIN_REQUEST:
if (debugMode) {
System.out.println("Reading thread received a 'join request' event.");
}
break;

case UNKNOWN:
if (debugMode) {
System.out.println("Reading thread encountered an event with an unknown type: " +
json.getAsJsonPrimitive("evt").getAsString());
}
break;
}

case UNKNOWN:
if (debugMode) {
System.out.println("Reading thread encountered an event with an unknown type: " +
json.getString("evt"));
}
break;
}
if (listener != null && json.has("cmd") && json.getString("cmd").equals("DISPATCH")) {
try {
JSONObject data = json.getJSONObject("data");
switch (Event.of(json.getString("evt"))) {
case ACTIVITY_JOIN:
listener.onActivityJoin(localInstance, data.getString("secret"));
break;

case ACTIVITY_SPECTATE:
listener.onActivitySpectate(localInstance, data.getString("secret"));
break;

case ACTIVITY_JOIN_REQUEST:
JSONObject u = data.getJSONObject("user");
User user = new User(
u.getString("username"),
u.getString("discriminator"),
Long.parseLong(u.getString("id")),
u.optString("avatar", null)
);
listener.onActivityJoinRequest(localInstance, data.optString("secret", null), user);
break;
if (listener != null && json.has("cmd") && json.getAsJsonPrimitive("cmd").getAsString().equals("DISPATCH")) {
try {
JsonObject data = json.getAsJsonObject("data");
switch (Event.of(json.getAsJsonPrimitive("evt").getAsString())) {
case ACTIVITY_JOIN:
listener.onActivityJoin(localInstance, data.getAsJsonObject("secret").getAsString());
break;

case ACTIVITY_SPECTATE:
listener.onActivitySpectate(localInstance, data.getAsJsonObject("secret").getAsString());
break;

case ACTIVITY_JOIN_REQUEST:
final JsonObject u = data.getAsJsonObject("user");
final User user = new User(
u.getAsJsonPrimitive("username").getAsString(),
u.getAsJsonPrimitive("discriminator").getAsString(),
Long.parseLong(u.getAsJsonPrimitive("id").getAsString()),
u.has("avatar") ? u.getAsJsonPrimitive("avatar").getAsString() : null
);
listener.onActivityJoinRequest(localInstance, data.has("secret") ? data.getAsJsonObject("secret").getAsString() : null, user);
break;
}
} catch (Exception e) {
System.out.println(String.format("Exception when handling event: %s", e));
}
} catch (Exception e) {
System.out.println(String.format("Exception when handling event: %s", e));
}
}
}
pipe.setStatus(PipeStatus.DISCONNECTED);
if (listener != null)
listener.onClose(localInstance, p.getJson());
} catch (IOException | JSONException ex) {
} catch (IOException | JsonParseException ex) {
if (ex instanceof IOException)
System.out.println(String.format("Reading thread encountered an IOException: %s", ex));
else
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/jagrosh/discordipc/IPCListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package com.jagrosh.discordipc;

import com.google.gson.JsonObject;
import com.jagrosh.discordipc.entities.Packet;
import com.jagrosh.discordipc.entities.User;
import org.json.JSONObject;

/**
* An implementable listener used to handle events caught by an {@link IPCClient}.<p>
Expand Down Expand Up @@ -91,9 +91,9 @@ default void onReady(IPCClient client) {
* Fired whenever an {@link IPCClient} has closed.
*
* @param client The now closed IPCClient.
* @param json A {@link JSONObject} with close data.
* @param json A {@link JsonObject} with close data.
*/
default void onClose(IPCClient client, JSONObject json) {
default void onClose(IPCClient client, JsonObject json) {
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/jagrosh/discordipc/entities/Packet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.jagrosh.discordipc.entities;

import org.json.JSONObject;
import com.google.gson.JsonObject;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
Expand All @@ -28,30 +28,30 @@
*/
public class Packet {
private final OpCode op;
private final JSONObject data;
private final JsonObject data;
private final String encoding;

/**
* Constructs a new Packet using an {@link OpCode} and {@link JSONObject}.
* Constructs a new Packet using an {@link OpCode} and {@link JsonObject}.
*
* @param op The OpCode value of this new Packet.
* @param data The JSONObject payload of this new Packet.
* @param encoding encoding to send packets as
*/
public Packet(OpCode op, JSONObject data, String encoding) {
public Packet(OpCode op, JsonObject data, String encoding) {
this.op = op;
this.data = data;
this.encoding = encoding;
}

/**
* Constructs a new Packet using an {@link OpCode} and {@link JSONObject}.
* Constructs a new Packet using an {@link OpCode} and {@link JsonObject}.
*
* @param op The OpCode value of this new Packet.
* @param data The JSONObject payload of this new Packet.
*/
@Deprecated
public Packet(OpCode op, JSONObject data) {
public Packet(OpCode op, JsonObject data) {
this(op, data, "UTF-8");
}

Expand Down Expand Up @@ -87,11 +87,11 @@ public OpCode getOp() {
}

/**
* Gets the {@link JSONObject} value as a part of this {@link Packet}.
* Gets the {@link JsonObject} value as a part of this {@link Packet}.
*
* @return The JSONObject value of this Packet.
*/
public JSONObject getJson() {
public JsonObject getJson() {
return data;
}

Expand Down
Loading

0 comments on commit 5d56373

Please sign in to comment.