Skip to content

Commit

Permalink
update unity sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
adolli committed Nov 8, 2017
1 parent 9069cf5 commit 8429f31
Show file tree
Hide file tree
Showing 20 changed files with 1,014 additions and 12 deletions.
36 changes: 34 additions & 2 deletions Unity3D/PocoManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Diagnostics;
using System;
using MiniJSON;
using TcpServer;
using Poco;

using Debug = UnityEngine.Debug;

public class PocoManager : MonoBehaviour
{
public int port = 5001;
private bool mRunning;
public AsyncTcpServer server = null;
private RPCParser rpc = null;
Expand All @@ -21,17 +25,25 @@ public class PocoManager : MonoBehaviour
private List<TcpClientState> inbox = new List<TcpClientState> ();
private object Lock = new object ();

private Dictionary<string, long> debugProfilingData = new Dictionary<string, long>() {
{"dump", 0},
{"handleRpcRequest", 0},
{"packRpcResponse", 0},
{"sendRpcResponse", 0},
};

void Awake ()
{
prot = new SimpleProtocolFilter ();
rpc = new RPCParser ();
rpc.addRpcMethod ("Add", Add);
rpc.addRpcMethod ("Screen", Screen);
rpc.addRpcMethod ("Dump", Dump);
rpc.addRpcMethod ("GetDebugProfilingData", GetDebugProfilingData);

mRunning = true;

server = new AsyncTcpServer (5001);
server = new AsyncTcpServer (port);
server.Encoding = Encoding.UTF8;
server.DatagramReceived +=
new EventHandler<TcpDatagramReceivedEventArgs<byte[]>> (server_Received);
Expand All @@ -57,7 +69,12 @@ static object Add (List<object> param)

private object Dump (List<object> param)
{
return dumper.dumpHierarchy ();
var sw = new Stopwatch ();
sw.Start ();
var h = dumper.dumpHierarchy ();
var t1 = sw.ElapsedMilliseconds;
debugProfilingData["dump"] = t1;
return h;
}

static object Screen (List<object> param)
Expand All @@ -72,6 +89,11 @@ public void stopListening ()
server.Stop ();
}

private object GetDebugProfilingData (List<object> param)
{
return debugProfilingData;
}

void Update ()
{
List<TcpClientState> toProcess;
Expand All @@ -83,9 +105,19 @@ void Update ()
foreach (TcpClientState client in toProcess) {
List<string> msgs = client.Prot.swap_msgs ();
msgs.ForEach (delegate(string msg) {
var sw = new Stopwatch ();
sw.Start ();
var t0 = sw.ElapsedMilliseconds;
string response = rpc.HandleMessage (msg);
var t1 = sw.ElapsedMilliseconds;
byte[] bytes = prot.pack (response);
var t2 = sw.ElapsedMilliseconds;
server.Send (client.TcpClient, bytes);
var t3 = sw.ElapsedMilliseconds;
debugProfilingData["handleRpcRequest"] = t1 - t0;
debugProfilingData["packRpcResponse"] = t2 - t1;
debugProfilingData["sendRpcResponse"] = t3 - t2;
Debug.Log (debugProfilingData);
});
}
}
Expand Down
15 changes: 8 additions & 7 deletions Unity3D/UnityDumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@ public override AbstractNode getRoot ()
{
return new RootNode ();
}

public new List<float> getPortSize ()
{
return new List<float>{ (float)Screen.width, (float)Screen.height };
}
}

