Esempio n. 1
0
        private IEnumerator Execute(string url, string savePath, Action <byte[]> callback)
        {
            C_DebugHelper.Log("AsyncDownloadFile 下载开始 url = " + url + ", savePath = " + savePath);

            //System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
            //stopWatch.Start();

            using (UnityWebRequest request = UnityWebRequest.Get(url))
            {
                yield return(request.SendWebRequest());

                if (request.isHttpError || request.isNetworkError)
                {
                    C_DebugHelper.LogWarning("request.isError" + request.isNetworkError);
                }
                else
                {
                    byte[] bytes = request.downloadHandler.data;

                    if (callback != null)
                    {
                        callback(bytes);
                    }

                    C_DownloadMgr.CreatFile(url, savePath, bytes);
                }

                C_DebugHelper.Log("下载完成");

                //stopWatch.Stop();
                // C_DebugHelper.Log("下载完成,耗时: " + stopWatch.ElapsedMilliseconds);
            }
        }
Esempio n. 2
0
        public static void SyncDownloadFile(string url, string savePath)
        {
            C_DebugHelper.Log("SyncDownloadFile 下载开始 url = " + url + ", savePath = " + savePath);

            using (UnityWebRequest request = UnityWebRequest.Get(url))
            {
                request.SendWebRequest();

                while (!request.isDone)
                {
                    if (request.isHttpError || request.isNetworkError)
                    {
                        C_DebugHelper.LogWarning("request.isError" + request.isNetworkError);
                        return;
                    }
                }

                if (request.isHttpError || request.isNetworkError)
                {
                    C_DebugHelper.LogWarning("request.isError" + request.isNetworkError);
                }
                else
                {
                    C_DownloadMgr.CreatFile(url, savePath, request.downloadHandler.data);
                }
            }
        }
Esempio n. 3
0
        public static byte[] GetUrlByte(string url)
        {
            C_DebugHelper.Log("GetUrlByte url = " + url);

            byte[] bytes = null;

            using (UnityWebRequest request = UnityWebRequest.Get(url))
            {
                request.SendWebRequest();

                while (!request.isDone)
                {
                    if (request.isHttpError || request.isNetworkError)
                    {
                        C_DebugHelper.LogWarning("request.isError" + request.isNetworkError);
                        return(bytes);
                    }
                }

                if (request.isHttpError || request.isNetworkError)
                {
                    C_DebugHelper.LogWarning("request.isError" + request.isNetworkError);
                }
                else
                {
                    bytes = request.downloadHandler.data;
                }
            }

            return(bytes);
        }
Esempio n. 4
0
        IEnumerator MergeFile(string url, string savePath, Action callback)
        {
            while (true)
            {
                IsMerge = true;

                for (int i = 0; i < ThreadNum; i++)
                {
                    if (ThreadStatus[i] == false)
                    {
                        IsMerge = false;

                        yield return(0);

                        System.Threading.Thread.Sleep(100);
                        break;
                    }
                }

                if (IsMerge)
                {
                    break;
                }
            }
            byte[] bytes = new byte[bufferSize];

            FileStream fs     = new FileStream(C_DownloadMgr.StandardDownloadSavePath(url, savePath), FileMode.OpenOrCreate);
            FileStream fsTemp = null;

            for (int i = 0; i < ThreadNum; i++)
            {
                fsTemp = new FileStream(FileNames[i], FileMode.OpenOrCreate);

                int readBytes;
                while ((readBytes = fsTemp.Read(bytes, 0, bytes.Length)) > 0)
                {
                    fs.Write(bytes, 0, readBytes);
                }

                fsTemp.Close();
            }

            fs.Close();

            if (callback != null)
            {
                callback();
            }

            m_StopWatch.Stop();
            C_DebugHelper.Log("下载完成,耗时: " + m_StopWatch.ElapsedMilliseconds);

            yield return(null);

            DeleteCacheFiles();
        }
