private void InitAssetEncrypter()
        {
            var types = AssemblyUtility.GetAssignableTypes(AssemblyUtility.UnityDefaultAssemblyEditorName, typeof(IAssetEncrypter));

            if (types.Count == 0)
            {
                return;
            }
            if (types.Count != 1)
            {
                throw new Exception($"Found more {nameof(IAssetEncrypter)} types. We only support one.");
            }

            BuildLogger.Log($"创建加密类 : {types[0].FullName}");
            _encrypter = (IAssetEncrypter)Activator.CreateInstance(types[0]);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 加密文件
        /// </summary>
        private List <string> EncryptFiles(IAssetEncrypter encrypter, AssetBundleManifest unityManifest, AssetBundleBuilder.BuildParametersContext buildParameters)
        {
            // 加密资源列表
            List <string> encryptList = new List <string>();

            // 如果没有设置加密类
            if (encrypter == null)
            {
                return(encryptList);
            }

            BuildLogger.Log($"开始加密资源文件");
            string[] allAssetBundles = unityManifest.GetAllAssetBundles();
            int      progressValue   = 0;

            foreach (string bundleName in allAssetBundles)
            {
                string filePath = $"{buildParameters.PipelineOutputDirectory}/{bundleName}";
                if (encrypter.Check(filePath))
                {
                    encryptList.Add(bundleName);

                    // 注意:通过判断文件合法性,规避重复加密一个文件
                    byte[] fileData = File.ReadAllBytes(filePath);
                    if (EditorTools.CheckBundleFileValid(fileData))
                    {
                        byte[] bytes = encrypter.Encrypt(fileData);
                        File.WriteAllBytes(filePath, bytes);
                        BuildLogger.Log($"文件加密完成:{filePath}");
                    }
                }

                // 进度条
                EditorTools.DisplayProgressBar("加密资源包", ++progressValue, allAssetBundles.Length);
            }
            EditorTools.ClearProgressBar();

            return(encryptList);
        }