Example #1
0
        /// <summary>
        /// 创建文件 参数1:文件路径 参数2:文件数据 参数3:是否覆盖创建
        /// </summary>
        public static bool CreateFile(string path, byte[] data, bool overCreate)
        {
            try
            {
                if (overCreate && File.Exists(path))
                {
                    File.Delete(path);
                }
                using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
                {
                    if (data != null)
                    {
                        fs.Write(data, 0, data.Length);
                    }
                    fs.Close();
                }
                DebugManager.Log(path + "文件创建成功!");
                return(true);
            }
            catch (Exception e)
            {
                DebugManager.LogError(path + "文件创建失败!error:" + e.Message);
            }

            return(false);
        }
        /// <summary>
        /// 安卓解包过程
        /// </summary>
        private IEnumerator StartAndroidUnPack(AssetBundleDownloadTaskModel taskModel, Action onComplete)
        {
            string resDicUrl    = ABDataConfig.AppAssetBundleDirName; //安卓包资源路径
            string targetDicUrl = ABDataConfig.localABFullName;       //目标路径

            string[] copyFile = new string[]
            {
                ABDataConfig.localCacheFileName,
                ABDataConfig.localDepFileName,
            };
            //先拷贝cache.txt 与dep.file文件
            taskModel.taskMessage = "解压配置文件";
            yield return(CopyAssetBundle(resDicUrl, targetDicUrl, copyFile));

            //解析dep文件 得到所有的ab 名字
            var abNames = AssetBundleDepFileManager.ResolveDepFile(targetDicUrl + copyFile[1]);
            //添加mainfest文件
            int count = abNames.Count;

            for (int i = 0; i < count; ++i)
            {
                abNames.Add(abNames[i] + ABDataConfig.assetBundleManifestFilePostfixName);
            }
            //在拷贝所有ab文件
            taskModel.taskMessage = "解压ab文件";
            yield return(CopyAssetBundle(resDicUrl, targetDicUrl, abNames));

            DebugManager.Log("解包完成!");
            taskModel.taskMessage = "解包完成";
            yield return(0);

            onComplete();
        }
        /// <summary>
        /// 解包过程 参数1:任务model  参数2:回调
        /// </summary>
        private void StartUnPack(AssetBundleDownloadTaskModel taskModel, Action onComplete)
        {
            taskModel.taskMessage = "检查安装包资源";
            var localAbFullName = ABDataConfig.localABFullName;

            if (!Directory.Exists(localAbFullName))
            {
                taskModel.taskMessage = "开始解包";
                Directory.CreateDirectory(localAbFullName);
                DebugManager.Log(localAbFullName + "文件夹不存在,自动创建!开始解包!");

                if (Application.platform == RuntimePlatform.Android)
                {
                    //安卓环境下必须用www访问StreamingAssets文件夹下的文件
                    StartCoroutine(StartAndroidUnPack(taskModel, onComplete));
                }
                else
                {
                    Util.CopyDirectoryToOtherDir(ABDataConfig.AppAssetBundleDirName, localAbFullName);
                    DebugManager.Log("解包完成!");
                    taskModel.taskMessage = "解包完成";
                    onComplete();
                }
            }
            else
            {
                DebugManager.Log(localAbFullName + "文件夹存在!不需要解包!");
                onComplete();
            }
        }
