Example #1
0
    public IEnumerable UpdateAutoCoroutine(ELEMENT_VER verBegin, ELEMENT_VER verLatest)
    {
        long packSizeOverAll = m_VersionMan.CalcSize(verBegin, verLatest);

        if (packSizeOverAll <= 0)
        {
            packSizeOverAll = 1;
        }
        long packFinishedSize = m_VersionMan.CalcSize(verBegin, m_PackFileVer.VerFrom);

        int nTotalPack   = m_VersionMan.CalcPackCount(verBegin, verLatest);
        int nCurrentPack = m_VersionMan.CalcPackCount(verBegin, m_PackFileVer.VerTo);

        GameUpdateMan.Instance.HotUpdateViewer.SetPackageNum(nCurrentPack, nTotalPack);

        UpdateInfoUtil.SetProgress(UPDATE_PROGRESS.Total, (float)((double)packFinishedSize / (double)packSizeOverAll));
        UpdateInfoUtil.SetProgress(UPDATE_PROGRESS.File, 0.0f);
        UpdateInfoUtil.SetStateString(UPDATE_STATE.UpdateStatus_BeginUpdate);

        yield return(null);

        UpdateRetCode ret    = UpdateRetCode.success;
        string        strMd5 = this.m_PackFileVer.md5;

        //要下载的路径
        this.strDownloadUPackName = HobaText.Format(
            "{0}-{1}.jup",
            this.m_PackFileVer.VerFrom.ToString(),
            this.m_PackFileVer.VerTo.ToString());
        string strDownloadUPack = this.strDownloadPath + this.strDownloadUPackName;

        //计时
        float nStartTime = Time.time;
        float nLasTime   = nStartTime;
        float nNowTime   = nStartTime;

        //准备下载
        using (DownloadMan downloadMan = new DownloadMan(this.strDownloadPath))              //DownloadMan
        {
            downloadMan.TaskEndEvent += delegate { this.IsDownloadDone = true; };

            int nTryTimes = 0;

            bool bFileEqual = false;
            while (!bFileEqual)
            {
                if (ret == UpdateRetCode.net_err || ret == UpdateRetCode.io_err || ret == UpdateRetCode.urlarg_error)
                {
                    ++nTryTimes;

                    //重连次数超过
                    if (nTryTimes > UpdateConfig.TotalReconnectTime)
                    {
                        ret = UpdateRetCode.connect_fail;
                        break;          //这次更新错误,等待选择重试
                    }

                    //重连,间隔一定时间
                    do
                    {
                        yield return(new WaitForSeconds(1.0f));

                        nNowTime = Time.time;
                    }while (nNowTime - nLasTime <= UpdateConfig.ReconnectTime);
                    nLasTime = nNowTime;

                    this.LogString(HobaText.Format("DownloadMan net_err begin retry {0} ... file: {1}", nTryTimes, this.strDownloadUPackName));
                }
                else
                {
                    nTryTimes = 0;
                }

                if (ret == UpdateRetCode.md5_not_match || ret == UpdateRetCode.download_fail)
                {
                    break;
                }

                //如果文件已存在,判断md5
                if (FileOperate.IsFileExist(strDownloadUPack))
                {
                    yield return(new WaitForSeconds(0.01f));

                    UpdateInfoUtil.SetProgress(UPDATE_PROGRESS.File, 100.0f);
                    UpdateInfoUtil.SetStateString(UPDATE_STATE.UpdateStatus_CheckingExistPack);
                    yield return(null);

                    //string md5_exist = FileOperate.CalcFileMd5(strDownloadUPack);
                    CalcMd5ThreadInfo calcMd5Info = CalcFileMd5(strDownloadUPack);
                    while (calcMd5Info.IsRunning)
                    {
                        yield return(_ShortWait);
                    }
                    OnCalcMd5Complete();
                    string md5_exist = calcMd5Info.Md5;

                    if (md5_exist == strMd5)
                    {
                        bFileEqual = true;
                        break;
                    }
                    FileOperate.DeleteFile(strDownloadUPack);       //删除旧文件
                }

                //重新开始下载
                this.IsDownloadDone = false;
                if (!FileOperate.MakeDir(strDownloadUPack))
                {
                    LogString(HobaText.Format("[UpdateAutoCoroutine] MakeDir {0} Failed!", strDownloadUPack));
                }

                //
                UpdateInfoUtil.SetProgress(UPDATE_PROGRESS.File, 0.0f);
                UpdateInfoUtil.SetDownStatusString(0.0f);
                GameUpdateMan.Instance.HotUpdateViewer.SetDownloadInfo_TextUpate(UpdateInfoUtil.GetStateString(UPDATE_STATE.UpdateString_TextUpdate));

                yield return(null);

//              foreach (var item in FetchPackByUrlCoroutine(downloadMan,
//                     this.strUpdateServerDir1, this.strUpdateServerHostName1, this.strDownloadUPackName, strMd5))
                foreach (var item in FetchPackCoroutine(downloadMan,
                                                        this.strUpdateServerDir1, this.strUpdateServerHostName1,
                                                        this.strUpdateServerDir2, this.strUpdateServerHostName2,
                                                        this.strUpdateServerDir3, this.strUpdateServerHostName3,
                                                        this.strDownloadUPackName, strMd5))
                {
                    if (item is UpdateRetCode)
                    {
                        ret = (UpdateRetCode)item;
                        break;
                    }
                    else
                    {
                        yield return(item);
                    }
                }

                if (ret != UpdateRetCode.success)
                {
                    bFileEqual = false;
                }
            }

            if (bFileEqual)
            {
                ret = UpdateRetCode.success;
            }
        }

        if (ret == UpdateRetCode.success)        //下载成功
        {
            UpdateInfoUtil.bShowWritingPack = true;

            //设置本地包路径
            strLocalPackFileName = strDownloadPath + strDownloadUPackName;
            UpdateInfoUtil.SetProgress(UPDATE_PROGRESS.File, 0.0f);

            yield return(null);

            //打开本地包,更新...
            //提示正在写包
            GameUpdateMan.Instance.HotUpdateViewer.SetInstallInfo(UpdateInfoUtil.GetStateString(UPDATE_STATE.UpdateStatus_WritingPack));
            GameUpdateMan.Instance.HotUpdateViewer.SetInstallPercent(-1.0f);

            foreach (var item in UpdateFileFromPackCoroutine(strLocalPackFileName, verBegin, verLatest))
            {
                if (item is UpdateRetCode)
                {
                    ret = (UpdateRetCode)item;
                    break;
                }

                yield return(item);
            }

            //关闭临时包
            FileOperate.DeleteFile(this.strLocalPackFileName);
            this.strLocalPackFileName = "";

            UpdateInfoUtil.bShowWritingPack = false;
        }

        if (ret == UpdateRetCode.invalid_param)
        {
            ret = UpdateRetCode.cancel;
            yield return(ret);
        }
        else if (ret == UpdateRetCode.pack_file_broken ||
                 ret == UpdateRetCode.net_err ||
                 ret == UpdateRetCode.connect_fail ||
                 ret == UpdateRetCode.md5_not_match ||
                 ret == UpdateRetCode.io_err ||
                 ret == UpdateRetCode.urlarg_error ||
                 ret == UpdateRetCode.download_fail)
        {
            yield return(ret);
        }
        else if (ret != UpdateRetCode.success)
        {
            ret = UpdateRetCode.fail;
            yield return(ret);
        }

        //写入本地版本
        UpdateInfoUtil.SetVersion(UPDATE_VERSION.Local, this.m_CurrentVersion);

        yield return(UpdateRetCode.success);
    }
