优化设置值和readonly的判断逻辑

This commit is contained in:
xuyanfeng 2021-06-19 11:25:57 +08:00
parent 459bd643e6
commit 837649c15d

View File

@ -64,19 +64,14 @@ class CCInspector {
} }
case Msg.SetProperty: { case Msg.SetProperty: {
const data: Info = pluginEvent.data; const data: Info = pluginEvent.data;
// path的设计有优化空间
const uuid = data.path[0];
const key = data.path[1];
let value = data.data; let value = data.data;
if (data.type === DataType.Color) { if (data.type === DataType.Color) {
// @ts-ignore // @ts-ignore
value = cc.color().fromHEX(value); value = cc.color().fromHEX(value);
} }
if (uuid && key) { this.setValue(data.path, value);
this.setValue(uuid, key, value); this.sendMsgToContent(Msg.UpdateProperty, data);
this.sendMsgToContent(Msg.UpdateProperty, data);
}
break; break;
} }
} }
@ -431,26 +426,31 @@ class CCInspector {
} }
_isReadonly(base: Object, key: string): boolean { _isReadonly(base: Object, key: string): boolean {
let obj = Object.getPrototypeOf(base); let ret = Object.getOwnPropertyDescriptor(base, key)
if (obj) { if (ret) {
let ret = Object.getOwnPropertyDescriptor(obj, key) return !(ret.set || ret.writable);
if (ret && ret.set) {
return false;
} else {
return this._isReadonly(obj, key)
}
} else { } else {
return true; let proto = Object.getPrototypeOf(base);
return this._isReadonly(proto, key)
} }
} }
setValue(uuid: string, key: string, value: string) { setValue(pathArray: Array<string>, value: string) {
let nodeOrComp = this.inspectorGameMemoryStorage[uuid]; let target = this.inspectorGameMemoryStorage;
if (nodeOrComp && key in nodeOrComp) {
if (this._isReadonly(nodeOrComp, key)) { for (let i = 0; i < pathArray.length; i++) {
console.warn(`值不允许修改`); let path = pathArray[i];
if (i === pathArray.length - 1) {
// 到最后的key了
if (this._isReadonly(target, path)) {
console.warn(`值不允许修改`);
} else {
target[path] = value;
}
} else { } else {
nodeOrComp[key] = value; if (target.hasOwnProperty(path)) {
target = target[path];
}
} }
} }
} }