public DownLoadTask(string ServerPath, string localPath, string ResName, DownLoadProgress func)
 {
     this.m_ServerPath = ServerPath;
     this.m_localPath  = localPath;
     this.m_ResName    = ResName;
     this.m_DownID     = DownLoadQueue.DownID;
     this.m_funPress   = func;
 }
 public DownLoadTask()
 {
     this.m_ServerPath = string.Empty;
     this.m_localPath  = string.Empty;
     this.m_ResName    = string.Empty;
     this.m_DownID     = DownLoadQueue.DownID;
     this.m_funPress   = null;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// 初始化
 /// </summary>
 public void Init()
 {
     if (progress == null)
     {
         progress = new DownLoadProgress();
     }
     if (readBuffer == null)
     {
         readBuffer = new byte[DEFAULT_BUFFER_SIZE];
     }
 }
Ejemplo n.º 4
0
    /// <summary>
    /// 初始化数据
    /// </summary>
    public virtual void Init()
    {
        this.beginDownload    = null;
        this.downloadProgress = null;
        this.downloadFinish   = null;
        this.downloadText     = null;
        this.downloadData     = null;
        this.downloadError    = null;

        this.isFinish = false;
        this.url      = string.Empty;
    }
    protected void OnAssetBundleProgress(DownLoadProgress downLoadProgress)
    {
        mFileCountText.text = "File Count : " + downLoadProgress.iFileIndex + "/" + downLoadProgress.iTotalFileCount;

        if (downLoadProgress.bCompressing)
        {
            mFileSizeText.text = "Compressing...";
        }
        else
        {
            mFileSizeText.text = "File Size : " + downLoadProgress.fDownLoadBytes + "/" + downLoadProgress.fTotalBytes + " ==> " + downLoadProgress.fProgress;
        }
    }
    public static void GetStringRequest(this WebRequest wwwUtil, string url, DownLoadProgress progress, DownLoadText text, DownLoadError error = null)
    {
        GetWWWItem item = WWWItemPool.GetWWWItem <GetWWWItem>();

        if (item == null)
        {
            item = new GetWWWItem();
            WWWItemPool.AddWWWItem(item);
        }

        item.SetValue(url);
        item.downloadProgress = progress;
        item.downloadText     = text;
        item.downloadError    = error;

        wwwUtil.AddTask(item);
    }
Ejemplo n.º 7
0
 private void OnProcess(string name, DownLoadProgress progress)
 {
     mProgress.Size = progress.Size;
     //Debug.LogFormat("OnProcess===>Success:{0}  {1}",name, progress.SizeKB);
 }
    public static void WWWFormWWWRequest(this WebRequest wwwUtil, string url, WWWForm form, BeginDownLoad begin, DownLoadProgress progress, DownLoadFinish finish, DownLoadError error = null)
    {
        FormWWWItem item = WWWItemPool.GetWWWItem <FormWWWItem>();

        if (item == null)
        {
            item = new FormWWWItem();
            WWWItemPool.AddWWWItem(item);
        }

        item.SetValue(url, form);
        item.beginDownload    = begin;
        item.downloadProgress = progress;
        item.downloadFinish   = finish;
        item.downloadError    = error;

        wwwUtil.AddTask(item);
    }
    public static void WWWFormByteRequest(this WebRequest wwwUtil, string url, WWWForm form, DownLoadProgress progress, DownLoadData data, DownLoadError error = null)
    {
        FormWWWItem item = WWWItemPool.GetWWWItem <FormWWWItem>();

        if (item == null)
        {
            item = new FormWWWItem();
            WWWItemPool.AddWWWItem(item);
        }

        item.SetValue(url, form);
        item.downloadProgress = progress;
        item.downloadData     = data;
        item.downloadError    = error;

        wwwUtil.AddTask(item);
    }
    public static void PostWWWRequest(this WebRequest wwwUtil, string url, byte[] postData, BeginDownLoad begin, DownLoadProgress progress, DownLoadFinish finish, DownLoadError error = null)
    {
        PostWWWItem item = WWWItemPool.GetWWWItem <PostWWWItem>();

        if (item == null)
        {
            item = new PostWWWItem();
            WWWItemPool.AddWWWItem(item);
        }

        item.SetValue(url, postData);
        item.beginDownload    = begin;
        item.downloadProgress = progress;
        item.downloadFinish   = finish;
        item.downloadError    = error;

        wwwUtil.AddTask(item);
    }
    public static void PostByteRequest(this WebRequest wwwUtil, string url, byte[] postData, DownLoadProgress progress, DownLoadData data, DownLoadError error = null)
    {
        PostWWWItem item = WWWItemPool.GetWWWItem <PostWWWItem>();

        if (item == null)
        {
            item = new PostWWWItem();
            WWWItemPool.AddWWWItem(item);
        }

        item.SetValue(url, postData);
        item.downloadProgress = progress;
        item.downloadData     = data;
        item.downloadError    = error;

        wwwUtil.AddTask(item);
    }
    public static void GetByteRequest(this WebRequest wwwUtil, string url, BeginDownLoad begin, DownLoadProgress progress, DownLoadData data, DownLoadError error = null)
    {
        GetWWWItem item = WWWItemPool.GetWWWItem <GetWWWItem>();

        if (item == null)
        {
            item = new GetWWWItem();
            WWWItemPool.AddWWWItem(item);
        }

        item.SetValue(url);
        item.beginDownload    = begin;
        item.downloadProgress = progress;
        item.downloadData     = data;
        item.downloadError    = error;

        wwwUtil.AddTask(item);
    }
    public static void AddDownTaskImmediate(string serverPath, string localPath, string name, DownLoadFinishNotify onFinish, DownLoadProgress onProgress = null)
    {
        SetFinishNotify(onFinish);

        DownLoadTask task = new DownLoadTask(serverPath, localPath, name, onProgress);

        AddDownTask(task);

        StartDown();
    }
    /// <summary>
    /// 初始化资源管理器.
    /// </summary>
    /// <param name="strUrlDirectory">下载地址.</param>
    /// <param name="initCb">结束下载.</param>
    /// <param name="progressCb">进度.</param>
    public static void InitlizeAssetBundle(string strPreUrlDirectory, string strRealUrlDirectory, AssetBundleInitFinish initCb, AssetBundleProgress progressCb)
    {
        if (s_bInitlized)
        {
            DownLoadProgress downLoadProgress = new DownLoadProgress();
            downLoadProgress.bDownload       = false;
            downLoadProgress.bDownloadFinsih = true;
            progressCb(downLoadProgress);
            //progressCb(100, false);
            initCb(true, "");
            return;
        }

        s_strPreHttpDirectory  = strPreUrlDirectory;
        s_strRealHttpDirectory = strRealUrlDirectory;

                #if UNITY_STANDALONE_WIN
        s_strPreHttpDirectory  += "/StandaloneWindows/";
        s_strRealHttpDirectory += "/StandaloneWindows/";
        s_strStreamAssetDir    += "/StandaloneWindows/";
                #elif UNITY_ANDROID
        s_strPreHttpDirectory  += "/Android/";
        s_strRealHttpDirectory += "/Android/";
        s_strStreamAssetDir    += "/Android/";
                #elif UNITY_IPHONE
        s_strPreHttpDirectory  += "/iPhone/";
        s_strRealHttpDirectory += "/iPhone/";
        s_strStreamAssetDir     = "file://" + Application.dataPath + "/Raw/iPhone/";
                #elif UNITY_WP8
        s_strPreHttpDirectory  += "/WP8Player/";
        s_strRealHttpDirectory += "/WP8Player/";
        s_strStreamAssetDir    += "/WP8Player/";
                #elif UNITY_METRO
        s_strPreHttpDirectory  += "/MetroPlayer";
        s_strRealHttpDirectory += "/MetroPlayer";
        s_strStreamAssetDir    += "/MetroPlayer";
                #endif

                #if UNITY_EDITOR
        s_strStreamAssetDir = "file:///" + s_strStreamAssetDir;
        s_strPersistUrlPath = "file:///" + Application.dataPath;
        s_strPersistPath    = Application.dataPath;
                #endif

                #if UNITY_EDITOR
        // 标记已初始化;
        s_bInitlized = true;

        // bin文件加载;
        AddDirBinaryFilesToAsset("Assets/Artwork/Download/Bytes/", "bytes", false);
        AddDirBinaryFilesToAsset("Assets/Artwork/Download/Bytes/", "txt", false);

        // Texture文件加载;
        AddDirBinaryFilesToAsset("Assets/Artwork/Download/Texture/", "png", false);
        AddDirBinaryFilesToAsset("Assets/Artwork/Download/Texture/", "jpg", false);

        // Prefab文件加载;
        AddDirBinaryFilesToAsset("Assets/Artwork/Download/Prefab/", "prefab", false);

        // 场景文件加载;
        AddDirBinaryFilesToAsset("Assets/Artwork/Download/Scene/", "unity", false);

        // 声音文件加载;
        AddDirBinaryFilesToAsset("Assets/Artwork/Download/Sound/", "mp3", false);
        AddDirBinaryFilesToAsset("Assets/Artwork/Download/Sound/", "ogg", false);

        DownLoadProgress downLoadProgresst = new DownLoadProgress();
        downLoadProgresst.bDownload       = false;
        downLoadProgresst.bDownloadFinsih = true;
        progressCb(downLoadProgresst);
        // progressCb(100, false);
        initCb(true, "");
                #else
        //Debug.Log ("StreamAsset Path:" + s_strStreamAssetDir);
        //Debug.Log ("PersistPath:" + Application.persistentDataPath);

        Coroutiner.StartCoroutine(_initlizeBaseContext(initCb, progressCb));
                #endif
    }
    /// <summary>
    /// Real 下载.
    /// </summary>
    /// <returns>下载协程.</returns>
    /// <param name="initCb">下载结束回调函数.</param>
    /// <param name="progressCb">下载进度回调函数.</param>
    private static IEnumerator _initlizeDependData(AssetBundleInitFinish initCb, AssetBundleProgress progressCb)
    {
        s_asset2BundleMap        = new Dictionary <string, string>();
        s_assetBundleAsstList    = new Dictionary <string, List <string> >();
        s_assetBundleDependency  = new Dictionary <string, string>();
        s_assetBundleList        = new HashSet <string> ();
        s_assetBundleListNew     = new Dictionary <string, string>();
        s_assetBundleListOld     = new Dictionary <string, string>();
        s_assetBundleSizeListNew = new Dictionary <string, string>();
        s_assetBundleSizeListOld = new Dictionary <string, string>();
        s_levelList         = new HashSet <string> ();
        s_preloadBundleList = new HashSet <string>();

        byte[] szResultObjBytes = null;

        double dSecond          = (System.DateTime.Now - new System.DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
        string strRemoteXmlFile = s_strPreHttpDirectory + "/" + s_StrAssetBundleDescFile + "?time=" + dSecond;
        string strDependXmlName = Path.GetFileNameWithoutExtension(s_StrAssetBundleDependFile);
        string strDependXmlFile = s_strPreHttpDirectory + "/" + s_StrAssetBundleDependFile + "?time=" + dSecond;

        //Debug.Log("strRemoteXmlFile : " + strRemoteXmlFile);
        //Debug.Log("strDependXmlFile : " + strDependXmlFile);

        // 缓存映射文件;
        CacheXml(s_strPersistPath + "/" + strDependXmlName + ".xml", true);

        //		foreach(var temp in s_assetBundleListOld)
        //		{
        //			Debug.Log(temp.Key + "|" + temp.Value);
        //		}

        using (WWW dependXmlDownload = new WWW(strDependXmlFile))
        {
            yield return(dependXmlDownload);

            if (dependXmlDownload.error != null && dependXmlDownload.error.Length > 0)
            {
                Debug.LogError("AssetBundleDepend Init Fail:" + dependXmlDownload.error);

                DownLoadProgress downLoadProgressError = new DownLoadProgress();
                downLoadProgressError.bDownload       = false;
                downLoadProgressError.bDownloadFinsih = true;

                initCb(false, "Get Resource Depend Object Fail:" + dependXmlDownload.error);
                yield break;
            }
            else
            {
                //Debug.Log("Load Xml Success|" + dependXmlDownload.bytes.Length);
                Ionic.Zlib.ZlibStream.DeCompressBuffToFile(dependXmlDownload.bytes, s_strPersistPath + "/" + strDependXmlName + ".xml");

                CacheXml(s_strPersistPath + "/" + strDependXmlName + ".xml", false);

                //				foreach(var temp in s_assetBundleListNew)
                //				{
                //					Debug.Log(temp.Key + "|" + temp.Value);
                //				}
            }
        }

        using (WWW xmlDownload = new WWW(strRemoteXmlFile))
        {
            yield return(xmlDownload);

            if (xmlDownload.error != null && xmlDownload.error.Length > 0)
            {
                Debug.LogError("AssetBundle Init Fail:" + xmlDownload.error);

                DownLoadProgress downLoadProgressError = new DownLoadProgress();
                downLoadProgressError.bDownload       = false;
                downLoadProgressError.bDownloadFinsih = true;

                progressCb(downLoadProgressError);                 /*出错了 这里显示进度为解压中*/
                initCb(false, "Get Resource Depend Object Fail:" + xmlDownload.error);
                yield break;
            }
            else
            {
                //Debug.Log("Load Xml Success|" + xmlDownload.bytes.Length);
                szResultObjBytes = xmlDownload.bytes;
            }

            byte[]              decompressResult    = Ionic.Zlib.ZlibStream.UncompressBuffer(szResultObjBytes);
            AssetBundle         depBundle           = AssetBundle.LoadFromMemory(decompressResult);
            string              strDepName          = Path.GetFileNameWithoutExtension(s_StrAssetBundleDescFile);
            AssetBundleManifest assetBundleManifest = depBundle.LoadAsset <AssetBundleManifest>(strDepName);
            string[]            allAssetBundles     = assetBundleManifest.GetAllAssetBundles();

            for (int i = 0; i < allAssetBundles.Length; i++)
            {
                if (s_assetBundleListNew.ContainsKey(allAssetBundles[i]))
                {
                    string realFileName = null;
                    s_assetBundleListNew.TryGetValue(allAssetBundles[i], out realFileName);
                    s_assetBundleList.Add(realFileName);

                    //Debug.Log("allAssetBundles[i] : " + allAssetBundles[i] + " realFileName : " + realFileName);
                }
            }

            depBundle.Unload(true);

            // 清理目录下不需要的文件(变动的文件需要清理掉重新下载);
            HashSet <string> delFileList = new HashSet <string>();
            DirectoryInfo    dirInfo     = new DirectoryInfo(s_strPersistPath);
            foreach (var fileInfo in dirInfo.GetFiles("*.bytes", System.IO.SearchOption.AllDirectories))
            {
                string strFileNameNoExt = System.IO.Path.GetFileNameWithoutExtension(fileInfo.FullName);
                if ((fileInfo.Length == 0) || !s_assetBundleList.Contains(strFileNameNoExt))
                {
                    delFileList.Add(fileInfo.FullName);
                }
            }

            foreach (var item in delFileList)
            {
                System.IO.File.Delete(item);
                //Debug.Log("Delete File|" + item);
            }

            int iDownloadCount = allAssetBundles.Length;
            int iProgress      = 0;
            int iLocalCount    = 0;
            for (int i = 0; i < iDownloadCount; i++)
            {
                // 获取文件真正的名字;
                string realName = null;
                if (s_assetBundleListNew.ContainsKey(allAssetBundles[i]))
                {
                    s_assetBundleListNew.TryGetValue(allAssetBundles[i], out realName);
                }

                string realSize = null;
                if (s_assetBundleSizeListNew.ContainsKey(allAssetBundles[i]))
                {
                    s_assetBundleSizeListNew.TryGetValue(allAssetBundles[i], out realSize);
                }

                if (realName == null || (realName != null && realName.Length <= 0))
                {
                    Debug.LogError("ERROR !!!!");
                }

                // 检查本地是否存在以下文件;
                string strPersistPath = s_strPersistPath + "/" + realName + s_strAssetBundleExt;
                if (System.IO.File.Exists(strPersistPath))
                {
                    iLocalCount++;

                    // 添加映射文件;
                    AddAsset2BundleMap(strPersistPath, realName);

                    continue;
                }

                // 检查程序包中是否存在文件;
                string strLocalPath = System.IO.Path.Combine(s_strStreamAssetDir, realName + s_strAssetBundleExt);
                using (WWW www = new WWW(strLocalPath))
                {
                    yield return(www);

                    if (www.error != null && www.error.Length > 0)
                    {
                        //iLocalCount = 0;
                        string strAssetBundleFile = s_strRealHttpDirectory + "/" + realName + s_strAssetBundleExt;

                        // 按照策划要求,先暴力处理,不停的尝试;
                        while (true)
                        {
                            using (WWW download = new WWW(strAssetBundleFile))
                            {
                                DownLoadProgress downLoadProgress = new DownLoadProgress();
                                downLoadProgress.bDownload       = true;
                                downLoadProgress.iFileIndex      = iLocalCount;
                                downLoadProgress.iTotalFileCount = iDownloadCount;

                                while (!download.isDone)
                                {
                                    downLoadProgress.fDownLoadBytes  = (long)(long.Parse(realSize) * download.progress);
                                    downLoadProgress.fTotalBytes     = long.Parse(realSize);
                                    downLoadProgress.fProgress       = (decimal)download.progress * 100;
                                    downLoadProgress.bDownloadFinsih = false;
                                    downLoadProgress.bCompressing    = false;
                                    downLoadProgress.bCompressFinish = false;

                                    progressCb(downLoadProgress);

                                    yield return(1);
                                }

                                downLoadProgress.fDownLoadBytes  = long.Parse(realSize);
                                downLoadProgress.fTotalBytes     = long.Parse(realSize);
                                downLoadProgress.fProgress       = 100;
                                downLoadProgress.bDownloadFinsih = true;
                                downLoadProgress.bCompressing    = true;
                                downLoadProgress.bCompressFinish = false;

                                progressCb(downLoadProgress);

                                if (download.error != null && download.error.Length > 0)
                                {
                                    yield return(new WaitForSeconds(0.1f));

                                    continue;
                                }

                                Debug.Log("Download: " + s_strPersistPath + "/" + realName + s_strAssetBundleExt);
                                Debug.Log("Size: " + download.bytes.Length);
                                Ionic.Zlib.ZlibStream.DeCompressBuffToFile(download.bytes, s_strPersistPath + "/" + realName + s_strAssetBundleExt);

                                // 添加映射文件;
                                AddAsset2BundleMap(strPersistPath, realName);

                                // 解压完;
                                DownLoadProgress downLoadCompress = new DownLoadProgress();
                                downLoadCompress.bDownload       = true;
                                downLoadCompress.iFileIndex      = iLocalCount;
                                downLoadCompress.iTotalFileCount = iDownloadCount;
                                downLoadCompress.fDownLoadBytes  = downLoadProgress.fDownLoadBytes;
                                downLoadCompress.fTotalBytes     = downLoadProgress.fTotalBytes;
                                downLoadCompress.fProgress       = 100;
                                downLoadCompress.bDownloadFinsih = true;
                                downLoadCompress.bCompressing    = true;
                                downLoadCompress.bCompressFinish = true;
                                progressCb(downLoadCompress);
                            }
                            break;
                        }
                    }
                    else
                    {
                        Debug.Log("DeCompressBuffToFile !!!!");

                        // 解压完;
                        DownLoadProgress downLoadCompress = new DownLoadProgress();
                        downLoadCompress.bDownload       = true;
                        downLoadCompress.bDownloadFinsih = true;
                        downLoadCompress.fProgress       = (decimal)(iLocalCount / (float)iDownloadCount) * 100;
                        downLoadCompress.bCompressing    = true;
                        downLoadCompress.bCompressFinish = false;
                        progressCb(downLoadCompress);

                        Debug.Log("iLocalCount:" + iLocalCount + "/" + iDownloadCount + " Export:" + s_strPersistPath + "/" + realName + s_strAssetBundleExt);
                        Debug.Log("size:" + www.bytes.Length);
                        Ionic.Zlib.ZlibStream.DeCompressBuffToFile(www.bytes, s_strPersistPath + "/" + realName + s_strAssetBundleExt);

                        Debug.Log("解压成功");

                        iLocalCount++;

                        // 添加映射文件;
                        AddAsset2BundleMap(strPersistPath, realName);

                        if (iLocalCount == iDownloadCount)
                        {
                            downLoadCompress.fProgress       = 100;
                            downLoadCompress.bCompressing    = true;
                            downLoadCompress.bCompressFinish = true;
                            progressCb(downLoadCompress);
                        }

                        continue;
                    }
                }

                iLocalCount++;
            }
        }

        DownLoadProgress downLoadProgressLocal = new DownLoadProgress();

        downLoadProgressLocal.bDownload       = false;
        downLoadProgressLocal.bDownloadFinsih = true;
        progressCb(downLoadProgressLocal);

        initCb(true, "");
    }