Example #4
0
 /// <summary>
 /// 删除文件
 /// </summary>
 public static void DeleteFile(string path)
 {
     if (File.Exists(path))
     {
         File.Delete(path);
         DebugManager.Log(path + "文件删除成功!");
     }
 }
        private Dictionary <string, AssetBundleData> curentDepFileABDataDic = null;//dep.all 读取出来的数据
        #endregion

        #region 公有方法
        public void GetUpdateDownLoadDepFile(string serveDepUrl, Action onComplete)
        {
            //下载服务器dep.all文件
            Util.DownLoadFileText(serveDepUrl, (serveDepFileText) =>
            {
                //比较dep文件,形成dep链表
                CompareDepFile(serveDepFileText);
                DebugManager.Log("dep 文件解析完毕!");
                onComplete();
            }, this);
        }
        /// <summary>
        /// 比较本地ab的cache.txt文件的文本与服务器的文本得出差异
        /// 参数1:本地cache.txt文本  参数2:服务器cache.txt 文本
        /// </summary>
        private void GetUpdateDownLoadAssetBundles(string localCacheFileText, string serverCacheFileText)
        {
            if (string.IsNullOrEmpty(serverCacheFileText))
            {
                DebugManager.LogError("服务器 cache.txt 的文本存在空值!serverCacheFileText:" + serverCacheFileText);
                return;
            }
            if (serverCacheFileText.Equals(localCacheFileText))
            {
                DebugManager.LogWarning("cache.txt 本地与服务器文本相同,不做比较,继续更新!  localCacheFileText:" + localCacheFileText + "serverCacheFileText:" + serverCacheFileText);
                //return;
            }

            var serverCacheFileData = ResolveCacheFile(serverCacheFileText);//服务器cache.txt数据

            //Dictionary<string, CacheFileDataModel> localCacheFileData = null;
            //if (localCacheFileText == null)
            //{
            //    localCacheFileData = new Dictionary<string, CacheFileDataModel>();
            //}
            //else
            //{
            //    localCacheFileData = ResolveCacheFile(localCacheFileText);//本地cache.txt数据
            //}

            if (waitDownloadAssetBundleList == null)
            {
                waitDownloadAssetBundleList = new List <AssetBundleDownloadModel>();
            }

            //对比服务器与本地的数据
            foreach (var v in serverCacheFileData)
            {
                var tempABData = AssetBundleDepFileManager.Instance.GetAssetBundleDataOfABFullName(v.Key); //dep.ll 文件中的ab信息

                var tempFileUrl = ABDataConfig.localABFullName + tempABData.fullName;                      //源资源文件路径
                //得到ab文件与ab依赖文件的哈希值组合
                var tempFileHash = HashUtil.GetFileHash(tempFileUrl) + "|" + HashUtil.GetFileHash(tempFileUrl + ABDataConfig.assetBundleManifestFilePostfixName);
                if (v.Value.fileHash.Equals(tempFileHash))
                {
                    DebugManager.Log(tempABData.fullName + "  " + v.Key + "  资源文件与依赖文件哈希值与服务器一致!不用更新!");
                    continue;
                }
                DebugManager.Log(tempABData.fullName + "  " + v.Key + "  需要更新!添加到更新列表!");
                waitDownloadAssetBundleList.Add(new AssetBundleDownloadModel
                {
                    assetBundleFileName         = tempABData.fullName,
                    assetBundleManifestFileName = tempABData.fullName + ABDataConfig.assetBundleManifestFilePostfixName,
                    assetBundleName             = tempABData.debugName,
                });
            }
        }
        /// <summary>
        /// 比较本地ab的dep.all文件的文本与服务器的文本得出差异
        /// 参数1:服务器dep.all文本
        /// </summary>
        private void CompareDepFile(string serveDepFileText)
        {
            string depFilePath = ABDataConfig.localABFullName + ABDataConfig.localDepFileName;

            //找到本地dep文件
            if (File.Exists(depFilePath))
            {
                string value = File.ReadAllText(depFilePath);
                if (serveDepFileText.Equals(value))
                {
                    DebugManager.Log("服务器dep文件与本地dep文件一致!不用更新!");
                    InitDepList(depFilePath);
                    return;
                }
            }
            DebugManager.Log("服务器dep文件与本地dep文件不一致!更新替换!");
            //TODO 此时应当加密写入(暂时不写了)
            FileManager.CreateFile(depFilePath, System.Text.Encoding.UTF8.GetBytes(serveDepFileText));
            InitDepList(depFilePath);
        }
        /// <summary>
        /// 解析dep文件  参数1:dep文件路径   返回所有资源名字
        /// </summary>
        public static List <string> ResolveDepFile(string depFilePath)
        {
            var tempReader = new AssetBundleDataReader();

            using (FileStream fs = new FileStream(depFilePath, FileMode.Open, FileAccess.Read))
            {
                tempReader.Read(fs);
                DebugManager.Log(depFilePath + "  dep文件读取成功!读取到的ab资源个数为:" + tempReader.infoMap.Count);
                List <string> resolveDepName = new List <string>();
                foreach (var v in tempReader.infoMap)
                {
                    if (!resolveDepName.Contains(v.Value.fullName))
                    {
                        resolveDepName.Add(v.Value.fullName);
                    }
                }
                tempReader = null;
                return(resolveDepName);
            }
        }
 /// <summary>
 /// 初始化当前最新的dep.all ab列表
 /// </summary>
 private void InitDepList(string depFilePath)
 {
     curentDepFileReader = new AssetBundleDataReader();
     using (FileStream fs = new FileStream(depFilePath, FileMode.Open, FileAccess.Read))
     {
         curentDepFileReader.Read(fs);
         DebugManager.Log(depFilePath + "  dep文件读取成功!读取到的ab资源个数为:" + curentDepFileReader.infoMap.Count);
         curentDepFileABDataDic = new Dictionary <string, AssetBundleData>();
         foreach (var v in curentDepFileReader.infoMap)
         {
             if (!curentDepFileABDataDic.ContainsKey(v.Value.debugName))
             {
                 curentDepFileABDataDic.Add(v.Value.debugName, v.Value);
             }
             else
             {
                 DebugManager.LogWarning("dep文件读取到的" + v.Value.debugName + "资源重复!");
             }
         }
     }
 }
        /// <summary>
        /// 任务下载器
        /// </summary>
        private IEnumerator DownloadTaskMangage(AssetBundleDownloadTaskModel task)
        {
            downloadTaskMangageState = true;//状态位标志
            string downLoadUrl = ABDataConfig.GetAssetBundleUrl + ABDataConfig.getAssetBundleApiName;

            DebugManager.Log("任务开始下载:" + task.ToString());

            AssetBundleDownloadModel tempABModel = null;//中间变量

            #region 遍历未下载列表资源,开启下载
            for (int i = 0; i < task.downloadNOModels.Count; ++i)
            {
                tempABModel = task.downloadNOModels[i];

                DownloadModel downModel = new DownloadModel();

                //下载资源文件
                task.taskMessage    = "下载资源" + tempABModel.assetBundleFileName;
                downModel.postDatas = System.Text.Encoding.UTF8.GetBytes(tempABModel.assetBundleFileName);
                yield return(DownloadABFile(downLoadUrl, downModel));

                tempABModel.assetBundleData = downModel.resultDatas;

                //下载资源mainfest文件
                task.taskMessage    = "下载资源" + tempABModel.assetBundleManifestFileName;
                downModel.postDatas = System.Text.Encoding.UTF8.GetBytes(tempABModel.assetBundleManifestFileName);
                yield return(DownloadABFile(downLoadUrl, downModel));

                tempABModel.assetBundleManifestData = downModel.resultDatas;


                if (tempABModel != null && tempABModel.assetBundleManifestData != null && tempABModel.assetBundleData != null)
                {
                    DebugManager.Log("资源:" + tempABModel.assetBundleName + "下载成功!准备写入本地文件!");
                    //TODO 写入文件
                    bool result = FileManager.CreateFile(ABDataConfig.localABFullName + tempABModel.assetBundleFileName, tempABModel.assetBundleData);
                    if (result)
                    {
                        result = FileManager.CreateFile(ABDataConfig.localABFullName + tempABModel.assetBundleManifestFileName, tempABModel.assetBundleManifestData);
                    }

                    if (result)
                    {
                        task.downloadOKModels.Add(tempABModel);//添加到成功队列中
                    }
                    else
                    {
                        DebugManager.Log("资源:" + tempABModel.assetBundleName + "下载成功,写入文件异常!");
                        task.downloadErrorModels.Add(tempABModel);//添加到失败队列中
                    }
                }
                else
                {
                    DebugManager.Log("资源:" + tempABModel.assetBundleName + "下载异常!数据错误!");
                    task.downloadErrorModels.Add(tempABModel);//添加到失败队列中
                }

                task.downloadNOModels.RemoveAt(i--);//移出未下载队列
                tempABModel = null;
            }
            #endregion

            //判断任务是否成功或者失败
            if (task.downloadErrorModels != null && task.downloadErrorModels.Count > 0)
            {
                DebugManager.LogWarning("任务:" + task.ToString() + "下载失败!");
                task.taskState   = TaskState.TaskError;
                task.taskMessage = task.downloadErrorModels.Count + "个资源下载失败!重新下载!";
                downloadErrorTasks.Enqueue(task);
            }
            else
            {
                DebugManager.LogWarning("任务:" + task.ToString() + "下载成功!");
                task.taskState   = TaskState.TaskOk;
                task.taskMessage = "下载完成";
                downloadSuccessTasks.Add(task);
            }

            //当前任务下载结束
            downloadTaskMangageState = false;
        }