fix remoteExecutor's data transform

This commit is contained in:
pengfei.zhou 2019-11-14 14:52:35 +08:00
parent 1dcdfff97d
commit 74d31edc26
2 changed files with 36 additions and 67 deletions

View File

@ -23,6 +23,10 @@ import android.widget.FrameLayout;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.io.IOException; import java.io.IOException;
import pub.doric.dev.DevPanel; import pub.doric.dev.DevPanel;
@ -33,7 +37,6 @@ import pub.doric.dev.event.EnterDebugEvent;
import pub.doric.dev.event.QuitDebugEvent; import pub.doric.dev.event.QuitDebugEvent;
import pub.doric.engine.ChangeEngineCallback; import pub.doric.engine.ChangeEngineCallback;
import pub.doric.utils.DoricUtils; import pub.doric.utils.DoricUtils;
import pub.doric.utils.ThreadMode;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {

View File

@ -1,15 +1,14 @@
package pub.doric.engine.remote; package pub.doric.engine.remote;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JavaFunction; import com.github.pengfeizhou.jscore.JavaFunction;
import com.github.pengfeizhou.jscore.JavaValue; import com.github.pengfeizhou.jscore.JavaValue;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.EOFException; import java.io.EOFException;
@ -25,14 +24,10 @@ import okhttp3.Response;
import okhttp3.WebSocket; import okhttp3.WebSocket;
import okhttp3.WebSocketListener; import okhttp3.WebSocketListener;
import pub.doric.dev.event.QuitDebugEvent; import pub.doric.dev.event.QuitDebugEvent;
import pub.doric.utils.DoricUtils;
public class RemoteJSExecutor { public class RemoteJSExecutor {
private final WebSocket webSocket; private final WebSocket webSocket;
private final Map<String, JavaFunction> globalFunctions = new HashMap<>(); private final Map<String, JavaFunction> globalFunctions = new HashMap<>();
private JSDecoder temp; private JSDecoder temp;
public RemoteJSExecutor() { public RemoteJSExecutor() {
@ -67,55 +62,28 @@ public class RemoteJSExecutor {
@Override @Override
public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) { public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
JsonElement je = DoricUtils.gson.fromJson(text, JsonElement.class); try {
JSONObject jsonObject = new JSONObject(text);
if (je instanceof JsonObject) { String cmd = jsonObject.optString("cmd");
JsonObject jo = ((JsonObject) je);
String cmd = jo.get("cmd").getAsString();
switch (cmd) { switch (cmd) {
case "injectGlobalJSFunction": { case "injectGlobalJSFunction": {
String name = jo.get("name").getAsString(); String name = jsonObject.optString("name");
JsonArray arguments = jo.getAsJsonArray("arguments"); JSONArray arguments = jsonObject.optJSONArray("arguments");
JSDecoder[] decoders = new JSDecoder[arguments.size()]; assert arguments != null;
for (int i = 0; i < arguments.size(); i++) { JSDecoder[] decoders = new JSDecoder[arguments.length()];
if (arguments.get(i).isJsonPrimitive()) { for (int i = 0; i < arguments.length(); i++) {
JsonPrimitive jp = ((JsonPrimitive) arguments.get(i)); Object o = arguments.get(i);
if (jp.isNumber()) { decoders[i] = new JSDecoder(new ValueBuilder(o).build());
ValueBuilder vb = new ValueBuilder(jp.getAsNumber());
JSDecoder decoder = new JSDecoder(vb.build());
decoders[i] = decoder;
} else if (jp.isBoolean()) {
ValueBuilder vb = new ValueBuilder(jp.getAsBoolean());
JSDecoder decoder = new JSDecoder(vb.build());
decoders[i] = decoder;
} else if (jp.isString()) {
ValueBuilder vb = new ValueBuilder(jp.getAsString());
JSDecoder decoder = new JSDecoder(vb.build());
decoders[i] = decoder;
}
} else {
try {
ValueBuilder vb = new ValueBuilder(new JSONObject(DoricUtils.gson.toJson(arguments.get(i))));
JSDecoder decoder = new JSDecoder(vb.build());
decoders[i] = decoder;
} catch (Exception ex) {
ex.printStackTrace();
}
}
} }
globalFunctions.get(name).exec(decoders); globalFunctions.get(name).exec(decoders);
} }
break; break;
case "invokeMethod": { case "invokeMethod": {
try { try {
Object result = jo.get("result"); Object result = jsonObject.opt("result");
ValueBuilder vb = new ValueBuilder(result); ValueBuilder vb = new ValueBuilder(result);
JSDecoder decoder = new JSDecoder(vb.build()); temp = new JSDecoder(vb.build());
temp = decoder;
System.out.println(result); System.out.println(result);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -125,6 +93,8 @@ public class RemoteJSExecutor {
} }
break; break;
} }
} catch (JSONException e) {
e.printStackTrace();
} }
} }
}); });
@ -141,33 +111,29 @@ public class RemoteJSExecutor {
public void injectGlobalJSFunction(String name, JavaFunction javaFunction) { public void injectGlobalJSFunction(String name, JavaFunction javaFunction) {
globalFunctions.put(name, javaFunction); globalFunctions.put(name, javaFunction);
webSocket.send(new JSONBuilder().put("cmd", "injectGlobalJSFunction")
JsonObject jo = new JsonObject(); .put("name", name).toString()
jo.addProperty("cmd", "injectGlobalJSFunction"); );
jo.addProperty("name", name);
webSocket.send(DoricUtils.gson.toJson(jo));
} }
public void injectGlobalJSObject(String name, JavaValue javaValue) { public void injectGlobalJSObject(String name, JavaValue javaValue) {
} }
public JSDecoder invokeMethod(String objectName, String functionName, JavaValue[] javaValues, boolean hashKey) { public JSDecoder invokeMethod(String objectName, String functionName, JavaValue[] javaValues, boolean hashKey) {
JsonObject jo = new JsonObject(); JSONArray jsonArray = new JSONArray();
jo.addProperty("cmd", "invokeMethod");
jo.addProperty("objectName", objectName);
jo.addProperty("functionName", functionName);
JsonArray ja = new JsonArray();
for (JavaValue javaValue : javaValues) { for (JavaValue javaValue : javaValues) {
JsonObject argument = new JsonObject(); jsonArray.put(new JSONBuilder()
argument.addProperty("type", javaValue.getType()); .put("type", javaValue.getType())
argument.addProperty("value", javaValue.getValue()); .put("value", javaValue.getValue())
ja.add(argument); .toJSONObject());
} }
jo.add("javaValues", ja); webSocket.send(new JSONBuilder()
.put("cmd", "invokeMethod")
jo.addProperty("hashKey", hashKey); .put("objectName", objectName)
webSocket.send(DoricUtils.gson.toJson(jo)); .put("functionName", functionName)
.put("javaValues", jsonArray)
.put("hashKey", hashKey)
.toString());
LockSupport.park(Thread.currentThread()); LockSupport.park(Thread.currentThread());
return temp; return temp;