Example #2
0
    public IEnumerable DoUpdateFrom7zCoroutine(string strPack, ELEMENT_VER verBegin, ELEMENT_VER verLatest)
    {
        //this.LogString(HobaText.Format("DoUpdateFrom7zCoroutine: {0}, {1}, {2}", strPack, verBegin.ToString(), verLatest.ToString()));

        UpdateRetCode code = UpdateRetCode.success;

        SevenZReader reader = new SevenZReader();

        if (!reader.Init(strPack))
        {
            code = UpdateRetCode.pack_file_broken;
            reader.Release();
            yield return(code);            //
        }

        int fileCount = reader.GetFileCount();

        if (fileCount > 0)
        {
            GameUpdateMan.Instance.HotUpdateViewer.SetInstallProgress(1, fileCount);
            yield return(null);
        }

        FileIncList fileIncList = new FileIncList();
        //找到7z包中的版本文件, inc, 得到版本
        bool bIncValid = false;

        for (int iFile = 0; iFile < fileCount; ++iFile)
        {
            string name = reader.GetFileName(iFile);

            //            LogString(HobaString.Format("7z pack file: {0}: {1}", iFile, name));
            if (name == "")
            {
                this.LogString(HobaText.Format("Fail to get file name: {0}", iFile));
                code = UpdateRetCode.pack_file_broken;
                reader.Release();
                yield return(code);        //
            }

            if (!bIncValid && name == "inc")
            {
                bIncValid = true;

                //读取
                ExtractPackThreadInfo threadInfo = ExtractFileFrom7zReader(reader, iFile);
                while (threadInfo.IsRunning)
                {
                    yield return(_ShortWait);
                }
                OnExtractFileComplete();

                int    dataSize = threadInfo.DataSize;
                IntPtr pData    = threadInfo.Pointer;
                if (pData == IntPtr.Zero)
                {
                    this.LogString(HobaText.Format("Fail to extract file name: {0}", iFile));
                    code = UpdateRetCode.pack_file_broken;
                    reader.Release();
                    yield return(code);    //
                }

                //写入
                if (!FileOperate.MakeDir(this.strUpdateIncFile))
                {
                    LogString(HobaText.Format("[UpdateAutoCoroutine] MakeDir {0} Failed!", this.strUpdateIncFile));
                }

                byte[] pText = new byte[dataSize];
                Marshal.Copy(pData, pText, 0, dataSize);
                char[] text = Encoding.UTF8.GetChars(pText);
                bool   ret  = FileOperate.WriteToTextFile(this.strUpdateIncFile, text, text.Length);

                if (!ret)
                {
                    this.LogString(HobaText.Format("Fail to write inc file name: %d", iFile));
                    code = UpdateRetCode.file_write_err;
                    reader.Release();
                    yield return(code);    //
                }

                //读取inc内容
                code = ReadIncFileList(this.strUpdateIncFile, out fileIncList, out m_currentNewVer);
                //删除本地inc文件
                FileOperate.DeleteFile(this.strUpdateIncFile);
            }

            if (bIncValid)
            {
                break;
            }
        }

        if (!bIncValid || code != UpdateRetCode.success)          //找不到inc文件
        {
            this.LogString("Pack has no list file: " + strPack);
            code = UpdateRetCode.pack_file_broken;
            reader.Release();
            yield return(code);    //
        }

        //计算进度
        long packSizeOverAll = m_VersionMan.CalcSize(verBegin, verLatest);

        if (packSizeOverAll <= 0)
        {
            packSizeOverAll = 1;
        }
        long packFinishedSize   = m_VersionMan.CalcSize(verBegin, m_CurrentVersion);
        long packNextFinishSize = m_VersionMan.CalcSize(verBegin, m_currentNewVer);

        float fFileProgress  = 0.0f;
        float fTotalProgress = (float)((packFinishedSize + (packNextFinishSize - packFinishedSize) * (double)fFileProgress) / (double)packSizeOverAll);

        UpdateInfoUtil.SetProgress(UPDATE_PROGRESS.File, fFileProgress);
        UpdateInfoUtil.SetProgress(UPDATE_PROGRESS.Total, fTotalProgress);
        //UpdateInfoUtil.SetWritingPackStatusString(fFileProgress);

        //新UI
        fileCount = reader.GetFileCount();
        if (fileCount > 0)
        {
            GameUpdateMan.Instance.HotUpdateViewer.SetInstallProgress(1, fileCount);
        }
        else
        {
            GameUpdateMan.Instance.HotUpdateViewer.SetInstallPercent(-1.0f);
        }

        yield return(null);

        //读取patch文件列表
        for (int iFile = 0; iFile < fileCount; ++iFile)
        {
            //新UI
            GameUpdateMan.Instance.HotUpdateViewer.SetInstallProgress(iFile + 1, fileCount);
            yield return(null);

            if (reader.IsDir(iFile))
            {
                continue;
            }

            string name = reader.GetFileName(iFile);

            //            LogString(HobaString.Format("7z pack file: {0}: {1}", iFile, name));
            if (string.IsNullOrEmpty(name))
            {
                this.LogString(HobaText.Format("Fail to get file name: {0}", iFile));
                code = UpdateRetCode.pack_file_broken;
                reader.Release();
                yield return(code);    //
            }

            if (name == "inc")      //skip inc
            {
                continue;
            }

            string strMd5;
            if (!fileIncList.GetFileMd5(name, out strMd5))
            {
                this.LogString(HobaText.Format("Fail to get file md5: {0} {1}", iFile, name));
                code = UpdateRetCode.pack_file_broken;
                reader.Release();
                yield return(code);        //
            }

            bool bInPack        = PackFunc.IsFileInPack(name); //是否属于指定的包
            bool bSameFileExist = false;

            if (bInPack)         //在包里的文件是否和更新文件md5一致,如果是则跳过更新
            {
                if (PackFunc.CalcPackFileMd5(name) == strMd5)
                {
                    bSameFileExist = true;
                }
            }
            if (bSameFileExist)
            {
                //if (iFile % (10 * 3) == 0)
                {
                    fFileProgress  = (float)(iFile + 1) / fileCount;
                    fTotalProgress = (float)(packFinishedSize + (packNextFinishSize - packFinishedSize) * fFileProgress) / packSizeOverAll;

                    UpdateInfoUtil.SetProgress(UPDATE_PROGRESS.File, fFileProgress);
                    UpdateInfoUtil.SetProgress(UPDATE_PROGRESS.Total, fTotalProgress);
                    //UpdateInfoUtil.SetWritingPackStatusString(fFileProgress);
                    GameUpdateMan.Instance.HotUpdateViewer.SetInstallProgress(fileCount, fileCount);

                    yield return(null);
                }
                continue;
            }

            //读取patch文件
            ExtractPackThreadInfo threadInfo = ExtractFileFrom7zReader(reader, iFile);
            while (threadInfo.IsRunning)
            {
                yield return(_ShortWait);
            }
            OnExtractFileComplete();

            int    dataSize = threadInfo.DataSize;
            IntPtr pData    = threadInfo.Pointer;

            if (pData == IntPtr.Zero)
            {
                this.LogString(HobaText.Format("Fail to extract file name: {0}", iFile));
                code = UpdateRetCode.pack_file_broken;
                reader.Release();
                yield return(code);        //
            }

            //检查md5
            //string memMd5 = FileOperate.CalcMemMd5(pData, dataSize);
            CalcMd5ThreadInfo calcMd5Info = CalcMemMd5(pData, dataSize);
            while (calcMd5Info.IsRunning)
            {
                yield return(_ShortWait);
            }
            OnCalcMd5Complete();
            string memMd5 = calcMd5Info.Md5;

            if (memMd5 != strMd5)
            {
                this.LogString(HobaText.Format("File md5 mismatch: {0} {1}", iFile, name));
                code = UpdateRetCode.pack_file_broken;
                reader.Release();
                yield return(code);        //
            }

            if (dataSize < 4)        //不是压缩文件
            {
                this.LogString(HobaText.Format("Compressed file has no header {0} {1}", iFile, name));
                code = UpdateRetCode.pack_file_broken;
                reader.Release();
                yield return(code);        //
            }

            if (bInPack) //add to package
            {
                this.WritePacking(true);
                bool retFlag = PackFunc.AddCompressedDataToPack(name, pData, dataSize);

                if (!retFlag)
                {
                    this.LogString(HobaText.Format("Update fail to add file: {0} {1}", iFile, name));
                    code = UpdateRetCode.pack_file_broken;
                    reader.Release();
                    yield return(code);        //
                }
                else
                {
                    //                  this.LogString(HobaString.Format("Success to add pack file: {0} {1}", iFile, name));
                    code = UpdateRetCode.success;
                }
            }
            else                   //解压,写文件
            {
                //使用更新目录, 将 AssetBundle/<Plaform> 和 AssetBundle/<Plaform>/Update 整合成AssetBundle
                string actualName;
                if (PackFunc.IsFileInAssetBundles(name))
                {
                    actualName = PackFunc.MakeShortAssetBundlesFileName(name);
                    //                    this.LogString(HobaString.Format("MakeShortAssetBundlesFileName: {0} {1} TO {2}", iFile, name, actualName));
                }
                else
                {
                    actualName = name;
                }

                UncompressToSepFileThreadInfo uncompressInfo = UncompressToSepFile(actualName, pData, dataSize);
                while (uncompressInfo.IsRunning)
                {
                    yield return(_ShortWait);
                }
                OnUncompressToSepFileComplete();

                bool retFlag = uncompressInfo.RetFlag;
                if (!retFlag)
                {
                    this.LogString(HobaText.Format("Update fail to uncompress file: {0} {1}", iFile, actualName));
                    code = UpdateRetCode.pack_file_broken;
                    reader.Release();
                    yield return(code);        //
                }
                else
                {
                    //                  this.LogString(HobaString.Format("Success to add sep file: {0} {1}", iFile, name));
                    code = UpdateRetCode.success;
                }
            }
        }

        reader.Release();

        PackFunc.FlushWritePack();
        if (!PackFunc.SaveAndOpenUpdatePack())
        {
            this.LogString(HobaText.Format("PackFunc.SaveAndOpenUpdatePack() Failed!!! {0}", strPack));
        }
        else
        {
            this.WritePacking(false);       //清除写包标志
        }

        yield return(code);        //
    }