示例#1
0
        IEnumerator DoDownloadFile(int index, int count, long size, EndPoint endpoint, string localPath, string remoteUrl)
        {
            EB.Debug.Log("Do Download file " + remoteUrl);

            var uri = new Uri(remoteUrl);

            FileStream stream     = null;
            var        md5        = Digest.MD5();
            string     etag       = string.Empty;
            long       downloaded = 0;

            EB.Debug.Log("downloading to  " + localPath);

            var onHeaders = (System.Action <Net.WebRequest>) delegate(Net.WebRequest r){
                if (r.statusCode != 200)
                {
                    EB.Debug.LogError("NON-200 " + r.statusCode);
                    return;
                }

                etag = r.GetResponseHeader("etag").Replace("\"", "").ToUpper();
                md5.Reset();
                downloaded = 0;

                EB.Debug.Log("etag: " + etag);

                try {
                    Directory.CreateDirectory(Path.GetDirectoryName(localPath));
                }
                catch (System.Exception ex) {
                    EB.Debug.Log("Failed to create directory " + ex.ToString());
                }

                try
                {
                    if (stream != null)
                    {
                        stream.Close();
                        stream = null;
                    }
                    stream = new FileStream(localPath, FileMode.Create, FileAccess.ReadWrite);
                }
                catch (System.Exception ex)
                {
                    EB.Debug.LogError("Failed to create file " + localPath + " " + ex);
                }
            };

            var onData = (System.Action <byte[]>) delegate(byte[] data)
            {
                try {
                    if (stream != null)
                    {
                        stream.Write(data, 0, data.Length);

                        // add to md5
                        md5.Update(data, 0, data.Length);
                        //md5.TransformBlock(data, 0, data.Length, null, 0);

                        downloaded += data.Length;
                    }
                }
                catch (System.Exception ex) {
                    EB.Debug.Log("Write Failed!! " + ex.ToString());
                    Error = "ID_SPARX_CONTENT_FAILED_EXTRACT";
                    // re-throw
                    throw ex;
                }
            };

            var request = endpoint.Get(uri.Path);

            request.dataCallback    = onData;
            request.headersCallback = onHeaders;

            var id = endpoint.Service(request, delegate(Response r){
                if (stream != null)
                {
                    stream.Flush();
                    stream.Close();
                }

                if (r.sucessful)
                {
                    // check md5
                    //md5.TransformFinalBlock(new byte[0], 0, 0);
                    //var hash = Encoding.ToHexString(md5.Hash);
                    var digest = md5.Final();
                    var hash   = Encoding.ToHexString(digest);
                    if (hash != etag)
                    {
                        EB.Debug.LogError("md5 failure " + hash + "!=" + etag);
                    }
                    else
                    {
                        return;
                    }
                }
                EB.Debug.LogError("download failed, removing file " + localPath);
                try {
                    File.Delete(localPath);
                }
                catch {}
            });

            var waiter = new WaitForFixedUpdate();
            var info   = "";

            while (string.IsNullOrEmpty(Error) == true)
            {
                var progress = (int)(100 * (float)downloaded / (float)size);
                info = Localizer.Format("ID_SPARX_CONTENT_DOWNLOAD_PROGRESS", (index + 1), count, progress);
                Notify(info);
                yield return(waiter);

                if (endpoint.IsDone(id))
                {
                    break;
                }
            }

            yield break;
        }