From 837649c15d1c8acea433cc055070f09522d19cbc Mon Sep 17 00:00:00 2001 From: xuyanfeng Date: Sat, 19 Jun 2021 11:25:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AE=BE=E7=BD=AE=E5=80=BC?= =?UTF-8?q?=E5=92=8Creadonly=E7=9A=84=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/src/devtools/inject.ts | 44 +++++++++++++++++------------------ 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/source/src/devtools/inject.ts b/source/src/devtools/inject.ts index 67ef2ec..df84e76 100644 --- a/source/src/devtools/inject.ts +++ b/source/src/devtools/inject.ts @@ -64,19 +64,14 @@ class CCInspector { } case Msg.SetProperty: { const data: Info = pluginEvent.data; - // path的设计有优化空间 - const uuid = data.path[0]; - const key = data.path[1]; let value = data.data; if (data.type === DataType.Color) { // @ts-ignore value = cc.color().fromHEX(value); } - if (uuid && key) { - this.setValue(uuid, key, value); - this.sendMsgToContent(Msg.UpdateProperty, data); - } + this.setValue(data.path, value); + this.sendMsgToContent(Msg.UpdateProperty, data); break; } } @@ -431,26 +426,31 @@ class CCInspector { } _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) - } + let ret = Object.getOwnPropertyDescriptor(base, key) + if (ret) { + return !(ret.set || ret.writable); } else { - return true; + let proto = Object.getPrototypeOf(base); + return this._isReadonly(proto, key) } } - setValue(uuid: string, key: string, value: string) { - let nodeOrComp = this.inspectorGameMemoryStorage[uuid]; - if (nodeOrComp && key in nodeOrComp) { - if (this._isReadonly(nodeOrComp, key)) { - console.warn(`值不允许修改`); + setValue(pathArray: Array, value: string) { + let target = this.inspectorGameMemoryStorage; + + 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; + } } else { - nodeOrComp[key] = value; + if (target.hasOwnProperty(path)) { + target = target[path]; + } } } }