AssetBundleCollectorSettingData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEditor;
  7. namespace YooAsset.Editor
  8. {
  9. public class AssetBundleCollectorSettingData
  10. {
  11. private static readonly Dictionary<string, System.Type> _cacheActiveRuleTypes = new Dictionary<string, Type>();
  12. private static readonly Dictionary<string, IActiveRule> _cacheActiveRuleInstance = new Dictionary<string, IActiveRule>();
  13. private static readonly Dictionary<string, System.Type> _cacheAddressRuleTypes = new Dictionary<string, System.Type>();
  14. private static readonly Dictionary<string, IAddressRule> _cacheAddressRuleInstance = new Dictionary<string, IAddressRule>();
  15. private static readonly Dictionary<string, System.Type> _cachePackRuleTypes = new Dictionary<string, System.Type>();
  16. private static readonly Dictionary<string, IPackRule> _cachePackRuleInstance = new Dictionary<string, IPackRule>();
  17. private static readonly Dictionary<string, System.Type> _cacheFilterRuleTypes = new Dictionary<string, System.Type>();
  18. private static readonly Dictionary<string, IFilterRule> _cacheFilterRuleInstance = new Dictionary<string, IFilterRule>();
  19. /// <summary>
  20. /// 配置数据是否被修改
  21. /// </summary>
  22. public static bool IsDirty { private set; get; } = false;
  23. private static AssetBundleCollectorSetting _setting = null;
  24. public static AssetBundleCollectorSetting Setting
  25. {
  26. get
  27. {
  28. if (_setting == null)
  29. LoadSettingData();
  30. return _setting;
  31. }
  32. }
  33. public static List<string> GetActiveRuleNames()
  34. {
  35. if (_setting == null)
  36. LoadSettingData();
  37. List<string> names = new List<string>();
  38. foreach (var pair in _cacheActiveRuleTypes)
  39. {
  40. names.Add(pair.Key);
  41. }
  42. return names;
  43. }
  44. public static List<string> GetAddressRuleNames()
  45. {
  46. if (_setting == null)
  47. LoadSettingData();
  48. List<string> names = new List<string>();
  49. foreach (var pair in _cacheAddressRuleTypes)
  50. {
  51. names.Add(pair.Key);
  52. }
  53. return names;
  54. }
  55. public static List<string> GetPackRuleNames()
  56. {
  57. if (_setting == null)
  58. LoadSettingData();
  59. List<string> names = new List<string>();
  60. foreach (var pair in _cachePackRuleTypes)
  61. {
  62. names.Add(pair.Key);
  63. }
  64. return names;
  65. }
  66. public static List<string> GetFilterRuleNames()
  67. {
  68. if (_setting == null)
  69. LoadSettingData();
  70. List<string> names = new List<string>();
  71. foreach (var pair in _cacheFilterRuleTypes)
  72. {
  73. names.Add(pair.Key);
  74. }
  75. return names;
  76. }
  77. public static bool HasActiveRuleName(string ruleName)
  78. {
  79. foreach (var pair in _cacheActiveRuleTypes)
  80. {
  81. if (pair.Key == ruleName)
  82. return true;
  83. }
  84. return false;
  85. }
  86. public static bool HasAddressRuleName(string ruleName)
  87. {
  88. foreach (var pair in _cacheAddressRuleTypes)
  89. {
  90. if (pair.Key == ruleName)
  91. return true;
  92. }
  93. return false;
  94. }
  95. public static bool HasPackRuleName(string ruleName)
  96. {
  97. foreach (var pair in _cachePackRuleTypes)
  98. {
  99. if (pair.Key == ruleName)
  100. return true;
  101. }
  102. return false;
  103. }
  104. public static bool HasFilterRuleName(string ruleName)
  105. {
  106. foreach (var pair in _cacheFilterRuleTypes)
  107. {
  108. if (pair.Key == ruleName)
  109. return true;
  110. }
  111. return false;
  112. }
  113. /// <summary>
  114. /// 加载配置文件
  115. /// </summary>
  116. private static void LoadSettingData()
  117. {
  118. _setting = EditorHelper.LoadSettingData<AssetBundleCollectorSetting>();
  119. // IPackRule
  120. {
  121. // 清空缓存集合
  122. _cachePackRuleTypes.Clear();
  123. _cachePackRuleInstance.Clear();
  124. // 获取所有类型
  125. List<Type> types = new List<Type>(100)
  126. {
  127. typeof(PackSeparately),
  128. typeof(PackDirectory),
  129. typeof(PackTopDirectory),
  130. typeof(PackCollector),
  131. typeof(PackGroup),
  132. typeof(PackRawFile),
  133. };
  134. var customTypes = EditorTools.GetAssignableTypes(typeof(IPackRule));
  135. types.AddRange(customTypes);
  136. for (int i = 0; i < types.Count; i++)
  137. {
  138. Type type = types[i];
  139. if (_cachePackRuleTypes.ContainsKey(type.Name) == false)
  140. _cachePackRuleTypes.Add(type.Name, type);
  141. }
  142. }
  143. // IFilterRule
  144. {
  145. // 清空缓存集合
  146. _cacheFilterRuleTypes.Clear();
  147. _cacheFilterRuleInstance.Clear();
  148. // 获取所有类型
  149. List<Type> types = new List<Type>(100)
  150. {
  151. typeof(CollectAll),
  152. typeof(CollectScene),
  153. typeof(CollectPrefab),
  154. typeof(CollectSprite)
  155. };
  156. var customTypes = EditorTools.GetAssignableTypes(typeof(IFilterRule));
  157. types.AddRange(customTypes);
  158. for (int i = 0; i < types.Count; i++)
  159. {
  160. Type type = types[i];
  161. if (_cacheFilterRuleTypes.ContainsKey(type.Name) == false)
  162. _cacheFilterRuleTypes.Add(type.Name, type);
  163. }
  164. }
  165. // IAddressRule
  166. {
  167. // 清空缓存集合
  168. _cacheAddressRuleTypes.Clear();
  169. _cacheAddressRuleInstance.Clear();
  170. // 获取所有类型
  171. List<Type> types = new List<Type>(100)
  172. {
  173. typeof(AddressByFileName),
  174. typeof(AddressByCollectorAndFileName),
  175. typeof(AddressByGroupAndFileName)
  176. };
  177. var customTypes = EditorTools.GetAssignableTypes(typeof(IAddressRule));
  178. types.AddRange(customTypes);
  179. for (int i = 0; i < types.Count; i++)
  180. {
  181. Type type = types[i];
  182. if (_cacheAddressRuleTypes.ContainsKey(type.Name) == false)
  183. _cacheAddressRuleTypes.Add(type.Name, type);
  184. }
  185. }
  186. // IActiveRule
  187. {
  188. // 清空缓存集合
  189. _cacheActiveRuleTypes.Clear();
  190. _cacheActiveRuleInstance.Clear();
  191. // 获取所有类型
  192. List<Type> types = new List<Type>(100)
  193. {
  194. typeof(EnableGroup),
  195. typeof(DisableGroup),
  196. };
  197. var customTypes = EditorTools.GetAssignableTypes(typeof(IActiveRule));
  198. types.AddRange(customTypes);
  199. for (int i = 0; i < types.Count; i++)
  200. {
  201. Type type = types[i];
  202. if (_cacheActiveRuleTypes.ContainsKey(type.Name) == false)
  203. _cacheActiveRuleTypes.Add(type.Name, type);
  204. }
  205. }
  206. }
  207. /// <summary>
  208. /// 存储配置文件
  209. /// </summary>
  210. public static void SaveFile()
  211. {
  212. if (Setting != null)
  213. {
  214. IsDirty = false;
  215. EditorUtility.SetDirty(Setting);
  216. AssetDatabase.SaveAssets();
  217. Debug.Log($"{nameof(AssetBundleCollectorSetting)}.asset is saved!");
  218. }
  219. }
  220. /// <summary>
  221. /// 修复配置文件
  222. /// </summary>
  223. public static void FixFile()
  224. {
  225. bool isFixed = Setting.FixConfigError();
  226. if (isFixed)
  227. {
  228. IsDirty = true;
  229. }
  230. }
  231. /// <summary>
  232. /// 清空所有数据
  233. /// </summary>
  234. public static void ClearAll()
  235. {
  236. Setting.EnableAddressable = false;
  237. Setting.Groups.Clear();
  238. SaveFile();
  239. }
  240. // 实例类相关
  241. public static IActiveRule GetActiveRuleInstance(string ruleName)
  242. {
  243. if (_cacheActiveRuleInstance.TryGetValue(ruleName, out IActiveRule instance))
  244. return instance;
  245. // 如果不存在创建类的实例
  246. if (_cacheActiveRuleTypes.TryGetValue(ruleName, out Type type))
  247. {
  248. instance = (IActiveRule)Activator.CreateInstance(type);
  249. _cacheActiveRuleInstance.Add(ruleName, instance);
  250. return instance;
  251. }
  252. else
  253. {
  254. throw new Exception($"{nameof(IActiveRule)}类型无效:{ruleName}");
  255. }
  256. }
  257. public static IAddressRule GetAddressRuleInstance(string ruleName)
  258. {
  259. if (_cacheAddressRuleInstance.TryGetValue(ruleName, out IAddressRule instance))
  260. return instance;
  261. // 如果不存在创建类的实例
  262. if (_cacheAddressRuleTypes.TryGetValue(ruleName, out Type type))
  263. {
  264. instance = (IAddressRule)Activator.CreateInstance(type);
  265. _cacheAddressRuleInstance.Add(ruleName, instance);
  266. return instance;
  267. }
  268. else
  269. {
  270. throw new Exception($"{nameof(IAddressRule)}类型无效:{ruleName}");
  271. }
  272. }
  273. public static IPackRule GetPackRuleInstance(string ruleName)
  274. {
  275. if (_cachePackRuleInstance.TryGetValue(ruleName, out IPackRule instance))
  276. return instance;
  277. // 如果不存在创建类的实例
  278. if (_cachePackRuleTypes.TryGetValue(ruleName, out Type type))
  279. {
  280. instance = (IPackRule)Activator.CreateInstance(type);
  281. _cachePackRuleInstance.Add(ruleName, instance);
  282. return instance;
  283. }
  284. else
  285. {
  286. throw new Exception($"{nameof(IPackRule)}类型无效:{ruleName}");
  287. }
  288. }
  289. public static IFilterRule GetFilterRuleInstance(string ruleName)
  290. {
  291. if (_cacheFilterRuleInstance.TryGetValue(ruleName, out IFilterRule instance))
  292. return instance;
  293. // 如果不存在创建类的实例
  294. if (_cacheFilterRuleTypes.TryGetValue(ruleName, out Type type))
  295. {
  296. instance = (IFilterRule)Activator.CreateInstance(type);
  297. _cacheFilterRuleInstance.Add(ruleName, instance);
  298. return instance;
  299. }
  300. else
  301. {
  302. throw new Exception($"{nameof(IFilterRule)}类型无效:{ruleName}");
  303. }
  304. }
  305. // 可寻址编辑相关
  306. public static void ModifyAddressable(bool enableAddressable)
  307. {
  308. Setting.EnableAddressable = enableAddressable;
  309. IsDirty = true;
  310. }
  311. // 资源分组编辑相关
  312. public static void CreateGroup(string groupName)
  313. {
  314. AssetBundleCollectorGroup group = new AssetBundleCollectorGroup();
  315. group.GroupName = groupName;
  316. Setting.Groups.Add(group);
  317. IsDirty = true;
  318. }
  319. public static void RemoveGroup(AssetBundleCollectorGroup group)
  320. {
  321. if (Setting.Groups.Remove(group))
  322. {
  323. IsDirty = true;
  324. }
  325. else
  326. {
  327. Debug.LogWarning($"Failed remove group : {group.GroupName}");
  328. }
  329. }
  330. public static void ModifyGroup(AssetBundleCollectorGroup group)
  331. {
  332. if (group != null)
  333. {
  334. IsDirty = true;
  335. }
  336. }
  337. // 资源收集器编辑相关
  338. public static void CreateCollector(AssetBundleCollectorGroup group)
  339. {
  340. AssetBundleCollector collector = new AssetBundleCollector();
  341. group.Collectors.Add(collector);
  342. IsDirty = true;
  343. }
  344. public static void RemoveCollector(AssetBundleCollectorGroup group, AssetBundleCollector collector)
  345. {
  346. if (group.Collectors.Remove(collector))
  347. {
  348. IsDirty = true;
  349. }
  350. else
  351. {
  352. Debug.LogWarning($"Failed remove collector : {collector.CollectPath}");
  353. }
  354. }
  355. public static void ModifyCollector(AssetBundleCollectorGroup group, AssetBundleCollector collector)
  356. {
  357. if (group != null && collector != null)
  358. {
  359. IsDirty = true;
  360. }
  361. }
  362. /// <summary>
  363. /// 获取所有的资源标签
  364. /// </summary>
  365. public static string GetAllTags()
  366. {
  367. var allTags = Setting.GetAllTags();
  368. return string.Join(";", allTags);
  369. }
  370. }
  371. }