Exemplo n.º 1
0
        private bool CheckedMD5(string fileName, string md5Value, out bool isUnExist)
        {
            //#if UNITY_EDITOR
            //            return false;
            //#endif
            isUnExist = false;
            bool result = false;

            if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(md5Value))
            {
                isUnExist = true;
                return(result);
            }
            string path      = targetDir + "/" + fileName;
            string localPath = string.Empty;

            if (!ResUtil.ExistsInLocal(path, out localPath))
            {
                isUnExist = true;
                return(result);
            }
            try
            {
                byte[] bytes     = File.ReadAllBytes(localPath);
                string encodeMD5 = EncryptUtil.Bytes2MD5(bytes);
                result = md5Value.Equals(encodeMD5);
                //result = true;
                if (!result)
                {
                    Debugger.Log(string.Format("file {0}被修改了", fileName));
                }
            }
            catch (Exception e)
            {
                Debugger.LogError(e.StackTrace);
            }
            return(result);
        }
Exemplo n.º 2
0
        private void CheckDifference()
        {
            _totalLength  = 0;
            _diffAssetDic = new Dictionary <string, AssetInfo>();
            AssetInfo localAsset;

            if (ResMgr.instance.localManifest.version != ResMgr.instance.remoteManifest.version)
            {
                ResMgr.instance.localManifest.version = ResMgr.instance.remoteManifest.version;
                //ResMgr.instance.localManifest.assetDic.Clear();
                //ResUtil.DelAllLocalAbRes();
            }

            string tmpStr;

            foreach (var kvp in ResMgr.instance.remoteManifest.assetDic)
            {
                if (IsExclude(kvp.Key))
                {
                    continue;
                }

                if (ResMgr.instance.localManifest.assetDic.TryGetValue(kvp.Key, out localAsset))
                {
                    if (!localAsset.Equals(kvp.Value) || !ResUtil.ExistsInLocal(kvp.Key + kvp.Value.Suffix, out tmpStr))
                    {
                        _diffAssetDic.Add(kvp.Key, kvp.Value);
                        _totalLength += kvp.Value.Length;
                    }
                }
                else
                {
                    _diffAssetDic.Add(kvp.Key, kvp.Value);
                    _totalLength += kvp.Value.Length;
                }
            }

            _totalNum   = _diffAssetDic.Count;
            _currentNum = 1;
            Debugger.Log(string.Format("检查更新结束_totalLength:{0},_totalNum{1}", _totalLength, _totalNum));
            if (checkedVersionHandler != null)
            {
                checkedVersionHandler(this, _totalLength, _totalNum);
            }

            if (_totalNum == 0)
            {
                int total = ResMgr.instance.remoteManifest.assetDic.Count;
                if (progressHandler != null)
                {
                    progressHandler(this, 1, total, total);
                }
                Debugger.Log("资源同步完成");
                if (completeHandler != null)
                {
                    completeHandler(this);
                }
                UnityEngine.Object.Destroy(this);
            }
            else
            {
                Preload();//检查完之后手动去调用
            }
        }
Exemplo n.º 3
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
                }
            }
        }
Exemplo n.º 4
0
        public static IEnumerator CheckLocalAndStreamingVersion(MonoBehaviour mono, string path, Action <bool> resultIsNew, int time)
        {
            string StreamingPath = ResUtil.GetStreamingAssetsWWWPath(path);
            var    www           = new WWW(StreamingPath + GameConfig.instance.param);

            Debugger.Log(www.url);
            www.threadPriority = ThreadPriority.High;
            yield return(www);

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

                    mono.StartCoroutine(CheckLocalAndStreamingVersion(mono, path, resultIsNew, time + 1));
                    yield break;
                }
                else
                {
                    Debugger.LogError("CheckLocalAndStreamingVersion 又加载失败了,心累了,不想加载了...");
                }
            }
            else
            {
                Debugger.LogError(string.Format("CheckLocalAndStreamingVersion:{0}加载成功!", www.url));
                //Debugger.Log(_md5WWW.text);
                string[] remoteLineArr = www.text.Split(CSVUtil.SYMBOL_LINE, StringSplitOptions.RemoveEmptyEntries);
                string   remoteVersion = remoteLineArr[0].ToArray(CSVUtil.SYMBOL_COLON)[1];

                string localMD5 = string.Empty;
                if (ResUtil.ExistsInLocal(path, out localMD5))
                {
                    string[] localLineArr = File.ReadAllText(localMD5).Split(CSVUtil.SYMBOL_LINE, StringSplitOptions.RemoveEmptyEntries);
                    string   localVersion = localLineArr[0].ToArray(CSVUtil.SYMBOL_COLON)[1];

                    if (resultIsNew != null)
                    {
                        Debug.LogFormat("remoteVersion:{0}, localVersion:{1}", remoteVersion, localVersion);
                        string[] remoteV = remoteVersion.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
                        string[] localV  = localVersion.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
                        for (int i = 0; i < remoteV.Length; i++)
                        {
                            int intRemote = 0;
                            int intlocalV = 0;
                            if (int.TryParse(remoteV[i], out intRemote) && int.TryParse(localV[i], out intlocalV))
                            {
                                if (intRemote > intlocalV)
                                {
                                    Debug.Log("intRemote > intlocalV");
                                    resultIsNew(true);
                                    www.Dispose();
                                    www = null;
                                    yield break;
                                }
                                else
                                if (intRemote < intlocalV)
                                {
                                    Debug.Log("intRemote < intlocalV");
                                    resultIsNew(false);
                                    www.Dispose();
                                    www = null;
                                    yield break;
                                }
                            }
                        }
                    }
                }
                Debug.Log("intRemote == intlocalV");
                www.Dispose();
                www = null;
            }
            resultIsNew(false);
        }
Exemplo n.º 5
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;
        }