public void StartDownLoad(IDownLoadParam downLoadParam)
    {
        //Debug.Log("开始下载:" + downLoadParam.GetURL());
        try
        {
            lock (LOCK_OBJ)
            {
                _http_request = (HttpWebRequest)HttpWebRequest.Create(downLoadParam.GetURL());
                _http_request.AddRange(_down_info.DownLoadSize);
                DownLoadCommon.AddHttpHeader(
                    _http_request,
                    downLoadParam.GetDownLoadType(),
                    downLoadParam.GetVersion());

                IAsyncResult ar = _http_request.BeginGetResponse(ResponseCallback, _http_request);
                RegisterTimeOut(ar.AsyncWaitHandle);  // 检测是否超时
            }
        }
        catch (Exception e)
        {
            Debug.Log(e.Message);
            UnregisterTimeOut();
            DownLoadError(eDownErrorCode.NoResponse);
        }
    }
Example #2
0
    /// <summary>
    ///   拷贝初始目录所有文件
    /// </summary>
    IEnumerator CopyAllInitialFiles()
    {
        //拷贝所有配置文件
        yield return(DownLoadCommon.StartCopyInitialFile(DownLoadCommon.MAIN_MANIFEST_FILE_NAME));

        string mainifestFullName    = DownLoadCommon.GetFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME);
        AssetBundleManifest initial = DownLoadCommon.LoadMainManifestByPath(mainifestFullName);

        if (initial == null)
        {
            yield break;
        }
        //拷贝AssetBundle文件
        string[] all_assetbundle = initial.GetAllAssetBundles();
        this.InitTotal = all_assetbundle.Length;
        for (int i = 0; i < all_assetbundle.Length; ++i)
        {
            this.InitCurrent = i;
            string name = all_assetbundle[i];
            yield return(DownLoadCommon.StartCopyInitialFile(name));
        }

        //拷贝结束资源 保证安装包拷贝结束
        yield return(DownLoadCommon.StartCopyInitialFile(DownLoadCommon.END_RESOUCES_FILE_NAME));
    }
Example #3
0
    /// <summary>
    ///   更新AssetBundle
    /// </summary>
    IEnumerator StartUpdateAssetBundle()
    {
        Debug.Log("AssetUpdater:StartUpdateAssetBundle");
        if (ErrorCode != EmErrorCode.None)
        {
            yield break;
        }

        UpdateCompleteValue(0f, 0f);

        ////载入MainManifest
        AssetBundleManifest manifest = ZTAssetBundleManager.GetInstance().MainManifest;
        //载入新的ResourcesManifest
        string file = DownLoadCommon.GetCacheFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME);
        AssetBundleManifest new_manifest = DownLoadCommon.LoadMainManifestByPath(file);

        if (new_manifest == null)
        {
            Error(EmErrorCode.LoadNewMainManifestFailed
                  , "Can't find new version MainManifest!");
            yield break;
        }

        ////获取需下载的资源列表与删除的资源的列表
        List <string> download_files = new List <string>();
        List <string> delete_files   = new List <string>();

        CompareAssetBundleDifference(ref download_files, ref delete_files
                                     , manifest, new_manifest);

        //删除已废弃的文件
        if (delete_files.Count > 0)
        {
            for (int i = 0; i < delete_files.Count; ++i)
            {
                string full_name = DownLoadCommon.GetFileFullName(delete_files[i]);
                if (File.Exists(full_name))
                {
                    File.Delete(full_name);
                    yield return(0);
                }
            }
        }

        //更新所有需下载的资源
        _ab_download = new AssetBundleDownloader(_current_ab_url);
        _ab_download.Start(DownLoadCommon.PATH, download_files);
        while (!_ab_download.IsDone)
        {
            UpdateCompleteValue(_ab_download.CompletedSize, _ab_download.TotalSize);
            yield return(0);
        }
        if (_ab_download.IsFailed)
        {
            Error(EmErrorCode.DownloadAssetBundleFailed);
            yield break;
        }
    }
