Esempio n. 1
0
        /// <summary>
        /// 检测构建结果
        /// </summary>
        private void CheckBuildMapContent(BuildMapContext buildMapContext)
        {
            foreach (var bundleInfo in buildMapContext.BundleInfos)
            {
                // 注意:原生文件资源包只能包含一个原生文件
                bool isRawFile = bundleInfo.IsRawFile;
                if (isRawFile)
                {
                    if (bundleInfo.BuildinAssets.Count != 1)
                    {
                        throw new Exception($"The bundle does not support multiple raw asset : {bundleInfo.BundleName}");
                    }
                    continue;
                }

                // 注意:原生文件不能被其它资源文件依赖
                foreach (var assetInfo in bundleInfo.BuildinAssets)
                {
                    if (assetInfo.AllDependAssetInfos != null)
                    {
                        foreach (var dependAssetInfo in assetInfo.AllDependAssetInfos)
                        {
                            if (dependAssetInfo.IsRawAsset)
                            {
                                throw new Exception($"{assetInfo.AssetPath} can not depend raw asset : {dependAssetInfo.AssetPath}");
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 创建补丁清单文件到输出目录
        /// </summary>
        private void CreatePatchManifestFile(AssetBundleBuilder.BuildParametersContext buildParameters,
                                             BuildMapContext buildMapContext, TaskEncryption.EncryptionContext encryptionContext)
        {
            int resourceVersion = buildParameters.Parameters.BuildVersion;

            // 创建新补丁清单
            PatchManifest patchManifest = new PatchManifest();

            patchManifest.EnableAddressable = buildParameters.Parameters.EnableAddressable;
            patchManifest.ResourceVersion   = buildParameters.Parameters.BuildVersion;
            patchManifest.BuildinTags       = buildParameters.Parameters.BuildinTags;
            patchManifest.BundleList        = GetAllPatchBundle(buildParameters, buildMapContext, encryptionContext);
            patchManifest.AssetList         = GetAllPatchAsset(buildParameters, buildMapContext, patchManifest);

            // 创建补丁清单文件
            string manifestFilePath = $"{buildParameters.PipelineOutputDirectory}/{YooAssetSettingsData.GetPatchManifestFileName(resourceVersion)}";

            BuildRunner.Log($"创建补丁清单文件:{manifestFilePath}");
            PatchManifest.Serialize(manifestFilePath, patchManifest);

            // 创建补丁清单哈希文件
            string manifestHashFilePath = $"{buildParameters.PipelineOutputDirectory}/{YooAssetSettingsData.GetPatchManifestHashFileName(resourceVersion)}";
            string manifestHash         = HashUtility.FileMD5(manifestFilePath);

            BuildRunner.Log($"创建补丁清单哈希文件:{manifestHashFilePath}");
            FileUtility.CreateFile(manifestHashFilePath, manifestHash);

            // 创建静态版本文件
            string staticVersionFilePath = $"{buildParameters.PipelineOutputDirectory}/{YooAssetSettings.VersionFileName}";
            string staticVersion         = resourceVersion.ToString();

            BuildRunner.Log($"创建静态版本文件:{staticVersionFilePath}");
            FileUtility.CreateFile(staticVersionFilePath, staticVersion);
        }
        /// <summary>
        /// 获取资源包列表
        /// </summary>
        private List <PatchBundle> GetAllPatchBundle(AssetBundleBuilder.BuildParametersContext buildParameters,
                                                     BuildMapContext buildMapContext, TaskEncryption.EncryptionContext encryptionContext)
        {
            List <PatchBundle> result = new List <PatchBundle>(1000);

            List <string> buildinTags   = buildParameters.Parameters.GetBuildinTags();
            var           buildMode     = buildParameters.Parameters.BuildMode;
            bool          standardBuild = buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild;

            foreach (var bundleInfo in buildMapContext.BundleInfos)
            {
                var      bundleName  = bundleInfo.BundleName;
                string   filePath    = $"{buildParameters.PipelineOutputDirectory}/{bundleName}";
                string   hash        = GetFileHash(filePath, standardBuild);
                string   crc32       = GetFileCRC(filePath, standardBuild);
                long     size        = GetFileSize(filePath, standardBuild);
                string[] tags        = buildMapContext.GetBundleTags(bundleName);
                bool     isEncrypted = encryptionContext.IsEncryptFile(bundleName);
                bool     isBuildin   = IsBuildinBundle(tags, buildinTags);
                bool     isRawFile   = bundleInfo.IsRawFile;

                // 附加文件扩展名
                if (buildParameters.Parameters.AppendFileExtension)
                {
                    hash += bundleInfo.GetAppendExtension();
                }

                PatchBundle patchBundle = new PatchBundle(bundleName, hash, crc32, size, tags);
                patchBundle.SetFlagsValue(isEncrypted, isBuildin, isRawFile);
                result.Add(patchBundle);
            }

            return(result);
        }
        /// <summary>
        /// 获取资源列表
        /// </summary>
        private List <PatchAsset> GetAllPatchAsset(AssetBundleBuilder.BuildParametersContext buildParameters,
                                                   BuildMapContext buildMapContext, PatchManifest patchManifest)
        {
            List <PatchAsset> result = new List <PatchAsset>(1000);

            foreach (var bundleInfo in buildMapContext.BundleInfos)
            {
                var assetInfos = bundleInfo.GetAllPatchAssetInfos();
                foreach (var assetInfo in assetInfos)
                {
                    PatchAsset patchAsset = new PatchAsset();
                    if (buildParameters.Parameters.EnableAddressable)
                    {
                        patchAsset.Address = assetInfo.Address;
                    }
                    else
                    {
                        patchAsset.Address = string.Empty;
                    }
                    patchAsset.AssetPath = assetInfo.AssetPath;
                    patchAsset.AssetTags = assetInfo.AssetTags.ToArray();
                    patchAsset.BundleID  = GetAssetBundleID(assetInfo.GetBundleName(), patchManifest);
                    patchAsset.DependIDs = GetAssetBundleDependIDs(patchAsset.BundleID, assetInfo, patchManifest);
                    result.Add(patchAsset);
                }
            }
            return(result);
        }