static void BuildActivityAssetBundle()
        {
            AssetDatabase.Refresh();
            List <AssetBundleBuild> buildMap = new List <AssetBundleBuild>();
            string topPath;
            string abFileNameNoSuffix;
            string abFileName;
            string outPath = FilePathTools.assetBundleOutPath + "/Activity" + DateTime.Now.ToString("yyyy-MM-dd");

            UnityEngine.Object[] selects = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets);
            int length = selects.Length;

            if (length > 0)
            {
                topPath = GetSelectedPathOrFallback(selects);
                //DebugUtil.Log("F: " + topPath);
                topPath = topPath.Substring(14);//删除"Asset/Export/"
                //DebugUtil.Log("S: " + topPath);
                abFileNameNoSuffix = topPath.Replace('/', '_').ToLower();
                abFileName         = string.Format("{0}.ab", abFileNameNoSuffix);
                //DebugUtil.Log(abFileNameNoSuffix);
            }
            else
            {
                DebugUtil.LogError("不要对空文件夹进行打包!!");
                return;
            }

            List <string>    assetnames = new List <string>();
            AssetBundleBuild build      = new AssetBundleBuild
            {
                assetBundleName = abFileName
            };

            for (int i = 0; i < length; i++)
            {
                string objPath = AssetDatabase.GetAssetPath(selects[i]);
                //DebugUtil.Log(objPath);
                assetnames.Add(objPath);
            }
            build.assetNames = assetnames.ToArray();
            buildMap.Add(build);


            //if (Directory.Exists(TargetPath))
            //Directory.Delete(TargetPath, true);

            if (!Directory.Exists(outPath))
            {
                Directory.CreateDirectory(outPath);
            }

            BuildPipeline.SetAssetBundleEncryptKey(AssetConfigController.Instance.EnhancedEncryptionSecret);
            AssetBundleManifest mainfest = BuildPipeline.BuildAssetBundles(outPath, buildMap.ToArray(), recommandBundleOptions, EditorUserBuildSettings.activeBuildTarget);

            //------------------------------------ 压缩,记录md5 ---------------------------------//

            string assetBundlePath = string.Format("{0}/{1}", outPath, abFileName);
            //string tempPath = assetBundlePath + ".temp";
            //Zip.Tool.CompressFileLZMA(assetBundlePath, tempPath);
            //File.Delete(assetBundlePath);
            string hash = mainfest.GetAssetBundleHash(abFileName).ToString();
            string md5  = AssetUtils.BuildFileMd5(assetBundlePath);
            string abFileNameWithMd5 = string.Format("{0}/{1}_{2}_{3}.ab", outPath, abFileNameNoSuffix, hash, md5);

            File.Move(assetBundlePath, abFileNameWithMd5);

            //----------------------------------- End 压缩,记录md5 ------------------------------//

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            DebugUtil.Log("BuildActivityAssetBundle finish");
        }