Example #4
0
    /// <summary>
    ///   写入下载缓存信息,用于断点续传
    /// </summary>
    void SaveDownloadCacheData()
    {
        if (CurrentState < EmState.UpdateAssetBundle)
        {
            return;
        }

        if (!Directory.Exists(DownLoadCommon.CACHE_PATH))
        {
            return;
        }

        //载入新的Manifest
        string new_manifest_name         = DownLoadCommon.GetCacheFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME);
        AssetBundleManifest new_manifest = DownLoadCommon.LoadMainManifestByPath(new_manifest_name);

        if (new_manifest == null)
        {
            return;
        }

        //先尝试读取旧的缓存信息,再保存现在已经下载的数据
        //PS:由于只有版本完整更新完才会移动Cache目录,且玩家可能多次尝试下载更新,所以必须保留旧的缓存信息
        DownloadCache cache = new DownloadCache();

        cache.Load(DownLoadCommon.DOWNLOADCACHE_FILE_PATH);
        if (_ab_download != null &&
            _ab_download.CompleteDownloads != null &&
            _ab_download.CompleteDownloads.Count > 0)
        {
            for (int i = 0; i < _ab_download.CompleteDownloads.Count; ++i)
            {
                string  assetbundle_name = _ab_download.CompleteDownloads[i];
                Hash128 hash_code        = new_manifest.GetAssetBundleHash(assetbundle_name);
                if (hash_code.isValid && !cache.Data.AssetBundles.ContainsKey(assetbundle_name))
                {
                    DownloadCacheData.AssetBundle elem = new DownloadCacheData.AssetBundle()
                    {
                        AssetBundleName = assetbundle_name,
                        Hash            = hash_code.ToString(),
                    };
                    Debug.Log(cache.Data.AssetBundles.Count + " - Cache Add:" + assetbundle_name);
                    cache.Data.AssetBundles.Add(assetbundle_name, elem);
                }
            }
        }
        if (cache.HasData())
        {
            cache.Save(DownLoadCommon.DOWNLOADCACHE_FILE_PATH);
        }
    }
Example #5
0
    /*
     * @brief 加载资源
     * @param path 资源路径
     * @param callback 回调函数
     */
    public static byte[] LoadLuaAsset(string path)
    {
        // Windows 平台分隔符为 '/', OS 平台 路径分隔符为 '\', 此处是一个大坑
        if (Application.platform == RuntimePlatform.WindowsEditor)
        {
            path = path.Replace('\\', '/');
        }

#if UNITY_EDITOR
        Object obj = null;
        //lua ab包地址
        path = PathManager.LuaPath + "/" + path;
        //编辑器模式下 资源获取
        obj = UnityEditor.AssetDatabase.LoadMainAssetAtPath(path);
        TextAsset text = (TextAsset)obj;
        if (null != text)
        {
            return(text.bytes);
        }
        return(null);
#else
        string filename = DownLoadCommon.GetLuaHotFullName(path);
        if (File.Exists(filename))
        {
            try
            {
                string res = File.ReadAllText(filename);
                return(System.Text.Encoding.UTF8.GetBytes(res));
            }catch (System.Exception e)
            {
                Debug.Log("LoadLuaAsset=====3>>" + e.ToString());
            }
        }

        Object        obj2       = null;
        string        fileNameEx = System.IO.Path.GetFileNameWithoutExtension(path);
        ZTAssetBundle bundle     = ZTAssetBundleManager.GetInstance().LoadAssetBundleAndDependencies("luascript");
        //加载assetBundleManifest文件
        if (null != bundle)
        {
            obj2 = bundle.GetAsset(fileNameEx);
        }
        TextAsset text2 = (TextAsset)obj2;
        if (null != text2)
        {
            return(text2.bytes);
        }

        return(null);
#endif
    }
Example #6
0
    /// <summary>
    ///   初始化
    /// </summary>
    IEnumerator Preprocess()
    {
        //创建资源根目录
        if (!Directory.Exists(DownLoadCommon.PATH))
        {
            Directory.CreateDirectory(DownLoadCommon.PATH);
        }

        //判断主资源文件是否存在,不存在则拷贝备份资源至资源根目录
        bool   do_initial_copy         = false;
        string resources_manifest_file = DownLoadCommon.GetFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME);

        if (!File.Exists(resources_manifest_file))
        {
            do_initial_copy = true;
        }
        else
        {
            // 拷贝安装包初始化目录中的ResourcesManifest,并判断是否重新拷贝初始化目录下的所有文件
            //string initial_full_name = DownLoadCommon.GetInitialFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME);
            //string cache_full_name = DownLoadCommon.GetCacheFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME);
            //yield return DownLoadCommon.StartCopyFile(initial_full_name, cache_full_name);

            ////判断安装包初始目录是否完整
            //ResourcesManifest initial = DownLoadCommon.LoadResourcesManifestByPath(cache_full_name);
            //if (initial == null)
            //{
            //    Error(EmErrorCode.PreprocessError
            //        , "Initial path don't contains "
            //            + DownLoadCommon.RESOURCES_MANIFEST_FILE_NAME + "!");
            //    yield break;
            //}

            //ResourcesManifest current = DownLoadCommon.LoadResourcesManifestByPath(full_name);
            //if (current == null)
            //    do_initial_copy = true;
            //else if (current.Data.Version < initial.Data.Version)
            //    do_initial_copy = true;

            //删除缓存中的文件
            //if (File.Exists(cache_full_name))
            //    File.Delete(cache_full_name);
        }

        if (do_initial_copy)
        {
            yield return(CopyAllInitialFiles());
        }

        PreprocessFinished();
    }
