feat : add IIgnoreRule interface

支持自定义过滤规则
移除了IgnoreDefaultType收集参数
pull/252/head
hevinci 2024-03-12 10:33:54 +08:00
parent 2a5a2626a4
commit ef8229981e
15 changed files with 235 additions and 87 deletions

View File

@ -37,7 +37,7 @@ namespace YooAsset.Editor
buildReport.Summary.EnableAddressable = buildMapContext.Command.EnableAddressable;
buildReport.Summary.LocationToLower = buildMapContext.Command.LocationToLower;
buildReport.Summary.IncludeAssetGUID = buildMapContext.Command.IncludeAssetGUID;
buildReport.Summary.IgnoreDefaultType = buildMapContext.Command.IgnoreDefaultType;
buildReport.Summary.IgnoreRuleName = buildMapContext.Command.IgnoreRule.GetType().FullName;
buildReport.Summary.AutoCollectShaders = buildMapContext.Command.AutoCollectShaders;
// 构建参数

View File

@ -166,7 +166,7 @@ namespace YooAsset.Editor
foreach (string assetPath in findAssets)
{
var assetInfo = new AssetInfo(assetPath);
if (IsValidateAsset(command, assetInfo) && IsCollectAsset(group, assetInfo))
if (command.IgnoreRule.IsIgnore(assetInfo) == false && IsCollectAsset(group, assetInfo))
{
if (result.ContainsKey(assetPath) == false)
{
@ -228,37 +228,6 @@ namespace YooAsset.Editor
return collectAssetInfo;
}
private bool IsValidateAsset(CollectCommand command, AssetInfo assetInfo)
{
if (assetInfo.AssetPath.StartsWith("Assets/") == false && assetInfo.AssetPath.StartsWith("Packages/") == false)
{
UnityEngine.Debug.LogError($"Invalid asset path : {assetInfo.AssetPath}");
return false;
}
// 忽略文件夹
if (AssetDatabase.IsValidFolder(assetInfo.AssetPath))
return false;
// 忽略编辑器下的类型资源
if (assetInfo.AssetType == typeof(LightingDataAsset))
return false;
// 忽略Unity引擎无法识别的文件
if (command.IgnoreDefaultType)
{
if (assetInfo.AssetType == typeof(UnityEditor.DefaultAsset))
{
UnityEngine.Debug.LogWarning($"Cannot pack default asset : {assetInfo.AssetPath}");
return false;
}
}
if (DefaultFilterRule.IsIgnoreFile(assetInfo.FileExtension))
return false;
return true;
}
private bool IsCollectAsset(AssetBundleCollectorGroup group, AssetInfo assetInfo)
{
// 根据规则设置过滤资源文件
@ -312,7 +281,7 @@ namespace YooAsset.Editor
continue;
AssetInfo assetInfo = new AssetInfo(assetPath);
if (IsValidateAsset(command, assetInfo))
if (command.IgnoreRule.IsIgnore(assetInfo) == false)
result.Add(assetInfo);
}
return result;

View File

@ -25,7 +25,7 @@ namespace YooAsset.Editor
public const string XmlEnableAddressable = "AutoAddressable";
public const string XmlLocationToLower = "LocationToLower";
public const string XmlIncludeAssetGUID = "IncludeAssetGUID";
public const string XmlIgnoreDefaultType = "IgnoreDefaultType";
public const string XmlIgnoreRuleName = "IgnoreRuleName";
public const string XmlGroup = "Group";
public const string XmlGroupActiveRule = "GroupActiveRule";
@ -101,7 +101,7 @@ namespace YooAsset.Editor
package.EnableAddressable = packageElement.GetAttribute(XmlEnableAddressable) == "True" ? true : false;
package.LocationToLower = packageElement.GetAttribute(XmlLocationToLower) == "True" ? true : false;
package.IncludeAssetGUID = packageElement.GetAttribute(XmlIncludeAssetGUID) == "True" ? true : false;
package.IgnoreDefaultType = packageElement.GetAttribute(XmlIgnoreDefaultType) == "True" ? true : false;
package.IgnoreRuleName = packageElement.GetAttribute(XmlIgnoreRuleName);
packages.Add(package);
// 读取分组配置
@ -213,7 +213,7 @@ namespace YooAsset.Editor
packageElement.SetAttribute(XmlEnableAddressable, package.EnableAddressable.ToString());
packageElement.SetAttribute(XmlLocationToLower, package.LocationToLower.ToString());
packageElement.SetAttribute(XmlIncludeAssetGUID, package.IncludeAssetGUID.ToString());
packageElement.SetAttribute(XmlIgnoreDefaultType, package.IgnoreDefaultType.ToString());
packageElement.SetAttribute(XmlIgnoreRuleName, package.IgnoreRuleName);
root.AppendChild(packageElement);
// 设置分组配置

View File

@ -35,16 +35,16 @@ namespace YooAsset.Editor
/// </summary>
public bool IncludeAssetGUID = false;
/// <summary>
/// 忽略Unity引擎无法识别的文件
/// </summary>
public bool IgnoreDefaultType = true;
/// <summary>
/// 自动收集所有着色器(所有着色器存储在一个资源包内)
/// </summary>
public bool AutoCollectShaders = true;
/// <summary>
/// 资源忽略规则名
/// </summary>
public string IgnoreRuleName = nameof(NormalIgnoreRule);
/// <summary>
/// 分组列表
/// </summary>

View File

@ -24,7 +24,6 @@ namespace YooAsset.Editor
/// </summary>
public bool UniqueBundleName = false;
/// <summary>
/// 包裹列表
/// </summary>
@ -100,13 +99,13 @@ namespace YooAsset.Editor
package.CheckConfigError();
// 创建资源收集命令
IIgnoreRule ignoreRule = AssetBundleCollectorSettingData.GetIgnoreRuleInstance(package.IgnoreRuleName);
CollectCommand command = new CollectCommand(buildMode, packageName,
package.EnableAddressable,
package.LocationToLower,
package.IncludeAssetGUID,
package.IgnoreDefaultType,
package.AutoCollectShaders,
UniqueBundleName);
UniqueBundleName, ignoreRule);
// 获取收集的资源集合
CollectResult collectResult = new CollectResult(command);

View File

@ -21,6 +21,9 @@ namespace YooAsset.Editor
private static readonly Dictionary<string, System.Type> _cacheFilterRuleTypes = new Dictionary<string, System.Type>();
private static readonly Dictionary<string, IFilterRule> _cacheFilterRuleInstance = new Dictionary<string, IFilterRule>();
private static readonly Dictionary<string, System.Type> _cacheIgnoreRuleTypes = new Dictionary<string, System.Type>();
private static readonly Dictionary<string, IIgnoreRule> _cacheIgnoreRuleInstance = new Dictionary<string, IIgnoreRule>();
/// <summary>
/// 配置数据是否被修改
/// </summary>
@ -129,6 +132,29 @@ namespace YooAsset.Editor
_cacheActiveRuleTypes.Add(type.Name, type);
}
}
// IIgnoreRule
{
// 清空缓存集合
_cacheIgnoreRuleTypes.Clear();
_cacheIgnoreRuleInstance.Clear();
// 获取所有类型
List<Type> types = new List<Type>(100)
{
typeof(NormalIgnoreRule),
typeof(RawFileIgnoreRule),
};
var customTypes = EditorTools.GetAssignableTypes(typeof(IIgnoreRule));
types.AddRange(customTypes);
for (int i = 0; i < types.Count; i++)
{
Type type = types[i];
if (_cacheIgnoreRuleTypes.ContainsKey(type.Name) == false)
_cacheIgnoreRuleTypes.Add(type.Name, type);
}
}
}
private static AssetBundleCollectorSetting _setting = null;
@ -225,6 +251,18 @@ namespace YooAsset.Editor
}
return names;
}
public static List<RuleDisplayName> GetIgnoreRuleNames()
{
List<RuleDisplayName> names = new List<RuleDisplayName>();
foreach (var pair in _cacheIgnoreRuleTypes)
{
RuleDisplayName ruleName = new RuleDisplayName();
ruleName.ClassName = pair.Key;
ruleName.DisplayName = GetRuleDisplayName(pair.Key, pair.Value);
names.Add(ruleName);
}
return names;
}
private static string GetRuleDisplayName(string name, Type type)
{
var attribute = DisplayNameAttributeHelper.GetAttribute<DisplayNameAttribute>(type);
@ -250,6 +288,10 @@ namespace YooAsset.Editor
{
return _cacheFilterRuleTypes.Keys.Contains(ruleName);
}
public static bool HasIgnoreRuleName(string ruleName)
{
return _cacheIgnoreRuleTypes.Keys.Contains(ruleName);
}
public static IActiveRule GetActiveRuleInstance(string ruleName)
{
@ -319,6 +361,23 @@ namespace YooAsset.Editor
throw new Exception($"{nameof(IFilterRule)} is invalid{ruleName}");
}
}
public static IIgnoreRule GetIgnoreRuleInstance(string ruleName)
{
if (_cacheIgnoreRuleInstance.TryGetValue(ruleName, out IIgnoreRule instance))
return instance;
// 如果不存在创建类的实例
if (_cacheIgnoreRuleTypes.TryGetValue(ruleName, out Type type))
{
instance = (IIgnoreRule)Activator.CreateInstance(type);
_cacheIgnoreRuleInstance.Add(ruleName, instance);
return instance;
}
else
{
throw new Exception($"{nameof(IIgnoreRule)} is invalid{ruleName}");
}
}
// 公共参数编辑相关
public static void ModifyShowPackageView(bool showPackageView)

