AssetBundleCollectorConfig.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Text;
  5. using System.Collections.Generic;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace YooAsset.Editor
  9. {
  10. public class AssetBundleCollectorConfig
  11. {
  12. public const string ConfigVersion = "2.4";
  13. public const string XmlVersion = "Version";
  14. public const string XmlCommon = "Common";
  15. public const string XmlEnableAddressable = "AutoAddressable";
  16. public const string XmlLocationToLower = "LocationToLower";
  17. public const string XmlIncludeAssetGUID = "IncludeAssetGUID";
  18. public const string XmlUniqueBundleName = "UniqueBundleName";
  19. public const string XmlShowPackageView = "ShowPackageView";
  20. public const string XmlShowEditorAlias = "ShowEditorAlias";
  21. public const string XmlPackage = "Package";
  22. public const string XmlPackageName = "PackageName";
  23. public const string XmlPackageDesc = "PackageDesc";
  24. public const string XmlGroup = "Group";
  25. public const string XmlGroupActiveRule = "GroupActiveRule";
  26. public const string XmlGroupName = "GroupName";
  27. public const string XmlGroupDesc = "GroupDesc";
  28. public const string XmlCollector = "Collector";
  29. public const string XmlCollectPath = "CollectPath";
  30. public const string XmlCollectorGUID = "CollectGUID";
  31. public const string XmlCollectorType = "CollectType";
  32. public const string XmlAddressRule = "AddressRule";
  33. public const string XmlPackRule = "PackRule";
  34. public const string XmlFilterRule = "FilterRule";
  35. public const string XmlUserData = "UserData";
  36. public const string XmlAssetTags = "AssetTags";
  37. /// <summary>
  38. /// 导入XML配置表
  39. /// </summary>
  40. public static void ImportXmlConfig(string filePath)
  41. {
  42. if (File.Exists(filePath) == false)
  43. throw new FileNotFoundException(filePath);
  44. if (Path.GetExtension(filePath) != ".xml")
  45. throw new Exception($"Only support xml : {filePath}");
  46. // 加载配置文件
  47. XmlDocument xmlDoc = new XmlDocument();
  48. xmlDoc.Load(filePath);
  49. XmlElement root = xmlDoc.DocumentElement;
  50. // 读取配置版本
  51. string configVersion = root.GetAttribute(XmlVersion);
  52. if (configVersion != ConfigVersion)
  53. {
  54. if (UpdateXmlConfig(xmlDoc) == false)
  55. throw new Exception($"The config version update failed : {configVersion} -> {ConfigVersion}");
  56. else
  57. Debug.Log($"The config version update succeed : {configVersion} -> {ConfigVersion}");
  58. }
  59. // 读取公共配置
  60. bool enableAddressable = false;
  61. bool locationToLower = false;
  62. bool includeAssetGUID = false;
  63. bool uniqueBundleName = false;
  64. bool showPackageView = false;
  65. bool showEditorAlias = false;
  66. var commonNodeList = root.GetElementsByTagName(XmlCommon);
  67. if (commonNodeList.Count > 0)
  68. {
  69. XmlElement commonElement = commonNodeList[0] as XmlElement;
  70. if (commonElement.HasAttribute(XmlEnableAddressable))
  71. enableAddressable = commonElement.GetAttribute(XmlEnableAddressable) == "True" ? true : false;
  72. if (commonElement.HasAttribute(XmlLocationToLower))
  73. locationToLower = commonElement.GetAttribute(XmlLocationToLower) == "True" ? true : false;
  74. if (commonElement.HasAttribute(XmlIncludeAssetGUID))
  75. includeAssetGUID = commonElement.GetAttribute(XmlIncludeAssetGUID) == "True" ? true : false;
  76. if (commonElement.HasAttribute(XmlUniqueBundleName))
  77. uniqueBundleName = commonElement.GetAttribute(XmlUniqueBundleName) == "True" ? true : false;
  78. if (commonElement.HasAttribute(XmlShowPackageView))
  79. showPackageView = commonElement.GetAttribute(XmlShowPackageView) == "True" ? true : false;
  80. if (commonElement.HasAttribute(XmlShowEditorAlias))
  81. showEditorAlias = commonElement.GetAttribute(XmlShowEditorAlias) == "True" ? true : false;
  82. }
  83. // 读取包裹配置
  84. List<AssetBundleCollectorPackage> packages = new List<AssetBundleCollectorPackage>();
  85. var packageNodeList = root.GetElementsByTagName(XmlPackage);
  86. foreach (var packageNode in packageNodeList)
  87. {
  88. XmlElement packageElement = packageNode as XmlElement;
  89. if (packageElement.HasAttribute(XmlPackageName) == false)
  90. throw new Exception($"Not found attribute {XmlPackageName} in {XmlPackage}");
  91. if (packageElement.HasAttribute(XmlPackageDesc) == false)
  92. throw new Exception($"Not found attribute {XmlPackageDesc} in {XmlPackage}");
  93. AssetBundleCollectorPackage package = new AssetBundleCollectorPackage();
  94. package.PackageName = packageElement.GetAttribute(XmlPackageName);
  95. package.PackageDesc = packageElement.GetAttribute(XmlPackageDesc);
  96. packages.Add(package);
  97. // 读取分组配置
  98. var groupNodeList = packageElement.GetElementsByTagName(XmlGroup);
  99. foreach (var groupNode in groupNodeList)
  100. {
  101. XmlElement groupElement = groupNode as XmlElement;
  102. if (groupElement.HasAttribute(XmlGroupActiveRule) == false)
  103. throw new Exception($"Not found attribute {XmlGroupActiveRule} in {XmlGroup}");
  104. if (groupElement.HasAttribute(XmlGroupName) == false)
  105. throw new Exception($"Not found attribute {XmlGroupName} in {XmlGroup}");
  106. if (groupElement.HasAttribute(XmlGroupDesc) == false)
  107. throw new Exception($"Not found attribute {XmlGroupDesc} in {XmlGroup}");
  108. if (groupElement.HasAttribute(XmlAssetTags) == false)
  109. throw new Exception($"Not found attribute {XmlAssetTags} in {XmlGroup}");
  110. AssetBundleCollectorGroup group = new AssetBundleCollectorGroup();
  111. group.ActiveRuleName = groupElement.GetAttribute(XmlGroupActiveRule);
  112. group.GroupName = groupElement.GetAttribute(XmlGroupName);
  113. group.GroupDesc = groupElement.GetAttribute(XmlGroupDesc);
  114. group.AssetTags = groupElement.GetAttribute(XmlAssetTags);
  115. package.Groups.Add(group);
  116. // 读取收集器配置
  117. var collectorNodeList = groupElement.GetElementsByTagName(XmlCollector);
  118. foreach (var collectorNode in collectorNodeList)
  119. {
  120. XmlElement collectorElement = collectorNode as XmlElement;
  121. if (collectorElement.HasAttribute(XmlCollectPath) == false)
  122. throw new Exception($"Not found attribute {XmlCollectPath} in {XmlCollector}");
  123. if (collectorElement.HasAttribute(XmlCollectorGUID) == false)
  124. throw new Exception($"Not found attribute {XmlCollectorGUID} in {XmlCollector}");
  125. if (collectorElement.HasAttribute(XmlCollectorType) == false)
  126. throw new Exception($"Not found attribute {XmlCollectorType} in {XmlCollector}");
  127. if (collectorElement.HasAttribute(XmlAddressRule) == false)
  128. throw new Exception($"Not found attribute {XmlAddressRule} in {XmlCollector}");
  129. if (collectorElement.HasAttribute(XmlPackRule) == false)
  130. throw new Exception($"Not found attribute {XmlPackRule} in {XmlCollector}");
  131. if (collectorElement.HasAttribute(XmlFilterRule) == false)
  132. throw new Exception($"Not found attribute {XmlFilterRule} in {XmlCollector}");
  133. if (collectorElement.HasAttribute(XmlUserData) == false)
  134. throw new Exception($"Not found attribute {XmlUserData} in {XmlCollector}");
  135. if (collectorElement.HasAttribute(XmlAssetTags) == false)
  136. throw new Exception($"Not found attribute {XmlAssetTags} in {XmlCollector}");
  137. AssetBundleCollector collector = new AssetBundleCollector();
  138. collector.CollectPath = collectorElement.GetAttribute(XmlCollectPath);
  139. collector.CollectorGUID = collectorElement.GetAttribute(XmlCollectorGUID);
  140. collector.CollectorType = EditorTools.NameToEnum<ECollectorType>(collectorElement.GetAttribute(XmlCollectorType));
  141. collector.AddressRuleName = collectorElement.GetAttribute(XmlAddressRule);
  142. collector.PackRuleName = collectorElement.GetAttribute(XmlPackRule);
  143. collector.FilterRuleName = collectorElement.GetAttribute(XmlFilterRule);
  144. collector.UserData = collectorElement.GetAttribute(XmlUserData);
  145. collector.AssetTags = collectorElement.GetAttribute(XmlAssetTags);
  146. group.Collectors.Add(collector);
  147. }
  148. }
  149. }
  150. // 检测配置错误
  151. foreach (var package in packages)
  152. {
  153. package.CheckConfigError();
  154. }
  155. // 保存配置数据
  156. AssetBundleCollectorSettingData.ClearAll();
  157. AssetBundleCollectorSettingData.Setting.EnableAddressable = enableAddressable;
  158. AssetBundleCollectorSettingData.Setting.LocationToLower = locationToLower;
  159. AssetBundleCollectorSettingData.Setting.IncludeAssetGUID = includeAssetGUID;
  160. AssetBundleCollectorSettingData.Setting.UniqueBundleName = uniqueBundleName;
  161. AssetBundleCollectorSettingData.Setting.ShowPackageView = showPackageView;
  162. AssetBundleCollectorSettingData.Setting.ShowEditorAlias = showEditorAlias;
  163. AssetBundleCollectorSettingData.Setting.Packages.AddRange(packages);
  164. AssetBundleCollectorSettingData.SaveFile();
  165. Debug.Log($"导入配置完毕!");
  166. }
  167. /// <summary>
  168. /// 导出XML配置表
  169. /// </summary>
  170. public static void ExportXmlConfig(string savePath)
  171. {
  172. if (File.Exists(savePath))
  173. File.Delete(savePath);
  174. StringBuilder sb = new StringBuilder();
  175. sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  176. sb.AppendLine("<root>");
  177. sb.AppendLine("</root>");
  178. XmlDocument xmlDoc = new XmlDocument();
  179. xmlDoc.LoadXml(sb.ToString());
  180. XmlElement root = xmlDoc.DocumentElement;
  181. // 设置配置版本
  182. root.SetAttribute(XmlVersion, ConfigVersion);
  183. // 设置公共配置
  184. var commonElement = xmlDoc.CreateElement(XmlCommon);
  185. commonElement.SetAttribute(XmlEnableAddressable, AssetBundleCollectorSettingData.Setting.EnableAddressable.ToString());
  186. commonElement.SetAttribute(XmlLocationToLower, AssetBundleCollectorSettingData.Setting.LocationToLower.ToString());
  187. commonElement.SetAttribute(XmlIncludeAssetGUID, AssetBundleCollectorSettingData.Setting.IncludeAssetGUID.ToString());
  188. commonElement.SetAttribute(XmlUniqueBundleName, AssetBundleCollectorSettingData.Setting.UniqueBundleName.ToString());
  189. commonElement.SetAttribute(XmlShowPackageView, AssetBundleCollectorSettingData.Setting.ShowPackageView.ToString());
  190. commonElement.SetAttribute(XmlShowEditorAlias, AssetBundleCollectorSettingData.Setting.ShowEditorAlias.ToString());
  191. root.AppendChild(commonElement);
  192. // 设置Package配置
  193. foreach (var package in AssetBundleCollectorSettingData.Setting.Packages)
  194. {
  195. var packageElement = xmlDoc.CreateElement(XmlPackage);
  196. packageElement.SetAttribute(XmlPackageName, package.PackageName);
  197. packageElement.SetAttribute(XmlPackageDesc, package.PackageDesc);
  198. root.AppendChild(packageElement);
  199. // 设置分组配置
  200. foreach (var group in package.Groups)
  201. {
  202. var groupElement = xmlDoc.CreateElement(XmlGroup);
  203. groupElement.SetAttribute(XmlGroupActiveRule, group.ActiveRuleName);
  204. groupElement.SetAttribute(XmlGroupName, group.GroupName);
  205. groupElement.SetAttribute(XmlGroupDesc, group.GroupDesc);
  206. groupElement.SetAttribute(XmlAssetTags, group.AssetTags);
  207. packageElement.AppendChild(groupElement);
  208. // 设置收集器配置
  209. foreach (var collector in group.Collectors)
  210. {
  211. var collectorElement = xmlDoc.CreateElement(XmlCollector);
  212. collectorElement.SetAttribute(XmlCollectPath, collector.CollectPath);
  213. collectorElement.SetAttribute(XmlCollectorGUID, collector.CollectorGUID);
  214. collectorElement.SetAttribute(XmlCollectorType, collector.CollectorType.ToString());
  215. collectorElement.SetAttribute(XmlAddressRule, collector.AddressRuleName);
  216. collectorElement.SetAttribute(XmlPackRule, collector.PackRuleName);
  217. collectorElement.SetAttribute(XmlFilterRule, collector.FilterRuleName);
  218. collectorElement.SetAttribute(XmlUserData, collector.UserData);
  219. collectorElement.SetAttribute(XmlAssetTags, collector.AssetTags);
  220. groupElement.AppendChild(collectorElement);
  221. }
  222. }
  223. }
  224. // 生成配置文件
  225. xmlDoc.Save(savePath);
  226. Debug.Log($"导出配置完毕!");
  227. }
  228. /// <summary>
  229. /// 升级XML配置表
  230. /// </summary>
  231. private static bool UpdateXmlConfig(XmlDocument xmlDoc)
  232. {
  233. XmlElement root = xmlDoc.DocumentElement;
  234. string configVersion = root.GetAttribute(XmlVersion);
  235. if (configVersion == ConfigVersion)
  236. return true;
  237. // 1.0 -> 2.0
  238. if (configVersion == "1.0")
  239. {
  240. // 添加公共元素属性
  241. var commonNodeList = root.GetElementsByTagName(XmlCommon);
  242. if (commonNodeList.Count > 0)
  243. {
  244. XmlElement commonElement = commonNodeList[0] as XmlElement;
  245. if (commonElement.HasAttribute(XmlShowPackageView) == false)
  246. commonElement.SetAttribute(XmlShowPackageView, "False");
  247. }
  248. // 添加包裹元素
  249. var packageElement = xmlDoc.CreateElement(XmlPackage);
  250. packageElement.SetAttribute(XmlPackageName, "DefaultPackage");
  251. packageElement.SetAttribute(XmlPackageDesc, string.Empty);
  252. root.AppendChild(packageElement);
  253. // 获取所有分组元素
  254. var groupNodeList = root.GetElementsByTagName(XmlGroup);
  255. List<XmlElement> temper = new List<XmlElement>(groupNodeList.Count);
  256. foreach (var groupNode in groupNodeList)
  257. {
  258. XmlElement groupElement = groupNode as XmlElement;
  259. var collectorNodeList = groupElement.GetElementsByTagName(XmlCollector);
  260. foreach (var collectorNode in collectorNodeList)
  261. {
  262. XmlElement collectorElement = collectorNode as XmlElement;
  263. if (collectorElement.HasAttribute(XmlCollectorGUID) == false)
  264. collectorElement.SetAttribute(XmlCollectorGUID, string.Empty);
  265. }
  266. temper.Add(groupElement);
  267. }
  268. // 将分组元素转移至包裹元素下
  269. foreach (var groupElement in temper)
  270. {
  271. root.RemoveChild(groupElement);
  272. packageElement.AppendChild(groupElement);
  273. }
  274. // 更新版本
  275. root.SetAttribute(XmlVersion, "2.0");
  276. return UpdateXmlConfig(xmlDoc);
  277. }
  278. // 2.0 -> 2.1
  279. if (configVersion == "2.0")
  280. {
  281. // 添加公共元素属性
  282. var commonNodeList = root.GetElementsByTagName(XmlCommon);
  283. if (commonNodeList.Count > 0)
  284. {
  285. XmlElement commonElement = commonNodeList[0] as XmlElement;
  286. if (commonElement.HasAttribute(XmlUniqueBundleName) == false)
  287. commonElement.SetAttribute(XmlUniqueBundleName, "False");
  288. }
  289. // 更新版本
  290. root.SetAttribute(XmlVersion, "2.1");
  291. return UpdateXmlConfig(xmlDoc);
  292. }
  293. // 2.1 -> 2.2
  294. if (configVersion == "2.1")
  295. {
  296. // 添加公共元素属性
  297. var commonNodeList = root.GetElementsByTagName(XmlCommon);
  298. if (commonNodeList.Count > 0)
  299. {
  300. XmlElement commonElement = commonNodeList[0] as XmlElement;
  301. if (commonElement.HasAttribute(XmlShowEditorAlias) == false)
  302. commonElement.SetAttribute(XmlShowEditorAlias, "False");
  303. }
  304. // 更新版本
  305. root.SetAttribute(XmlVersion, "2.2");
  306. return UpdateXmlConfig(xmlDoc);
  307. }
  308. // 2.2 -> 2.3
  309. if (configVersion == "2.2")
  310. {
  311. // 获取所有分组元素
  312. var groupNodeList = root.GetElementsByTagName(XmlGroup);
  313. foreach (var groupNode in groupNodeList)
  314. {
  315. XmlElement groupElement = groupNode as XmlElement;
  316. var collectorNodeList = groupElement.GetElementsByTagName(XmlCollector);
  317. foreach (var collectorNode in collectorNodeList)
  318. {
  319. XmlElement collectorElement = collectorNode as XmlElement;
  320. if (collectorElement.HasAttribute(XmlUserData) == false)
  321. collectorElement.SetAttribute(XmlUserData, string.Empty);
  322. }
  323. }
  324. // 更新版本
  325. root.SetAttribute(XmlVersion, "2.3");
  326. return UpdateXmlConfig(xmlDoc);
  327. }
  328. // 2.3 -> 2.4
  329. if (configVersion == "2.3")
  330. {
  331. // 获取所有分组元素
  332. var groupNodeList = root.GetElementsByTagName(XmlGroup);
  333. foreach (var groupNode in groupNodeList)
  334. {
  335. XmlElement groupElement = groupNode as XmlElement;
  336. if (groupElement.HasAttribute(XmlGroupActiveRule) == false)
  337. groupElement.SetAttribute(XmlGroupActiveRule, $"{nameof(EnableGroup)}");
  338. }
  339. // 更新版本
  340. root.SetAttribute(XmlVersion, "2.4");
  341. return UpdateXmlConfig(xmlDoc);
  342. }
  343. return false;
  344. }
  345. }
  346. }