269 lines
7.8 KiB
JavaScript
Raw Normal View History

2019-03-15 10:08:39 +08:00
// eval 注入脚本的代码,变量尽量使用var,后来发现在import之后,let会自动变为var
2021-04-03 11:42:08 +08:00
const PluginMsg = require("./core/plugin-msg");
2021-04-05 19:31:34 +08:00
import {
ArrayData,
BoolData,
ColorData,
Group,
NullOrUndefinedData,
NumberData, ObjectData,
Property,
StringData,
Vec2Data,
Vec3Data
} from "./devtools/data";
2019-03-18 12:05:07 +08:00
let cc_inspector = {
inspectorGameMemoryStorage: {},
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事件
2021-04-05 18:38:44 +08:00
window.addEventListener("message", function (event) {
2019-03-18 18:03:07 +08:00
if (event.data.msg === PluginMsg.Msg.UrlChange) {
2021-04-05 18:38:44 +08:00
let isCocosGame = this.checkIsGamePage();
2019-03-18 18:03:07 +08:00
}
}.bind(this));
2019-03-18 13:30:06 +08:00
},
updateTreeInfo() {
2021-04-05 18:38:44 +08:00
let isCocosCreatorGame = this.checkIsGamePage();
2019-03-18 12:05:07 +08:00
if (isCocosCreatorGame) {
let scene = cc.director.getScene();
if (scene) {
2021-04-05 18:38:44 +08:00
let sendData = {
2019-03-18 12:05:07 +08:00
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];
2021-04-05 18:38:44 +08:00
this.getNodeChildren(node, sendData.children);
2019-03-18 12:05:07 +08:00
}
2021-04-05 18:38:44 +08:00
this.sendMsgToDevTools(PluginMsg.Msg.ListInfo, sendData);
2019-03-18 12:05:07 +08:00
} else {
this.sendMsgToDevTools(PluginMsg.Msg.Support, {support: false, msg: "未发现游戏场景,不支持调试游戏!"});
}
}
},
2021-04-05 19:31:34 +08:00
// 收集节点信息
getNodeChildren(node, data) {
let nodeData = {
uuid: node.uuid,
name: node.name,
children: [],
};
this.inspectorGameMemoryStorage[node.uuid] = node;
let nodeChildren = node.getChildren();
for (let i = 0; i < nodeChildren.length; i++) {
let childItem = nodeChildren[i];
this.getNodeChildren(childItem, nodeData.children);
}
data.push(nodeData);
},
2021-04-05 18:38:44 +08:00
// 检测是否包含cc变量
checkIsGamePage() {
let isCocosGame = typeof cc !== "undefined";
this.sendMsgToDevTools(PluginMsg.Msg.Support, {support: isCocosGame});
return isCocosGame;
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
},
2021-04-05 18:38:44 +08:00
_getNodeKeys(node) {
let keys = [];
2021-04-07 17:26:16 +08:00
let excludeProperty = [
"children", "quat", "node",
// 生命周期函数
"onFocusInEditor", "onRestore", "start", "lateUpdate", "update", "resetInEditor", "onLostFocusInEditor",
];
2021-04-05 18:38:44 +08:00
for (let key in node) {
if (!key.startsWith("_") &&
!excludeProperty.includes(key) &&
typeof node[key] !== "function") {
keys.push(key);
}
}
return keys;
},
_getPairProperty(key) {
let pairProperty = {
rotation: ["rotationX", "rotationY"],
anchor: ["anchorX", "anchorY"],
size: ["width", "height"],
position: ["x", "y", "z"],
scale: ["scaleX", "scaleY", "scaleZ"],
2021-04-05 19:31:34 +08:00
designResolution: ["width", "height"], // 这个比较特殊在key下边其他的都不是在key下
2021-04-05 18:38:44 +08:00
};
for (let value in pairProperty) {
let pair = pairProperty[value];
if (pair.includes(key)) {
return {key: value, values: pair};
}
}
return null;
},
2021-04-07 17:26:16 +08:00
_genInfoData(propertyValue, path) {
2021-04-05 18:38:44 +08:00
let info = null;
switch (typeof propertyValue) {
case "boolean":
info = new BoolData(propertyValue);
break;
case "number":
info = new NumberData(propertyValue);
break;
case "string":
info = new StringData(propertyValue);
break;
default:
2021-04-05 19:31:34 +08:00
if (propertyValue == null || typeof propertyValue === "undefined") {
info = new NullOrUndefinedData();
2021-04-07 17:26:16 +08:00
} else if (propertyValue instanceof cc.Color) {
let hex = propertyValue.toHEX();
info = new ColorData(`#${hex}`);
2021-04-05 19:31:34 +08:00
} else if (Array.isArray(propertyValue)) {
info = new ArrayData();
} else if (propertyValue instanceof Object) {
info = new ObjectData();
2021-04-05 18:38:44 +08:00
} else {
}
break;
}
2021-04-07 17:26:16 +08:00
if (info) {
info.path = path;
} else {
2021-04-05 18:38:44 +08:00
console.error(`暂不支持的属性值`, propertyValue);
2021-04-07 17:26:16 +08:00
2021-04-05 18:38:44 +08:00
}
return info;
},
2021-04-05 19:31:34 +08:00
_getGroupData(node) {
let nodeGroup = new Group(node.constructor.name);
let keys = this._getNodeKeys(node);
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
let propertyValue = node[key];
let pair = this._getPairProperty(key);
if (pair) {
// 把这个成对的属性剔除掉
pair.values.forEach(item => {
let index = keys.findIndex(el => el === item);
if (index !== -1) {
keys.splice(index, 1);
}
});
// 序列化成对的属性
let info = null;
let pairValues = pair.values;
if (pairValues.length === 2) {
info = new Vec2Data();
} else if (pairValues.length === 3) {
info = new Vec3Data();
}
pairValues.forEach(el => {
if (el in node) {
2021-04-07 17:26:16 +08:00
let propertyPath = [node.uuid, el];
let vecData = this._genInfoData(node[el], propertyPath);
2021-04-05 19:31:34 +08:00
if (vecData) {
info.add(new Property(el, vecData));
}
} else {
console.warn(`属性异常,节点丢失属性: ${el},请检查 pairProperty的设置`);
}
});
if (info) {
let property = new Property(pair.key, info);
nodeGroup.addProperty(property);
}
} else {
2021-04-07 17:26:16 +08:00
let propertyPath = [node.uuid, key];
let info = this._genInfoData(propertyValue, propertyPath);
2021-04-05 19:31:34 +08:00
if (info) {
nodeGroup.addProperty(new Property(key, info));
}
}
}
return nodeGroup;
},
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) {
2021-04-05 19:31:34 +08:00
let groupData = [];
// 收集节点信息
let nodeGroup = this._getGroupData(node);
groupData.push(nodeGroup);
// 收集组件信息
let nodeComp = node._components;
for (let i = 0; i < nodeComp.length; i++) {
let itemComp = nodeComp[i];
this.inspectorGameMemoryStorage[itemComp.uuid] = itemComp;
let compGroup = this._getGroupData(itemComp);
groupData.push(compGroup);
2021-04-05 18:38:44 +08:00
}
2021-04-05 19:31:34 +08:00
this.sendMsgToDevTools(PluginMsg.Msg.NodeInfo, groupData);
2019-03-15 10:08:39 +08:00
} else {
// 未获取到节点数据
console.log("未获取到节点数据");
}
2019-03-18 12:05:07 +08:00
},
2021-04-07 17:26:16 +08:00
logValue(uuid, key) {
let nodeOrComp = this.inspectorGameMemoryStorage[uuid];
if (nodeOrComp) {
console.log(nodeOrComp[key]);
}
},
setValue(uuid, key, value) {
let nodeOrComp = this.inspectorGameMemoryStorage[uuid];
if (nodeOrComp && key in nodeOrComp) {
nodeOrComp[key] = value;
}
},
2019-03-18 12:05:07 +08:00
sendMsgToDevTools(msg, data) {
2021-04-05 18:38:44 +08:00
// 发送给content.js处理
2019-03-18 12:05:07 +08:00
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,
},
});
}
2021-04-05 18:38:44 +08:00
};
2021-04-07 17:26:16 +08:00
window.addEventListener("message", (a, b, c) => {
console.log(a, b, c);
});
2019-03-18 12:05:07 +08:00
window.ccinspector = window.ccinspector || cc_inspector;
window.ccinspector.init && window.ccinspector.init();// 执行初始化函数