@kkarum/framework 2.3.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. package/Framework.ts +337 -0
  2. package/Framework.ts.meta +10 -0
  3. package/FrameworkBase.ts +207 -0
  4. package/FrameworkBase.ts.meta +10 -0
  5. package/FrameworkInitialize.ts +79 -0
  6. package/FrameworkInitialize.ts.meta +10 -0
  7. package/README.md +93 -0
  8. package/README.md.meta +6 -0
  9. package/animation/FWAnimation.ts +13 -0
  10. package/animation/FWAnimation.ts.meta +10 -0
  11. package/animation/FWSkeleton.ts +174 -0
  12. package/animation/FWSkeleton.ts.meta +10 -0
  13. package/animation/FWTween.ts +60 -0
  14. package/animation/FWTween.ts.meta +10 -0
  15. package/animation.meta +13 -0
  16. package/component/FWVirtuaScrollViewComponent.ts +56 -0
  17. package/component/FWVirtuaScrollViewComponent.ts.meta +10 -0
  18. package/component/FWVirtualViewComponent.ts +2212 -0
  19. package/component/FWVirtualViewComponent.ts.meta +10 -0
  20. package/component.meta +13 -0
  21. package/config/FWAssetConfig.ts +7 -0
  22. package/config/FWAssetConfig.ts.meta +10 -0
  23. package/config/FWSystemConfig.ts +29 -0
  24. package/config/FWSystemConfig.ts.meta +10 -0
  25. package/config.meta +13 -0
  26. package/controller/FWLayerController.ts +178 -0
  27. package/controller/FWLayerController.ts.meta +10 -0
  28. package/controller.meta +13 -0
  29. package/data/FWData.ts +6 -0
  30. package/data/FWData.ts.meta +10 -0
  31. package/data.meta +13 -0
  32. package/define/FWEventDefine.ts +28 -0
  33. package/define/FWEventDefine.ts.meta +10 -0
  34. package/define/FWSystemDefine.ts +154 -0
  35. package/define/FWSystemDefine.ts.meta +10 -0
  36. package/define.meta +13 -0
  37. package/entry/FWEntry.ts +219 -0
  38. package/entry/FWEntry.ts.meta +10 -0
  39. package/entry.meta +13 -0
  40. package/expand/FWCocosExpand.ts +73 -0
  41. package/expand/FWCocosExpand.ts.meta +10 -0
  42. package/expand/FWDecorator.ts +366 -0
  43. package/expand/FWDecorator.ts.meta +10 -0
  44. package/expand/FWNodeExpand.ts +147 -0
  45. package/expand/FWNodeExpand.ts.meta +10 -0
  46. package/expand/FWRollingViewNesting.ts +195 -0
  47. package/expand/FWRollingViewNesting.ts.meta +10 -0
  48. package/expand/FWTweenExpand.ts +44 -0
  49. package/expand/FWTweenExpand.ts.meta +10 -0
  50. package/expand.meta +13 -0
  51. package/item/FWVirtualListItem.ts +141 -0
  52. package/item/FWVirtualListItem.ts.meta +10 -0
  53. package/item.meta +13 -0
  54. package/language/FWLanguage.ts +71 -0
  55. package/language/FWLanguage.ts.meta +10 -0
  56. package/language/FWLanguageLabelLocalize.ts +144 -0
  57. package/language/FWLanguageLabelLocalize.ts.meta +10 -0
  58. package/language/FWLanguageSkeletonLocalize.ts +106 -0
  59. package/language/FWLanguageSkeletonLocalize.ts.meta +10 -0
  60. package/language/FWLanguageSpriteLocalize.ts +81 -0
  61. package/language/FWLanguageSpriteLocalize.ts.meta +10 -0
  62. package/language.meta +13 -0
  63. package/layer/FWLayer.ts +14 -0
  64. package/layer/FWLayer.ts.meta +10 -0
  65. package/layer.meta +13 -0
  66. package/log/FWLog.ts +135 -0
  67. package/log/FWLog.ts.meta +10 -0
  68. package/log.meta +13 -0
  69. package/logic/FWLogic.ts +6 -0
  70. package/logic/FWLogic.ts.meta +10 -0
  71. package/logic.meta +13 -0
  72. package/machine/FWAnimationMachine.ts +28 -0
  73. package/machine/FWAnimationMachine.ts.meta +10 -0
  74. package/machine/FWStateMachine.ts +74 -0
  75. package/machine/FWStateMachine.ts.meta +10 -0
  76. package/machine.meta +13 -0
  77. package/manager/FWAnimationManager.ts +89 -0
  78. package/manager/FWAnimationManager.ts.meta +10 -0
  79. package/manager/FWAssetManager.ts +451 -0
  80. package/manager/FWAssetManager.ts.meta +10 -0
  81. package/manager/FWAudioManager.ts +339 -0
  82. package/manager/FWAudioManager.ts.meta +10 -0
  83. package/manager/FWBundleManager.ts +97 -0
  84. package/manager/FWBundleManager.ts.meta +10 -0
  85. package/manager/FWComponentManager.ts +24 -0
  86. package/manager/FWComponentManager.ts.meta +10 -0
  87. package/manager/FWEngineManager.ts +74 -0
  88. package/manager/FWEngineManager.ts.meta +10 -0
  89. package/manager/FWEventManager.ts +361 -0
  90. package/manager/FWEventManager.ts.meta +10 -0
  91. package/manager/FWHotUpdateManager.ts +344 -0
  92. package/manager/FWHotUpdateManager.ts.meta +10 -0
  93. package/manager/FWLanguageManager.ts +114 -0
  94. package/manager/FWLanguageManager.ts.meta +10 -0
  95. package/manager/FWLayerManager.ts +862 -0
  96. package/manager/FWLayerManager.ts.meta +10 -0
  97. package/manager/FWManager.ts +9 -0
  98. package/manager/FWManager.ts.meta +10 -0
  99. package/manager/FWObjectManager.ts +161 -0
  100. package/manager/FWObjectManager.ts.meta +10 -0
  101. package/manager/FWPerformanceManager.ts +198 -0
  102. package/manager/FWPerformanceManager.ts.meta +10 -0
  103. package/manager/FWPromiseManager.ts +504 -0
  104. package/manager/FWPromiseManager.ts.meta +10 -0
  105. package/manager/FWResManager.ts +299 -0
  106. package/manager/FWResManager.ts.meta +10 -0
  107. package/manager/FWSocketManager.ts +93 -0
  108. package/manager/FWSocketManager.ts.meta +10 -0
  109. package/manager/FWStateManager.ts +100 -0
  110. package/manager/FWStateManager.ts.meta +10 -0
  111. package/manager/FWTaskManager.ts +41 -0
  112. package/manager/FWTaskManager.ts.meta +10 -0
  113. package/manager/FWTimeManager.ts +442 -0
  114. package/manager/FWTimeManager.ts.meta +10 -0
  115. package/manager/FWUiManager.ts +217 -0
  116. package/manager/FWUiManager.ts.meta +10 -0
  117. package/manager.meta +13 -0
  118. package/package.json +11 -0
  119. package/package.json.meta +6 -0
  120. package/registry/FWRegistry.ts +62 -0
  121. package/registry/FWRegistry.ts.meta +10 -0
  122. package/registry.meta +13 -0
  123. package/render/FWRenderAssembler.ts +620 -0
  124. package/render/FWRenderAssembler.ts.meta +10 -0
  125. package/render.meta +13 -0
  126. package/scene/FWScene.ts +23 -0
  127. package/scene/FWScene.ts.meta +10 -0
  128. package/scene.meta +13 -0
  129. package/service/FWService.ts +11 -0
  130. package/service/FWService.ts.meta +10 -0
  131. package/service/http/FWHttp.ts +103 -0
  132. package/service/http/FWHttp.ts.meta +10 -0
  133. package/service/http.meta +13 -0
  134. package/service/socket/FWSocket.ts +367 -0
  135. package/service/socket/FWSocket.ts.meta +10 -0
  136. package/service/socket/FWSocketHandle.ts +16 -0
  137. package/service/socket/FWSocketHandle.ts.meta +10 -0
  138. package/service/socket/FWSocketSender.ts +14 -0
  139. package/service/socket/FWSocketSender.ts.meta +10 -0
  140. package/service/socket/mock/FWSocketMock.ts +202 -0
  141. package/service/socket/mock/FWSocketMock.ts.meta +10 -0
  142. package/service/socket/mock.meta +13 -0
  143. package/service/socket.meta +13 -0
  144. package/service.meta +13 -0
  145. package/state/FWState.ts +8 -0
  146. package/state/FWState.ts.meta +10 -0
  147. package/state.meta +13 -0
  148. package/types/Creator.d.ts +121 -0
  149. package/types/Creator.d.ts.meta +6 -0
  150. package/types/FW.d.ts +3016 -0
  151. package/types/FW.d.ts.meta +6 -0
  152. package/types/Global.d.ts +22 -0
  153. package/types/Global.d.ts.meta +10 -0
  154. package/types.meta +13 -0
  155. package/utils/FWLodash.ts +105 -0
  156. package/utils/FWLodash.ts.meta +10 -0
  157. package/utils/FWMask.ts +222 -0
  158. package/utils/FWMask.ts.meta +10 -0
  159. package/utils/FWObject.ts +27 -0
  160. package/utils/FWObject.ts.meta +10 -0
  161. package/utils/FWObjectPool.ts +177 -0
  162. package/utils/FWObjectPool.ts.meta +10 -0
  163. package/utils/FWQueue.ts +55 -0
  164. package/utils/FWQueue.ts.meta +10 -0
  165. package/utils/FWResLoader.ts +139 -0
  166. package/utils/FWResLoader.ts.meta +10 -0
  167. package/utils/FWTask.ts +227 -0
  168. package/utils/FWTask.ts.meta +10 -0
  169. package/utils/FWUtils.ts +15 -0
  170. package/utils/FWUtils.ts.meta +10 -0
  171. package/utils.meta +13 -0
