目录— 个文件
⌕
目录加载中…
…/core/resource/ResourceInfoAnalysis.ts
1import { JsonAsset, resources, TextAsset, Asset } from 'cc';
2import { Framework } from "../../Framework";
3import { ResourceInfo, ResourceInfoJSON } from "./ResourceInfo";
4import { ResourceRetryConfig } from "./ResourceFrameConfig";
5
6/**
7 * 资源信息解析脚本
8 */
9export class ResourceInfoAnalysis {
10
11 /**
12 * 带重试的资源加载(resources.load)
13 */
14 private static async LoadResourceConfig<T extends Asset>(path: string, type: new (...args: any[]) => T): Promise<T | null> {
15 const maxRetry = ResourceRetryConfig.maxRetryCount;
16 const interval = ResourceRetryConfig.retryInterval;
17
18 for (let attempt = 0; attempt <= maxRetry; attempt++) {
19 if (attempt > 0) {
20 Framework.Log.Warn(`${Framework.Res.Name}_ResourceInfoAnalysis`, "LoadResourceConfig",
21 `重试第 ${attempt}/${maxRetry} 次加载: ${path}`);
22 await new Promise<void>(r => setTimeout(r, interval));
23 }
24
25 const asset = await new Promise<T | null>((resolve) => {
26 resources.load(path, type, (err, asset) => {
27 if (err) {
28 Framework.Log.Warn(`${Framework.Res.Name}_ResourceInfoAnalysis`, "LoadResourceConfig",
29 `第 ${attempt + 1} 次加载失败: ${path}`, err);
30 resolve(null);
31 } else {
32 resolve(asset);
33 }
34 });
35 });
36
37 if (asset) {
38 return asset;
39 }
40 }
41
42 Framework.Log.Warn(`${Framework.Res.Name}_ResourceInfoAnalysis`, "LoadResourceConfig",
43 `配置加载最终失败(已重试 ${maxRetry} 次): ${path}`);
44 return null;
45 }
46
47 /**
48 * 解析资源配置信息
49 * @constructor
50 */
51 public static async AnalysisResourceInfosXML(resourceInfos: Map<string, ResourceInfo>) {
52 try {
53 // 1. 从 resources 目录加载 XML 配置文件(resource_config.xml)
54 const xmlAsset = await ResourceInfoAnalysis.LoadResourceConfig("resource_config", TextAsset);
55
56 if (!xmlAsset) {
57 return;
58 }
59
60 // 2. 解析 XML 内容
61 const xmlContent = xmlAsset.text;
62 const parser = new DOMParser(); // 使用浏览器内置的 XML 解析器(Cocos 环境支持)
63 const xmlDoc = parser.parseFromString(xmlContent, "text/xml");
64
65 // 3. 提取所有 <Bundle> 节点下的 <Resource> 配置
66 const bundleNodes = xmlDoc.getElementsByTagName("Bundle");
67 for (let i = 0; i < bundleNodes.length; i++) {
68 const bundleNode = bundleNodes[i];
69 const bundleName = bundleNode.getAttribute("name") || ""; // Bundle 名称(如 "common")
70
71 // 遍历 Bundle 下的所有 Resource 节点
72 const resourceNodes = bundleNode.getElementsByTagName("Resource");
73 for (let j = 0; j < resourceNodes.length; j++) {
74 const resourceNode = resourceNodes[j];
75
76 // 提取 Resource 节点属性(对应 XML 中的 name、path、type)
77 const resourceName = resourceNode.getAttribute("name");
78 const resourcePath = resourceNode.getAttribute("path");
79 const resourceType = resourceNode.getAttribute("type");
80 const inBundlePath = resourceNode.getAttribute("inBundlePath");
81
82 // 校验必要属性
83 if (!resourceName || !resourcePath || !resourceType) {
84 Framework.Log.Warn(`${Framework.Res.Name}_ResourceInfoAnalysis`, "AnalysisResourceInfos",
85 `XML 配置异常:缺少必要属性,Bundle: ${bundleName},节点索引: ${j}`);
86 continue;
87 }
88
89 // 4. 生成 ResourceInfo 并添加到字典
90 const resourceInfo: ResourceInfo = {
91 Name: resourceName,
92 Path: resourcePath, // 相对于 assets 的路径(如 "remoteRes/common/背景使用说明.png")
93 Type: resourceType, // 资源类型(如 "Texture2D")
94 Bundle: bundleName, // 资源所属 Bundle 名称
95 InBundlePath: inBundlePath!, // 资源在 Bundle 中的路径(如 "背景使用说明")
96 };
97
98 // 检查资源名称唯一性
99 if (resourceInfos.has(resourceName)) {
100 Framework.Log.Warn(`${Framework.Res.Name}_ResourceInfoAnalysis`, "AnalysisResourceInfos",
101 `资源名称重复,自动覆盖:${resourceName}`);
102 }
103 resourceInfos.set(resourceName, resourceInfo);
104 }
105 }
106
107 Framework.Log.Log(`${Framework.Res.Name}_ResourceInfoAnalysis`, "AnalysisResourceInfos",
108 `配置加载完成,共加载资源信息 ${resourceInfos.size} 条`);
109 } catch (e) {
110 Framework.Log.Warn(`${Framework.Res.Name}_ResourceInfoAnalysis`, "AnalysisResourceInfos", `配置加载失败:${JSON.stringify(e)}`);
111 }
112 }
113
114 public static async AnalysisResourceInfosJSON(resourceInfos: Map<string, ResourceInfo>) {
115 try {
116 // 1. 从 resources 目录加载 JSON 配置文件(resource_config.json)
117 const jsonAsset = await ResourceInfoAnalysis.LoadResourceConfig("resource_config", JsonAsset);
118
119 if (!jsonAsset) {
120 return;
121 }
122 for (const key in jsonAsset.json) {
123 const resourceInfo: ResourceInfoJSON[] = jsonAsset.json?.[key];
124 if (!resourceInfo) {
125 continue;
126 }
127 for (const resourceInfoItem of resourceInfo) {
128 resourceInfos.set(resourceInfoItem.n, {
129 Name: resourceInfoItem.n,
130 Path: resourceInfoItem.p,
131 Bundle: key,
132 InBundlePath: resourceInfoItem.i,
133 Type: resourceInfoItem.t,
134 });
135 }
136 }
137 } catch (e) {
138 Framework.Log.Warn(`${Framework.Res.Name}_ResourceInfoAnalysis`, "AnalysisResourceInfos", `配置加载失败:${JSON.stringify(e)}`);
139 }
140 }
141}
讲解142 行
资源清单解析器人工通读
ResourceInfoAnalysis.ts
从 resources 目录加载 resource_config 清单,XML 与 JSON 两种格式都解析为名字到 ResourceInfo 的 Map。资源管理器实际只调用 JSON 版本。
先看带重试的 LoadResourceConfig,再看 JSON 解析,XML 版本可后看。
特殊处理与易错点
- 异常兜底加载失败按全局 maxRetryCount 重试,每次重试前等待 retryInterval,最终失败返回 null 而不是抛错。L18
- 易错点XML 与 JSON 都请求 resource_config 路径,靠传入的资源类型区分扩展名,实际只装了对应类型时另一种会加载失败。L54
- 特殊处理XML 解析遇到同名资源只打警告并覆盖旧条目,JSON 解析则直接 set,同样会静默覆盖。L99
- 易错点XML 分支依赖浏览器的 DOMParser,原生小游戏环境不一定存在该 API,异常会被外层 catch 吞掉。L62
9注释ResourceInfoAnalysis:资源信息解析脚本
14方法循环调用 resources.load,失败按全局配置等待后重试,成功即返回资源。
51方法加载文本资源解析 XML,遍历 Bundle 下节点,校验必要属性后写入字典。
54注释1. 从 resources 目录加载 XML 配置文件(resource_config.xml)
61注释2. 解析 XML 内容
66注释3. 提取所有 <Bundle> 节点下的 <Resource> 配置
72注释遍历 Bundle 下的所有 Resource 节点
77注释提取 Resource 节点属性(对应 XML 中的 name、path、type)
83注释校验必要属性
90注释4. 生成 ResourceInfo 并添加到字典
99注释检查资源名称唯一性
114方法加载 JsonAsset,按顶层键作为 Bundle 名遍历数组,把短键条目转换后写入资源字典。
117注释1. 从 resources 目录加载 JSON 配置文件(resource_config.json)