Example #7
0
    /// <summary>
    ///   判断一个AssetBundle是否存在缓存
    /// </summary>
    public bool IsExist(string assetbundlename)
    {
        if (!IsReady)
        {
            return(false);
        }

        if (string.IsNullOrEmpty(assetbundlename))
        {
            return(false);
        }

        return(File.Exists(DownLoadCommon.GetFileFullName(assetbundlename)));
    }
Example #8
0
    /// <summary>
    ///   拷贝文件并覆盖旧数据文件
    /// </summary>
    IEnumerator StartCopyCacheFile()
    {
        Debug.Log("AssetUpdater:StartCopyCacheFile");
        if (ErrorCode != EmErrorCode.None)
        {
            yield break;
        }

        string str  = DownLoadCommon.GetCacheFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME);
        string dest = DownLoadCommon.GetFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME);

        UpdateCompleteValue(1, 1);
        yield return(DownLoadCommon.StartCopyFile(str, dest));
    }
Example #9
0
    IEnumerator LoadAsyncCoroutine(string assetbundlename)
    {
        //临时
        if (!IsExist(assetbundlename))
        {
            _loadSyncAbNames.RemoveAt(0);
            if (_loadSyncAbList.ContainsKey(assetbundlename))
            {
                List <Action <AssetBundle> > callbackList = _loadSyncAbList[assetbundlename];
                for (int i = 0; i < callbackList.Count; i++)
                {
                    Action <AssetBundle> callback = callbackList[i];
                    callback(null);
                }

                _loadSyncAbList.Remove(assetbundlename);
            }

            yield break;
        }

        string assetbundle_path       = DownLoadCommon.GetFileFullName(assetbundlename);
        AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync(assetbundle_path);

        //Debug.LogWarning(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + assetbundlename);
        while (!abcr.isDone)
        {
            yield return(null);
        }
        //Debug.LogWarning("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" + assetbundlename);
        if (null != abcr.assetBundle)
        {
            SaveInCahce(assetbundlename, abcr.assetBundle);
            _loadSyncAbNames.RemoveAt(0);
        }

        if (_loadSyncAbList.ContainsKey(assetbundlename))
        {
            List <Action <AssetBundle> > callbackList = _loadSyncAbList[assetbundlename];
            for (int i = 0; i < callbackList.Count; i++)
            {
                Action <AssetBundle> callback = callbackList[i];
                callback(abcr.assetBundle);
            }

            _loadSyncAbList.Remove(assetbundlename);
        }
    }
Example #10
0
    /// <summary>
    /// 初始化结束
    /// </summary>
    bool PreprocessFinished()
    {
        this.InitCurrent = this.InitTotal;

        this.MainManifest = DownLoadCommon.LoadMainManifest();

        if (this.MainManifest == null)
        {
            Error(EmErrorCode.LoadMainManifestFailed
                  , "Can't load MainManifest file!");
            return(false);
        }

        ////标记已准备好
        IsReady = ErrorCode == EmErrorCode.None;

        return(true);
    }
Example #11
0
    /// <summary>
    /// 初始化
    /// </summary>
    bool PreprocessFinished()
    {
        MainManifest = DownLoadCommon.LoadMainManifest();
        if (MainManifest == null)
        {
            Error(EmErrorCode.LoadMainManifestFailed
                  , "Can't load MainManifest file!");
            return(false);
        }


        ////记录当前版本号
        //Version = ResourcesManifest.Data.Version;
        ////标记已准备好
        IsReady = ErrorCode == EmErrorCode.None;

        return(true);
    }
