Example #1
0
    public static DownloadTaskErrorCode FetchByUrl(string url, string destFile, int timeout, out string errMsg)
    {
        if (!FileOperate.MakeDir(destFile))
        {
            Common.HobaDebuger.LogWarning(HobaText.Format("[FetchByUrl] MakeDir {0} Failed!", destFile));
        }

        if (FileOperate.IsFileExist(destFile))
        {
            FileOperate.DeleteFile(destFile);
        }

        string hostName = UpdateUtility.GetHostName(url);

        var code = UpdateUtility.GetByUrl(
            url,
            hostName,
            destFile,
            timeout,          //10s
            null,
            null,
            out errMsg);

        return(code);
    }
Example #2
0
    //从服务器下载 version.txt 到 strGameNewVerFile
    public DownloadTaskErrorCode FetchServerVersionFile(string url, string hostName, string savedFile, int timeout, out string errMsg)
    {
        if (!FileOperate.MakeDir(savedFile))
        {
            LogString(HobaText.Format("[FetchServerVersionFile] MakeDir {0} Failed!", savedFile));
        }

        if (FileOperate.IsFileExist(savedFile))
        {
            FileOperate.DeleteFile(savedFile);
        }

        //测试
        //         int filesize2 = (int)SeasideResearch.LibCurlNet.External.CURL_GetUrlFileSize(url, timeout);
        //         LogString(HobaString.Format("c++ url FileSize: {0}, fileSize: {1}", url, filesize2));
        //
        //         int filesize = (int)UpdateUtility.GetUrlFileSizeEx(url, hostName, timeout);
        //         LogString(HobaString.Format("url FileSize: {0}, fileSize: {1}", url, filesize));

        var code = UpdateUtility.GetByUrl(
            url,
            hostName,
            savedFile,
            timeout,          //10s
            null,
            null,
            out errMsg);

        return(code);
    }
Example #3
0
    //如果本地文件不存在,设置为初始版本, 如果当前版本 < 初始版本,则写新版本, ret是否新写入baseVersion
    public bool SetFirstVersion(ELEMENT_VER ver, bool bForceWrite)
    {
        m_baseVer = ver;

        if (bForceWrite || !FileOperate.IsFileExist(strGameOldVerFile))
        {
            if (!FileOperate.MakeDir(strGameOldVerFile))
            {
                LogString(HobaText.Format("[SetFirstVersion] MakeDir {0} Failed!", strGameOldVerFile));
            }
            UpdateRetCode ret = SetLocalVersion(ver);
            if (ret != UpdateRetCode.success)
            {
                LogString(HobaText.Format("[SetFirstVersion] SetLocalVersion {0} Failed!", strGameOldVerFile));
            }
            else
            {
                return(true);
            }
        }
        else
        {
            ELEMENT_VER localVersion;
            if (GetLocalVersion(out localVersion) && localVersion < m_baseVer)
            {
                LogString(HobaText.Format("[SetFirstVersion] Local Version File Exist {0}! Write New Version From: {1} To {2}", strGameOldVerFile, localVersion.ToString(), ver.ToString()));

                if (!FileOperate.MakeDir(strGameOldVerFile))
                {
                    LogString(HobaText.Format("[SetFirstVersion] MakeDir {0} Failed2!", strGameOldVerFile));
                }
                UpdateRetCode ret = SetLocalVersion(ver);
                if (ret != UpdateRetCode.success)
                {
                    LogString(HobaText.Format("[SetFirstVersion] SetLocalVersion {0} Failed2!", strGameOldVerFile));
                }
                else
                {
                    return(true);
                }
            }
            else
            {
                LogString(HobaText.Format("[SetFirstVersion] Local Version File Exist {0}!", strGameOldVerFile));
            }
        }

        return(false);
    }
Example #4
0
    private bool CreateFile(string filename)
    {
        string strLogPath = filename;

        if (FileOperate.IsFileExist(strLogPath))
        {
            FileOperate.DeleteFile(strLogPath);
        }
        try
        {
            FileOperate.MakeDir(strLogPath);

            logWriter = File.CreateText(strLogPath);
            LogWriteLine("StatisticsInfo Created!");
        }
        catch (Exception)
        {
            logWriter = null;
        }
        return(logWriter != null);
    }
