217 lines
5.9 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 () {
2019-03-18 18:03:07 +08:00
// this.checkIsGamePage(true);
2019-03-18 12:05:07 +08:00
// if (this.stop) {
// } else {
// }
}.bind(this), 1000);
2019-03-18 13:30:06 +08:00
// 注册cc_after_render事件
2019-03-18 18:03:07 +08:00
window.addEventListener('message', function (event) {
if (event.data.msg === PluginMsg.Msg.UrlChange) {
this.checkIsGamePage(true);
}
}.bind(this));
2019-03-18 13:30:06 +08:00
},
updateTreeInfo() {
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);
2019-03-18 13:30:06 +08:00
this.sendMsgToDevTools(PluginMsg.Msg.ListInfo, this.postData);
2019-03-18 12:05:07 +08:00
} else {
this.postData.scene = null;
this.sendMsgToDevTools(PluginMsg.Msg.Support, {support: false, msg: "未发现游戏场景,不支持调试游戏!"});
}
}
},
checkIsGamePage(isLog) {
// 检测是否包含cc变量
let isCocosCreatorGame = true;
2019-03-18 13:30:06 +08:00
let msg = "支持调试游戏!";
2019-03-18 12:05:07 +08:00
try {
cc
} catch (e) {
isCocosCreatorGame = false;
2019-03-18 13:30:06 +08:00
msg = "不支持调试游戏!";
2019-03-18 12:05:07 +08:00
}
2019-03-18 13:30:06 +08:00
this.sendMsgToDevTools(PluginMsg.Msg.Support, {support: isCocosCreatorGame, msg: msg, log: isLog});
2019-03-18 12:05:07 +08:00
return isCocosCreatorGame;
},
testEval() {
console.log("hello devtools eval")
},
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];
2019-03-18 13:30:06 +08:00
this.inspectorGameMemoryStorage[itemComp.uuid] = itemComp;
2019-03-15 10:08:39 +08:00
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-18 18:03:07 +08:00
let node = this.inspectorGameMemoryStorage[uuid];
2019-03-15 10:08:39 +08:00
if (node) {
node.color = cc.hexToColor(colorHex);
}
2019-03-18 12:05:07 +08:00
},
pluginSetNodeRotation(uuid, rotation) {
2019-03-18 18:03:07 +08:00
let node = this.inspectorGameMemoryStorage[uuid];
2019-03-15 10:08:39 +08:00
if (node) {
node.rotation = rotation;
}
2019-03-18 12:05:07 +08:00
},
pluginSetNodePosition(uuid, x, y) {
2019-03-18 18:03:07 +08:00
let node = this.inspectorGameMemoryStorage[uuid];
2019-03-15 10:08:39 +08:00
if (node) {
node.x = x;
node.y = y;
}
2019-03-18 12:05:07 +08:00
},
pluginSetNodeSize(uuid, width, height) {
2019-03-18 18:03:07 +08:00
let node = this.inspectorGameMemoryStorage[uuid];
2019-03-15 10:08:39 +08:00
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-18 18:03:07 +08:00
let node = this.inspectorGameMemoryStorage[uuid];
2019-03-15 10:08:39 +08:00
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-18 13:30:06 +08:00
let node = this.inspectorGameMemoryStorage[uuid];
2019-03-15 10:08:39 +08:00
if (node) {
2019-03-18 13:30:06 +08:00
let nodeComp = this.getNodeComponentsInfo(node);
2019-03-15 10:08:39 +08:00
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;
}
2019-03-18 13:30:06 +08:00
this.sendMsgToDevTools(PluginMsg.Msg.NodeInfo, nodeData);
2019-03-15 10:08:39 +08:00
} 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: [],
};
2019-03-18 13:30:06 +08:00
this.inspectorGameMemoryStorage[node.uuid] = node;
2019-03-15 10:08:39 +08:00
let nodeChildren = node.getChildren();
for (let i = 0; i < nodeChildren.length; i++) {
let childItem = nodeChildren[i];
// console.log("childName: " + childItem.name);
2019-03-18 13:30:06 +08:00
this.getNodeChildren(childItem, nodeData.children);
2019-03-15 10:08:39 +08:00
}
data.push(nodeData);
2019-03-18 12:05:07 +08:00
},
sendMsgToDevTools(msg, data) {
window.postMessage({msg: msg, data: data}, "*");
},
2019-03-18 18:03:07 +08:00
onMemoryInfo() {
this.sendMsgToDevTools(PluginMsg.Msg.MemoryInfo, {
performance: {
jsHeapSizeLimit: window.performance.memory.jsHeapSizeLimit,
totalJSHeapSize: window.performance.memory.totalJSHeapSize,
usedJSHeapSize: window.performance.memory.usedJSHeapSize,
},
console: {
jsHeapSizeLimit: console.memory.jsHeapSizeLimit,
totalJSHeapSize: console.memory.totalJSHeapSize,
usedJSHeapSize: console.memory.usedJSHeapSize,
},
});
}
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();// 执行初始化函数