예제 #1
0
 public void Load()
 {
     if (_resObj == null)
     {
         throw new System.Exception("ResLoader:: you must set ResObj before Load");
     }
     if (_state != EResLoaderState.READY)
     {
         throw new System.Exception("ResLoader:: is loading you can not reload it");
     }
     _state = EResLoaderState.Loading;
     _reLoadNum++;
     _currentProgress  = 0f;
     _lastProgressTime = Time.realtimeSinceStartup;
     StopCoroutine("LoadFileCoroutine");
     StartCoroutine("LoadFileCoroutine");
 }
예제 #2
0
        private System.Collections.IEnumerator LoadFileCoroutine()
        {
            if (startHandler != null)
            {
                startHandler(this);
            }
            string    localPath;
            AssetInfo localAssetInfo;
            AssetInfo remoteAssetInfo;

            if (ResMgr.instance.localManifest.assetDic.TryGetValue(_resObj.SubPath, out localAssetInfo) &&
                ResMgr.instance.remoteManifest.assetDic.TryGetValue(_resObj.SubPath, out remoteAssetInfo) &&
                localAssetInfo.Equals(remoteAssetInfo) && ResUtil.ExistsInLocal(_resObj.SubPath + _resObj.Suffix, out localPath))
            {
                _formLocal = true;
                byte[] bytes = System.IO.File.ReadAllBytes(localPath);
                if (bytes == null)
                {
                    if (errorHandler != null)
                    {
                        errorHandler(this);
                    }
                    Debugger.LogError(string.Format("{0}读取ab文件失败,可能本地文件已损坏", _resObj));
                    UnityEngine.Object.Destroy(this);
                }
                AssetBundleCreateRequest abcr = AssetBundle.CreateFromMemory(bytes);
                while (!abcr.isDone)
                {
                    if (_state == EResLoaderState.Error)
                    {
                        break;
                    }
                    CurrentProgress = abcr.progress;
                    yield return(null);
                }
                if (_state == EResLoaderState.Error)
                {
                    abcr = null;

                    if (_resObj.MaxTryLoadNum > 0 && _reLoadNum > _resObj.MaxTryLoadNum)
                    {
                        if (errorHandler != null)
                        {
                            errorHandler(this);
                        }
                        Debugger.LogError(string.Format("{0}加载失败…{1}次尝试加载,可能本地文件已损坏", _resObj, _reLoadNum));
                        //#if !UNITY_EDITOR
                        UnityEngine.Object.Destroy(this);
                        //#endif
                    }
                    else
                    {
                        Debugger.LogError(string.Format("{0}加载本地文件失败,可能本地文件已损坏,直接删除本地文件,重新下载", _resObj));
                        System.IO.File.Delete(localPath);
                        yield return(new WaitForSeconds(1f));

                        _state = EResLoaderState.READY;
                        Load();
                    }
                }
                else
                {
                    //_assetBundle = abcr.assetBundle;
                    //if (_assetBundle == null)
                    //    Debugger.LogError("you must load assetBundle file with resloader" + _resObj);
                    _state          = EResLoaderState.Complete;
                    CurrentProgress = 1f;
                    if (completeHandler != null)
                    {
                        completeHandler(this);
                    }
                    //#if !UNITY_EDITOR
                    UnityEngine.Object.Destroy(this);
                    //#endif
                }
            }
            else
            {
                WWW www = new WWW(ResUtil.GetRemotePath(_resObj.SubPath + _resObj.Suffix) + Logic.Game.GameConfig.instance.param);
                Debugger.Log(www.url);
                www.threadPriority = ThreadPriority.High;
                while (!www.isDone)
                {
                    if (_state == EResLoaderState.Error)
                    {
                        break;
                    }
                    CurrentProgress = www.progress;
                    yield return(null);
                }
                if (!string.IsNullOrEmpty(www.error))
                {
                    _state = EResLoaderState.Error;
                }
                if (_state == EResLoaderState.Error)
                {
                    Debugger.LogError(string.Format("{0}加载失败", www.url));
                    www.Dispose();
                    www = null;
                    if (_resObj.MaxTryLoadNum > 0 && _reLoadNum > _resObj.MaxTryLoadNum)
                    {
                        if (errorHandler != null)
                        {
                            errorHandler(this);
                        }
                        //#if !UNITY_EDITOR
                        UnityEngine.Object.Destroy(this);
                        //#endif
                    }
                    else
                    {
                        Debugger.LogError(string.Format("{0}加载失败…第{1}次尝试加载", _resObj, _reLoadNum));
                        yield return(new WaitForSeconds(1f));

                        _state = EResLoaderState.READY;
                        Load();
                    }
                }
                else
                {
                    ResUtil.Save2Local(_resObj.SubPath + _resObj.Suffix, www.bytes);

                    if (ResMgr.instance.remoteManifest.assetDic.TryGetValue(_resObj.SubPath, out remoteAssetInfo))
                    {
                        ResMgr.instance.localManifest.assetDic[_resObj.SubPath] = remoteAssetInfo;
                        ResUtil.SaveManifest2Local(ResMgr.instance.localManifest);//同步远程和本地的manifest
                    }
                    _state = EResLoaderState.Complete;
                    //_assetBundle = www.assetBundle;
                    www.Dispose();
                    www = null;
                    //if (_assetBundle == null)
                    //    Debugger.LogError("you must load assetBundle file with resloader");
                    //if(_resObj)
                    //_assetBundle.Unload(true);
                    CurrentProgress = 1f;
                    if (completeHandler != null)
                    {
                        completeHandler(this);
                    }
                    //#if !UNITY_EDITOR
                    UnityEngine.Object.Destroy(this);
                    //#endif
                }
            }
        }
