using XGame.Framework.Asset;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace XGame.Editor.Build.AssetBundles
{
    public static class AssetBundleManifestExt
    {
        /// <summary>
        /// 编辑器扩展方法
        /// 返回所有bundle的FullName
        /// </summary>
        /// <param name="manifest"></param>
        /// <returns></returns>
        public static string[] GetAllBundleNames(this IAssetBundleManifest manifest)
        {
            if (manifest is CSharpAssetBundleManifest csBundleManifest)
            {
                var fileInfo = csBundleManifest.GetType().GetField("_bundleIdToNameMap", BindingFlags.NonPublic | BindingFlags.Instance);
                if (fileInfo != null)
                {
                    var bundles = (Dictionary<uint, string>)fileInfo.GetValue(csBundleManifest);
                    return bundles.Values.ToArray();
                }
            }
            return null;
        }
        /// <summary>
        /// 编辑器扩展方法
        /// 返回所有的bundleInfo
        /// </summary>
        /// <param name="manifest"></param>
        /// <returns></returns>
        public static AssetBundleInfo[] GetAllBundleInfos(this IAssetBundleManifest manifest)
        {
            if (manifest is CSharpAssetBundleManifest csBundleManifest)
            {
                var fileInfo = csBundleManifest.GetType().GetField("_bundleInfoMap", BindingFlags.NonPublic | BindingFlags.Instance);
                if (fileInfo != null)
                {
                    var bundles = (Dictionary<string, AssetBundleInfo>)fileInfo.GetValue(csBundleManifest);
                    return bundles.Values.ToArray();
                }
            }
            return null;
        }
    }

}
