Exemplo n.º 1
0
    public delegate void DownloadPictureCallback(string strFileName, int retCode, string err);      //retCode: 下载图片的自定义错误码, err: 下载错误信息

    public bool DownloadPicture(string strFile, int timeout, DownloadPictureCallback callback)
    {
        string url      = _ServerConfigParams.GetCustomPicDownloadUrl();
        string fullPath = Path.Combine(EntryPoint.Instance.CustomPicDir, strFile);
        string md5      = Util.md5file(fullPath);
        string realUrl  = HobaText.Format(url, strFile, md5);

        StartCoroutine(DownloadPictureFromUrl(strFile, realUrl, timeout, callback));

        return(true);
    }
Exemplo n.º 2
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));
            }
        }
    }