예제 #3
0
        private IEnumerator LoadFilesCoroutine()
        {
            int   currentNum = _toatlNumber - _modifiedList.Count;
            float progress   = (float)currentNum / (float)_toatlNumber;

            progress         = fromPorgress + (toPorgress - fromPorgress) * progress;
            _currentProgress = progress;
            if (progressUpdateHandler != null)
            {
                progressUpdateHandler(progress, currentNum, _toatlNumber);
            }

            if (_modifiedList.Count > 0)
            {
                _loadFileNumber++;
                string flieName = _modifiedList.First();
                string path     = string.Empty;
                if (isRemote)
                {
                    path = ResUtil.GetRemotePath(targetDir + "/" + flieName);
                }
                else
                {
                    path = ResUtil.GetStreamingAssetsWWWPath(targetDir + "/" + flieName);
                }
                _fileWWW = new WWW(path + GameConfig.instance.param);
                Debugger.Log(path + GameConfig.instance.param);
                _fileWWW.threadPriority = ThreadPriority.High;
                yield return(_fileWWW);

                if (!string.IsNullOrEmpty(_fileWWW.error))
                {
                    _state = EResLoaderState.Error;
                }
                if (_state == EResLoaderState.Error)
                {
                    Debugger.LogError(string.Format("{0}加载失败", _fileWWW.url));
                    Debugger.LogError(string.Format("加载失败…第{0}次尝试加载", _loadFileNumber));
                    if (_loadFileNumber < MAX_LOAD_NUMBER)
                    {
                        yield return(new WaitForSeconds(1f));

                        _state = EResLoaderState.READY;
                    }
                }
                else
                {
                    string localPath = targetDir + "/" + flieName;
                    ResUtil.Save2Local(localPath, _fileWWW.bytes);
                    _modifiedList.Remove(flieName);
                    _loadFileNumber = 0;
                }
                _fileWWW.Dispose();
                _fileWWW = null;
                StartCoroutine("LoadFilesCoroutine");
            }
            else
            {
                if (completeHandler != null)
                {
                    completeHandler();
                }
                Debugger.Log(string.Format("所有文件加载成功!"));
                UnityEngine.Object.Destroy(this);
            }
        }
