Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parse method result #2485

Merged
merged 8 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/loader/src/SceneLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
Scene,
TonemappingEffect
} from "@galacean/engine-core";
import { IClassObject, IScene, ReflectionParser, SceneParser, SpecularMode } from "./resource-deserialize";
import { IClass, IScene, ReflectionParser, SceneParser, SpecularMode } from "./resource-deserialize";

@resourceLoader(AssetType.Scene, ["scene"], true)
class SceneLoader extends Loader<Scene> {
Expand Down Expand Up @@ -144,14 +144,14 @@
}
}

ReflectionParser.registerCustomParseComponent(

Check failure on line 147 in packages/loader/src/SceneLoader.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `⏎··"TextRenderer",⏎·` with `"TextRenderer",`
"TextRenderer",
async (instance: any, item: Omit<IClassObject, "class">) => {
async (instance: any, item: Omit<IClass, "class">) => {
const { props } = item;

Check failure on line 150 in packages/loader/src/SceneLoader.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
if (!props.font) {

Check failure on line 151 in packages/loader/src/SceneLoader.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
// @ts-ignore

Check failure on line 152 in packages/loader/src/SceneLoader.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `······` with `····`
instance.font = Font.createFromOS(instance.engine, props.fontFamily || "Arial");

Check failure on line 153 in packages/loader/src/SceneLoader.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
}

Check failure on line 154 in packages/loader/src/SceneLoader.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
return instance;

Check failure on line 155 in packages/loader/src/SceneLoader.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
}

Check failure on line 156 in packages/loader/src/SceneLoader.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `··}⏎` with `}`
);
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { EngineObject, Entity, Loader } from "@galacean/engine-core";
import type {
IAssetRef,
IBasicType,
IClassObject,
IClass,
IClassType,
IComponentRef,
IEntity,
IEntityRef,
IComponentRef,
IHierarchyFile,
IMethod,
IMethodParams,
IRefEntity
} from "../schema";
import { ParserContext, ParserType } from "./ParserContext";
Expand Down Expand Up @@ -34,15 +37,15 @@ export class ReflectionParser {
});
}

parseClassObject(item: IClassObject) {
parseClassObject(item: IClass) {
const Class = Loader.getClass(item.class);
const params = item.constructParams ?? [];
return Promise.all(params.map((param) => this.parseBasicType(param)))
.then((resultParams) => new Class(...resultParams))
.then((instance) => this.parsePropsAndMethods(instance, item));
}

parsePropsAndMethods(instance: any, item: Omit<IClassObject, "class">) {
parsePropsAndMethods(instance: any, item: Omit<IClass, "class">) {
const promises = [];
if (item.methods) {
for (let methodName in item.methods) {
Expand Down Expand Up @@ -70,17 +73,27 @@ export class ReflectionParser {
});
}

parseMethod(instance: any, methodName: string, methodParams: Array<IBasicType>) {
return Promise.all(methodParams.map((param) => this.parseBasicType(param))).then((result) => {
return instance[methodName](...result);
parseMethod(instance: any, methodName: string, methodParams: IMethodParams) {
const isMethodObject = ReflectionParser._isMethodObject(methodParams);
const params = isMethodObject ? methodParams.params : methodParams;

return Promise.all(params.map((param) => this.parseBasicType(param))).then((result) => {
const methodResult = instance[methodName](...result);
if (isMethodObject && methodParams.result) {
return this.parsePropsAndMethods(methodResult, methodParams.result);
} else {
return methodResult;
}
Comment on lines +76 to +86
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Modular method handling
This block properly handles both a simple param array and the new IMethod structure. Consider factoring out the isMethodObject logic into a separate helper function for clarity and to keep parseMethod minimal.

});
}

parseBasicType(value: IBasicType, originValue?: any): Promise<any> {
if (Array.isArray(value)) {
return Promise.all(value.map((item) => this.parseBasicType(item)));
} else if (typeof value === "object" && value != null) {
if (ReflectionParser._isClass(value)) {
if (ReflectionParser._isClassType(value)) {
return Promise.resolve(Loader.getClass(value["classType"]));
} else if (ReflectionParser._isClass(value)) {
// class object
return this.parseClassObject(value);
} else if (ReflectionParser._isAssetRef(value)) {
Expand Down Expand Up @@ -154,10 +167,14 @@ export class ReflectionParser {
}
}

private static _isClass(value: any): value is IClassObject {
private static _isClass(value: any): value is IClass {
return value["class"] !== undefined;
}

private static _isClassType(value: any): value is IClassType {
return value["classType"] !== undefined;
}

private static _isAssetRef(value: any): value is IAssetRef {
return value["refId"] !== undefined;
}
Expand All @@ -169,4 +186,8 @@ export class ReflectionParser {
private static _isComponentRef(value: any): value is IComponentRef {
return value["ownerId"] !== undefined && value["componentId"] !== undefined;
}

private static _isMethodObject(value: any): value is IMethod {
return Array.isArray(value?.params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ export interface IHierarchyFile {
entities: Array<IEntity>;
}

export type IMethodParams = Array<IBasicType>;
export type IMethod = {
params: Array<IBasicType>;
result?: IInstance;
};

export type IMethodParams = Array<IBasicType> | IMethod;

export interface IBasicEntity {
name?: string;
Expand All @@ -49,11 +54,9 @@ export interface IRefEntity extends IBasicEntity {
assetRefId: string;
key?: string;
isClone?: boolean;
modifications: {
modifications: (IInstance & {
target: IPrefabModifyTarget;
methods?: { [methodName: string]: Array<IMethodParams> };
props?: { [key: string]: IBasicType | IMethodParams };
}[];
})[];
removedEntities: IPrefabModifyTarget[];
removedComponents: IPrefabModifyTarget[];
}
Expand All @@ -69,13 +72,20 @@ export interface IStrippedEntity extends IBasicEntity {
prefabSource: { assetId: string; entityId: string };
}

export type IComponent = { id: string; refId?: string } & IClassObject;
export type IComponent = { id: string; refId?: string } & IClass;

export type IClassObject = {
export type IClass = {
class: string;
constructParams?: IMethodParams;
constructParams?: Array<IBasicType>;
} & IInstance;

export interface IInstance {
methods?: { [methodName: string]: Array<IMethodParams> };
props?: { [key: string]: IBasicType | IMethodParams };
}

export type IClassType = {
classType: string;
};

export type IBasicType =
Expand All @@ -85,7 +95,8 @@ export type IBasicType =
| null
| undefined
| IAssetRef
| IClassObject
| IClass
| IClassType
| IMethodParams
| IEntityRef;

Expand Down
Loading