mirror of
https://github.com/tidys/cc-inspector-chrome
synced 2025-04-20 08:58:41 +00:00
修3.x设置x无效的问题
This commit is contained in:
parent
59cf18f2f2
commit
e3215376df
@ -21,6 +21,7 @@ import {Msg, Page, PluginEvent} from "@/core/types"
|
|||||||
import {BuildArrayOptions, BuildImageOptions, BuildObjectOptions, BuildVecOptions} from "@/inject/types";
|
import {BuildArrayOptions, BuildImageOptions, BuildObjectOptions, BuildVecOptions} from "@/inject/types";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import {uniq} from "lodash"
|
import {uniq} from "lodash"
|
||||||
|
import {trySetValueWithConfig, getValue} from "@/inject/setValue";
|
||||||
|
|
||||||
declare const cc: any;
|
declare const cc: any;
|
||||||
|
|
||||||
@ -72,13 +73,16 @@ class CCInspector {
|
|||||||
value = cc.color().fromHEX(value);
|
value = cc.color().fromHEX(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setValue(data.path, value);
|
if (this.setValue(data.path, value)) {
|
||||||
this.sendMsgToContent(Msg.UpdateProperty, data);
|
this.sendMsgToContent(Msg.UpdateProperty, data);
|
||||||
|
} else {
|
||||||
|
console.warn(`设置失败:${data.path}`)
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Msg.GetObjectItemData: {
|
case Msg.GetObjectItemData: {
|
||||||
const data: ObjectData = pluginEvent.data;
|
const data: ObjectData = pluginEvent.data;
|
||||||
let val = this.getValue(data.path);
|
let val = getValue(this.inspectorGameMemoryStorage, data.path);
|
||||||
if (val) {
|
if (val) {
|
||||||
let itemData: Property[] = this._buildObjectItemData({
|
let itemData: Property[] = this._buildObjectItemData({
|
||||||
data: data,
|
data: data,
|
||||||
@ -296,8 +300,8 @@ class CCInspector {
|
|||||||
const data: ImageData = options.data;
|
const data: ImageData = options.data;
|
||||||
const path: Array<string> = options.path;
|
const path: Array<string> = options.path;
|
||||||
if (ctor && value instanceof ctor) {
|
if (ctor && value instanceof ctor) {
|
||||||
// 2.4.6 没有了这个属性
|
|
||||||
data.path = path;
|
data.path = path;
|
||||||
|
// 2.4.6 没有了这个属性
|
||||||
if (value.hasOwnProperty("_textureFilename")) {
|
if (value.hasOwnProperty("_textureFilename")) {
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
data.data = `${window.location.origin}/${value._textureFilename}`;
|
data.data = `${window.location.origin}/${value._textureFilename}`;
|
||||||
@ -309,7 +313,7 @@ class CCInspector {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
_genInfoData(node: any, key: string | number, path: Array<string>, filterKey = true) {
|
_genInfoData(node: any, key: string | number, path: Array<string>, filterKey = true): Info | null {
|
||||||
let propertyValue = node[key];
|
let propertyValue = node[key];
|
||||||
let info = null;
|
let info = null;
|
||||||
let invalidType = this._isInvalidValue(propertyValue);
|
let invalidType = this._isInvalidValue(propertyValue);
|
||||||
@ -597,13 +601,20 @@ class CCInspector {
|
|||||||
return !(ret.set || ret.writable);
|
return !(ret.set || ret.writable);
|
||||||
} else {
|
} else {
|
||||||
let proto = Object.getPrototypeOf(base);
|
let proto = Object.getPrototypeOf(base);
|
||||||
|
if (proto) {
|
||||||
return this._isReadonly(proto, key)
|
return this._isReadonly(proto, key)
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setValue(pathArray: Array<string>, value: string) {
|
setValue(pathArray: Array<string>, value: string): boolean {
|
||||||
let target = this.inspectorGameMemoryStorage;
|
let target = this.inspectorGameMemoryStorage;
|
||||||
|
// 尝试设置creator3.x的数据
|
||||||
|
if (trySetValueWithConfig(pathArray, target, value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
for (let i = 0; i < pathArray.length; i++) {
|
for (let i = 0; i < pathArray.length; i++) {
|
||||||
let path = pathArray[i];
|
let path = pathArray[i];
|
||||||
if (i === pathArray.length - 1) {
|
if (i === pathArray.length - 1) {
|
||||||
@ -612,26 +623,18 @@ class CCInspector {
|
|||||||
console.warn(`值不允许修改`);
|
console.warn(`值不允许修改`);
|
||||||
} else {
|
} else {
|
||||||
target[path] = value;
|
target[path] = value;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (target.hasOwnProperty(path)) {
|
// creator3.x的enumerable导致无法判断
|
||||||
|
if (target.hasOwnProperty(path) || target[path]) {
|
||||||
target = target[path];
|
target = target[path];
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
} else {
|
||||||
return null;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return target;
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
99
source/src/inject/setValue.ts
Normal file
99
source/src/inject/setValue.ts
Normal file
@ -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;
|
||||||
|
}
|
8
source/src/inject/util.ts
Normal file
8
source/src/inject/util.ts
Normal file
@ -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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user