From ba64e8d5865097a2011e75bcbe6a330e5e30f0f7 Mon Sep 17 00:00:00 2001 From: dgflash Date: Sat, 13 Jun 2026 18:59:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20@classname=20?= =?UTF-8?q?=E8=A3=85=E9=A5=B0=E5=99=A8=EF=BC=8C=E4=BF=AE=E5=A4=8D=E6=89=93?= =?UTF-8?q?=E5=8C=85=E5=90=8E=E7=B1=BB=E5=90=8D=E6=B7=B7=E6=B7=86=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=20addBusiness=20=E5=B1=9E=E6=80=A7=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/module/common/CCEntity.ts | 4 +- assets/module/decorator/ClassNameDecorator.ts | 37 +++++++++++++++++++ .../decorator/ClassNameDecorator.ts.meta | 9 +++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 assets/module/decorator/ClassNameDecorator.ts create mode 100644 assets/module/decorator/ClassNameDecorator.ts.meta diff --git a/assets/module/common/CCEntity.ts b/assets/module/common/CCEntity.ts index 15540ee..0bad37c 100644 --- a/assets/module/common/CCEntity.ts +++ b/assets/module/common/CCEntity.ts @@ -7,6 +7,7 @@ import { ViewUtil } from '../../core/utils/ViewUtil'; import { ecs } from '../../libs/ecs/ECS'; import type { CompType } from '../../libs/ecs/registry/ECSTypes'; import type { CCBusiness } from './CCBusiness'; +import { getClassName } from '../decorator/ClassNameDecorator'; import { GameComponent } from './GameComponent'; import { ECSEntity } from '../../libs/ecs/entity/ECSEntity'; @@ -269,7 +270,8 @@ export abstract class CCEntity extends ecs.Entity { this.businesss.set(cls, business); // 将业务逻辑组件直接附加到实体对象身上,方便直接获取 - Reflect.set(this, cls.name, business); + // 使用 classname.getName 获取注册名,避免打包压缩后 cls.name 被混淆 + Reflect.set(this, getClassName(cls), business); return business as T; } diff --git a/assets/module/decorator/ClassNameDecorator.ts b/assets/module/decorator/ClassNameDecorator.ts new file mode 100644 index 0000000..4742440 --- /dev/null +++ b/assets/module/decorator/ClassNameDecorator.ts @@ -0,0 +1,37 @@ +/** + * 类名注册装饰器 + * + * 由于 JS 打包压缩会改变类名(`ctor.name` 被混淆为单字母), + * 使用此装饰器将原始类名保存到构造函数的静态属性上,打包后仍可获取。 + * + * @example + * ```typescript + * @classname('B_Account_Login') + * export class B_Account_Login extends CCBusiness { ... } + * + * // 获取注册名 + * getClassName(B_Account_Login); // 'B_Account_Login' + * ``` + */ + +/** 装饰器写入的静态属性名 */ +const CLASS_NAME_KEY = 'CLASS_NAME'; + +/** + * 类装饰器工厂:注册类名,防止打包压缩后 `ctor.name` 被混淆。 + * @param name 类注册名(须与源码中的类名一致) + */ +export function classname(name: string): ClassDecorator { + return function (ctor: Function): void { + (ctor as any)[CLASS_NAME_KEY] = name; + }; +} + +/** + * 获取类的注册名,未注册时回退到 `ctor.name`。 + * @param ctor 类构造函数 + * @returns 注册名或原始类名 + */ +export function getClassName(ctor: Function): string { + return (ctor as any)[CLASS_NAME_KEY] || ctor.name; +} diff --git a/assets/module/decorator/ClassNameDecorator.ts.meta b/assets/module/decorator/ClassNameDecorator.ts.meta new file mode 100644 index 0000000..ef140ae --- /dev/null +++ b/assets/module/decorator/ClassNameDecorator.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "8f61f8d6-9de0-4f33-bff6-d4946105cc11", + "files": [], + "subMetas": {}, + "userData": {} +}