457 lines
13 KiB
TypeScript
Raw Normal View History

2019-03-15 10:08:39 +08:00
// eval 注入脚本的代码,变量尽量使用var,后来发现在import之后,let会自动变为var
2021-04-05 19:31:34 +08:00
import {
ArrayData,
BoolData,
2021-06-14 18:46:45 +08:00
ColorData,
DataType,
2021-06-14 19:41:58 +08:00
Group, ImageData,
2021-06-14 18:46:45 +08:00
Info,
2021-04-05 19:31:34 +08:00
NullOrUndefinedData,
2021-06-14 18:46:45 +08:00
NumberData,
ObjectData,
2021-04-05 19:31:34 +08:00
Property,
2021-06-14 18:46:45 +08:00
StringData,
TreeData,
2021-04-05 19:31:34 +08:00
Vec2Data,
Vec3Data
2021-04-22 19:09:35 +08:00
} from "./data";
2021-06-14 18:46:45 +08:00
import {Msg, Page, PluginEvent} from '@/core/types'
2021-04-22 19:09:35 +08:00
2021-06-14 18:46:45 +08:00
// @ts-ignore typescript-eslint/no-namespace
declare namespace cc {
export function v2(x: number, y: number): any;
}
2021-04-22 19:09:35 +08:00
2021-04-26 22:27:47 +08:00
class CCInspector {
2021-04-22 19:09:35 +08:00
inspectorGameMemoryStorage: Record<string, any> = {}
2019-03-18 12:05:07 +08:00
init() {
2021-04-26 22:27:47 +08:00
console.log('cc-inspector init ~~~');
2021-05-08 22:05:59 +08:00
let timer = setInterval(() => {
if (this._isCocosGame()) {
clearInterval(timer)
// @ts-ignore
cc.director.on(cc.Director.EVENT_AFTER_SCENE_LAUNCH, () => {
2021-05-11 21:26:36 +08:00
let isCocosGame = this._isCocosGame();
this.notifySupportGame(isCocosGame)
2021-05-08 22:05:59 +08:00
})
}
}, 300)
2021-04-28 13:59:16 +08:00
window.addEventListener("message", (event) => {
// 接受来自content的事件有可能也会受到其他插件的
2021-04-26 22:27:47 +08:00
if (!event || !event.data) {
return
}
let pluginEvent: PluginEvent = event.data;
2021-04-28 19:49:26 +08:00
if (PluginEvent.check(pluginEvent, Page.Content, Page.Inject)) {
2021-05-08 22:05:59 +08:00
console.log(`%c[Inject] ${JSON.stringify(pluginEvent)}`, 'color:green;');
2021-04-28 19:49:26 +08:00
PluginEvent.finish(pluginEvent)
2021-04-28 13:59:16 +08:00
switch (pluginEvent.msg) {
case Msg.UrlChange:
case Msg.Support: {
let isCocosGame = this._isCocosGame();
2021-04-28 19:49:26 +08:00
this.notifySupportGame(isCocosGame)
2021-04-28 13:59:16 +08:00
break;
}
case Msg.TreeInfo: {
2021-04-28 19:49:26 +08:00
this.updateTreeInfo();
2021-04-28 13:59:16 +08:00
break;
}
case Msg.NodeInfo: {
2021-04-28 19:49:26 +08:00
let nodeUUID = pluginEvent.data;
this.getNodeInfo(nodeUUID);
2021-04-28 13:59:16 +08:00
break;
}
case Msg.SetProperty: {
2021-05-08 17:52:29 +08:00
const data: Info = pluginEvent.data;
// path的设计有优化空间
const uuid = data.path[0];
const key = data.path[1];
2021-06-14 18:46:45 +08:00
let value = data.data;
if (data.type === DataType.Color) {
// @ts-ignore
value = cc.color().fromHEX(value);
}
2021-05-08 17:52:29 +08:00
if (uuid && key) {
this.setValue(uuid, key, value);
this.sendMsgToContent(Msg.UpdateProperty, data);
}
break;
2021-04-28 13:59:16 +08:00
}
2021-04-26 22:27:47 +08:00
}
2019-03-18 18:03:07 +08:00
}
2021-04-22 19:09:35 +08:00
});
}
2021-04-28 13:59:16 +08:00
sendMsgToContent(msg: Msg, data: any) {
// 发送给content.js处理也会导致发送给了自身死循环
2021-04-28 19:49:26 +08:00
window.postMessage(new PluginEvent(Page.Inject, Page.Content, msg, data), "*");
2021-04-28 13:59:16 +08:00
}
2021-04-28 19:49:26 +08:00
notifySupportGame(b: boolean) {
this.sendMsgToContent(Msg.Support, b);
2021-04-22 19:09:35 +08:00
}
2019-03-18 13:30:06 +08:00
updateTreeInfo() {
2021-04-22 19:09:35 +08:00
let isCocosCreatorGame = this._isCocosGame();
2019-03-18 12:05:07 +08:00
if (isCocosCreatorGame) {
2021-04-22 19:09:35 +08:00
//@ts-ignore
2019-03-18 12:05:07 +08:00
let scene = cc.director.getScene();
if (scene) {
2021-05-08 17:52:29 +08:00
let treeData = new TreeData();
this.getNodeChildren(scene, treeData)
this.sendMsgToContent(Msg.TreeInfo, treeData);
2019-03-18 12:05:07 +08:00
} else {
2021-04-28 13:59:16 +08:00
this.sendMsgToContent(Msg.Support, {support: false, msg: "未发现游戏场景,不支持调试游戏!"});
2019-03-18 12:05:07 +08:00
}
2021-04-28 19:49:26 +08:00
} else {
this.notifySupportGame(false)
2019-03-18 12:05:07 +08:00
}
2021-04-22 19:09:35 +08:00
}
2021-06-14 18:46:45 +08:00
// @ts-ignore
draw: cc.Graphics = null;
_drawRect(node: any) {
let draw = this.draw;
if (!draw) {
// @ts-ignore
let node = new cc.Node('draw-node');
// @ts-ignore
cc.director.getScene().addChild(node);
// @ts-ignore
draw = this.draw = node.addComponent(cc.Graphics);
}
draw.clear()
draw.lineWidth = 10;
// @ts-ignore
draw.strokeColor = new cc.Color().fromHEX('#ff0000')
const {anchorX, anchorY, width, height, x, y} = node;
let halfWidth = width / 2;
let halfHeight = height / 2;
let leftBottom = node.convertToWorldSpaceAR(cc.v2(-halfWidth, -halfHeight))
let leftTop = node.convertToWorldSpaceAR(cc.v2(-halfWidth, halfHeight));
let rightBottom = node.convertToWorldSpaceAR(cc.v2(halfWidth, -halfHeight));
let rightTop = node.convertToWorldSpaceAR(cc.v2(halfWidth, halfHeight));
function line(began: any, end: any) {
draw.moveTo(began.x, began.y);
draw.lineTo(end.x, end.y);
}
line(leftBottom, rightBottom)
line(rightBottom, rightTop)
line(rightTop, leftTop)
line(leftTop, leftBottom)
this.draw.stroke();
}
2021-04-05 19:31:34 +08:00
// 收集节点信息
2021-05-08 17:52:29 +08:00
getNodeChildren(node: any, data: TreeData) {
data.uuid = node.uuid;
data.name = node.name;
2021-05-11 22:15:29 +08:00
// @ts-ignore
if (node instanceof cc.Scene) {
// 场景不允许获取active引擎会报错
} else {
data.active = !!node.active;
}
2021-04-05 19:31:34 +08:00
this.inspectorGameMemoryStorage[node.uuid] = node;
let nodeChildren = node.getChildren();
for (let i = 0; i < nodeChildren.length; i++) {
let childItem = nodeChildren[i];
2021-05-08 17:52:29 +08:00
let treeData = new TreeData();
this.getNodeChildren(childItem, treeData);
data.children.push(treeData)
2021-04-05 19:31:34 +08:00
}
2021-04-22 19:09:35 +08:00
}
_isCocosGame() {
// @ts-ignore 检测是否包含cc变量
return typeof cc !== "undefined";
}
_getNodeKeys(node: any) {
2021-04-05 18:38:44 +08:00
let keys = [];
2021-04-07 17:26:16 +08:00
let excludeProperty = [
"children", "quat", "node",
// 生命周期函数
"onFocusInEditor", "onRestore", "start", "lateUpdate", "update", "resetInEditor", "onLostFocusInEditor",
2021-04-20 11:15:30 +08:00
"onEnable", "onDisable", "onDestroy", "onLoad",
2021-04-07 17:26:16 +08:00
];
2021-04-05 18:38:44 +08:00
for (let key in node) {
if (!key.startsWith("_") &&
2021-04-22 19:09:35 +08:00
!excludeProperty.includes(key) &&
typeof node[key] !== "function") {
2021-04-05 18:38:44 +08:00
keys.push(key);
}
}
return keys;
2021-04-22 19:09:35 +08:00
}
_getPairProperty(key: string) {
let pairProperty: Record<string, any> = {
2021-04-05 18:38:44 +08:00
rotation: ["rotationX", "rotationY"],
anchor: ["anchorX", "anchorY"],
size: ["width", "height"],
2021-05-08 18:44:10 +08:00
skew: ['skewX', 'skewY'],
2021-04-05 18:38:44 +08:00
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) {
2021-04-22 19:09:35 +08:00
if (pairProperty.hasOwnProperty(value)) {
let pair = pairProperty[value];
if (pair.includes(key)) {
return {key: value, values: pair};
}
2021-04-05 18:38:44 +08:00
}
}
return null;
2021-04-22 19:09:35 +08:00
}
2021-06-14 18:46:45 +08:00
_buildVecData(options: any) {
const ctor: Function = options.ctor;
const keys: Array<string> = options.keys;
const value: Object = options.value;
const data: Vec3Data | Vec2Data = options.data;
const path: Array<string> = options.path;
if (ctor && value instanceof ctor) {
let hasUnOwnProperty = keys.find(key => !value.hasOwnProperty(key))
if (!hasUnOwnProperty) {
for (let key in keys) {
let propName = keys[key];
if (value.hasOwnProperty(propName)) {
let propPath = path.concat(propName);
let itemData = this._genInfoData(value, propName, propPath)
if (itemData) {
data.add(new Property(propName, itemData))
}
}
}
return data;
}
}
return null;
}
2021-06-14 19:41:58 +08:00
_buildImageData(options: any) {
const ctor: Function = options.ctor;
const value: Object = options.value;
const data: ImageData = options.data;
const path: Array<string> = options.path;
if (ctor && value instanceof ctor) {
if (value.hasOwnProperty('_textureFilename')) {
data.path = path;
//@ts-ignore
data.data = `${window.location.origin}/${value._textureFilename}`;
return data;
}
}
return null;
}
2021-05-08 20:36:32 +08:00
_genInfoData(node: any, key: string, path: any) {
let propertyValue = node[key];
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-22 19:09:35 +08:00
//@ts-ignore
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) {
2021-06-14 18:46:45 +08:00
!info && (info = this._buildVecData({
// @ts-ignore
ctor: cc.Vec3,
path: path,
data: new Vec3Data(),
keys: ['x', 'y', 'z'],
value: propertyValue,
}))
!info && (info = this._buildVecData({
// @ts-ignore
ctor: cc.Vec2,
path: path,
data: new Vec2Data(),
keys: ['x', 'y'],
value: propertyValue
}))
2021-06-14 19:41:58 +08:00
!info && (info = this._buildImageData({
//@ts-ignore
ctor: cc.SpriteFrame,
data: new ImageData(),
path: path,
value: propertyValue,
}))
2021-06-14 18:46:45 +08:00
!info && (info = new ObjectData());
2021-04-05 18:38:44 +08:00
} else {
}
break;
}
2021-04-07 17:26:16 +08:00
if (info) {
2021-05-08 20:36:32 +08:00
info.readonly = this._isReadonly(node, key)
2021-04-07 17:26:16 +08:00
info.path = path;
} else {
2021-04-05 18:38:44 +08:00
console.error(`暂不支持的属性值`, propertyValue);
}
return info;
2021-04-22 19:09:35 +08:00
}
_getGroupData(node: any) {
2021-04-05 19:31:34 +08:00
let nodeGroup = new Group(node.constructor.name);
let keys = this._getNodeKeys(node);
2021-05-08 18:44:10 +08:00
for (let i = 0; i < keys.length;) {
2021-04-05 19:31:34 +08:00
let key = keys[i];
let propertyValue = node[key];
let pair = this._getPairProperty(key);
if (pair) {
2021-05-08 18:44:10 +08:00
let bSplice = false;
2021-04-05 19:31:34 +08:00
// 把这个成对的属性剔除掉
2021-04-22 19:09:35 +08:00
pair.values.forEach((item: string) => {
2021-04-05 19:31:34 +08:00
let index = keys.findIndex(el => el === item);
if (index !== -1) {
keys.splice(index, 1);
2021-05-08 18:44:10 +08:00
bSplice = true;
2021-04-05 19:31:34 +08:00
}
});
// 序列化成对的属性
2021-04-22 19:09:35 +08:00
let info: Vec2Data | Vec3Data | null = null;
2021-04-05 19:31:34 +08:00
let pairValues = pair.values;
if (pairValues.length === 2) {
info = new Vec2Data();
} else if (pairValues.length === 3) {
info = new Vec3Data();
}
2021-04-22 19:09:35 +08:00
pairValues.forEach((el: string) => {
2021-04-05 19:31:34 +08:00
if (el in node) {
2021-04-07 17:26:16 +08:00
let propertyPath = [node.uuid, el];
2021-05-08 20:36:32 +08:00
let vecData = this._genInfoData(node, el, propertyPath);
2021-04-05 19:31:34 +08:00
if (vecData) {
2021-04-22 19:09:35 +08:00
info && info.add(new Property(el, vecData));
2021-04-05 19:31:34 +08:00
}
} else {
console.warn(`属性异常,节点丢失属性: ${el},请检查 pairProperty的设置`);
}
});
if (info) {
let property = new Property(pair.key, info);
nodeGroup.addProperty(property);
}
2021-05-08 18:44:10 +08:00
if (!bSplice) {
i++;
}
2021-04-05 19:31:34 +08:00
} else {
2021-04-07 17:26:16 +08:00
let propertyPath = [node.uuid, key];
2021-05-08 20:36:32 +08:00
let info = this._genInfoData(node, key, propertyPath);
2021-04-05 19:31:34 +08:00
if (info) {
nodeGroup.addProperty(new Property(key, info));
}
2021-05-08 18:44:10 +08:00
i++;
2021-04-05 19:31:34 +08:00
}
}
2021-05-08 18:44:10 +08:00
nodeGroup.sort();
2021-04-05 19:31:34 +08:00
return nodeGroup;
2021-04-22 19:09:35 +08:00
}
2019-03-15 10:08:39 +08:00
// 获取节点信息
2021-04-22 19:09:35 +08:00
getNodeInfo(uuid: string) {
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-28 13:59:16 +08:00
this.sendMsgToContent(Msg.NodeInfo, groupData);
2019-03-15 10:08:39 +08:00
} else {
// 未获取到节点数据
console.log("未获取到节点数据");
}
2021-04-22 19:09:35 +08:00
}
logValue(uuid: string, key: string) {
2021-04-07 17:26:16 +08:00
let nodeOrComp = this.inspectorGameMemoryStorage[uuid];
if (nodeOrComp) {
console.log(nodeOrComp[key]);
}
2021-04-22 19:09:35 +08:00
}
2021-05-08 20:36:32 +08:00
_isReadonly(base: Object, key: string): boolean {
let obj = Object.getPrototypeOf(base);
if (obj) {
let ret = Object.getOwnPropertyDescriptor(obj, key)
if (ret && ret.set) {
return false;
} else {
return this._isReadonly(obj, key)
}
} else {
return true;
}
}
2021-04-22 19:09:35 +08:00
setValue(uuid: string, key: string, value: string) {
2021-04-07 17:26:16 +08:00
let nodeOrComp = this.inspectorGameMemoryStorage[uuid];
if (nodeOrComp && key in nodeOrComp) {
2021-05-08 20:36:32 +08:00
if (this._isReadonly(nodeOrComp, key)) {
console.warn(`值不允许修改`);
} else {
2021-05-08 17:52:29 +08:00
nodeOrComp[key] = value;
}
2021-04-07 17:26:16 +08:00
}
2021-04-22 19:09:35 +08:00
}
2019-03-18 18:03:07 +08:00
onMemoryInfo() {
2021-04-28 13:59:16 +08:00
this.sendMsgToContent(Msg.MemoryInfo, {
2019-03-18 18:03:07 +08:00
performance: {
2021-04-22 19:09:35 +08:00
// @ts-ignore
2019-03-18 18:03:07 +08:00
jsHeapSizeLimit: window.performance.memory.jsHeapSizeLimit,
2021-04-22 19:09:35 +08:00
// @ts-ignore
2019-03-18 18:03:07 +08:00
totalJSHeapSize: window.performance.memory.totalJSHeapSize,
2021-04-22 19:09:35 +08:00
// @ts-ignore
2019-03-18 18:03:07 +08:00
usedJSHeapSize: window.performance.memory.usedJSHeapSize,
},
console: {
jsHeapSizeLimit: console.memory.jsHeapSizeLimit,
totalJSHeapSize: console.memory.totalJSHeapSize,
usedJSHeapSize: console.memory.usedJSHeapSize,
},
});
}
2021-04-22 19:09:35 +08:00
}
let inspector = new CCInspector();
inspector.init();
2021-04-26 22:27:47 +08:00
//@ts-ignore
2021-04-22 19:09:35 +08:00
window.CCInspector = inspector;