Esempio n. 1
0
        void SmartAdd(UFileVersionRemote _info)
        {
            List <UFileVersionRemote> outList;

            if (false == DownloadInfomation.TryGetValue(_info.ModuleName, out outList))
            {
                outList = new List <UFileVersionRemote>();
                DownloadInfomation.Add(_info.ModuleName, outList);
            }
            outList.Add(_info);
        }
Esempio n. 2
0
        IEnumerator DoStart(List <UFileVersionRemote> _fileList, string _downloadAddress, string _persistentPathRoot, string _tempPathRoot, Action <bool, string> _completed, bool _breakOnDownloadFailing, Action <string> _onDebug)
        {
            List <UFileVersionRemote> downloadList;
            List <UFileVersionRemote> copyList = null;

            //> 排除缓存目录已经存在的文件 -- 待考虑功能
            //> 过滤掉缓存目录已经下载完毕的文件,根据MD5进行筛选

            if (_persistentPathRoot != _tempPathRoot)
            {
                downloadList = UFile.CompareFileVersion(_tempPathRoot, _fileList);
                copyList     = _fileList;
            }
            else
            {
                downloadList = _fileList;
            }


            ulong allBytes        = Sum(downloadList);
            ulong fluxOnSeconds   = 0;
            ulong downloadingByte = 0;
            ulong downloadAllByte = 0;
            int   fileCount       = downloadList.Count;

            if (downloadList.Count > 0)
            {
                //---------------------------------------------------
                float progressRatio = _tempPathRoot != _persistentPathRoot ? 0.9f / downloadList.Count : 1f / downloadList.Count;
                //> 这个列表中存储这下载到缓存目录的所有文件,在结尾,我们需要把这些文件拷贝到持久化目录
                List <string> tempFiles = new List <string>();
                //> 如果中途有文件没有成功下载到缓存目录则视为更新失败
                string module = "", file = "";
                for (var i = 0; i < _fileList.Count; i++)
                {
                    UFileVersionRemote info            = _fileList[i];
                    string             downloadAddress = UFile.Combine(_downloadAddress, info.RelativePath);
                    var request   = UnityWebRequest.Get(downloadAddress);
                    var asyncSend = request.SendWebRequest();
                    module = info.ModuleName;
                    file   = info.FileName;
                    while (asyncSend.isDone == false)
                    {
                        yield return(new WaitForSeconds(1));

                        fluxOnSeconds   = request.downloadedBytes - downloadingByte;
                        downloadingByte = request.downloadedBytes;
                        //> 在这里更新字节数
                        RefreshProgress(downloadAllByte + downloadingByte, allBytes, i + 1, fileCount, fluxOnSeconds, Progress, module, file);
                    }

                    if (string.IsNullOrEmpty(request.error))
                    {
                        string tempFilePath = UFile.Combine(_tempPathRoot, info.RelativePath).Replace('\\', '/');

                        int    index = tempFilePath.LastIndexOf('/');
                        string url   = tempFilePath.Remove(index);
                        if (!Directory.Exists(url))
                        {
                            Directory.CreateDirectory(url);
                        }

                        File.WriteAllBytes(tempFilePath, request.downloadHandler.data);
                        tempFiles.Add(tempFilePath);
                    }
                    else if (_breakOnDownloadFailing)
                    {
                        Error    = string.Format("Can't download the file [{0}] from address [{1}]", info.RelativePath, _downloadAddress);
                        Progress = 1f;
                        _completed?.Invoke(false, Error);
                        yield break;
                    }

                    Progress        += progressRatio;
                    downloadAllByte += (ulong)info.Size;
                    //> 在这里更新字节数
                    //RefreshProgress(downloadAllByte, allBytes, i + 2 > fileCount ? fileCount : i + 2, fileCount, fluxOnSeconds);
                    fluxOnSeconds   = 0;
                    downloadingByte = 0;
                }

                RefreshProgress(downloadAllByte, allBytes, fileCount, fileCount, 0, Progress, module, file);
            }

            if (copyList != null && copyList.Count > 0)
            {
                float  progressRatio = 0.1f / _fileList.Count;
                string module, file;
                //> 文件拷贝不计算在进度内
                foreach (var v in _fileList)
                {
                    module = v.ModuleName;
                    file   = v.FileName;
                    string tempFile       = UFile.Combine(_tempPathRoot, v.RelativePath);
                    string persistentFile = UFile.Combine(_persistentPathRoot, v.RelativePath).Replace('\\', '/');

                    int    index = persistentFile.LastIndexOf('/');
                    string url   = persistentFile.Remove(index);
                    if (!Directory.Exists(url))
                    {
                        Directory.CreateDirectory(url);
                    }

                    if (File.Exists(tempFile))
                    {
                        File.Copy(tempFile, persistentFile, true);
                        File.Delete(tempFile);
                        if (_onDebug != null)
                        {
                            _onDebug(persistentFile);
                        }
                        yield return(null);
                    }
                    else if (_breakOnDownloadFailing)
                    {
                        Debug.LogErrorFormat("更新失败:没有找到->{0}", tempFile);
                        Error    = string.Format("The file is mission->{0}", tempFile);
                        Progress = 1f;
                        _completed(false, Error);
                        yield break;
                    }
                    Progress += progressRatio;
                    //RefreshProgress(allBytes, allBytes, fileCount, fileCount, 0, Progress, v.ModuleName, v.FileName);
                }
            }



            IsSuccess = true;
            Progress  = 1f;
            //RefreshProgress(allBytes, allBytes, fileCount, fileCount, 0, 1f, "", "");
            if (_completed != null)
            {
                _completed(true, "");
            }
            yield break;
        }