View File

@ -24,6 +24,7 @@ namespace YooAsset.Editor
private List<RuleDisplayName> _addressRuleList;
private List<RuleDisplayName> _packRuleList;
private List<RuleDisplayName> _filterRuleList;
private List<RuleDisplayName> _ignoreRuleList;
private VisualElement _helpBoxContainer;
@ -39,8 +40,8 @@ namespace YooAsset.Editor
private Toggle _enableAddressableToogle;
private Toggle _locationToLowerToogle;
private Toggle _includeAssetGUIDToogle;
private Toggle _ignoreDefaultTypeToogle;
private Toggle _autoCollectShadersToogle;
private PopupField<RuleDisplayName> _ignoreRulePopupField;
private VisualElement _packageContainer;
private ListView _packageListView;
@ -77,6 +78,7 @@ namespace YooAsset.Editor
_addressRuleList = AssetBundleCollectorSettingData.GetAddressRuleNames();
_packRuleList = AssetBundleCollectorSettingData.GetPackRuleNames();
_filterRuleList = AssetBundleCollectorSettingData.GetFilterRuleNames();
_ignoreRuleList = AssetBundleCollectorSettingData.GetIgnoreRuleNames();
VisualElement root = this.rootVisualElement;
@ -151,17 +153,6 @@ namespace YooAsset.Editor
RefreshWindow();
}
});
_ignoreDefaultTypeToogle = root.Q<Toggle>("IgnoreDefaultType");
_ignoreDefaultTypeToogle.RegisterValueChangedCallback(evt =>
{
var selectPackage = _packageListView.selectedItem as AssetBundleCollectorPackage;
if (selectPackage != null)
{
selectPackage.IgnoreDefaultType = evt.newValue;
AssetBundleCollectorSettingData.ModifyPackage(selectPackage);
RefreshWindow();
}
});
_autoCollectShadersToogle = root.Q<Toggle>("AutoCollectShaders");
_autoCollectShadersToogle.RegisterValueChangedCallback(evt =>
{
@ -173,6 +164,25 @@ namespace YooAsset.Editor
RefreshWindow();
}
});
{
_ignoreRulePopupField = new PopupField<RuleDisplayName>(_ignoreRuleList, 0);
_ignoreRulePopupField.label = "File Ignore Rule";
_ignoreRulePopupField.name = "IgnoreRulePopupField";
_ignoreRulePopupField.style.unityTextAlign = TextAnchor.MiddleLeft;
_ignoreRulePopupField.style.width = 300;
_ignoreRulePopupField.formatListItemCallback = FormatListItemCallback;
_ignoreRulePopupField.formatSelectedValueCallback = FormatSelectedValueCallback;
_ignoreRulePopupField.RegisterValueChangedCallback(evt =>
{
var selectPackage = _packageListView.selectedItem as AssetBundleCollectorPackage;
if(selectPackage != null)
{
selectPackage.IgnoreRuleName = evt.newValue.ClassName;
AssetBundleCollectorSettingData.ModifyPackage(selectPackage);
}
});
_setting2Container.Add(_ignoreRulePopupField);
}
// 配置修复按钮
var fixBtn = root.Q<Button>("FixButton");
@ -475,8 +485,8 @@ namespace YooAsset.Editor
_enableAddressableToogle.SetValueWithoutNotify(selectPackage.EnableAddressable);
_locationToLowerToogle.SetValueWithoutNotify(selectPackage.LocationToLower);
_includeAssetGUIDToogle.SetValueWithoutNotify(selectPackage.IncludeAssetGUID);
_ignoreDefaultTypeToogle.SetValueWithoutNotify(selectPackage.IgnoreDefaultType);
_autoCollectShadersToogle.SetValueWithoutNotify(selectPackage.AutoCollectShaders);
_ignoreRulePopupField.SetValueWithoutNotify(GetIgnoreRuleIndex(selectPackage.IgnoreRuleName));
}
else
{
@ -980,14 +990,15 @@ namespace YooAsset.Editor
try
{
IIgnoreRule ignoreRule = AssetBundleCollectorSettingData.GetIgnoreRuleInstance(_ignoreRulePopupField.value.ClassName);
CollectCommand command = new CollectCommand(EBuildMode.SimulateBuild,
_packageNameTxt.value,
_enableAddressableToogle.value,
_locationToLowerToogle.value,
_includeAssetGUIDToogle.value,
_ignoreDefaultTypeToogle.value,
_autoCollectShadersToogle.value,
_uniqueBundleNameToogle.value);
_uniqueBundleNameToogle.value,
ignoreRule);
collector.CheckConfigError();
collectAssetInfos = collector.GetAllCollectAssets(command, group);
}
@ -1078,6 +1089,15 @@ namespace YooAsset.Editor
}
return 0;
}
private RuleDisplayName GetIgnoreRuleIndex(string ruleName)
{
for (int i = 0; i < _ignoreRuleList.Count; i++)
{
if (_ignoreRuleList[i].ClassName == ruleName)
return _ignoreRuleList[i];
}
return _ignoreRuleList[0];
}
private RuleDisplayName GetActiveRuleIndex(string ruleName)
{
for (int i = 0; i < _activeRuleList.Count; i++)

View File

@ -13,11 +13,6 @@ namespace YooAsset.Editor
/// </summary>
public string PackageName { private set; get; }
/// <summary>
/// 忽略Unity引擎无法识别的文件
/// </summary>
public bool IgnoreDefaultType { private set; get; }
/// <summary>
/// 启用可寻址资源定位
/// </summary>
@ -48,17 +43,24 @@ namespace YooAsset.Editor
/// </summary>
public string ShadersBundleName { private set; get; }
/// <summary>
/// 忽略规则实例
/// </summary>
public IIgnoreRule IgnoreRule { private set; get; }
public CollectCommand(EBuildMode buildMode, string packageName, bool enableAddressable, bool locationToLower, bool includeAssetGUID, bool ignoreDefaultType, bool autoCollectShaders, bool uniqueBundleName)
public CollectCommand(EBuildMode buildMode, string packageName,
bool enableAddressable, bool locationToLower, bool includeAssetGUID,
bool autoCollectShaders, bool uniqueBundleName, IIgnoreRule ignoreRule)
{
BuildMode = buildMode;
PackageName = packageName;
EnableAddressable = enableAddressable;
LocationToLower = locationToLower;
IncludeAssetGUID = includeAssetGUID;
IgnoreDefaultType = ignoreDefaultType;
AutoCollectShaders = autoCollectShaders;
UniqueBundleName = uniqueBundleName;
IgnoreRule = ignoreRule;
// 着色器统一全名称
var packRuleResult = DefaultPackRule.CreateShadersPackRuleResult();

View File

@ -0,0 +1,11 @@

namespace YooAsset.Editor
{
/// <summary>
/// 资源忽略规则接口
/// </summary>
public interface IIgnoreRule
{
bool IsIgnore(AssetInfo assetInfo);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bd55753dbb880fa449bf4e37b33d8ba7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -6,22 +6,6 @@ using UnityEditor;
namespace YooAsset.Editor
{
public class DefaultFilterRule
{
/// <summary>
/// 忽略的文件类型
/// </summary>
private readonly static HashSet<string> _ignoreFileExtensions = new HashSet<string>() { "", ".so", ".dll", ".cs", ".js", ".boo", ".meta", ".cginc", ".hlsl" };
/// <summary>
/// 查询是否为忽略文件
/// </summary>
public static bool IsIgnoreFile(string fileExtension)
{
return _ignoreFileExtensions.Contains(fileExtension);
}
}
[DisplayName("收集所有资源")]
public class CollectAll : IFilterRule
{

View File

@ -0,0 +1,82 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace YooAsset.Editor
{
public class DefaultIgnoreRule
{
/// <summary>
/// 忽略的文件类型
/// </summary>
public readonly static HashSet<string> IgnoreFileExtensions = new HashSet<string>() { "", ".so", ".dll", ".cs", ".js", ".boo", ".meta", ".cginc", ".hlsl" };
}
/// <summary>
/// 适配常规的资源构建管线
/// </summary>
public class NormalIgnoreRule : IIgnoreRule
{
/// <summary>
/// 查询是否为忽略文件
/// </summary>
public bool IsIgnore(AssetInfo assetInfo)
{
if (assetInfo.AssetPath.StartsWith("Assets/") == false && assetInfo.AssetPath.StartsWith("Packages/") == false)
{
UnityEngine.Debug.LogError($"Invalid asset path : {assetInfo.AssetPath}");
return true;
}
// 忽略文件夹
if (AssetDatabase.IsValidFolder(assetInfo.AssetPath))
return true;
// 忽略编辑器下的类型资源
if (assetInfo.AssetType == typeof(LightingDataAsset))
return true;
if (assetInfo.AssetType == typeof(LightmapParameters))
return true;
// 忽略Unity引擎无法识别的文件
if (assetInfo.AssetType == typeof(UnityEditor.DefaultAsset))
{
UnityEngine.Debug.LogWarning($"Cannot pack default asset : {assetInfo.AssetPath}");
return true;
}
return DefaultIgnoreRule.IgnoreFileExtensions.Contains(assetInfo.FileExtension);
}
}
/// <summary>
/// 适配原生文件构建管线
/// </summary>
public class RawFileIgnoreRule : IIgnoreRule
{
/// <summary>
/// 查询是否为忽略文件
/// </summary>
public bool IsIgnore(AssetInfo assetInfo)
{
if (assetInfo.AssetPath.StartsWith("Assets/") == false && assetInfo.AssetPath.StartsWith("Packages/") == false)
{
UnityEngine.Debug.LogError($"Invalid asset path : {assetInfo.AssetPath}");
return true;
}
// 忽略文件夹
if (AssetDatabase.IsValidFolder(assetInfo.AssetPath))
return true;
// 忽略编辑器下的类型资源
if (assetInfo.AssetType == typeof(LightingDataAsset))
return true;
if (assetInfo.AssetType == typeof(LightmapParameters))
return true;
return DefaultIgnoreRule.IgnoreFileExtensions.Contains(assetInfo.FileExtension);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b9c1900577194bb4ab49ce4332ccc4bc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -58,8 +58,8 @@ namespace YooAsset.Editor
public bool EnableAddressable;
public bool LocationToLower;
public bool IncludeAssetGUID;
public bool IgnoreDefaultType;
public bool AutoCollectShaders;
public string IgnoreRuleName;
// 构建参数
public bool EnableSharePackRule;

View File

@ -72,8 +72,8 @@ namespace YooAsset.Editor
_items.Add(new ItemWrapper("Enable Addressable", $"{buildReport.Summary.EnableAddressable}"));
_items.Add(new ItemWrapper("Location To Lower", $"{buildReport.Summary.LocationToLower}"));
_items.Add(new ItemWrapper("Include Asset GUID", $"{buildReport.Summary.IncludeAssetGUID}"));
_items.Add(new ItemWrapper("Ignore Default Type", $"{buildReport.Summary.IgnoreDefaultType}"));
_items.Add(new ItemWrapper("Auto Collect Shaders", $"{buildReport.Summary.AutoCollectShaders}"));
_items.Add(new ItemWrapper("Ignore Rule Name", $"{buildReport.Summary.IgnoreRuleName}"));
_items.Add(new ItemWrapper(string.Empty, string.Empty));
_items.Add(new ItemWrapper("Build Params", string.Empty));