Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完成ccc3.x适配 #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions cocos-creator/3X/CocosCreator3Dumper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import AbstractDumper from "./sdk/AbstractDumper"
import CCNode from "./CocosCreator3Node"
import { director, Node, View } from "cc"

export default class Dumper extends AbstractDumper {
getRoot() {
const scene = director.getScene()! as unknown as Node
const size = View.instance.getVisibleSize()
CCNode.screenHeight = size.height
CCNode.screenWidth = size.width
return new CCNode(scene)
}
}
116 changes: 116 additions & 0 deletions cocos-creator/3X/CocosCreator3Node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Node, UITransformComponent } from "cc"
import AbstractNode from "./sdk/AbstractNode"

export default class CCCNode implements AbstractNode {
node: Node
static screenWidth: number = 0
static screenHeight: number = 0
constructor(node: Node) {
this.node = node
}

getParent() {
let parent = this.node.parent
if (!parent) {
return null
}
return new CCCNode(parent)
}

getChildren() {
let children: CCCNode[] = []
let nodeChildren = this.node.children
for (const i of nodeChildren) {
children.push(new CCCNode(i))
}
return children
}

getAttr(attrName: string) {
if (attrName === "visible") {
return this.node.activeInHierarchy
} else if (attrName === "name") {
return this.node.name || "<no-name>"
} else if (attrName === "text") {
for (const component of this.node.components) {
if ("string" in component) {
return (component as any).string
}
}
return ""
} else if (attrName === "type") {
const componentSize = this.node.components.length
let ntype: string = ""
//一般第一个是UI组件,pass
switch (componentSize) {
case 0:
return ""
case 1:
ntype = this.node.components[0].name
break
default:
ntype = this.node.components[1].name
break
}
return ntype.replace(/\w+\./, "")
} else if (attrName === "pos") {
// 转换成归一化坐标系,原点左上角
let pos = this.node.worldPosition
return [pos.x / CCCNode.screenWidth, 1 - pos.y / CCCNode.screenHeight]
} else if (attrName === "size") {
// 转换成归一化坐标系
let size = this.node.getComponent(UITransformComponent)?.contentSize
if (!size) return [0, 0]
return [
size.width / CCCNode.screenWidth,
size.height / CCCNode.screenHeight,
]
} else if (attrName === "scale") {
return [this.node.scale.x, this.node.scale.y]
} else if (attrName === "anchorPoint") {
let ui = this.node.getComponent(UITransformComponent)
if (!ui) return [0, 0]
return [ui.anchorX, ui.anchorY]
} else if (attrName == "touchable") {
return true
} else if (attrName === "tag") {
return ""
} else if (attrName === "enabled") {
return true
} else if (attrName === "rotation") {
return this.node.rotation
}

return undefined
}

getAvailableAttributeNames() {
return [
"name",
"type",
"visible",
"pos",
"size",
"scale",
"anchorPoint",
"text",
"enabled",
"rotation",
]
}

setAttr() {}

enumerateAttrs() {
var ret: any = {}
var allAttrNames = this.getAvailableAttributeNames()
for (var i in allAttrNames) {
var attrName = allAttrNames[i]
var attrVal = this.getAttr(attrName)
if (attrVal !== undefined) {
ret[attrName] = attrVal
}
}
return ret
}
}
108 changes: 108 additions & 0 deletions cocos-creator/3X/Poco.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//@ts-nocheck
import Dumper from "./CocosCreator3Dumper"
const POCO_SDK_VERSION = "1.1.0"

type retInfo = {
id: string
jsonrpc: string
result: any
error: { message: string } | null
}

