inject重构

This commit is contained in:
xyf-mac 2021-11-04 22:22:11 +08:00
parent cff5edf263
commit 57a3b8a39c
3 changed files with 47 additions and 16 deletions

View File

@ -14,13 +14,12 @@ import {
TreeData, TreeData,
Vec2Data, Vec2Data,
Vec3Data Vec3Data
} from "./data"; } from "@/devtools/data";
import {Msg, Page, PluginEvent} from "@/core/types" import {Msg, Page, PluginEvent} from "@/core/types"
import {BuildDataOptions, BuildImageOptions, BuildVecOptions} from "@/inject/types";
declare var cc: any;
// @ts-ignore typescript-eslint/no-namespace
declare namespace cc {
export function v2(x: number, y: number): any;
}
class CCInspector { class CCInspector {
inspectorGameMemoryStorage: Record<string, any> = {} inspectorGameMemoryStorage: Record<string, any> = {}
@ -207,7 +206,7 @@ class CCInspector {
return null; return null;
} }
_buildVecData(options: any) { _buildVecData(options: BuildVecOptions) {
const ctor: Function = options.ctor; const ctor: Function = options.ctor;
const keys: Array<string> = options.keys; const keys: Array<string> = options.keys;
const value: Object = options.value; const value: Object = options.value;
@ -233,7 +232,7 @@ class CCInspector {
return null; return null;
} }
_buildImageData(options: any) { _buildImageData(options: BuildImageOptions) {
const ctor: Function = options.ctor; const ctor: Function = options.ctor;
const value: Object = options.value; const value: Object = options.value;
const data: ImageData = options.data; const data: ImageData = options.data;
@ -249,7 +248,7 @@ class CCInspector {
return null; return null;
} }
_genInfoData(node: any, key: string, path: Array<string>) { _genInfoData(node: any, key: string | number, path: Array<string>) {
let propertyValue = node[key]; let propertyValue = node[key];
let info = null; let info = null;
switch (typeof propertyValue) { switch (typeof propertyValue) {
@ -270,11 +269,11 @@ class CCInspector {
let hex = propertyValue.toHEX(); let hex = propertyValue.toHEX();
info = new ColorData(`#${hex}`); info = new ColorData(`#${hex}`);
} else if (Array.isArray(propertyValue)) { } else if (Array.isArray(propertyValue)) {
let keys = []; let keys: number[] = [];
for (let i = 0; i < propertyValue.length; i++) { for (let i = 0; i < propertyValue.length; i++) {
keys.push(i); keys.push(i);
} }
info = this._buildObjectOrArrayData({ info = this._buildArrayData({
data: new ArrayData(), data: new ArrayData(),
path: path, path: path,
value: propertyValue, value: propertyValue,
@ -313,7 +312,7 @@ class CCInspector {
info.engineName = propertyValue.name; info.engineName = propertyValue.name;
info.engineUUID = propertyValue.uuid; info.engineUUID = propertyValue.uuid;
} else { } else {
info = this._buildObjectOrArrayData({ info = this._buildObjectData({
data: new ObjectData(), data: new ObjectData(),
path: path, path: path,
value: propertyValue, value: propertyValue,
@ -334,11 +333,20 @@ class CCInspector {
return info; return info;
} }
_buildObjectOrArrayData(options: any) { _buildArrayData(options: BuildDataOptions) {
return this._buildObjectOrArrayData(options);
}
_buildObjectData(options: BuildDataOptions) {
// todo 只返回一级key更深层级的key需要的时候再获取防止circle object导致的死循环
}
_buildObjectOrArrayData(options: BuildDataOptions) {
let propertyValue: Object = options.value; let propertyValue: Object = options.value;
let path: Array<string> = options.path; let path: Array<string> = options.path;
let data: ObjectData | ArrayData = options.data; let data: ObjectData | ArrayData = options.data;
let keys: Array<string> = options.keys; let keys: Array<string | number> = options.keys;
// 剔除_开头的属性 // 剔除_开头的属性
keys = keys.filter(key => !key.toString().startsWith("_")); keys = keys.filter(key => !key.toString().startsWith("_"));
for (let i = 0; i < keys.length; i++) { for (let i = 0; i < keys.length; i++) {
@ -423,7 +431,7 @@ class CCInspector {
return nodeGroup; return nodeGroup;
} }
// 获取节点信息 // 获取节点信息只获取一级key即可后续
getNodeInfo(uuid: string) { getNodeInfo(uuid: string) {
let node = this.inspectorGameMemoryStorage[uuid]; let node = this.inspectorGameMemoryStorage[uuid];
if (node) { if (node) {
@ -453,7 +461,7 @@ class CCInspector {
} }
} }
_isReadonly(base: Object, key: string): boolean { _isReadonly(base: Object, key: string | number): boolean {
let ret = Object.getOwnPropertyDescriptor(base, key) let ret = Object.getOwnPropertyDescriptor(base, key)
if (ret) { if (ret) {
return !(ret.set || ret.writable); return !(ret.set || ret.writable);

View File

@ -0,0 +1,23 @@
import {ArrayData, ImageData, ObjectData, Vec2Data, Vec3Data} from "@/devtools/data";
export interface BuildDataOptions {
path: string[];
value: Object;
data: ObjectData | ArrayData;
keys: string[] | number[];
}
export interface BuildVecOptions {
path: string[];
keys: string[];
ctor: Function;
value: Object;
data: Vec3Data | Vec2Data;
}
export interface BuildImageOptions {
path: string[];
ctor: Function;
value: Object;
data: ImageData;
}

View File

@ -18,7 +18,7 @@ module.exports = {
contentScripts: { contentScripts: {
entries: { entries: {
content: "src/content.ts", content: "src/content.ts",
inject: "src/devtools/inject.ts", inject: "src/inject/index.ts",
}, },
}, },
background: { background: {