예제 #4
0
        private IEnumerator LoadMD5Coroutine()
        {
            _loadMD5Number++;
            string path = string.Empty;

            if (isRemote)
            {
                path = ResUtil.GetRemotePath(md5File);
            }
            else
            {
                path = ResUtil.GetStreamingAssetsWWWPath(md5File);
            }
            _md5WWW = new WWW(path + GameConfig.instance.param);
            Debugger.Log(_md5WWW.url);
            _md5WWW.threadPriority = ThreadPriority.High;
            yield return(_md5WWW);

            if (!string.IsNullOrEmpty(_md5WWW.error))
            {
                _state = EResLoaderState.Error;
            }
            if (_state == EResLoaderState.Error)
            {
                Debugger.LogError(string.Format("{0}加载失败", _md5WWW.url));
                _md5WWW.Dispose();
                _md5WWW = null;
                Debugger.LogError(string.Format("加载失败…第{0}次尝试加载", _loadMD5Number));
                if (_loadMD5Number < MAX_LOAD_NUMBER)
                {
                    yield return(new WaitForSeconds(1f));

                    _state = EResLoaderState.READY;
                    StopCoroutine("LoadMD5Coroutine");
                    StartCoroutine("LoadMD5Coroutine");
                }
                else
                {
                    Debugger.LogError("又加载失败了,心累了,不想加载了...");
                    if (failHandler != null)
                    {
                        failHandler();
                    }
                }
            }
            else
            {
                //Debugger.Log(_md5WWW.text);
                string[] remoteLineArr = _md5WWW.text.Split(CSVUtil.SYMBOL_LINE, StringSplitOptions.RemoveEmptyEntries);
                string   remoteVersion = remoteLineArr[0].ToArray(CSVUtil.SYMBOL_COLON)[1];
                if (_md5Name.Contains(ResUtil.CONFIG_DIRECTORY))
                {
                    GameConfig.instance.csvVersion = remoteVersion;
                }
                else if (_md5Name.Contains(ResUtil.LUA_DIRECTORY))
                {
                    GameConfig.instance.luaVersion = remoteVersion;
                }
                for (int i = 1, count = remoteLineArr.Length; i < count; i++)
                {
                    KeyValuePair <string, string> kvp = remoteLineArr[i].SplitToKeyValuePair(CSVUtil.SYMBOL_COLON);
                    _fileDic.Add(kvp.Key, kvp.Value);
                }
                bool   isSameVersion = false;
                string localMD5      = string.Empty;
                if (!ResUtil.ExistsInLocal(md5File, out localMD5))
                {
                    Debugger.Log("md5本地不存在");
                    ResUtil.Save2Local(md5File, _md5WWW.bytes);
                    _modifiedList.AddRange(_fileDic.GetKeys());
                    LoadFiles();
                }
                else
                {
                    string[] localLineArr = File.ReadAllText(localMD5).Split(CSVUtil.SYMBOL_LINE, StringSplitOptions.RemoveEmptyEntries);
                    string   localVersion = localLineArr[0].ToArray(CSVUtil.SYMBOL_COLON)[1];
                    for (int i = 1, count = localLineArr.Length; i < count; i++)
                    {
                        KeyValuePair <string, string> kvp = localLineArr[i].SplitToKeyValuePair(CSVUtil.SYMBOL_COLON);
                        _localFileDic.Add(kvp.Key, kvp.Value);
                    }

                    Dictionary <string, string> .Enumerator locale = _localFileDic.GetEnumerator();
                    int delCount = 0;
                    while (locale.MoveNext())
                    {
                        KeyValuePair <string, string> kvp = locale.Current;
                        if (!_fileDic.ContainsKey(kvp.Key))
                        {
                            ResUtil.DelLocalFile(targetDir + "/" + kvp.Key);
                            delCount++;
                        }
                    }

                    locale.Dispose();
                    Debug.LogFormat("remoteVersion:{0}, localVersion:{1}", remoteVersion, localVersion);
                    isSameVersion = remoteVersion == localVersion;
                    //if (isSameVersion)
                    {
                        Debugger.Log("本地跟远程版本是否相同,都比较" + md5File + "   isSameVersion:" + isSameVersion);
                        int addCount = 0;
                        Dictionary <string, string> .Enumerator e = _fileDic.GetEnumerator();
                        while (e.MoveNext())
                        {
                            bool isUnExist = false;
                            KeyValuePair <string, string> kvp = e.Current;
                            if (!CheckedMD5(kvp.Key, kvp.Value, out isUnExist))
                            {
                                _modifiedList.Add(kvp.Key);
                                if (isUnExist)
                                {
                                    addCount++;
                                }
                            }
                        }
                        e.Dispose();
                        Debugger.Log(string.Format("{0}个新增文件", addCount));
                        Debugger.Log(string.Format("{0}个本地文件MD5改变", _modifiedList.Count - addCount));
                        if (_modifiedList.Count > 0)
                        {
                            //Debugger.Log(string.Format("{0}个本地文件MD5改变", _modifiedList.Count));
                            LoadFiles();
                        }
                        else
                        {
                            if (progressUpdateHandler != null)
                            {
                                progressUpdateHandler(_toProgress, 0, _toatlNumber);
                            }

                            StartCoroutine("CompleteCoroutine");
                        }
                        if (!isSameVersion || _modifiedList.Count > 0 || delCount > 0)
                        {
                            ResUtil.Save2Local(md5File, _md5WWW.bytes);
                        }
                        Debugger.Log(string.Format("{0}个本地文件已删除", delCount));
                    }
                    //else
                    //{
                    //    Debugger.Log(string.Format("{0}个本地文件已删除", delCount));

                    //    Debugger.Log("本地跟远程版本不同,重新加载文件");
                    //    ResUtil.Save2Local(md5File, _md5WWW.bytes);
                    //    //_md5WWW.Dispose();
                    //    //_md5WWW = null;
                    //    _modifiedList.AddRange(_fileDic.GetKeys());
                    //    LoadFiles();
                    //}
                }
            }
            _md5WWW.Dispose();
            _md5WWW = null;
        }