export default class PocoManager {
port: number
poco: Dumper
rpc_dispacher: any
constructor(port: number) {
this.port = port || 5003
this.poco = new Dumper()
this.rpc_dispacher = {
getSDKVersion: function () {
return POCO_SDK_VERSION
},
GetSDKVersion: function () {
return POCO_SDK_VERSION
}, // for the compatibility
dump: this.poco.dumpHierarchy,
Dump: this.poco.dumpHierarchy, // for the compatibility
test: function () {
return "test"
},
}
this.init_server()
}

handle_request(req: any) {
var ret: retInfo = {
id: req.id,
jsonrpc: req.jsonrpc,
result: null,
error: null,
}
var method = req.method
var func = this.rpc_dispacher[method]
if (!func) {
ret.error = {
message: 'No such rpc method "' + method + '", reqid: ' + req.id,
}
} else {
var params = req.params
try {
var result = func.apply(this.poco, params)
ret.result = result
} catch (error: any) {
ret.error = { message: error.stack }
}
}
console.log(ret)
return ret
}

init_server() {
console.log("try starting wss..")
var that = this
try {
if (typeof WebSocketServer == "undefined") {
console.error("WebSocketServer is not enabled!")
return
}

var s = new WebSocketServer()

s.listen(this.port, (err: any) => {
if (!err) console.log("server booted!")
})

s.onconnection = function (conn: any) {
console.log("Network onConnection...")
conn.ondata = function (data: any) {
console.log("Network onMessage...")
console.log(data)
try {
var req = JSON.parse(data)
var res = that.handle_request(req)
var sres = JSON.stringify(res)

conn.send(sres, (err: any) => {})
} catch (error: any) {
console.log(
"[Poco] error when handling rpc request. req=" +
data +
"\nerror message: " +
error.stack
)
}
}
conn.onclose = function () {
console.log("connection gone!")
}
}

s.onclose = function () {
console.log("server is closed!")
}
} catch (err: any) {
console.log(err.stack + "\n" + err.message)
}
}
}
46 changes: 46 additions & 0 deletions cocos-creator/3X/sdk/AbstractDumper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type AbstractNode from "./AbstractNode"
type dumpInfo = {
name: string
payload: any
children: dumpInfo[]
}
export default class AbstractDumper {
getRoot(): AbstractNode {
throw new Error("not impl")
}

dumpHierarchy(
node: AbstractNode|boolean,
onlyVisibleNode: boolean=true
): dumpInfo | null {
if (!node) {
return null
}
if (node===true){
node=this.getRoot()
}

var payload = node.enumerateAttrs()
payload['zOrders']={'local':0,'global':0}
var result: dumpInfo = {
name: payload["name"] || node.getAttr("name"),
payload,
children: [],
}
var nodeChildren = node.getChildren()
for (var i in nodeChildren) {
var child = nodeChildren[i]
if (
!onlyVisibleNode ||
payload["visible"] ||
child.getAttr("visible")
) {
result.children.push(
this.dumpHierarchy(child, onlyVisibleNode)!
)
}
}

return result
}
}
8 changes: 8 additions & 0 deletions cocos-creator/3X/sdk/AbstractNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default interface AbstractNode {
getParent(): AbstractNode | null
getChildren(): AbstractNode[]
getAttr(attrName: string): any
setAttr(): void
getAvailableAttributeNames(): string[]
enumerateAttrs(): { [key: string]: any }
}
16 changes: 16 additions & 0 deletions cocos-creator/3X/sdk/Attributor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export namespace Attributor{
function getAttr(node, attrName) {
let node_ = node
if (!node.__isPocoNodeWrapper__) {
node_ = node[0]
}
return node_.getAttr(attrName)
}
function setAttr (node, attrName, attrVal) {
let node_ = node
if (!node.__isPocoNodeWrapper__) {
node_ = node[0]
}
node_.setAttr(attrName, attrVal)
}
}
61 changes: 61 additions & 0 deletions cocos-creator/3X/sdk/DefaultMatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
interface IComparator {
compare(cond: any, node: any): boolean
}

class EqualizationComparator implements IComparator {
compare(cond: any, node: any): boolean {
return cond === node
}
}

class RegexpComparator implements IComparator {
compare(origin: any, pattern: any): boolean {
if (!origin || !pattern) {
return false
}
return origin.toString().match(pattern) !== null
}
}

export default class DefaultMatcher {
comparators: { [key: string]: IComparator } = {
"attr=": new EqualizationComparator(),
"attr.*=": new RegexpComparator(),
}
match(cond: any, node: any): boolean {
var op = cond[0]
var args = cond[1]

// 条件匹配
if (op === "and") {
for (var i in args) {
var arg = args[i]
if (!this.match(arg, node)) {
return false
}
}
return true
}

if (op === "or") {
for (var i in args) {
var arg = args[i]
if (this.match(arg, node)) {
return true
}
}
return false
}

// 属性匹配
var comparator: IComparator = this.comparators[op]
if (comparator) {
var attribute = args[0]
var value = args[1]
var targetValue = node.getAttr(attribute)
return comparator.compare(targetValue, value)
}

return false
}
}
Loading