Esempio n. 5
0
        void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
        {
#if !C_Framework
            if (string.IsNullOrEmpty(_MainActiveSceneName))
            {
                SceneManager.sceneLoaded -= OnSceneLoaded;
                _LoadOver = true;
            }

            if (!string.IsNullOrEmpty(_MainActiveSceneName) && scene != null && scene.name.Equals(_MainActiveSceneName))
            {
                Utility.SetMainScene(_MainActiveSceneName);
                _LoadOver = true;
                SceneManager.sceneLoaded -= OnSceneLoaded;
            }

            C_DebugHelper.Log(" OnSceneLoaded" + scene.name);
#endif
        }
Esempio n. 6
0
        private const int m_nTimeOutWait      = 5 * 1000; //超时等待时间

        public void DownloadFile(string url, string savePath, Action callback, System.Threading.ThreadPriority threadPriority = System.Threading.ThreadPriority.Normal)
        {
            C_DebugHelper.Log("C_HttpDownloader DownloadFile url = " + url);

            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();

            Progress           = 0;
            DownloadFileLength = 0;
            m_bIsStop          = false;

            m_Thread = new Thread(delegate()
            {
                stopWatch.Start();

                //判断保存路径是否存在
                if (!Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);
                }

                string filePath = C_DownloadMgr.StandardDownloadSavePath(url, savePath);
                //string fileName = C_DownloadMgr.StandardDownloadName(url);

                //使用流操作文件
                FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);

                //获取文件现在的长度
                DownloadFileLength = fs.Length;

                //获取下载文件的总长度
                long totalLength = C_DownloadMgr.GetLength(url);
                //Debug.LogFormat("<color=red>文件:{0} 已下载{1}M,剩余{2}M</color>", fileName, fileLength / 1024 / 1024, (totalLength - fileLength) / 1024 / 1024);

                //如果没下载完
                if (DownloadFileLength < totalLength)
                {
                    //断点续传核心,设置本地文件流的起始位置
                    fs.Seek(DownloadFileLength, SeekOrigin.Begin);

                    HttpWebRequest request = C_DownloadMgr.GetWebRequest(url);

                    request.ReadWriteTimeout = m_nReadWriteTimeOut;
                    request.Timeout          = m_nTimeOutWait;

                    //断点续传核心,设置远程访问文件流的起始位置
                    request.AddRange((int)DownloadFileLength);

                    Stream stream = request.GetResponse().GetResponseStream();
                    byte[] buffer = new byte[4096];

                    //使用流读取内容到buffer中
                    //注意方法返回值代表读取的实际长度,并不是buffer有多大,stream就会读进去多少
                    int length = stream.Read(buffer, 0, buffer.Length);
                    while (length > 0)
                    {
                        //如果Unity客户端关闭,停止下载
                        if (m_bIsStop)
                        {
                            break;
                        }

                        //将内容再写入本地文件中
                        fs.Write(buffer, 0, length);

                        //计算进度
                        DownloadFileLength += length;
                        Progress            = (float)DownloadFileLength / (float)totalLength;

                        //类似尾递归
                        length = stream.Read(buffer, 0, buffer.Length);
                    }

                    stream.Close();
                    stream.Dispose();
                }
                else
                {
                    Progress = 1;
                }

                fs.Close();

                stopWatch.Stop();
                C_DebugHelper.Log("下载完成,耗时: " + stopWatch.ElapsedMilliseconds);

                //如果下载完毕,执行回调
                if (Progress == 1)
                {
                    if (callback != null)
                    {
                        callback();
                    }

                    m_Thread.Abort();
                }
            });

            //开启子线程
            m_Thread.IsBackground = true;
            m_Thread.Priority     = threadPriority;
            m_Thread.Start();
        }