Example #2
0
        private void ReadHttpStream(HTTPRequest request, HTTPResponse response)
        {
            string localfile = this.m_DownloadInfo.tempPath;

            try
            {
                List <byte[]> streamedFragments = response.GetStreamedFragments();
                if (streamedFragments != null)
                {
                    int num = 0;
                    using (FileStream fileStream = new FileStream(localfile, FileMode.Append))
                    {
                        foreach (byte[] array in streamedFragments)
                        {
                            num += array.Length;
                            fileStream.Write(array, 0, array.Length);
                        }
                    }
                    this.m_DownloadInfo.downloadedSize += num;
                    m_DownloadInfo?.OnProgressChange();
                }
            }
            catch (Exception ex)
            {
                if (ex.GetType() == typeof(System.IO.IOException))
                {
                    if (Utils.IsDiskFull(ex))
                    {
                        EventManager.Instance.Trigger <SDKEvents.DownloadFileEvent>().Data(this.m_DownloadInfo.fileName, "DiskFullException", this.m_DownloadInfo.downloadedSize).Trigger();
                    }
                    else
                    {
                        EventManager.Instance.Trigger <SDKEvents.DownloadFileEvent>().Data(this.m_DownloadInfo.fileName, "IOException", this.m_DownloadInfo.downloadedSize).Trigger();
                    }
                }
                else
                {
                    EventManager.Instance.Trigger <SDKEvents.DownloadFileEvent>().Data(this.m_DownloadInfo.fileName, "failure", this.m_DownloadInfo.downloadedSize).Trigger();
                }
                DebugUtil.Log("An error occured while downloading {0} due to {1}.Cancelling!", localfile, ex);
                this.m_DownloadInfo.result = DownloadResult.Failed;

                CancelDownload(true);
                this.CancelDownload(true);
                return;
            }

            float num2 = (float)this.m_DownloadInfo.downloadedSize / (float)this.m_DownloadInfo.downloadSize;

            this.m_DownloadInfo.currProgress = num2;

            //DebugUtil.Log("Downloading {0} Status: Range {1}/{2} ({3:0.00})", this.m_DownloadInfo.fileName, this.m_DownloadInfo.downloadedSize, this.m_DownloadInfo.downloadSize, this.m_DownloadInfo.currProgress);
            if (!response.IsStreamingFinished || request.State != HTTPRequestStates.Finished)
            {
                return;
            }

            DebugUtil.Log("Download finished : {0}", this.m_DownloadInfo.savePath);
            this.m_DownloadInfo.currProgress = 1.0f;

            // 续传完成后,做一手MD5验证
            string localMd5 = AssetUtils.BuildFileMd5(localfile);

            if (localMd5.Trim() != m_DownloadInfo.fileMd5.Trim())
            {
                //md5验证失败,删除临时文件
                if (File.Exists(localfile))
                {
                    File.Delete(localfile);
                }

                DebugUtil.Log("md5 error, retry download:" + localfile + " =>" + localMd5 + " ## " + m_DownloadInfo.fileMd5);
                this.m_DownloadInfo.result       = DownloadResult.Md5Error;
                this.m_DownloadInfo.currProgress = 0.0f;
                CancelDownload(true);
            }
            else
            {// md5验证通过,临时文件转为最终文件
                string localfile2 = m_DownloadInfo.savePath;
                if (File.Exists(localfile2))
                {
                    File.Delete(localfile2);
                }

                DebugUtil.Log("md5 check success, move temp to final :" + this.m_DownloadInfo.fileName);
                File.Move(localfile, localfile2);
                this.m_DownloadInfo.result = DownloadResult.Success;
                DownloadManager.Instance.FinishDownloadTask(this.m_DownloadInfo);
            }

            EventManager.Instance.Trigger <SDKEvents.DownloadFileEvent>().Data(this.m_DownloadInfo.fileName, "finish", m_DownloadInfo.downloadedSize).Trigger();
        }
        IEnumerator CheckInitPackage()
        {
            if (Directory.Exists(FilePathTools.persistentDataPath_Platform))
            {
                DirectoryInfo dir = new DirectoryInfo(FilePathTools.persistentDataPath_Platform);
                dir.Empty();
            }

            Log("clean initpackage done", false);

            List <string> bundles = new List <string>();
            List <string> md5List = new List <string>();

            foreach (BundleGroup group in AssetConfigController.Instance.Groups)
            {
                List <string> temp = group.GetInInitialPacketPaths();
                foreach (var p in temp)
                {
                    bundles.Add(p + ".ab");
                    md5List.Add(localVersion.GetAssetBundleMd5(group.GroupName, p.ToLower() + ".ab"));
                }
            }

            Log(string.Format("copy progress:{00:F1}%", 0.0f), false);

            int cnt = 0;

            foreach (var p in bundles)
            {
                string path = string.Format("{0}/{1}", FilePathTools.streamingAssetsPath_Platform_ForWWWLoad, p);

                using (UnityWebRequest www = UnityWebRequest.Get(path))
                {
                    yield return(www.SendWebRequest());

                    if (www.isNetworkError || www.isHttpError)
                    {
                        Fail(string.Format("can not copy file : {0}", path));
                    }
                    else
                    {
                        try
                        {
                            byte[] b        = www.downloadHandler.data;
                            string destFile = string.Format("{0}/{1}", FilePathTools.persistentDataPath_Platform, p);
                            string destDir  = Path.GetDirectoryName(destFile);
                            if (!Directory.Exists(destDir))
                            {
                                Directory.CreateDirectory(destDir);
                            }

                            if (File.Exists(destFile))
                            {
                                File.Delete(destFile);
                            }

                            using (FileStream fs = new FileStream(destFile, FileMode.Create))
                            {
                                fs.Seek(0, SeekOrigin.Begin);
                                fs.Write(b, 0, b.Length);
                                fs.Close();
                            }

                            string md5            = AssetUtils.BuildFileMd5(destFile);
                            string md5_persistent = md5List[cnt];

                            if (!md5.Equals(md5_persistent))
                            {
                                Fail(string.Format("{0} md5 not equal version record", path));
                            }

                            Log(string.Format("copy progress:{00:F1}%", (++cnt / (bundles.Count * 1.0f)) * 100.0f), true);
                        }
                        catch (Exception e)
                        {
                            Fail(e.ToString());
                        }
                    }
                }

                yield return(null);
            }

            Log(string.Format("copy progress:{00:F1}%", 100.0f), true);

            Log("copy initpackage done", false);

            yield return(StartCoroutine(CheckAsset(bundles)));

            Log("check initpackage done", false);

            yield return(null);
        }