public class RootNode: AbstractNode
{
public override List<AbstractNode> getChildren () //<Modified>
private List<AbstractNode> children = null;

public RootNode()
{
List<AbstractNode> children = new List<AbstractNode> ();
children = new List<AbstractNode> ();
foreach (GameObject obj in Transform.FindObjectsOfType(typeof(GameObject))) {
if (obj.transform.parent == null) {
children.Add (new UnityNode (obj));
}
}
}

public override List<AbstractNode> getChildren () //<Modified>
{
return children;
}
}
Expand Down
40 changes: 37 additions & 3 deletions Unity3D/ugui/UnityNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ namespace Poco
{
public class UnityNode: AbstractNode
{
public static Dictionary<string, string> TypeNames = new Dictionary<string, string> () {
{"Text", "Text"},
{"Gradient Text", "Gradient.Text"},
{"Image", "Image"},
{"Raw Image", "Raw.Image"},
{"Mask", "Mask"},
{"2D Rect Mask", "2D-Rect.Mask"},
{"Button", "Button"},
{"Input Field", "InputField"},
{"Toggle", "Toggle"},
{"Toggle Group", "ToggleGroup"},
{"Slider", "Slider"},
{"ScrollBar", "ScrollBar"},
{"DropDown", "DropDown"},
{"Scroll Rect", "ScrollRect"},
{"Selectable", "Selectable"},
{"Camera", "Camera"},
{"RectTransform", "Node"},
};
public static string DefaultTypeName = "GameObject";

private GameObject gameObject;

public UnityNode (GameObject obj)
Expand All @@ -34,14 +55,15 @@ public override object getAttr (string attrName)
{
Renderer renderer = gameObject.GetComponent<Renderer> ();
RectTransform rectTransform = gameObject.GetComponent<RectTransform> ();

Rect rect = GameObjectRect (renderer, rectTransform);
Vector2 objectPos = renderer ? WorldToGUIPoint (renderer.bounds.center) : Vector2.zero;
List<string> components = GameObjectAllComponents ();
switch (attrName) {
case "name":
return gameObject.name;
case "type":
return gameObject.GetType ().Name;
return GuessObjectTypeFromComponentNames(components);
case "visible":
return GameObjectVisible (renderer, components);
case "pos":
Expand Down Expand Up @@ -74,11 +96,11 @@ public override Dictionary<string, object> enumerateAttrs ()
List<string> components = GameObjectAllComponents ();
Dictionary<string, object> payload = new Dictionary<string, object> () {
{ "name", gameObject.name },
{ "type", gameObject.GetType ().Name },
{ "type", GuessObjectTypeFromComponentNames(components) },
{ "visible", GameObjectVisible (renderer, components) },
{ "pos", GameObjectPosInScreen (objectPos, renderer, rectTransform, rect) },
{ "size", GameObjectSizeInScreen (rect) },
{ "scale", new List<float> (){ 1.0f, 1.0f } },
{ "scale", new float[] { 1.0f, 1.0f } },
{ "anchorPoint", GameObjectAnchorInScreen (renderer, rect, objectPos) },
{ "zOrders", GameObjectzOrders () },
{ "clickable", GameObjectClickable (components) },
Expand All @@ -88,6 +110,18 @@ public override Dictionary<string, object> enumerateAttrs ()
return payload;
}

public string GuessObjectTypeFromComponentNames (List<string> componentNames)
{
var cns = new List<string> (componentNames);
cns.Reverse ();
foreach (string name in cns) {
if (TypeNames.ContainsKey(name)) {
return TypeNames[name];
}
}
return DefaultTypeName;
}

public bool GameObjectVisible (Renderer renderer, List<string> components)
{
if (gameObject.activeInHierarchy) {
Expand Down
6 changes: 6 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

# this script is introduced to collect sdk source file from each engine implementation
# so that you don't need to copy the folder manually.
cp -r cocos2dx-js/sdk/ sdk/js/
cp -r Unity3D/sdk/ sdk/csharp/
16 changes: 16 additions & 0 deletions cocos2dx-js/Cocos2dxDumper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// var cc = cc

var AbstractDumper = hunter.require('support.poco.sdk.AbstractDumper')
var Node = hunter.require('support.poco.cocos2dx.Node')

var Dumper = function () {
AbstractDumper.call(this)
}
Dumper.prototype = Object.create(AbstractDumper.prototype)

Dumper.prototype.getRoot = function () {
var winSize = cc.director.getWinSize()
return new Node(cc.director.getScene(), winSize.width, winSize.height)
}

module.exports = Dumper;
150 changes: 150 additions & 0 deletions cocos2dx-js/Cocos2dxNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// var cc = cc

var AbstractNode = hunter.require('support.poco.sdk.AbstractNode')

var Node = function (node, screenWidth, screenHeight) {
AbstractNode.call(this)
this.node = node
this.screenWidth = screenWidth
this.screenHeight = screenHeight
}
Node.prototype = Object.create(AbstractNode.prototype)

Node.prototype.getParent = function () {
var parent = this.node.getParent()
if (!parent) {
return null
}
return new Node(parent, this.screenWidth, this.screenHeight)
}

Node.prototype.getChildren = function () {
var children = null
var nodeChildren = this.node.getChildren()
if (nodeChildren) {
children = []
for (var i in nodeChildren) {
var child = nodeChildren[i]
children.push(new Node(child, this.screenWidth, this.screenHeight))
}
}
return children
}

Node.prototype.getAttr = function (attrName) {
if (attrName === 'visible') {
if (this.node.isVisible) {
var visible = this.node.isVisible()
if (!visible) {
return false
}

// if the node is visible, check its parent's visibility
// 举一个很简单的例子:
// 有两个layer,一个layer中有个button,这个button点下去后把layer的visible设为false,
// 这时候这个button的visible仍然是true的,如果这时候判断这个button是否存在或可见,
// 则需要判断他的所有父节点是否可见了
var parent = this.node.getParent()
while (parent) {
var parentVisible = parent.isVisible()
if (!parentVisible) {
return false
}
parent = parent.getParent()
}
return true
} else {
return this.node._activeInHierarchy
}
}
else if (attrName === 'name') {
return this.node.getName() || '<no-name>'
}
else if (attrName === 'text') {
for (var i in this.node._components) {
var c = this.node._components[i]
if (c.string !== undefined) {
return c.string
}
}
}
else if (attrName === 'type') {
var ntype = ''
if (this.node._components) {
for (var i = this.node._components.length - 1; i >= 0; i--) {
ntype = this.node._components[i].__classname__
if (ntype.startsWith('cc')) {
break
}
}
} else {
ntype = this.node.__classname__
}
return ntype.replace(/\w+\./, '')
}
else if (attrName === 'pos') {
// 转换成归一化坐标系,原点左上角
var pos = this.node.convertToWorldSpaceAR(new cc.Vec2(0, 0))
pos.x /= this.screenWidth
pos.y /= this.screenHeight
pos.y = 1 - pos.y
return [pos.x, pos.y]
}
else if (attrName === 'size') {
// 转换成归一化坐标系
var size = new cc.Size(this.node.getContentSize())
size.width /= this.screenWidth
size.height /= this.screenHeight
return [size.width, size.height]
}
else if (attrName === 'scale') {
return [this.node.getScaleX(), this.node.getScaleY()]
}
else if (attrName === 'anchorPoint') {
var anchor = this.node.getAnchorPoint()
return [anchor.x, 1 - anchor.y]
}
else if (attrName === 'zOrders') {
return {
local: this.node.getLocalZOrder(),
global: this.node.getGlobalZOrder(),
}
}
else if (attrName == 'touchable') {
if (this.node.isTouchEnabled) {
return this.node.isTouchEnabled()
}
}
else if (attrName === 'tag') {
return this.node.getTag()
}
else if (attrName === 'enabled') {
if (this.node.isEnabled) {
return this.node.isEnabled()
}
}
else if (attrName === 'rotation') {
return this.node.getRotation()
}

return undefined
}

Node.prototype.setAttr = function (attrName, val) {
// raise UnableToSetAttributeException(attrName, self.node)
}

Node.prototype.getAvailableAttributeNames = function () {
// enumerate all available attributes' name of this node
// :rettype: Iterable<string>

return AbstractNode.prototype.getAvailableAttributeNames.call(this).concat([
'text',
'touchable',
'enabled',
'tag',
'rotation',
])
}

module.exports = Node;
Loading

0 comments on commit 8429f31

Please sign in to comment.