687 lines
20 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,
2021-11-06 20:19:21 +08:00
DataType,
EngineData,
Group,
ImageData,
2021-06-14 18:46:45 +08:00
Info,
2021-11-20 21:57:54 +08:00
InvalidData, NodeInfoData,
2021-06-14 18:46:45 +08:00
NumberData,
2021-11-06 20:19:21 +08:00
ObjectData, ObjectItemRequestData,
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-11-04 22:22:11 +08:00
} from "@/devtools/data";
import {Msg, Page, PluginEvent} from "@/core/types"
2021-11-06 20:19:21 +08:00
import {BuildArrayOptions, BuildImageOptions, BuildObjectOptions, BuildVecOptions} from "@/inject/types";
2021-11-07 23:03:06 +08:00
// @ts-ignore
import {uniq} from "lodash"
2021-11-09 15:24:10 +08:00
import {trySetValueWithConfig, getValue} from "@/inject/setValue";
2021-11-12 21:20:57 +08:00
import {isHasProperty} from "@/inject/util";
2021-11-04 22:22:11 +08:00
2021-11-06 20:19:21 +08:00
declare const cc: 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
2021-11-18 22:49:11 +08:00
private watchIsCocosGame() {
const timer = setInterval(() => {
2021-05-08 22:05:59 +08:00
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-11-18 22:49:11 +08:00
}
init() {
console.log("cc-inspector init ~~~");
this.watchIsCocosGame();
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)) {
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.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;
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-11-09 15:24:10 +08:00
if (this.setValue(data.path, value)) {
this.sendMsgToContent(Msg.UpdateProperty, data);
} else {
console.warn(`设置失败:${data.path}`)
}
2021-05-08 17:52:29 +08:00
break;
2021-04-28 13:59:16 +08:00
}
2021-11-06 20:19:21 +08:00
case Msg.GetObjectItemData: {
const data: ObjectData = pluginEvent.data;
2021-11-09 15:24:10 +08:00
let val = getValue(this.inspectorGameMemoryStorage, data.path);
2021-11-06 20:19:21 +08:00
if (val) {
let itemData: Property[] = this._buildObjectItemData({
data: data,
path: data.path,
value: val,
filterKey: false,
});
let result: ObjectItemRequestData = {
id: data.id,
data: itemData,
}
this.sendMsgToContent(Msg.GetObjectItemData, result);
}
break;
}
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-11-21 21:36:16 +08:00
console.warn("can't execute api : cc.director.getScene")
this.notifySupportGame(false);
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");
2021-06-14 18:46:45 +08:00
// @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")
2021-06-14 18:46:45 +08:00
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;
2021-11-07 18:31:49 +08:00
let nodeChildren = node.children;
2021-04-05 19:31:34 +08:00
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";
}
2021-11-07 23:03:06 +08:00
getAllPropertyDescriptors(obj: Object): string[] {
let keys: string[] = [];
function circle(root: Object) {
const descriptors = Object.getOwnPropertyDescriptors(root);
for (let descriptorsKey in descriptors) {
if (Object.hasOwnProperty.call(descriptors, descriptorsKey)) {
const value = descriptors[descriptorsKey];
// 不可枚举的属性并且允许修改get set的才有效
if (!value.enumerable && value.configurable) {
keys.push(descriptorsKey);
}
}
}
const proto = Object.getPrototypeOf(root);
if (proto) {
circle(proto)
}
}
circle(obj)
return keys;
}
2021-04-22 19:09:35 +08:00
_getNodeKeys(node: any) {
2021-11-07 23:03:06 +08:00
// 3.x变成了getter
2021-04-07 17:26:16 +08:00
let excludeProperty = [
2021-11-07 23:03:06 +08:00
"children", "quat", "node", "components", "parent",
2021-04-07 17:26:16 +08:00
// 生命周期函数
"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-11-07 23:03:06 +08:00
const keyHidden = this.getAllPropertyDescriptors(node);
const keyVisible1 = Object.keys(node); // Object不走原型链
let keyVisible2: string[] = [];
for (let nodeKey in node) {// 走原型链
2021-11-07 23:04:43 +08:00
keyVisible2.push(nodeKey)
2021-04-05 18:38:44 +08:00
}
2021-11-07 23:03:06 +08:00
let allKeys: string[] = uniq(keyHidden.concat(keyVisible1, keyVisible2)).sort();
allKeys = allKeys.filter(key => {
return !key.startsWith("_") && !excludeProperty.includes(key);
});
allKeys = allKeys.filter(key => {
try {
return typeof node[key] !== "function"
} catch (e) {
// console.warn(`属性${key}出现异常:\n`, e);
return false;
}
})
return allKeys;
2021-04-22 19:09:35 +08:00
}
2021-11-10 22:04:51 +08:00
_getPairProperty(key: string): null | { key: string, values: string[] } {
2021-04-22 19:09:35 +08:00
let pairProperty: Record<string, any> = {
2021-04-05 18:38:44 +08:00
rotation: ["rotationX", "rotationY"],
anchor: ["anchorX", "anchorY"],
size: ["width", "height"],
skew: ["skewX", "skewY"],
2021-11-10 22:04:51 +08:00
position: ["x", "y", "z"], // position比较特殊过来的key就是position也需要能处理
2021-04-05 18:38:44 +08:00
scale: ["scaleX", "scaleY", "scaleZ"],
};
2021-11-10 22:04:51 +08:00
for (let pairPropertyKey in pairProperty) {
if (pairProperty.hasOwnProperty(pairPropertyKey)) {
let pair = pairProperty[pairPropertyKey];
if (pair.includes(key) || key === pairPropertyKey) {
return {key: pairPropertyKey, values: pair};
2021-04-22 19:09:35 +08:00
}
2021-04-05 18:38:44 +08:00
}
}
return null;
2021-04-22 19:09:35 +08:00
}
2021-11-04 22:22:11 +08:00
_buildVecData(options: BuildVecOptions) {
2021-06-14 18:46:45 +08:00
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-11-04 22:22:11 +08:00
_buildImageData(options: BuildImageOptions) {
2021-06-14 19:41:58 +08:00
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) {
2021-11-07 18:31:49 +08:00
data.path = path;
2021-11-09 15:24:10 +08:00
// 2.4.6 没有了这个属性
if (value.hasOwnProperty("_textureFilename")) {
2021-06-14 19:41:58 +08:00
//@ts-ignore
data.data = `${window.location.origin}/${value._textureFilename}`;
2021-11-07 18:31:49 +08:00
} else {
data.data = null;
2021-06-14 19:41:58 +08:00
}
2021-11-07 18:31:49 +08:00
return data;
2021-06-14 19:41:58 +08:00
}
return null;
}
2021-11-09 15:24:10 +08:00
_genInfoData(node: any, key: string | number, path: Array<string>, filterKey = true): Info | null {
2021-05-08 20:36:32 +08:00
let propertyValue = node[key];
2021-04-05 18:38:44 +08:00
let info = null;
2021-11-07 00:09:02 +08:00
let invalidType = this._isInvalidValue(propertyValue);
if (invalidType) {
info = new InvalidData(invalidType);
} else {
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-22 19:09:35 +08:00
//@ts-ignore
2021-11-07 00:09:02 +08:00
if (propertyValue instanceof cc.Color) {
let hex = propertyValue.toHEX();
info = new ColorData(`#${hex}`);
} else if (Array.isArray(propertyValue)) {
let keys: number[] = [];
for (let i = 0; i < propertyValue.length; i++) {
keys.push(i);
}
info = this._buildArrayData({
data: new ArrayData(),
path: path,
value: propertyValue,
keys: keys,
})
} else {
!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
}))
!info && (info = this._buildImageData({
//@ts-ignore
ctor: cc.SpriteFrame,
data: new ImageData(),
path: path,
value: propertyValue,
}))
if (!info) {
if (typeof propertyValue === "object") {
2021-11-07 12:14:50 +08:00
let ctorName = propertyValue.constructor?.name;
if (ctorName) {
2021-11-07 18:31:49 +08:00
if (ctorName.startsWith("cc_") ||
// 2.4.0
ctorName === "CCClass") {
2021-11-07 12:14:50 +08:00
info = new EngineData();
info.engineType = ctorName;
info.engineName = propertyValue.name;
info.engineUUID = propertyValue.uuid;
}
2021-11-07 18:31:49 +08:00
}
if (!info) {
2021-11-07 12:14:50 +08:00
// 空{}
2021-11-07 18:31:49 +08:00
// MaterialVariant 2.4.0
2021-11-07 00:09:02 +08:00
info = this._buildObjectData({
data: new ObjectData(),
path: path,
value: propertyValue,
filterKey: filterKey,
})
}
2021-06-19 19:51:05 +08:00
}
}
}
2021-11-07 00:09:02 +08:00
break;
}
2021-04-05 18:38:44 +08:00
}
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
}
2021-11-06 20:19:21 +08:00
_buildArrayData({value, path, data, keys}: BuildArrayOptions) {
keys = keys.filter(key => !key.toString().startsWith("_"));
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
let propPath = path.concat(key.toString());
let itemData = this._genInfoData(value, key, propPath);
if (itemData) {
data.add(new Property(key.toString(), itemData))
}
}
return data;
2021-11-04 22:22:11 +08:00
}
2021-11-06 20:19:21 +08:00
_buildObjectItemData({value, path, data, filterKey}: BuildObjectOptions): Property[] {
let keys = Object.keys(value);
if (filterKey) {
keys = this.filterKeys(keys);// 不再进行开发者定义的数据
}
let ret: Property[] = []
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
let propPath = path.concat(key.toString());
let itemData = this._genInfoData(value, key, propPath, filterKey);
if (itemData) {
ret.push(new Property(key, itemData))
}
}
return ret;
2021-11-04 22:22:11 +08:00
}
2021-11-06 20:19:21 +08:00
filterKeys(keys: string[]) {
2021-07-16 16:36:00 +08:00
// 剔除_开头的属性
2021-11-06 20:19:21 +08:00
return keys.filter(key => !key.toString().startsWith("_"));
}
2021-11-06 21:42:37 +08:00
_isInvalidValue(value: any) {
2021-11-07 00:09:02 +08:00
// !!Infinity=true
2021-11-07 12:14:50 +08:00
if ((value && value !== Infinity) || value === 0 || value === false || value === "") {
2021-11-07 00:09:02 +08:00
return false;
}
if (value === null) {
return "null"
} else if (value === Infinity) {
return "Infinity"
} else if (value === undefined) {
return "undefined"
} else if (Number.isNaN(value)) {
return "NaN";
} else {
debugger
return false;
}
2021-11-06 21:42:37 +08:00
}
2021-11-06 20:19:21 +08:00
_buildObjectData({value, path, data, filterKey}: BuildObjectOptions) {
let keys = Object.keys(value);
if (filterKey) {
keys = this.filterKeys(keys)
}
// 只返回一级key更深层级的key需要的时候再获取防止circle object导致的死循环
let desc: Record<string, any> = {};
2021-06-17 22:35:37 +08:00
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
let propPath = path.concat(key.toString());
2021-11-06 20:19:21 +08:00
let propValue = (value as any)[key];
let keyDesc = "";
if (Array.isArray(propValue)) {
// 只收集一级key
propValue.forEach(item => {
})
keyDesc = `(${propValue.length}) [...]`
2021-11-06 21:42:37 +08:00
} else if (this._isInvalidValue(propValue)) { // 不能改变顺序
keyDesc = propValue;
2021-11-06 20:19:21 +08:00
} else if (typeof propValue === "object") {
keyDesc = `${propValue.constructor.name} {...}`;
} else {
keyDesc = propValue;
2021-06-17 22:35:37 +08:00
}
2021-11-06 20:19:21 +08:00
desc[key] = keyDesc;
2021-06-17 22:35:37 +08:00
}
2021-11-06 20:19:21 +08:00
data.data = JSON.stringify(desc);
2021-06-17 22:35:37 +08:00
return data;
}
private getCompName(comp: any): string {
const nameKeys = [
"__classname__", // 2.4.0 验证通过
];
for (let i = 0; i < nameKeys.length; i++) {
let key = nameKeys[i];
// 一般来说这里的name是不会出现假值
if (comp[key]) {
return comp[key];
}
}
return comp.constructor.name;
}
2021-11-12 21:20:57 +08:00
// 校验keys的有效性3.x有position没有x,y,z
_checkKeysValid(obj: any, keys: string[]) {
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (!isHasProperty(obj, key)) {
return false;
}
}
return true;
}
2021-04-22 19:09:35 +08:00
_getGroupData(node: any) {
const name = this.getCompName(node);
let nodeGroup = new Group(name);
2021-04-05 19:31:34 +08:00
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 pair = this._getPairProperty(key);
2021-11-12 21:20:57 +08:00
if (pair && this._checkKeysValid(node, pair.values)) {
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-11-10 22:04:51 +08:00
if (pair && item === pair.key) {
// 切掉了自己,才能步进+1
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-11-12 00:02:58 +08:00
// todo path
2021-04-22 19:09:35 +08:00
pairValues.forEach((el: string) => {
2021-11-12 21:20:57 +08:00
let propertyPath = [node.uuid, el];
let vecData = this._genInfoData(node, el, propertyPath);
if (vecData) {
info && info.add(new Property(el, vecData));
2021-04-05 19:31:34 +08:00
}
});
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
}
2021-11-04 22:22:11 +08:00
// 获取节点信息只获取一级key即可后续
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);
// 收集组件信息
2021-11-20 21:57:54 +08:00
const nodeComp = node._components;
2021-04-05 19:31:34 +08:00
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-11-20 21:57:54 +08:00
const data: NodeInfoData = {
uuid: uuid,
group: groupData,
}
this.sendMsgToContent(Msg.NodeInfo, data);
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-11-04 22:22:11 +08:00
_isReadonly(base: Object, key: string | number): boolean {
let ret = Object.getOwnPropertyDescriptor(base, key)
if (ret) {
return !(ret.set || ret.writable);
2021-05-08 20:36:32 +08:00
} else {
let proto = Object.getPrototypeOf(base);
2021-11-09 15:24:10 +08:00
if (proto) {
return this._isReadonly(proto, key)
} else {
return false;
}
2021-05-08 20:36:32 +08:00
}
}
2021-11-09 15:24:10 +08:00
setValue(pathArray: Array<string>, value: string): boolean {
let target = this.inspectorGameMemoryStorage;
2021-11-09 15:24:10 +08:00
// 尝试设置creator3.x的数据
if (trySetValueWithConfig(pathArray, target, value)) {
return true;
}
for (let i = 0; i < pathArray.length; i++) {
let path = pathArray[i];
if (i === pathArray.length - 1) {
// 到最后的key了
if (this._isReadonly(target, path)) {
console.warn(`值不允许修改`);
} else {
target[path] = value;
2021-11-09 15:24:10 +08:00
return true;
}
2021-05-08 20:36:32 +08:00
} else {
2021-11-09 15:24:10 +08:00
// creator3.x的enumerable导致无法判断
if (target.hasOwnProperty(path) || target[path]) {
target = target[path];
2021-11-09 15:24:10 +08:00
} else {
return false;
}
2021-05-08 17:52:29 +08:00
}
2021-04-07 17:26:16 +08:00
}
2021-11-09 15:24:10 +08:00
return false;
2021-11-06 20:19:21 +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;