@@ -0,0 +1,74 @@
1
+ import { FWState } from '../state/FWState';
2
+
3
+ export default class FWStateMachine implements FW.StateMachine {
4
+ private stateMap: Map<number, FWState>;
5
+ private stateType: number;
6
+ private paused: boolean;
7
+
8
+ public get state() {
9
+ return this.stateType;
10
+ }
11
+ constructor() {
12
+ this.stateMap = new Map<number, FWState>();
13
+ this.stateType = 0;
14
+ this.paused = false;
15
+ }
16
+ /** 进入状态 */
17
+ enter(stateType: number, ...args: any): void {
18
+ if (this.stateMap.size == 0) {
19
+ FW.Log.error('stateMachine is not register!');
20
+ return;
21
+ }
22
+
23
+ if (this.paused) return;
24
+ if (this.state == stateType) return;
25
+
26
+ if (this.stateMap.has(this.stateType)) {
27
+ this.stateMap?.get(this.stateType)?.onExit();
28
+ }
29
+ this.stateType = stateType;
30
+ if (this.stateMap.has(this.stateType)) {
31
+ this.stateMap?.get(this.stateType)?.onEnter(args);
32
+ }
33
+ }
34
+ /**
35
+ * 暂停状态机
36
+ * */
37
+ pause() {
38
+ this.paused = true;
39
+ FW.Entry.timeMgr.pauseSchedule(this);
40
+ }
41
+ /**
42
+ * 恢复状态机
43
+ */
44
+ resume() {
45
+ this.paused = false;
46
+ FW.Entry.timeMgr.resumeSchedule(this);
47
+ }
48
+ /**
49
+ * 注册
50
+ * @param type
51
+ * @param state
52
+ */
53
+ register<T extends FWState>(state: FW.StateMachineRegisterArgs<T>) {
54
+ const constructor = new state.stateConstructor();
55
+ constructor.onInit();
56
+ FW.Entry.timeMgr.update(constructor.onUpdate.bind(state), this);
57
+ this.stateMap.set(state.stateType, constructor);
58
+ }
59
+ /**
60
+ * 注销
61
+ * @param type
62
+ */
63
+ unRegister(stateType: number) {
64
+ if (!this.stateMap.has(stateType)) return;
65
+ const constructor = this.stateMap.get(stateType);
66
+ constructor.onDestroy();
67
+ FW.Entry.timeMgr.unSchedule(this);
68
+ this.stateMap.delete(stateType);
69
+ }
70
+ onDestroy() {
71
+ this.stateMap.clear();
72
+ this.stateMap = null;
73
+ }
74
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "ver": "1.1.0",
3
+ "uuid": "71dbc901-1182-44eb-9c23-111d7a3e2963",
4
+ "importer": "typescript",
5
+ "isPlugin": false,
6
+ "loadPluginInWeb": true,
7
+ "loadPluginInNative": true,
8
+ "loadPluginInEditor": false,
9
+ "subMetas": {}
10
+ }
package/machine.meta ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "ver": "1.1.3",
3
+ "uuid": "a2c00ae3-16c2-4bd9-9cc7-e4ad4e9c0c88",
4
+ "importer": "folder",
5
+ "isBundle": false,
6
+ "bundleName": "",
7
+ "priority": 1,
8
+ "compressionType": {},
9
+ "optimizeHotUpdate": {},
10
+ "inlineSpriteFrames": {},
11
+ "isRemoteBundle": {},
12
+ "subMetas": {}
13
+ }
@@ -0,0 +1,89 @@
1
+ import { FWSkeleton } from '../animation/FWSkeleton';
2
+ import { FWTween } from '../animation/FWTween';
3
+ import { FWManager } from './FWManager';
4
+ import { FWAnimationMachine } from '../machine/FWAnimationMachine';
5
+
6
+ export default class FWAnimationManager extends FWManager {
7
+ private animationMachineMap: Map<string, FWAnimationMachine>;
8
+ public initialize(): void {
9
+ this.animationMachineMap = new Map<string, FWAnimationMachine>();
10
+
11
+ this.createAnimationMachine(FW.SystemDefine.FWAnimationMachineType.TWEEN);
12
+ this.createAnimationMachine(FW.SystemDefine.FWAnimationMachineType.SKELETON);
13
+ this.createAnimationMachine(FW.SystemDefine.FWAnimationMachineType.ANIMATION);
14
+ }
15
+
16
+ /**
17
+ * 创建一个tween动画
18
+ * @param args
19
+ * @returns
20
+ */
21
+ createTween(args?: FW.TweenArgs) {
22
+ const t = new FWTween();
23
+ const machine = this.animationMachineMap.get(FW.SystemDefine.FWAnimationMachineType.TWEEN);
24
+ t.animationName = args?.tag || '';
25
+ t.animation = cc.tween(args?.target);
26
+ t.timeScale(args?.timeScale || 1);
27
+ machine.addAnimation(t);
28
+ return t;
29
+ }
30
+
31
+ /**
32
+ * 创建一个skeleton动画
33
+ * @param skeleton
34
+ * @returns
35
+ */
36
+ createSkeleton(skeleton: sp.Skeleton) {
37
+ const s = new FWSkeleton();
38
+ const machine = this.animationMachineMap.get(FW.SystemDefine.FWAnimationMachineType.SKELETON);
39
+ s.animationName = skeleton.name;
40
+ s.animation = skeleton;
41
+ machine.addAnimation(s);
42
+ return s;
43
+ }
44
+
45
+ /**
46
+ * 暂停所有动画
47
+ */
48
+ pauseAll() {
49
+ this.animationMachineMap.forEach((v) => v.pause());
50
+ }
51
+
52
+ /** 恢复所有动画 */
53
+ resumeAll() {
54
+ this.animationMachineMap.forEach((v) => v.resume());
55
+ }
56
+
57
+ /**
58
+ * 创建动画机
59
+ * @param animationMachineName
60
+ * @returns
61
+ */
62
+ private createAnimationMachine(animationMachineName: string) {
63
+ if (this.animationMachineMap.has(animationMachineName)) {
64
+ FW.Log.warn(`已创建动画机:${animationMachineName},请勿重复注册!`);
65
+ return;
66
+ }
67
+ const machine = new FWAnimationMachine();
68
+ this.animationMachineMap.set(animationMachineName, machine);
69
+ return machine;
70
+ }
71
+
72
+ /**
73
+ * 移除动画机
74
+ * @param animationMachineName
75
+ * @returns
76
+ */
77
+ private removeAnimationMachine(animationMachineName: string) {
78
+ if (!this.animationMachineMap.has(animationMachineName)) {
79
+ return;
80
+ }
81
+ this.animationMachineMap.delete(animationMachineName);
82
+ }
83
+
84
+ onDestroy(): void {
85
+ this.animationMachineMap.forEach((v, k) => {
86
+ this.removeAnimationMachine(k);
87
+ });
88
+ }
89
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "ver": "1.1.0",
3
+ "uuid": "c2448e26-e096-4b98-83bc-f54fe3600ac8",
4
+ "importer": "typescript",
5
+ "isPlugin": false,
6
+ "loadPluginInWeb": true,
7
+ "loadPluginInNative": true,
8
+ "loadPluginInEditor": false,
9
+ "subMetas": {}
10
+ }
@@ -0,0 +1,451 @@
1
+ import { FWSystemConfig } from "../config/FWSystemConfig";
2
+ import { FWManager } from "./FWManager";
3
+
4
+ /**
5
+ * 资源数据
6
+ */
7
+ class FWAssetData implements FW.AssetData {
8
+ asset: cc.Asset;
9
+ uuid: string;
10
+ user: string;
11
+ depend: string[];
12
+ reference: string[];
13
+ type: typeof cc.Asset;
14
+ dependentBundle: string;
15
+ loaded: boolean;
16
+ autoRelease: boolean;
17
+ assetProperty: FW.AssetProperty;
18
+ }
19
+
20
+ export class FWAssetManager extends FWManager implements FW.AssetManager {
21
+ assetsMap: Map<string, FWAssetData>;
22
+
23
+ constructor(protected readonly resMgr: FW.ResManager) {
24
+ super();
25
+ }
26
+
27
+ public async initialize(): Promise<void> {
28
+ this.assetsMap = new Map<string, FWAssetData>();
29
+ }
30
+
31
+ /**
32
+ * 从远程加载spine动画
33
+ * @param data
34
+ * @returns
35
+ */
36
+ async loadSpineDataFromRemote(data: {
37
+ img: string;
38
+ ske: string;
39
+ atlas: string;
40
+ bin?: boolean;
41
+ }): Promise<sp.SkeletonData> {
42
+ try {
43
+ return await this.invoke(
44
+ FW.Entry.promiseMgr.execute((resolve, reject, signal) => {
45
+ const bin = data.bin === undefined ? false : data.bin;
46
+ const img = data.img;
47
+ const ske = data.ske;
48
+ const atlas = data.atlas;
49
+ const type = bin ? ".bin" : ".txt";
50
+ cc.assetManager.loadAny(
51
+ [
52
+ { url: atlas, ext: type },
53
+ { url: ske, ext: type },
54
+ ],
55
+ async (error, assets) => {
56
+ if (error != null) {
57
+ reject(error);
58
+ return;
59
+ }
60
+ const fileName = FW.Entry.utils.getFileName(img);
61
+ const texture: cc.Texture2D = await this.loadRemote(img, {
62
+ ext: ".png",
63
+ });
64
+ var asset = new sp.SkeletonData();
65
+ asset.atlasText = assets[0];
66
+ asset.skeletonJson = assets[1];
67
+ asset.textures.push(texture);
68
+ asset["textureNames"] = [`${fileName}`];
69
+ asset["_uuid"] = ske;
70
+ resolve(asset);
71
+ },
72
+ );
73
+ }, FWSystemConfig.PromiseConfig.loadAsset).promise,
74
+ );
75
+ } catch (e) {
76
+ FW.Log.error("从远程加载spine动画资源失败!");
77
+ throw e;
78
+ }
79
+ }
80
+
81
+ /** 加载远程资源 */
82
+ async loadRemote<T extends cc.Asset = cc.Asset>(
83
+ url?: string,
84
+ options?: {
85
+ ext?: string;
86
+ enableCache?: boolean;
87
+ maxRetryCount?: number;
88
+ cb?: (asset: T) => void;
89
+ },
90
+ ): Promise<T> {
91
+ return await this.invoke(
92
+ FW.Entry.promiseMgr.execute((resolve, reject, signal) => {
93
+ cc.assetManager.loadRemote(
94
+ options?.enableCache === undefined || !options?.enableCache
95
+ ? `${url}?t=${new Date().getTime()}`
96
+ : `${url}`,
97
+ {
98
+ ext: options?.ext,
99
+ cacheEnabled: options?.enableCache || false,
100
+ maxRetryCount: options?.maxRetryCount || 0,
101
+ },
102
+ (err, asset) => {
103
+ if (err || !asset) {
104
+ reject(err);
105
+ }
106
+ options?.cb?.(asset as T);
107
+ resolve(asset as T);
108
+ },
109
+ );
110
+ }, FWSystemConfig.PromiseConfig.loadAsset).promise,
111
+ `loadAssets -> ${url}`,
112
+ );
113
+ }
114
+
115
+ async preLoad(assetProperty: FW.AssetProperty | FW.AssetProperty[]) {
116
+ if (!assetProperty) {
117
+ FW.Log.error(`加载资源失败,请检查参数列表:${assetProperty}!`);
118
+ return undefined;
119
+ }
120
+
121
+ const propertys = Array.isArray(assetProperty)
122
+ ? assetProperty
123
+ : [assetProperty];
124
+
125
+ await Promise.all(
126
+ propertys.map(async (property) => {
127
+ const bundleName = property.bundle || FW.Entry.bundleName;
128
+ const type = property.type;
129
+ const path = property.path;
130
+ const progress = property.progress;
131
+
132
+ if (!bundleName || bundleName === "") {
133
+ FW.Log.error(`找不到bundle${bundleName},或者bundle未加载!`);
134
+ return undefined;
135
+ }
136
+
137
+ if (!path || path === "") {
138
+ FW.Log.error(`找不到资源路径${path},请检查!`);
139
+ return undefined;
140
+ }
141
+
142
+ let bundle: cc.AssetManager.Bundle =
143
+ await this.resMgr.loadBundle(bundleName);
144
+
145
+ if (!bundle) {
146
+ FW.Log.error(`加载bundle失败,请检查!`);
147
+ return undefined;
148
+ }
149
+
150
+ return await this.invoke(
151
+ FW.Entry.promiseMgr.execute((resolve, reject, signal) => {
152
+ bundle.preload(
153
+ path,
154
+ type,
155
+ (
156
+ finish: number,
157
+ total: number,
158
+ item: cc.AssetManager.RequestItem,
159
+ ) => {
160
+ progress?.(finish, total, item);
161
+ },
162
+ (err: Error, item: cc.AssetManager.RequestItem[]) => {
163
+ if (err || !item || item?.length == 0) {
164
+ reject(err || "preload failed!");
165
+ }
166
+ resolve(item);
167
+ },
168
+ );
169
+ }, FWSystemConfig.PromiseConfig.loadAsset).promise,
170
+ `preLoadAssets -> ${property.path}`,
171
+ );
172
+ }),
173
+ );
174
+ }
175
+
176
+ /**
177
+ * 加载资源
178
+ * @param assetProperty
179
+ * @returns
180
+ */
181
+ async load(assetProperty: FW.AssetProperty): Promise<FW.AssetData> {
182
+ if (!assetProperty) {
183
+ FW.Log.error(`加载资源失败,请检查参数列表:${assetProperty}!`);
184
+ return undefined;
185
+ }
186
+ const bundleName = assetProperty.bundle || FW.Entry.bundleName;
187
+ const type = assetProperty.type;
188
+ const path = assetProperty.path;
189
+ const cb = assetProperty.cb;
190
+ const progress = assetProperty.progress;
191
+ const autoRelease = assetProperty.autoRelease;
192
+
193
+ if (!bundleName || bundleName === "") {
194
+ FW.Log.error(`找不到bundle${bundleName},或者bundle未加载!`);
195
+ return undefined;
196
+ }
197
+
198
+ if (!path || path === "") {
199
+ FW.Log.error(`找不到资源路径${path},请检查!`);
200
+ return undefined;
201
+ }
202
+
203
+ let bundle: cc.AssetManager.Bundle =
204
+ await this.resMgr.loadBundle(bundleName);
205
+
206
+ if (!bundle) {
207
+ FW.Log.error(`加载bundle失败,请检查!`);
208
+ return undefined;
209
+ }
210
+ const key = this.createAssetMapKey(assetProperty);
211
+
212
+ if (this.assetsMap.has(key)) {
213
+ const assetData = this.assetsMap.get(key);
214
+
215
+ if (!assetData.loaded || !cc.isValid(assetData.asset)) {
216
+ this.assetsMap.delete(key);
217
+ } else {
218
+ return assetData;
219
+ }
220
+ }
221
+
222
+ const assetData = new FWAssetData();
223
+
224
+ assetData.loaded = false;
225
+
226
+ return this.invoke(
227
+ FW.Entry.promiseMgr.execute((resolve, reject, signal) => {
228
+ const self = this;
229
+ bundle.load(
230
+ path,
231
+ type,
232
+ (
233
+ finish: number,
234
+ total: number,
235
+ item: cc.AssetManager.RequestItem,
236
+ ) => {
237
+ progress?.(finish, total, item);
238
+ },
239
+ (err: Error, asset: cc.Asset) => {
240
+ if (err || !asset) {
241
+ reject(err);
242
+ return;
243
+ }
244
+ assetData.loaded = true;
245
+ assetData.dependentBundle = bundleName;
246
+ assetData.uuid = asset["_uuid"];
247
+ assetData.autoRelease = autoRelease;
248
+ assetData.asset = asset;
249
+ assetData.user = assetProperty.user;
250
+ assetData.assetProperty = assetProperty;
251
+
252
+ self.assetsMap.set(key, assetData);
253
+
254
+ cb?.(assetData);
255
+ resolve(assetData);
256
+ },
257
+ );
258
+ }, FWSystemConfig.PromiseConfig.loadAsset).promise,
259
+ `loadAssets -> ${assetProperty.path}`,
260
+ );
261
+ }
262
+ /**
263
+ * 加载文件夹
264
+ * @param assetProperty
265
+ * @returns
266
+ */
267
+ async loadDir(assetProperty: FW.AssetProperty): Promise<FW.AssetData[]> {
268
+ if (!assetProperty) {
269
+ FW.Log.error(`加载资源失败,请检查参数列表:${assetProperty}!`);
270
+ return undefined;
271
+ }
272
+ const bundleName = assetProperty.bundle || FW.Entry.bundleName;
273
+ const type = assetProperty.type;
274
+ const path = assetProperty.path;
275
+ const cb = assetProperty.cb;
276
+ const autoRelease = assetProperty.autoRelease;
277
+
278
+ if (!bundleName || bundleName === "") {
279
+ FW.Log.error(`找不到bundle${bundleName},或者bundle未加载!`);
280
+ return undefined;
281
+ }
282
+
283
+ if (!path || path === "") {
284
+ FW.Log.error(`找不到资源路径${path},请检查!`);
285
+ return undefined;
286
+ }
287
+
288
+ const bundle: cc.AssetManager.Bundle =
289
+ await this.resMgr.loadBundle(bundleName);
290
+
291
+ if (!bundle) {
292
+ FW.Log.error(`加载bundle失败,请检查!`);
293
+ return undefined;
294
+ }
295
+
296
+ return this.invoke(
297
+ FW.Entry.promiseMgr.execute((resolve, reject) => {
298
+ bundle.loadDir(path, type, (err: Error, assets: cc.Asset[]) => {
299
+ if (err || assets.length === 0) {
300
+ FW.Log.error(`加载资源失败:${path},请检查!`, err);
301
+ reject(err);
302
+ return;
303
+ }
304
+ const assetDataList = [];
305
+ for (const asset of assets) {
306
+ const assetData = new FWAssetData();
307
+ assetData.loaded = true;
308
+ assetData.dependentBundle = bundleName;
309
+ assetData.uuid = asset["_uuid"];
310
+ assetData.autoRelease = autoRelease;
311
+ assetData.asset = asset;
312
+ assetData.type = type;
313
+ assetData.assetProperty = assetProperty;
314
+
315
+ const key = `${this.createAssetMapKey({
316
+ bundle: bundleName,
317
+ path: path,
318
+ })}/${asset.name}${type ? `_${type?.name}` : ""}`;
319
+
320
+ this.assetsMap.set(key, assetData);
321
+ assetDataList.push(assetData);
322
+ }
323
+ cb?.(assetDataList);
324
+ resolve(assetDataList);
325
+ });
326
+ }, FWSystemConfig.PromiseConfig.loadAsset).promise,
327
+ `loadDir -> ${path}`,
328
+ );
329
+ }
330
+
331
+ /**
332
+ * 获取资源
333
+ * @param assetProperty
334
+ * @returns
335
+ */
336
+ get(assetProperty: FW.AssetProperty): FW.AssetData {
337
+ if (!assetProperty) {
338
+ FW.Log.error(`加载资源失败,请检查参数列表:${assetProperty}!`);
339
+ return undefined;
340
+ }
341
+ const bundleName = assetProperty.bundle || FW.Entry.bundleName;
342
+ const path = assetProperty.path;
343
+
344
+ if (!bundleName || bundleName === "") {
345
+ FW.Log.error(`找不到bundle${bundleName},或者bundle未加载!`);
346
+ return undefined;
347
+ }
348
+
349
+ if (!path || path === "") {
350
+ FW.Log.error(`找不到资源路径${path},请检查!`);
351
+ return undefined;
352
+ }
353
+
354
+ if (!this.has(assetProperty)) {
355
+ FW.Log.error("获取资源失败,请先加载!", assetProperty);
356
+ return undefined;
357
+ }
358
+
359
+ const key = this.createAssetMapKey(assetProperty);
360
+ const assetsData = this.assetsMap.get(key);
361
+ return assetsData;
362
+ }
363
+
364
+ /**
365
+ * 释放资源
366
+ * @param assetProperty
367
+ * @returns
368
+ */
369
+ release(assetProperty: FW.AssetProperty): void {
370
+ if (!assetProperty) {
371
+ FW.Log.error(`加载资源失败,请检查参数列表:${assetProperty}!`);
372
+ return undefined;
373
+ }
374
+ const bundleName = assetProperty.bundle || FW.Entry.bundleName;
375
+ const path = assetProperty.path;
376
+ const autoRelease = assetProperty.autoRelease;
377
+
378
+ if (!bundleName || bundleName === "") {
379
+ FW.Log.error(`找不到bundle${bundleName},或者bundle未加载!`);
380
+ return undefined;
381
+ }
382
+
383
+ if (!path || path === "") {
384
+ FW.Log.error(`找不到资源路径${path},请检查!`);
385
+ return undefined;
386
+ }
387
+
388
+ if (!this.has(assetProperty)) {
389
+ return undefined;
390
+ }
391
+
392
+ const key = this.createAssetMapKey(assetProperty);
393
+ const assetData = this.assetsMap.get(key);
394
+
395
+ if (!assetData || !assetData.asset) {
396
+ this.assetsMap.delete(key);
397
+ return;
398
+ }
399
+
400
+ if (assetData.asset.refCount <= 0 && autoRelease) {
401
+ this.resMgr.getBundle(bundleName)?.release(path);
402
+ this.assetsMap.delete(key);
403
+ }
404
+ }
405
+
406
+ /**
407
+ * 获取资源引用计数
408
+ * @param assetProperty
409
+ * @returns
410
+ */
411
+ public getReferenceCount(assetProperty: FW.AssetProperty): number {
412
+ const key = this.createAssetMapKey(assetProperty);
413
+ return this.assetsMap.get(key)?.asset?.refCount || 0;
414
+ }
415
+
416
+ /**
417
+ * 资源是否存在
418
+ * @param uid
419
+ * @returns
420
+ */
421
+ has(assetProperty: FW.AssetProperty) {
422
+ if (!assetProperty) {
423
+ FW.Log.error(`判断资源是否存在失败,请检查参数列表:${assetProperty}!`);
424
+ return;
425
+ }
426
+
427
+ return this.assetsMap.has(this.createAssetMapKey(assetProperty));
428
+ }
429
+
430
+ private createAssetMapKey(assetProperty: FW.AssetProperty) {
431
+ const bundleName = assetProperty.bundle || FW.Entry.bundleName;
432
+ const path = assetProperty.path;
433
+ const type = assetProperty.type;
434
+
435
+ let typeKey: string | undefined;
436
+ if (typeof type === "string") {
437
+ typeKey = type;
438
+ } else if (type) {
439
+ typeKey = (type as any).name;
440
+ }
441
+
442
+ if (!typeKey) {
443
+ return `${bundleName}_${path}`;
444
+ }
445
+ return `${bundleName}_${path}_${typeKey}`;
446
+ }
447
+
448
+ public onDestroy(): void {
449
+ this.assetsMap.clear();
450
+ }
451
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "ver": "1.1.0",
3
+ "uuid": "eb4a5a74-0281-4aba-af9b-ae616f1d94f2",
4
+ "importer": "typescript",
5
+ "isPlugin": false,
6
+ "loadPluginInWeb": true,
7
+ "loadPluginInNative": true,
8
+ "loadPluginInEditor": false,
9
+ "subMetas": {}
10
+ }