Example #12
0
    /// <summary>
    ///   拷贝初始目录所有文件
    /// </summary>
    IEnumerator CopyAllInitialFiles()
    {
        //拷贝所有配置文件
        yield return(DownLoadCommon.StartCopyInitialFile(DownLoadCommon.MAIN_MANIFEST_FILE_NAME));

        string initial_full_name    = DownLoadCommon.GetFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME);
        AssetBundleManifest initial = DownLoadCommon.LoadMainManifestByPath(initial_full_name);

        if (initial == null)
        {
            yield break;
        }
        //拷贝AssetBundle文件
        string[] all_assetbundle = initial.GetAllAssetBundles();
        for (int i = 0; i < all_assetbundle.Length; ++i)
        {
            string name = all_assetbundle[i];
            yield return(DownLoadCommon.StartCopyInitialFile(name));
        }
        //ResourcesManifest resources_manifest = DownLoadCommon.LoadResourcesManifest();
        //if (resources_manifest == null)
        //{
        //    Debug.LogWarning("Can't load ResourcesManifest file!");
        //    yield break;
        //}
        //var itr = resources_manifest.Data.AssetBundles.GetEnumerator();
        //while (itr.MoveNext())
        //{
        //    if (itr.Current.Value.IsNative)
        //    {
        //        string assetbundlename = itr.Current.Value.AssetBundleName;
        //        string dest = DownLoadCommon.GetFileFullName(assetbundlename);

        //        //保证路径存在
        //        string directory = Path.GetDirectoryName(dest);
        //        if (!Directory.Exists(directory))
        //            Directory.CreateDirectory(directory);

        //        //拷贝数据
        //        yield return DownLoadCommon.StartCopyInitialFile(assetbundlename);
        //    }
        //}
        //itr.Dispose();
    }
Example #13
0
    /// <summary>
    ///   初始化(检测资源完整性)
    /// </summary>
    IEnumerator Preprocess()
    {
        //创建资源根目录
        if (!Directory.Exists(DownLoadCommon.PATH))
        {
            Directory.CreateDirectory(DownLoadCommon.PATH);
        }

        //判断主资源文件 和 初始化结束资源 是否存在,不存在则拷贝备份资源至资源根目录
        string resources_manifest_file = DownLoadCommon.GetFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME);
        string resources_end_File      = DownLoadCommon.GetFileFullName(DownLoadCommon.END_RESOUCES_FILE_NAME);

        if (!File.Exists(resources_manifest_file) || !File.Exists(resources_end_File))
        {
            yield return(CopyAllInitialFiles());
        }

        PreprocessFinished();
    }
Example #14
0
    IEnumerator StartUnZipLua()
    {
        Debug.Log("AssetUpdater:StartUnZipLua");
        if (ErrorCode != EmErrorCode.None)
        {
            yield break;
        }

        string path    = DownLoadCommon.GetCacheFileFullName("LuaScript.zip");
        string outPath = DownLoadCommon.HOT_LUA_PATH;

        CompressHelper.CompressTask task = CompressHelper.UnCompressAsync(path, outPath);
        while (!task.IsDone)
        {
            Debug.Log(task.Progress);
            UpdateCompleteValue(Mathf.Floor(task.Progress * 100), 100f);
            yield return(null);
        }
        Debug.Log("AssetUpdater:StartUnZipLua Delete: " + path);
        File.Delete(path);

        UpdateCompleteValue(1f, 1f);
    }
Example #15
0
    /// <summary>
    ///   加载AssetBundle
    /// </summary>
    AssetBundle LoadAssetBundle(string assetbundlename)
    {
        if (assetbundlename == null || MainManifest == null)
        {
            return(null);
        }

        AssetBundle ab = LoadInCahce(assetbundlename);

        if (null != ab)
        {
            return(ab);
        }
        string assetbundle_path = DownLoadCommon.GetFileFullName(assetbundlename);

        if (System.IO.File.Exists(assetbundle_path))
        {
            ab = AssetBundle.LoadFromFile(assetbundle_path);
            SaveInCahce(assetbundlename, ab);
        }

        return(ab);
    }
Example #16
0
    private Dictionary <string, Object> _assetDic; //已经读取资源

    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="abName"></param>
    public ZTAssetBundle(string abName)
    {
        this.AbPath    = DownLoadCommon.GetFileFullName(abName);;
        this._assetDic = new Dictionary <string, Object>();
        this.Load();
    }
