From e3215376df9dcd0193637203c65bf6fc6abd3d4e Mon Sep 17 00:00:00 2001 From: xyf-mac Date: Tue, 9 Nov 2021 15:24:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE3.x=E8=AE=BE=E7=BD=AEx=E6=97=A0?= =?UTF-8?q?=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/src/devtools/data.ts | 2 +- source/src/inject/index.ts | 47 +++++++++-------- source/src/inject/setValue.ts | 99 +++++++++++++++++++++++++++++++++++ source/src/inject/util.ts | 8 +++ 4 files changed, 133 insertions(+), 23 deletions(-) create mode 100644 source/src/inject/setValue.ts create mode 100644 source/src/inject/util.ts diff --git a/source/src/devtools/data.ts b/source/src/devtools/data.ts index 890c04f..6535089 100644 --- a/source/src/devtools/data.ts +++ b/source/src/devtools/data.ts @@ -79,7 +79,7 @@ export class ObjectData extends Info { } export class InvalidData extends Info { - data: "undefined" | "null" | "Infinity" | "NaN"|string; + data: "undefined" | "null" | "Infinity" | "NaN" | string; constructor(data: any) { super(); diff --git a/source/src/inject/index.ts b/source/src/inject/index.ts index 85fdeec..58bc90b 100644 --- a/source/src/inject/index.ts +++ b/source/src/inject/index.ts @@ -21,6 +21,7 @@ import {Msg, Page, PluginEvent} from "@/core/types" import {BuildArrayOptions, BuildImageOptions, BuildObjectOptions, BuildVecOptions} from "@/inject/types"; // @ts-ignore import {uniq} from "lodash" +import {trySetValueWithConfig, getValue} from "@/inject/setValue"; declare const cc: any; @@ -72,13 +73,16 @@ class CCInspector { value = cc.color().fromHEX(value); } - this.setValue(data.path, value); - this.sendMsgToContent(Msg.UpdateProperty, data); + if (this.setValue(data.path, value)) { + this.sendMsgToContent(Msg.UpdateProperty, data); + } else { + console.warn(`设置失败:${data.path}`) + } break; } case Msg.GetObjectItemData: { const data: ObjectData = pluginEvent.data; - let val = this.getValue(data.path); + let val = getValue(this.inspectorGameMemoryStorage, data.path); if (val) { let itemData: Property[] = this._buildObjectItemData({ data: data, @@ -296,8 +300,8 @@ class CCInspector { const data: ImageData = options.data; const path: Array = options.path; if (ctor && value instanceof ctor) { - // 2.4.6 没有了这个属性 data.path = path; + // 2.4.6 没有了这个属性 if (value.hasOwnProperty("_textureFilename")) { //@ts-ignore data.data = `${window.location.origin}/${value._textureFilename}`; @@ -309,7 +313,7 @@ class CCInspector { return null; } - _genInfoData(node: any, key: string | number, path: Array, filterKey = true) { + _genInfoData(node: any, key: string | number, path: Array, filterKey = true): Info | null { let propertyValue = node[key]; let info = null; let invalidType = this._isInvalidValue(propertyValue); @@ -597,13 +601,20 @@ class CCInspector { return !(ret.set || ret.writable); } else { let proto = Object.getPrototypeOf(base); - return this._isReadonly(proto, key) + if (proto) { + return this._isReadonly(proto, key) + } else { + return false; + } } } - setValue(pathArray: Array, value: string) { + setValue(pathArray: Array, value: string): boolean { let target = this.inspectorGameMemoryStorage; - + // 尝试设置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) { @@ -612,26 +623,18 @@ class CCInspector { console.warn(`值不允许修改`); } else { target[path] = value; + return true; } } else { - if (target.hasOwnProperty(path)) { + // creator3.x的enumerable导致无法判断 + if (target.hasOwnProperty(path) || target[path]) { target = target[path]; + } else { + return false; } } } - } - - getValue(path: string[]) { - let target = this.inspectorGameMemoryStorage; - for (let i = 0; i < path.length; i++) { - let key = path[i]; - if (target[key] !== undefined || target.hasOwnProperty(key)) { - target = target[key] - } else { - return null; - } - } - return target; + return false; } diff --git a/source/src/inject/setValue.ts b/source/src/inject/setValue.ts new file mode 100644 index 0000000..6a231ae --- /dev/null +++ b/source/src/inject/setValue.ts @@ -0,0 +1,99 @@ +import {isVersion3} from "@/inject/util"; + +interface ConfigItem { + path: string[], + func: Function; +} + +const config: ConfigItem[] = [ + { + path: ["position", "x"], + func: (target: any, value: any) => { + let pos = target.getPosition(); + pos.x = value; + target.setPosition(pos); + } + }, + { + path: ["position", "y"], + func: (target: any, value: any) => { + let pos = target.getPosition(); + pos.y = value; + target.setPosition(pos); + } + }, + { + path: ["position", "z"], + func: (target: any, value: any) => { + let pos = target.getPosition(); + pos.z = value; + target.setPosition(pos); + } + }, + { + path: ["scale", "x"], + func: ((target: any, value: any) => { + let scale = target.getScale(); + scale.x = value; + target.setScale(scale); + }) + }, + { + path: ["scale", "y"], + func: ((target: any, value: any) => { + let scale = target.getScale(); + scale.y = value; + target.setScale(scale); + }) + }, + { + path: ["scale", "z"], + func: ((target: any, value: any) => { + let scale = target.getScale(); + scale.z = value; + target.setScale(scale); + }) + } +] + +// 3.x不允许直接设置xyz,需要走setPosition +export function trySetValueWithConfig(pathArray: string[], targetObject: any, targetValue: any) { + if (isVersion3()) { + let fullPath: string = pathArray.toString() + let item = config.find(el => { + return fullPath.endsWith(el.path.toString()) + }); + if (item) { + // 将多余的path去掉 + let leftPathArray = []; + let max = pathArray.length - item.path.length; + for (let i = 0; i < max; i++) { + leftPathArray.push(pathArray[i]) + } + + let pathObjectValue = getValue(targetObject, leftPathArray); + if (pathObjectValue) { + try { + item.func(pathObjectValue, targetValue); + return true; + } catch (e) { + console.error(e); + return false + } + } + } + } + return false; +} + +export function getValue(target: any, path: string[]) { + for (let i = 0; i < path.length; i++) { + let key = path[i]; + if (target[key] !== undefined || target.hasOwnProperty(key)) { + target = target[key] + } else { + return null; + } + } + return target; +} diff --git a/source/src/inject/util.ts b/source/src/inject/util.ts new file mode 100644 index 0000000..ee24c07 --- /dev/null +++ b/source/src/inject/util.ts @@ -0,0 +1,8 @@ +declare const cc:any; +export function isVersion3() { + if (typeof cc.ENGINE_VERSION === "string") { + const version: string = cc.ENGINE_VERSION; + return version.startsWith("3.") + } + return false; +}