private bool VerificationFile(UpdateFileInfo updateFileInfo, string filePath)
        {
//            return true;
            var length = 0;

            using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite))
            {
                length = (int)fileStream.Length;
                if (length != updateFileInfo.ZipLength)
                {
                    FileUtility.DeleteFile(filePath);
                    return(false);
                }
                if (updateFileCache == null || updateFileCache.Length < length)
                {
                    updateFileCache = new byte[(length / OneMegaBytes + 1) * OneMegaBytes];
                }
                int offset = 0;
                int count  = length;
                while (count > 0)
                {
                    int bytesRead = fileStream.Read(updateFileCache, offset, count);
                    offset += bytesRead;
                    count  -= bytesRead;
                }
            }

            if (serverBundleIsZip)
            {
                byte[] zipMd5Bytes = MD5Utility.GetMd5Bytes(updateFileCache, 0, length);
                int    zipMd5Code  = BitConverter.ToInt32(zipMd5Bytes, 0);
                if (zipMd5Code != updateFileInfo.ZipMd5Code)
                {
                    FileUtility.DeleteFile(filePath);
                    return(false);
                }
                int decompressSize = ZipUtility.DeCompressionFileInSamePath(filePath, serverBundleZipPassword);
                if (decompressSize != updateFileInfo.Length)
                {
                    FileUtility.DeleteFile(filePath);
                    return(false);
                }
            }
            else
            {
                byte[] md5Bytes = MD5Utility.GetMd5Bytes(updateFileCache, 0, length);
                int    md5Code  = BitConverter.ToInt32(md5Bytes, 0);
                if (md5Code != updateFileInfo.Md5Code)
                {
                    FileUtility.DeleteFile(filePath);
                    return(false);
                }
            }
            string srcPath = PathUtility.GetCombinePath(AppConst.Path.HotUpdateDownloadDataPath,
                                                        updateFileInfo.AssetBundleName);
            string desPath = PathUtility.GetCombinePath(AppConst.Path.PresistentDataPath,
                                                        updateFileInfo.AssetBundleName);

            if (FileUtility.IsFileExist(desPath))
            {
                FileUtility.DeleteFile(desPath);
            }
            FileUtility.Move(srcPath, desPath);
            if (FileUtility.IsFileExist(filePath))
            {
                FileUtility.DeleteFile(filePath);
            }
            return(true);
        }
示例#2
0
        private static void ProcessPackage(string workingPath, string tempworkingPath, string packagePath,
                                           AssetBundleBuildRule.Platform platform)
        {
            AssetBundleFileDatas.Clear();
            string packageVersionPath = $"{packagePath}{AppConst.AssetBundleConfig.VersionFile}";
            string assetbundleZipPath = $"{packagePath}{AppConst.AssetBundleConfig.AssetBundlePackageFile}";
            string fileInfoPath       = $"{packagePath}{AppConst.AssetBundleConfig.FileListFile}";

            string[]      allFiles            = Directory.GetFiles(workingPath, "*", SearchOption.AllDirectories);
            List <string> allAssetBundleFiles = new List <string>();

            foreach (string tempFile in allFiles)
            {
                string path = tempFile.Replace("\\", "/");
                if (Path.GetExtension(path) == ".manifest")
                {
                    continue;
                }
                allAssetBundleFiles.Add(path);
            }
            EditorUtility.ClearProgressBar();
            int  index     = 0;
            int  count     = allAssetBundleFiles.Count;
            long sizeCount = 0;

            foreach (string assetBundleFile in allAssetBundleFiles)
            {
                index++;
                EditorUtility.DisplayProgressBar("复制AssetBundle", "复制AssetBundle文件", (float)index / (float)count);
                string target     = assetBundleFile.Replace(workingPath, tempworkingPath);
                string targetPath = Path.GetDirectoryName(target);
                if (!Directory.Exists(targetPath))
                {
                    if (targetPath != null)
                    {
                        Directory.CreateDirectory(targetPath);
                    }
                }
                File.Copy(assetBundleFile, target);

                AssetBundleFileData assetBundleFileData = new AssetBundleFileData();
                byte[] srcBytes  = File.ReadAllBytes(assetBundleFile);
                int    srcLength = srcBytes.Length;
                sizeCount += srcBytes.Length;
                byte[] srcMd5Bytes     = MD5Utility.GetMd5Bytes(srcBytes);
                int    srcMd5Code      = BitConverter.ToInt32(srcMd5Bytes, 0);
                string assetBundleName = target.Replace(tempworkingPath, "");
                assetBundleFileData.AssetBundleName = assetBundleName;
                assetBundleFileData.Length          = srcLength;
                assetBundleFileData.Md5Code         = srcMd5Code;
                assetBundleFileData.ZipLength       = 0;
                assetBundleFileData.ZipMd5Code      = 0;
                AssetBundleFileDatas.Add(assetBundleFileData);
            }
            EditorUtility.ClearProgressBar();
            ZipUtility.CompressFolder(assetbundleZipPath, tempworkingPath, "*", AssetBundleRule.ZipPassWord);
            using (ByteBuffer buffer = new ByteBuffer())
            {
                ValueParse.WriteValue(buffer, platform.ToString(), ValueParse.StringParse);
                ValueParse.WriteValue(buffer, CurrentVersion.MasterVersion, ValueParse.IntParse);
                ValueParse.WriteValue(buffer, CurrentVersion.MinorVersion, ValueParse.IntParse);
                ValueParse.WriteValue(buffer, CurrentVersion.RevisedVersion, ValueParse.IntParse);
                ValueParse.WriteValue(buffer, allAssetBundleFiles.Count, ValueParse.IntParse);
                ValueParse.WriteValue(buffer, sizeCount, ValueParse.LongParse);
                ValueParse.WriteValue(buffer,
                                      string.IsNullOrEmpty(AssetBundleRule.AssetBundleVariant) ? "" : AssetBundleRule.AssetBundleVariant,
                                      ValueParse.StringParse);
                ValueParse.WriteValue(buffer, AssetBundleRule.ZipSelected, ValueParse.BoolParse);
                ValueParse.WriteValue(buffer,
                                      string.IsNullOrEmpty(AssetBundleRule.ZipPassWord) ? "" : AssetBundleRule.ZipPassWord,
                                      ValueParse.StringParse);
                File.WriteAllBytes(packageVersionPath, buffer.ToBytes());
            }

            using (ByteBuffer buffer = new ByteBuffer())
            {
                ValueParse.WriteValue(buffer, AssetBundleFileDatas.Count, ValueParse.IntParse);
                foreach (AssetBundleFileData assetBundleFileData in AssetBundleFileDatas)
                {
                    ValueParse.WriteValue(buffer, assetBundleFileData.AssetBundleName, ValueParse.StringParse);
                    ValueParse.WriteValue(buffer, assetBundleFileData.Length, ValueParse.IntParse);
                    ValueParse.WriteValue(buffer, assetBundleFileData.Md5Code, ValueParse.IntParse);
                    ValueParse.WriteValue(buffer, assetBundleFileData.ZipLength, ValueParse.IntParse);
                    ValueParse.WriteValue(buffer, assetBundleFileData.ZipMd5Code, ValueParse.IntParse);
                }
                File.WriteAllBytes(fileInfoPath, buffer.ToBytes());
            }
        }