Ejemplo n.º 1
0
        private void DownLoadFile()
        {
            if (mSynchronizeData.needDownloadSet.Count == 0)
            {
                mNextHandler.Invoke(FileDownloadStateId.WritePersistentVersionFile);
                return;
            }
            ShowTipInfo();
            FileDetailInfo downloadFileInfo = mSynchronizeData.needDownloadSet [mSynchronizeData.needDownloadSet.Count - 1];

            mSynchronizeData.needDownloadSet.RemoveAt(mSynchronizeData.needDownloadSet.Count - 1);

            string fileRelativePath = null;

            if (string.IsNullOrEmpty(downloadFileInfo.filePath))
            {
                //服务器中是以MD5命名的。
                fileRelativePath = downloadFileInfo.fileMd5;
            }
            else
            {
                fileRelativePath = Path.Combine(downloadFileInfo.filePath, downloadFileInfo.fileMd5);
            }
            string fileUrl = Path.Combine(mSynchronizeData.ServerDataPath, fileRelativePath);

            mDownloadId = GameMain.Instance.HttpMgr.DownLoad(fileUrl, delegate(byte[] fileBytes) {
                Logger.LogInfo("NewDownload download file success,name:" + downloadFileInfo.fileName + " url:" + fileUrl);
                if (downloadFileInfo.fileLength != fileBytes.Length)
                {
                    mErrorHandler.Invoke(FileErrorCode.FileLengthError, fileUrl + " serverL:" + downloadFileInfo.fileLength + "  realL:" + fileBytes.Length);
                }
                else
                {
                    string md5 = Utils.MD5(fileBytes);
                    if (md5 != downloadFileInfo.fileMd5)
                    {
                        mErrorHandler.Invoke(FileErrorCode.FileMd5Error, fileUrl + " serverMd5:" + downloadFileInfo.fileMd5 + "  realMd5:" + md5);
                    }
                    else
                    {
                        string errorInfo;
                        FileErrorCode errorCode = FileOperateUtils.TryFileWrite(delegate(){
                            string relativePath = FileSystemUtils.GetFileRelativePath(downloadFileInfo.fileName, downloadFileInfo.filePath, true);
                            GameMain.Instance.FileOperateMgr.CreateDirIfNotExist(Path.GetDirectoryName(relativePath));
                            GameMain.Instance.FileOperateMgr.WriteBinaryFile(relativePath, fileBytes);
                        }, out errorInfo);
                        if (FileErrorCode.Null != errorCode)
                        {
                            mErrorHandler.Invoke(errorCode, errorInfo);
                        }
                        else
                        {
                            mSynchronizeData.persistentFileListDic.Add(downloadFileInfo.fileName, downloadFileInfo);
                            errorCode = FileOperateUtils.TryFileWrite(delegate(){
                                string detailStr    = FileListUtils.DetailInfoToString(downloadFileInfo);
                                string relativePath = FileSystemUtils.GetFileRelativePath(FileDownloadData.FileListFileName, string.Empty);
                                GameMain.Instance.FileOperateMgr.WriteTextFile(relativePath, detailStr, true);
                            }, out errorInfo);
                            if (FileErrorCode.Null != errorCode)
                            {
                                mErrorHandler.Invoke(errorCode, errorInfo);
                            }
                            else
                            {
                                mFinishFilesSize += (ulong)downloadFileInfo.fileLength;
                                mCurFileSize      = 0;
                                DownLoadFile();
                            }
                        }
                    }
                }
            }, delegate(string errorCode) {
                Logger.LogError("NewDownload download file failed,name:" + downloadFileInfo.fileName + " url:" + fileUrl);
                mErrorHandler.Invoke(FileErrorCode.DownloadFileError, fileUrl);
            }, delegate(ulong curDownloadedBytes) {
                mCurFileSize = curDownloadedBytes;
                ShowTipInfo();
            });
        }