Esempio n. 7
0
        void Update()
        {
            try
            {
                //强制卸载
                if (m_ForceDirtyAssetBundleRefList.Count > 0)
                {
                    for (int i = 0; i < m_ForceDirtyAssetBundleRefList.Count; i++)
                    {
                        // Debug.Log("--ab name:" + m_ForceDirtyAssetBundleRefList[i].Bundle.name + "start unload.........");
                        if (m_ForceDirtyAssetBundleRefList[i].Bundle != null)
                        {
                            m_ForceDirtyAssetBundleRefList[i].Bundle.Unload(true);
                        }
                    }

                    m_ForceDirtyAssetBundleRefList.Clear();
                }

                if (m_UnloadAssetBundleRefList.Count > 0)
                {
                    for (int i = 0; i < m_UnloadAssetBundleRefList.Count; i++)
                    {
                        if (m_UnloadAssetBundleRefList[i].Bundle != null)
                        {
                            m_UnloadAssetBundleRefList[i].Bundle.Unload(false);
                        }
                    }

                    m_UnloadAssetBundleRefList.Clear();

                    //黄志龙,后续修改到loading界面去释放
                    Resources.UnloadUnusedAssets();
                }

                for (int i = m_DirtyAssetBundleRefList.Count - 1; i >= 0; i--)
                {
                    if (m_DirtyAssetBundleRefList[i] == null || m_DirtyAssetBundleRefList[i].Bundle == null)
                    {
                        m_DirtyAssetBundleRefList.RemoveAt(i);
                    }
                    else
                    {
                        m_UnloadAssetBundleRefList.Add(m_DirtyAssetBundleRefList[i]);

                        m_DirtyAssetBundleRefList.RemoveAt(i);
                    }
                }
                if (m_NeedLoadCacheDict.Count > 0)
                {
                    List <string> keyList = new List <string>(m_NeedLoadCacheDict.Keys);

                    for (int i = m_NeedLoadCacheDict.Count - 1; i >= 0; i--)
                    {
                        List <string> valueList = m_NeedLoadCacheDict[keyList[i]];

                        if (valueList.Count > 4)
                        {
                            LoadFromFile(valueList[valueList.Count - 1], true);
                            LoadFromFile(valueList[valueList.Count - 2], true);
                            LoadFromFile(valueList[valueList.Count - 3], true);
                            LoadFromFile(valueList[valueList.Count - 4], true);
                            m_NeedLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                            m_NeedLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                            m_NeedLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                            m_NeedLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                        }
                        else if (valueList.Count > 0)
                        {
                            C_DebugHelper.Log("id:" + valueList.Count + "--ab name:" + valueList[valueList.Count - 1] + "start");

                            LoadFromFile(valueList[valueList.Count - 1], true);
                            C_DebugHelper.Log("id:" + valueList.Count + "--ab name:" + valueList[valueList.Count - 1] + "end");

                            m_NeedLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                        }
                        else
                        {
                            m_NeedLoadCacheDict.Remove(keyList[i]);
                        }
                    }
                }



                if (m_UnLoadCacheDict.Count > 0)
                {
                    List <string> keyList = new List <string>(m_UnLoadCacheDict.Keys);

                    for (int i = m_UnLoadCacheDict.Count - 1; i >= 0; i--)
                    {
                        List <string> valueList = m_UnLoadCacheDict[keyList[i]];

                        /* if (valueList.Count > 4)
                         * {
                         *  C_AssetBundleRef abr = GetAssetBundleRefList(valueList[valueList.Count - 1]);
                         *  if (abr != null)
                         *      abr.CacheUnload();
                         *
                         *  abr = GetAssetBundleRefList(valueList[valueList.Count - 2]);
                         *  if (abr != null)
                         *      abr.CacheUnload();
                         *  abr = GetAssetBundleRefList(valueList[valueList.Count - 3]);
                         *  if (abr != null)
                         *      abr.CacheUnload();
                         *  abr = GetAssetBundleRefList(valueList[valueList.Count - 4]);
                         *  if (abr != null)
                         *      abr.CacheUnload();
                         *
                         *  m_UnLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                         *  m_UnLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 2);
                         *  m_UnLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 3);
                         *  m_UnLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 4);
                         *
                         * }  else*/if (valueList.Count > 0)
                        {
                            C_AssetBundleRef abr = GetAssetBundleRefList(valueList[valueList.Count - 1]);
                            if (abr != null)
                            {
                                abr.CacheUnload();
                                C_DebugHelper.Log("id:" + valueList.Count + "--ab name:" + abr.Bundle.name);
                            }

                            m_UnLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                        }
                        else
                        {
                            m_UnLoadCacheDict.Remove(keyList[i]);
                        }
                    }
                }
            }
            catch (System.Exception e)
            {
                C_DebugHelper.LogError("NeedLoadCache error is :" + e);
            }
        }