AssetBundleCollectorSettingData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. static AssetBundleCollectorSettingData()
  24. {
  25. // IPackRule
  26. {
  27. // 清空缓存集合
  28. _cachePackRuleTypes.Clear();
  29. _cachePackRuleInstance.Clear();
  30. // 获取所有类型
  31. List<Type> types = new List<Type>(100)
  32. {
  33. typeof(PackSeparately),
  34. typeof(PackDirectory),
  35. typeof(PackTopDirectory),
  36. typeof(PackCollector),
  37. typeof(PackGroup),
  38. typeof(PackRawFile),
  39. typeof(PackShaderVariants)
  40. };
  41. var customTypes = EditorTools.GetAssignableTypes(typeof(IPackRule));
  42. types.AddRange(customTypes);
  43. for (int i = 0; i < types.Count; i++)
  44. {
  45. Type type = types[i];
  46. if (_cachePackRuleTypes.ContainsKey(type.Name) == false)
  47. _cachePackRuleTypes.Add(type.Name, type);
  48. }
  49. }
  50. // IFilterRule
  51. {
  52. // 清空缓存集合
  53. _cacheFilterRuleTypes.Clear();
  54. _cacheFilterRuleInstance.Clear();
  55. // 获取所有类型
  56. List<Type> types = new List<Type>(100)
  57. {
  58. typeof(CollectAll),
  59. typeof(CollectScene),
  60. typeof(CollectPrefab),
  61. typeof(CollectSprite)
  62. };
  63. var customTypes = EditorTools.GetAssignableTypes(typeof(IFilterRule));
  64. types.AddRange(customTypes);
  65. for (int i = 0; i < types.Count; i++)
  66. {
  67. Type type = types[i];
  68. if (_cacheFilterRuleTypes.ContainsKey(type.Name) == false)
  69. _cacheFilterRuleTypes.Add(type.Name, type);
  70. }
  71. }
  72. // IAddressRule
  73. {
  74. // 清空缓存集合
  75. _cacheAddressRuleTypes.Clear();
  76. _cacheAddressRuleInstance.Clear();
  77. // 获取所有类型
  78. List<Type> types = new List<Type>(100)
  79. {
  80. typeof(AddressByFileName),
  81. typeof(AddressByFilePath),
  82. typeof(AddressByFolderAndFileName),
  83. typeof(AddressByGroupAndFileName)
  84. };
  85. var customTypes = EditorTools.GetAssignableTypes(typeof(IAddressRule));
  86. types.AddRange(customTypes);
  87. for (int i = 0; i < types.Count; i++)
  88. {
  89. Type type = types[i];
  90. if (_cacheAddressRuleTypes.ContainsKey(type.Name) == false)
  91. _cacheAddressRuleTypes.Add(type.Name, type);
  92. }
  93. }
  94. // IActiveRule
  95. {
  96. // 清空缓存集合
  97. _cacheActiveRuleTypes.Clear();
  98. _cacheActiveRuleInstance.Clear();
  99. // 获取所有类型
  100. List<Type> types = new List<Type>(100)
  101. {
  102. typeof(EnableGroup),
  103. typeof(DisableGroup),
  104. };
  105. var customTypes = EditorTools.GetAssignableTypes(typeof(IActiveRule));
  106. types.AddRange(customTypes);
  107. for (int i = 0; i < types.Count; i++)
  108. {
  109. Type type = types[i];
  110. if (_cacheActiveRuleTypes.ContainsKey(type.Name) == false)
  111. _cacheActiveRuleTypes.Add(type.Name, type);
  112. }
  113. }
  114. }
  115. private static AssetBundleCollectorSetting _setting = null;
  116. public static AssetBundleCollectorSetting Setting
  117. {
  118. get
  119. {
  120. if (_setting == null)
  121. _setting = SettingLoader.LoadSettingData<AssetBundleCollectorSetting>();
  122. return _setting;
  123. }
  124. }
  125. /// <summary>
  126. /// 存储配置文件
  127. /// </summary>
  128. public static void SaveFile()
  129. {
  130. if (Setting != null)
  131. {
  132. IsDirty = false;
  133. EditorUtility.SetDirty(Setting);
  134. AssetDatabase.SaveAssets();
  135. Debug.Log($"{nameof(AssetBundleCollectorSetting)}.asset is saved!");
  136. }
  137. }
  138. /// <summary>
  139. /// 修复配置文件
  140. /// </summary>
  141. public static void FixFile()
  142. {
  143. bool isFixed = Setting.FixConfigError();
  144. if (isFixed)
  145. {
  146. IsDirty = true;
  147. }
  148. }
  149. /// <summary>
  150. /// 清空所有数据
  151. /// </summary>
  152. public static void ClearAll()
  153. {
  154. Setting.ClearAll();
  155. SaveFile();
  156. }
  157. public static List<RuleDisplayName> GetActiveRuleNames()
  158. {
  159. List<RuleDisplayName> names = new List<RuleDisplayName>();
  160. foreach (var pair in _cacheActiveRuleTypes)
  161. {
  162. RuleDisplayName ruleName = new RuleDisplayName();
  163. ruleName.ClassName = pair.Key;
  164. ruleName.DisplayName = GetRuleDisplayName(pair.Key, pair.Value);
  165. names.Add(ruleName);
  166. }
  167. return names;
  168. }
  169. public static List<RuleDisplayName> GetAddressRuleNames()
  170. {
  171. List<RuleDisplayName> names = new List<RuleDisplayName>();
  172. foreach (var pair in _cacheAddressRuleTypes)
  173. {
  174. RuleDisplayName ruleName = new RuleDisplayName();
  175. ruleName.ClassName = pair.Key;
  176. ruleName.DisplayName = GetRuleDisplayName(pair.Key, pair.Value);
  177. names.Add(ruleName);
  178. }
  179. return names;
  180. }
  181. public static List<RuleDisplayName> GetPackRuleNames()
  182. {
  183. List<RuleDisplayName> names = new List<RuleDisplayName>();
  184. foreach (var pair in _cachePackRuleTypes)
  185. {
  186. RuleDisplayName ruleName = new RuleDisplayName();
  187. ruleName.ClassName = pair.Key;
  188. ruleName.DisplayName = GetRuleDisplayName(pair.Key, pair.Value);
  189. names.Add(ruleName);
  190. }
  191. return names;
  192. }
  193. public static List<RuleDisplayName> GetFilterRuleNames()
  194. {
  195. List<RuleDisplayName> names = new List<RuleDisplayName>();
  196. foreach (var pair in _cacheFilterRuleTypes)
  197. {
  198. RuleDisplayName ruleName = new RuleDisplayName();
  199. ruleName.ClassName = pair.Key;
  200. ruleName.DisplayName = GetRuleDisplayName(pair.Key, pair.Value);
  201. names.Add(ruleName);
  202. }
  203. return names;
  204. }
  205. private static string GetRuleDisplayName(string name, Type type)
  206. {
  207. var attribute = DisplayNameAttributeHelper.GetAttribute<DisplayNameAttribute>(type);
  208. if (attribute != null && string.IsNullOrEmpty(attribute.DisplayName) == false)
  209. return attribute.DisplayName;
  210. else
  211. return name;
  212. }
  213. public static bool HasActiveRuleName(string ruleName)
  214. {
  215. return _cacheActiveRuleTypes.Keys.Contains(ruleName);
  216. }
  217. public static bool HasAddressRuleName(string ruleName)
  218. {
  219. return _cacheAddressRuleTypes.Keys.Contains(ruleName);
  220. }
  221. public static bool HasPackRuleName(string ruleName)
  222. {
  223. return _cachePackRuleTypes.Keys.Contains(ruleName);
  224. }
  225. public static bool HasFilterRuleName(string ruleName)
  226. {
  227. return _cacheFilterRuleTypes.Keys.Contains(ruleName);
  228. }
  229. public static IActiveRule GetActiveRuleInstance(string ruleName)
  230. {
  231. if (_cacheActiveRuleInstance.TryGetValue(ruleName, out IActiveRule instance))
  232. return instance;
  233. // 如果不存在创建类的实例
  234. if (_cacheActiveRuleTypes.TryGetValue(ruleName, out Type type))
  235. {
  236. instance = (IActiveRule)Activator.CreateInstance(type);
  237. _cacheActiveRuleInstance.Add(ruleName, instance);
  238. return instance;
  239. }
  240. else
  241. {
  242. throw new Exception($"{nameof(IActiveRule)}类型无效:{ruleName}");
  243. }
  244. }
  245. public static IAddressRule GetAddressRuleInstance(string ruleName)
  246. {
  247. if (_cacheAddressRuleInstance.TryGetValue(ruleName, out IAddressRule instance))
  248. return instance;
  249. // 如果不存在创建类的实例
  250. if (_cacheAddressRuleTypes.TryGetValue(ruleName, out Type type))
  251. {
  252. instance = (IAddressRule)Activator.CreateInstance(type);
  253. _cacheAddressRuleInstance.Add(ruleName, instance);
  254. return instance;
  255. }
  256. else
  257. {
  258. throw new Exception($"{nameof(IAddressRule)}类型无效:{ruleName}");
  259. }
  260. }
  261. public static IPackRule GetPackRuleInstance(string ruleName)
  262. {
  263. if (_cachePackRuleInstance.TryGetValue(ruleName, out IPackRule instance))
  264. return instance;
  265. // 如果不存在创建类的实例
  266. if (_cachePackRuleTypes.TryGetValue(ruleName, out Type type))
  267. {
  268. instance = (IPackRule)Activator.CreateInstance(type);
  269. _cachePackRuleInstance.Add(ruleName, instance);
  270. return instance;
  271. }
  272. else
  273. {
  274. throw new Exception($"{nameof(IPackRule)}类型无效:{ruleName}");
  275. }
  276. }
  277. public static IFilterRule GetFilterRuleInstance(string ruleName)
  278. {
  279. if (_cacheFilterRuleInstance.TryGetValue(ruleName, out IFilterRule instance))
  280. return instance;
  281. // 如果不存在创建类的实例
  282. if (_cacheFilterRuleTypes.TryGetValue(ruleName, out Type type))
  283. {
  284. instance = (IFilterRule)Activator.CreateInstance(type);
  285. _cacheFilterRuleInstance.Add(ruleName, instance);
  286. return instance;
  287. }
  288. else
  289. {
  290. throw new Exception($"{nameof(IFilterRule)}类型无效:{ruleName}");
  291. }
  292. }
  293. // 公共参数编辑相关
  294. public static void ModifyPackageView(bool showPackageView)
  295. {
  296. Setting.ShowPackageView = showPackageView;
  297. IsDirty = true;
  298. }
  299. public static void ModifyAddressable(bool enableAddressable)
  300. {
  301. Setting.EnableAddressable = enableAddressable;
  302. IsDirty = true;
  303. }
  304. public static void ModifyLocationToLower(bool locationToLower)
  305. {
  306. Setting.LocationToLower = locationToLower;
  307. IsDirty = true;
  308. }
  309. public static void ModifyIncludeAssetGUID(bool includeAssetGUID)
  310. {
  311. Setting.IncludeAssetGUID = includeAssetGUID;
  312. IsDirty = true;
  313. }
  314. public static void ModifyUniqueBundleName(bool uniqueBundleName)
  315. {
  316. Setting.UniqueBundleName = uniqueBundleName;
  317. IsDirty = true;
  318. }
  319. public static void ModifyShowEditorAlias(bool showAlias)
  320. {
  321. Setting.ShowEditorAlias = showAlias;
  322. IsDirty = true;
  323. }
  324. // 资源包裹编辑相关
  325. public static AssetBundleCollectorPackage CreatePackage(string packageName)
  326. {
  327. AssetBundleCollectorPackage package = new AssetBundleCollectorPackage();
  328. package.PackageName = packageName;
  329. Setting.Packages.Add(package);
  330. IsDirty = true;
  331. return package;
  332. }
  333. public static void RemovePackage(AssetBundleCollectorPackage package)
  334. {
  335. if (Setting.Packages.Remove(package))
  336. {
  337. IsDirty = true;
  338. }
  339. else
  340. {
  341. Debug.LogWarning($"Failed remove package : {package.PackageName}");
  342. }
  343. }
  344. public static void ModifyPackage(AssetBundleCollectorPackage package)
  345. {
  346. if (package != null)
  347. {
  348. IsDirty = true;
  349. }
  350. }
  351. // 资源分组编辑相关
  352. public static AssetBundleCollectorGroup CreateGroup(AssetBundleCollectorPackage package, string groupName)
  353. {
  354. AssetBundleCollectorGroup group = new AssetBundleCollectorGroup();
  355. group.GroupName = groupName;
  356. package.Groups.Add(group);
  357. IsDirty = true;
  358. return group;
  359. }
  360. public static void RemoveGroup(AssetBundleCollectorPackage package, AssetBundleCollectorGroup group)
  361. {
  362. if (package.Groups.Remove(group))
  363. {
  364. IsDirty = true;
  365. }
  366. else
  367. {
  368. Debug.LogWarning($"Failed remove group : {group.GroupName}");
  369. }
  370. }
  371. public static void ModifyGroup(AssetBundleCollectorPackage package, AssetBundleCollectorGroup group)
  372. {
  373. if (package != null && group != null)
  374. {
  375. IsDirty = true;
  376. }
  377. }
  378. // 资源收集器编辑相关
  379. public static void CreateCollector(AssetBundleCollectorGroup group, AssetBundleCollector collector)
  380. {
  381. group.Collectors.Add(collector);
  382. IsDirty = true;
  383. }
  384. public static void RemoveCollector(AssetBundleCollectorGroup group, AssetBundleCollector collector)
  385. {
  386. if (group.Collectors.Remove(collector))
  387. {
  388. IsDirty = true;
  389. }
  390. else
  391. {
  392. Debug.LogWarning($"Failed remove collector : {collector.CollectPath}");
  393. }
  394. }
  395. public static void ModifyCollector(AssetBundleCollectorGroup group, AssetBundleCollector collector)
  396. {
  397. if (group != null && collector != null)
  398. {
  399. IsDirty = true;
  400. }
  401. }
  402. /// <summary>
  403. /// 获取所有的资源标签
  404. /// </summary>
  405. public static string GetPackageAllTags(string packageName)
  406. {
  407. var allTags = Setting.GetPackageAllTags(packageName);
  408. return string.Join(";", allTags);
  409. }
  410. }
  411. }