AssetBundleCollectorConfig.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 = "1.0";
  13. public const string XmlVersion = "Version";
  14. public const string XmlCommon = "Common";
  15. public const string XmlEnableAddressable = "AutoAddressable";
  16. public const string XmlGroup = "Group";
  17. public const string XmlGroupName = "GroupName";
  18. public const string XmlGroupDesc = "GroupDesc";
  19. public const string XmlCollector = "Collector";
  20. public const string XmlCollectPath = "CollectPath";
  21. public const string XmlCollectorGUID = "CollectGUID";
  22. public const string XmlCollectorType = "CollectType";
  23. public const string XmlAddressRule = "AddressRule";
  24. public const string XmlPackRule = "PackRule";
  25. public const string XmlFilterRule = "FilterRule";
  26. public const string XmlAssetTags = "AssetTags";
  27. /// <summary>
  28. /// 导入XML配置表
  29. /// </summary>
  30. public static void ImportXmlConfig(string filePath)
  31. {
  32. if (File.Exists(filePath) == false)
  33. throw new FileNotFoundException(filePath);
  34. if (Path.GetExtension(filePath) != ".xml")
  35. throw new Exception($"Only support xml : {filePath}");
  36. // 加载配置文件
  37. XmlDocument xml = new XmlDocument();
  38. xml.Load(filePath);
  39. XmlElement root = xml.DocumentElement;
  40. // 读取配置版本
  41. string configVersion = root.GetAttribute(XmlVersion);
  42. if (configVersion != ConfigVersion)
  43. {
  44. throw new Exception($"The config version is invalid : {configVersion}");
  45. }
  46. // 读取公共配置
  47. bool enableAddressable = false;
  48. var commonNodeList = root.GetElementsByTagName(XmlCommon);
  49. if (commonNodeList.Count > 0)
  50. {
  51. XmlElement commonElement = commonNodeList[0] as XmlElement;
  52. if (commonElement.HasAttribute(XmlEnableAddressable) == false)
  53. throw new Exception($"Not found attribute {XmlEnableAddressable} in {XmlCommon}");
  54. enableAddressable = commonElement.GetAttribute(XmlEnableAddressable) == "True" ? true : false;
  55. }
  56. // 读取分组配置
  57. List<AssetBundleCollectorGroup> groupTemper = new List<AssetBundleCollectorGroup>();
  58. var groupNodeList = root.GetElementsByTagName(XmlGroup);
  59. foreach (var groupNode in groupNodeList)
  60. {
  61. XmlElement groupElement = groupNode as XmlElement;
  62. if (groupElement.HasAttribute(XmlGroupName) == false)
  63. throw new Exception($"Not found attribute {XmlGroupName} in {XmlGroup}");
  64. if (groupElement.HasAttribute(XmlGroupDesc) == false)
  65. throw new Exception($"Not found attribute {XmlGroupDesc} in {XmlGroup}");
  66. if (groupElement.HasAttribute(XmlAssetTags) == false)
  67. throw new Exception($"Not found attribute {XmlAssetTags} in {XmlGroup}");
  68. AssetBundleCollectorGroup group = new AssetBundleCollectorGroup();
  69. group.GroupName = groupElement.GetAttribute(XmlGroupName);
  70. group.GroupDesc = groupElement.GetAttribute(XmlGroupDesc);
  71. group.AssetTags = groupElement.GetAttribute(XmlAssetTags);
  72. groupTemper.Add(group);
  73. // 读取收集器配置
  74. var collectorNodeList = groupElement.GetElementsByTagName(XmlCollector);
  75. foreach (var collectorNode in collectorNodeList)
  76. {
  77. XmlElement collectorElement = collectorNode as XmlElement;
  78. if (collectorElement.HasAttribute(XmlCollectPath) == false)
  79. throw new Exception($"Not found attribute {XmlCollectPath} in {XmlCollector}");
  80. if (collectorElement.HasAttribute(XmlCollectorType) == false)
  81. throw new Exception($"Not found attribute {XmlCollectorType} in {XmlCollector}");
  82. if (collectorElement.HasAttribute(XmlAddressRule) == false)
  83. throw new Exception($"Not found attribute {XmlAddressRule} in {XmlCollector}");
  84. if (collectorElement.HasAttribute(XmlPackRule) == false)
  85. throw new Exception($"Not found attribute {XmlPackRule} in {XmlCollector}");
  86. if (collectorElement.HasAttribute(XmlFilterRule) == false)
  87. throw new Exception($"Not found attribute {XmlFilterRule} in {XmlCollector}");
  88. if (collectorElement.HasAttribute(XmlAssetTags) == false)
  89. throw new Exception($"Not found attribute {XmlAssetTags} in {XmlCollector}");
  90. string collectorGUID = string.Empty;
  91. if (collectorElement.HasAttribute(XmlCollectorGUID))
  92. collectorGUID = collectorElement.GetAttribute(XmlCollectorGUID);
  93. AssetBundleCollector collector = new AssetBundleCollector();
  94. collector.CollectPath = collectorElement.GetAttribute(XmlCollectPath);
  95. collector.CollectorGUID = collectorGUID;
  96. collector.CollectorType = StringUtility.NameToEnum<ECollectorType>(collectorElement.GetAttribute(XmlCollectorType));
  97. collector.AddressRuleName = collectorElement.GetAttribute(XmlAddressRule);
  98. collector.PackRuleName = collectorElement.GetAttribute(XmlPackRule);
  99. collector.FilterRuleName = collectorElement.GetAttribute(XmlFilterRule);
  100. collector.AssetTags = collectorElement.GetAttribute(XmlAssetTags);
  101. group.Collectors.Add(collector);
  102. }
  103. }
  104. // 保存配置数据
  105. AssetBundleCollectorSettingData.ClearAll();
  106. AssetBundleCollectorSettingData.Setting.EnableAddressable = enableAddressable;
  107. AssetBundleCollectorSettingData.Setting.Groups.AddRange(groupTemper);
  108. AssetBundleCollectorSettingData.SaveFile();
  109. Debug.Log($"导入配置完毕!");
  110. }
  111. /// <summary>
  112. /// 导出XML配置表
  113. /// </summary>
  114. public static void ExportXmlConfig(string savePath)
  115. {
  116. if (File.Exists(savePath))
  117. File.Delete(savePath);
  118. StringBuilder sb = new StringBuilder();
  119. sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  120. sb.AppendLine("<root>");
  121. sb.AppendLine("</root>");
  122. XmlDocument xmlDoc = new XmlDocument();
  123. xmlDoc.LoadXml(sb.ToString());
  124. XmlElement root = xmlDoc.DocumentElement;
  125. // 设置配置版本
  126. root.SetAttribute(XmlVersion, ConfigVersion);
  127. // 设置公共配置
  128. var commonElement = xmlDoc.CreateElement(XmlCommon);
  129. commonElement.SetAttribute(XmlEnableAddressable, AssetBundleCollectorSettingData.Setting.EnableAddressable.ToString());
  130. root.AppendChild(commonElement);
  131. // 设置分组配置
  132. foreach (var group in AssetBundleCollectorSettingData.Setting.Groups)
  133. {
  134. var groupElement = xmlDoc.CreateElement(XmlGroup);
  135. groupElement.SetAttribute(XmlGroupName, group.GroupName);
  136. groupElement.SetAttribute(XmlGroupDesc, group.GroupDesc);
  137. groupElement.SetAttribute(XmlAssetTags, group.AssetTags);
  138. root.AppendChild(groupElement);
  139. // 设置收集器配置
  140. foreach (var collector in group.Collectors)
  141. {
  142. var collectorElement = xmlDoc.CreateElement(XmlCollector);
  143. collectorElement.SetAttribute(XmlCollectPath, collector.CollectPath);
  144. collectorElement.SetAttribute(XmlCollectorGUID, collector.CollectorGUID);
  145. collectorElement.SetAttribute(XmlCollectorType, collector.CollectorType.ToString());
  146. collectorElement.SetAttribute(XmlAddressRule, collector.AddressRuleName);
  147. collectorElement.SetAttribute(XmlPackRule, collector.PackRuleName);
  148. collectorElement.SetAttribute(XmlFilterRule, collector.FilterRuleName);
  149. collectorElement.SetAttribute(XmlAssetTags, collector.AssetTags);
  150. groupElement.AppendChild(collectorElement);
  151. }
  152. }
  153. // 生成配置文件
  154. xmlDoc.Save(savePath);
  155. Debug.Log($"导出配置完毕!");
  156. }
  157. }
  158. }