199 lines
5.3 KiB
JavaScript
Raw Normal View History

2019-03-15 10:08:39 +08:00
// eval 注入脚本的代码,变量尽量使用var,后来发现在import之后,let会自动变为var
2019-03-18 12:05:07 +08:00
const PluginMsg = require("../core/plugin-msg");
let cc_inspector = {
inspectorGameMemoryStorage: {},
msgType: {
2019-03-15 10:08:39 +08:00
nodeInfo: 2,//节点信息
nodeListInfo: 1,// 节点列表信息
notSupport: 0,// 不支持的游戏
2019-03-18 12:05:07 +08:00
},
postData: {
2019-03-15 10:08:39 +08:00
scene: {
name: "",
children: []
},
2019-03-18 12:05:07 +08:00
},
init() {
setInterval(function () {
// this.checkIsGamePage(false);
// if (this.stop) {
// } else {
// }
}.bind(this), 1000);
2019-03-15 10:08:39 +08:00
2019-03-18 12:05:07 +08:00
let isCocosCreatorGame = this.checkIsGamePage(true);
if (isCocosCreatorGame) {
let scene = cc.director.getScene();
if (scene) {
this.postData.scene = {
type: 1,// 标识类型
uuid: scene.uuid,
name: scene.name,
children: [],
};
this.inspectorGameMemoryStorage[scene.uuid] = scene;
let sceneChildren = scene.getChildren();
for (let i = 0; i < sceneChildren.length; i++) {
let node = sceneChildren[i];
this.getNodeChildren(node, this.postData.scene.children);
}
// console.log(postData);
this.sendMsgToDevTools(PluginMsg.Msg.ListInfo, {data: this.postData});
} else {
this.postData.scene = null;
this.sendMsgToDevTools(PluginMsg.Msg.Support, {support: false, msg: "未发现游戏场景,不支持调试游戏!"});
}
} else {
console.log("未发现cocos creator game");
}
},
checkIsGamePage(isLog) {
debugger
// 检测是否包含cc变量
let isCocosCreatorGame = true;
try {
cc
} catch (e) {
isCocosCreatorGame = false;
this.sendMsgToDevTools(PluginMsg.Msg.Support, {support: false, msg: "不支持调试游戏!",log:isLog});
}
return isCocosCreatorGame;
},
testEval() {
console.log("hello devtools eval")
},
testMsg1() {
window.postMessage("testMsg1")
},
testMsg2() {
debugger
chrome.runtime.connect({name: "inject"});
},
testMsg3() {
debugger
chrome.runtime.sendMessage("ffff");
},
2019-03-15 10:08:39 +08:00
// 收集组件信息
2019-03-18 12:05:07 +08:00
getNodeComponentsInfo(node) {
2019-03-15 10:08:39 +08:00
let ret = [];
let nodeComp = node._components;
for (let i = 0; i < nodeComp.length; i++) {
let itemComp = nodeComp[i];
window.inspectorGameMemoryStorage[itemComp.uuid] = itemComp;
ret.push({
uuid: itemComp.uuid,
type: itemComp.constructor.name,
name: itemComp.name,
});
}
return ret;
2019-03-18 12:05:07 +08:00
},
2019-03-15 10:08:39 +08:00
2019-03-18 12:05:07 +08:00
pluginSetNodeColor(uuid, colorHex) {
2019-03-15 10:08:39 +08:00
let node = window.inspectorGameMemoryStorage[uuid];
if (node) {
node.color = cc.hexToColor(colorHex);
}
2019-03-18 12:05:07 +08:00
},
pluginSetNodeRotation(uuid, rotation) {
2019-03-15 10:08:39 +08:00
let node = window.inspectorGameMemoryStorage[uuid];
if (node) {
node.rotation = rotation;
}
2019-03-18 12:05:07 +08:00
},
pluginSetNodePosition(uuid, x, y) {
2019-03-15 10:08:39 +08:00
let node = window.inspectorGameMemoryStorage[uuid];
if (node) {
node.x = x;
node.y = y;
}
2019-03-18 12:05:07 +08:00
},
pluginSetNodeSize(uuid, width, height) {
2019-03-15 10:08:39 +08:00
let node = window.inspectorGameMemoryStorage[uuid];
if (node) {
node.width = width;
node.height = height;
}
2019-03-18 12:05:07 +08:00
},
2019-03-15 10:08:39 +08:00
// 设置节点是否可视
2019-03-18 12:05:07 +08:00
pluginSetNodeActive(uuid, isActive) {
2019-03-15 10:08:39 +08:00
let node = window.inspectorGameMemoryStorage[uuid];
if (node) {
if (isActive === 1) {
node.active = true;
} else if (isActive === 0) {
node.active = false;
}
}
2019-03-18 12:05:07 +08:00
},
2019-03-15 10:08:39 +08:00
// 获取节点信息
2019-03-18 12:05:07 +08:00
getNodeInfo(uuid) {
2019-03-15 10:08:39 +08:00
let node = window.inspectorGameMemoryStorage[uuid];
if (node) {
let nodeComp = getNodeComponentsInfo(node);
let nodeData = {
type: node.constructor.name,
uuid: node.uuid,
name: node.name,
x: node.x,
y: node.y,
zIndex: node.zIndex,
childrenCount: node.childrenCount,
children: [],
width: node.width,
height: node.height,
color: node.color.toCSS(),
opacity: node.opacity,
rotation: node.rotation,
rotationX: node.rotationX,
rotationY: node.rotationY,
anchorX: node.anchorX,
anchorY: node.anchorY,
scaleX: node.scaleX,
scaleY: node.scaleY,
skewX: node.skewX,
skewY: node.skewY,
components: nodeComp
};
let nodeType = node.constructor.name;
if (nodeType === 'cc_Scene') {
} else {
nodeData.active = node.active;
}
window.sendMsgToDevTools(msgType.nodeInfo, nodeData);
} else {
// 未获取到节点数据
console.log("未获取到节点数据");
}
2019-03-18 12:05:07 +08:00
},
2019-03-15 10:08:39 +08:00
// 收集节点信息
2019-03-18 12:05:07 +08:00
getNodeChildren(node, data) {
2019-03-15 10:08:39 +08:00
// console.log("nodeName: " + node.name);
let nodeData = {
uuid: node.uuid,
name: node.name,
children: [],
};
window.inspectorGameMemoryStorage[node.uuid] = node;
let nodeChildren = node.getChildren();
for (let i = 0; i < nodeChildren.length; i++) {
let childItem = nodeChildren[i];
// console.log("childName: " + childItem.name);
getNodeChildren(childItem, nodeData.children);
}
data.push(nodeData);
2019-03-18 12:05:07 +08:00
},
sendMsgToDevTools(msg, data) {
window.postMessage({msg: msg, data: data}, "*");
},
2019-03-15 10:08:39 +08:00
}
2019-03-18 12:05:07 +08:00
window.ccinspector = window.ccinspector || cc_inspector;
window.ccinspector.init && window.ccinspector.init();// 执行初始化函数