Example #17
0
    //<summary>
    //  比较AssetBundle差异,获得下载列表与删除列表
    //</summary>
    static void CompareAssetBundleDifference(ref List <string> download_files
                                             , ref List <string> delete_files
                                             , AssetBundleManifest old_manifest
                                             , AssetBundleManifest new_manifest
                                             )
    {
        if (download_files != null)
        {
            download_files.Clear();
        }
        if (delete_files != null)
        {
            delete_files.Clear();
        }

        if (old_manifest == null)
        {
            return;
        }
        if (new_manifest == null)
        {
            return;
        }

        //采用位标记的方式判断资源
        //位标记: 0: 存在旧资源中 1: 存在新资源中 2:本地资源标记
        int old_version_bit = 0x1;                      // 存在旧资源中
        int new_version_bit = 0x2;                      // 存在新资源中

        Dictionary <string, int> temp_dic = new Dictionary <string, int>();

        //标记旧资源
        string[] all_assetbundle = old_manifest.GetAllAssetBundles();
        for (int i = 0; i < all_assetbundle.Length; ++i)
        {
            string name = all_assetbundle[i];
            _SetDictionaryBit(ref temp_dic, name, old_version_bit);
        }
        //标记新资源
        string[] new_all_assetbundle = new_manifest.GetAllAssetBundles();
        for (int i = 0; i < new_all_assetbundle.Length; ++i)
        {
            string name = new_all_assetbundle[i];
            _SetDictionaryBit(ref temp_dic, name, new_version_bit);
        }


        //获得对应需操作的文件名, 优先级: both > add > delete
        //both: 第0位与第1位都被标记的
        //delete: 仅第0位被标记的
        //add: 第2位未标记,且第3位被标记的
        int           both_bit   = old_version_bit | new_version_bit; // 二个版本资源都存在
        List <string> add_files  = new List <string>();
        List <string> both_files = new List <string>();
        var           itr        = temp_dic.GetEnumerator();

        while (itr.MoveNext())
        {
            string name = itr.Current.Key;
            int    mask = itr.Current.Value;
            if ((mask & new_version_bit) == new_version_bit &&
                (mask & old_version_bit) == 0)
            {
                add_files.Add(name);
            }
            else if ((mask & both_bit) == both_bit)
            {
                both_files.Add(name);
            }
            else if ((mask & old_version_bit) == old_version_bit)
            {
                delete_files.Add(name);
            }
        }
        itr.Dispose();

        //载入下载缓存数据
        DownloadCache download_cache = new DownloadCache();

        download_cache.Load(DownLoadCommon.DOWNLOADCACHE_FILE_PATH);
        if (!download_cache.HasData())
        {
            download_cache = null;
        }

        //记录需下载的文件
        {
            //加入新增的文件
            download_files.AddRange(add_files);
            //比较所有同时存在的文件,判断哪些需要更新
            for (int i = 0; i < both_files.Count; ++i)
            {
                string name      = both_files[i];
                string full_name = DownLoadCommon.GetFileFullName(name);
                if (File.Exists(full_name))
                {
                    //判断哈希值是否相等
                    string old_hash = old_manifest.GetAssetBundleHash(name).ToString();
                    string new_hash = new_manifest.GetAssetBundleHash(name).ToString();
                    if (old_hash.CompareTo(new_hash) == 0)
                    {
                        continue;
                    }
                    download_files.Add(name);
                }
                else
                {
                    download_files.Add(name);
                }
            }

            //过滤缓存中已下载的文件
            if (download_cache != null)
            {
                var cache_itr = download_cache.Data.AssetBundles.GetEnumerator();
                while (cache_itr.MoveNext())
                {
                    DownloadCacheData.AssetBundle elem = cache_itr.Current.Value;
                    string name      = elem.AssetBundleName;
                    string full_name = DownLoadCommon.GetFileFullName(name);
                    if (File.Exists(full_name))
                    {
                        string cache_hash = elem.Hash;
                        string new_hash   = new_manifest.GetAssetBundleHash(name).ToString();
                        if (!string.IsNullOrEmpty(cache_hash) &&
                            cache_hash.CompareTo(new_hash) == 0)
                        {
                            download_files.Remove(name);
                        }
                    }
                }
            }
        }
    }
Example #18
0
    /// <summary>
    ///   载入Manifest
    /// </summary>
    public static AssetBundleManifest LoadMainManifest()
    {
        string file = DownLoadCommon.GetFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME);

        return(LoadMainManifestByPath(file));
    }