Example #5
0
    public void WritePacking(bool bWrite)
    {
        string strFile = strTempWritePackFileName;

        if (bWrite && UpdateInfo.bWrittingPack == false)
        {
            char[] buffer = new char[1] {
                '0'
            };
            FileOperate.WriteToTextFile(strFile, buffer, 1);
        }
        else
        {
            if (FileOperate.IsFileExist(strFile))
            {
                FileOperate.DeleteFile(strFile);
            }
        }

        UpdateInfo.bWrittingPack = bWrite;
    }
Example #6
0
    public bool CreateLog(string filename)
    {
        string strLogPath = filename;

        if (FileOperate.IsFileExist(strLogPath))
        {
            FileOperate.DeleteFile(strLogPath);
        }
        try
        {
            FileOperate.MakeDir(strLogPath);

            logWriter = File.CreateText(strLogPath);
            LogWriteLine(string.Format("UpdateLog Created! {0:G}", System.DateTime.Now));
        }
        catch (Exception)
        {
            logWriter = null;
        }
        return(logWriter != null);
    }
Example #7
0
    public bool IsWritingPackFileExist()
    {
        string strFile = strTempWritePackFileName;

        return(FileOperate.IsFileExist(strFile));
    }
Example #8
0
    private IEnumerator DownloadPictureFromUrl(string strFileName, string url, int timeout, DownloadPictureCallback callback)
    {
        yield return(new WaitForEndOfFrame());

        string strFile     = Path.Combine(EntryPoint.Instance.CustomPicDir, strFileName);
        string contentType = Patcher.GetUrlContentType(url, timeout);
        int    retCode     = 0;

        if (contentType.StartsWith("text/"))
        {
            string tmpPath = Path.Combine(EntryPoint.Instance.TmpPath, "tmp.txt");
            string errMsg;
            var    code = Patcher.FetchByUrl(url, tmpPath, timeout, out errMsg);
            if (code == Downloader.DownloadTaskErrorCode.Success)
            {
                try
                {
                    var    bytes = File.ReadAllBytes(tmpPath);
                    var    chars = System.Text.Encoding.UTF8.GetChars(bytes);
                    string str   = new string(chars, 0, chars.Length);
                    retCode = int.Parse(str);
                }
                catch (Exception)
                {
                    retCode = -1;
                }
            }
            else
            {
                retCode = -1;
            }

            callback(strFileName, retCode, null);
        }
        else //下载图片
        {
            UnityWebRequest request            = UnityWebRequest.Get(url);
            string          authorizationValue = GameCustomConfigParams.Authorization;
            request.SetRequestHeader("Authorization", authorizationValue);
            yield return(request.Send());

            int resCode = -1;
            if (request.responseCode == 200)
            {
                var dic = JsonUtility.FromJson <URLResult>(request.downloadHandler.text);
                url = dic.url;

                request = UnityWebRequest.Get(url);
                yield return(request.Send());

                if (request.responseCode == 200)
                {
                    if (!FileOperate.MakeDir(strFile))
                    {
                        Common.HobaDebuger.LogWarning(HobaText.Format("[FetchByUrl] MakeDir {0} Failed!", strFileName));
                    }

                    if (FileOperate.IsFileExist(strFile))
                    {
                        FileOperate.DeleteFile(strFile);
                    }

                    var streamFile = new FileStream(strFile, FileMode.OpenOrCreate);
                    streamFile.Write(request.downloadHandler.data, 0, (int)request.downloadedBytes);
                    streamFile.Close();

                    //string errMsg;
                    //var code = Patcher.FetchByUrl(url, strFile, timeout, out errMsg);
                    if (dic.resCode == (int)Downloader.DownloadTaskErrorCode.Success)
                    {
                        callback(strFileName, retCode, null);
                    }
                    else
                    {
                        callback(strFileName, retCode, HobaText.Format("{0}, {1}", (int)dic.resCode, url));
                    }
                }
                else
                {
                    callback(strFileName, retCode, HobaText.Format("{0}, {1}", (int)resCode, url));
                }
            }
            else
            {
                callback(strFileName, retCode, HobaText.Format("{0}, {1}", (int)resCode, url));
            }
        }
    }
Example #9
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);
    }