示例#1
0
        public void LoadZip()
        {
            string zipPath = ResUtil.GetStreamingAssetsWWWPath(md5File);
            //string unzipPath = ResUtil.GetLocalPath("");
            string unzipPath = Path.Combine(Application.persistentDataPath, ResConf.eResPlatform.ToString());

            this.StartCoroutine(UnCompress(zipPath, unzipPath));
        }
示例#2
0
 private void LoadLocalManifest()
 {
     ResMgr.instance.localManifest = ResUtil.GetManifestFromLocal();
     if (ResMgr.instance.localManifest == null)
     {
         ResMgr.instance.localManifest = new ManifestInfo();
     }
 }
示例#3
0
 //把manifest文件信息保存在本地
 public static void SaveManifest2Local(ManifestInfo manifestInfo)
 {
     try
     {
         string path = GetLocalManifestPath();
         string dir  = ResUtil.GetDirectoryByPath(path);
         if (!System.IO.Directory.Exists(dir))
         {
             System.IO.Directory.CreateDirectory(dir);
         }
         System.IO.File.WriteAllBytes(path, GetBytesFromManifest(manifestInfo));
     }
     catch (Exception e)
     {
         Debugger.LogError("SaveManifest2Local:" + e);
     }
     finally
     {
     }
 }
示例#4
0
        //=============================================www加载有bug==========================end

        private System.Collections.IEnumerator LoadRemoteManifestCoroutine()
        {
            WWW www = new WWW(ResUtil.GetRemoteManifestPath() + Logic.Game.GameConfig.instance.param);

            while (!www.isDone)
            {
                if (IsErrorProgress(www.progress))
                {
                    Debugger.Log("manifest加载失败:" + www.url);
                    www.Dispose();
                    www           = null;
                    _isPreloading = false;
                    LoadRemoteManifest();
                    yield break;
                }
                yield return(1);
            }
            if (!string.IsNullOrEmpty(www.error))
            {
                Debugger.Log("manifest加载失败:" + www.url);
                www.Dispose();
                www = null;
                yield return(new WaitForSeconds(0.2f));

                _isPreloading = false;
                LoadRemoteManifest();
            }
            else
            {
                Debugger.Log("manifest加载成功:" + www.url);
                ResMgr.instance.remoteManifest = ResUtil.GetManifestFromBytes(www.bytes);
                if (ResMgr.instance.remoteManifest == null)
                {
                    ResMgr.instance.remoteManifest = new ManifestInfo();
                }

                www.Dispose();
                www = null;
                CheckDifference();
            }
        }
示例#5
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);
        }
示例#6
0
 public static void Save2LocalFullPath(string path, byte[] bytes)
 {
     try
     {
         string dir = ResUtil.GetDirectoryByPath(path);
         if (!System.IO.Directory.Exists(dir))
         {
             System.IO.Directory.CreateDirectory(dir);
         }
         System.IO.FileStream fs = System.IO.File.Open(path, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
         fs.SetLength(0);
         if (bytes != null)
         {
             fs.Write(bytes, 0, bytes.Length);
         }
         fs.Close();
         fs.Dispose();
         Debugger.Log("Save2Local:{0}", path);
     }
     catch (Exception e)
     {
         Debugger.LogError("Save2Local:{0} error {1}!", path, e.ToString());
     }
 }
示例#7
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();//检查完之后手动去调用
            }
        }
示例#8
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
                }
            }
        }
示例#9
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);
        }
示例#10
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);
            }
        }
示例#11
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;
        }