Example #1
0
        public void Init(int versionCode)
        {
            this.VersionCode = versionCode;
            //检查是否有patchIndex文件,并读取内容
            var indexFile = UResources.ReqFile(INDEX_FILE_NAME);

            if (indexFile != null)
            {
                string fileJson = DESFileUtil.ReadAllText(indexFile.FullName, DESKey.DefaultKey, DESKey.DefaultIV);
                _patchIndexLocal = new PatchIndex(this.VersionCode, JObject.Parse(fileJson));
                if (_patchIndexLocal.Version != this.VersionCode)
                {
                    UpdaterLog.Log("发现patchIndex中的主版本不等于目前的主版本,说明刚经历过主包更新,清空补丁");
                    foreach (var file in ResManager.Ins.DownloadedFiles.Values)
                    {
                        File.Delete(file.FullName);
                    }
                    ResManager.Ins.ReadDownloadDir();
                    _patchIndexLocal = new PatchIndex();
                }
            }
            else
            {
                _patchIndexLocal = new PatchIndex();
            }
        }
Example #2
0
        public Dictionary <string, Patch> VerifyPatches()
        {
            var ret = new Dictionary <string, Patch>();

            //验证本地补丁
            foreach (var patchKV in Patches)
            {
                var      name  = patchKV.Key;
                var      patch = patchKV.Value;
                FileInfo file  = UResources.ReqFile(name);
                if (patch.NeedDelete)                  //如果这个补丁是删除补丁,验证他是否真的被删除
                {
                    if (file != null)
                    {
                        UpdaterLog.LogError(string.Format("{0} 这个文件理应被删除,但验证发现没有被删除,将更新", name));
                        ret.Add(name, patch);
                    }
                }
                else                    //这是更新补丁,检测是否存在,并验证md5
                {
                    if (file == null)
                    {
                        UpdaterLog.LogError(string.Format("{0} 这个文件验证时发现应该存在,但实际不存在,将更新", name));
                        ret.Add(name, patch);
                    }
                    else
                    {
                        string fileMd5   = MD5Util.GetMD5ForFile(file.FullName);
                        string recordMd5 = patch.MD5;
                        if (!fileMd5.EqualIgnoreCase(recordMd5))
                        {
                            UpdaterLog.LogError(string.Format("{0} 这个文件验证的md5和记录的md5不一致{1}//{2},将更新", name, fileMd5, recordMd5));
                            ret.Add(name, patch);
                        }
                    }
                }
            }
            return(ret);
        }
Example #3
0
        void DownloadPatch(Dictionary <string, Patch> .Enumerator enumerator, int i, int totalToDownload)
        {
            if (!enumerator.MoveNext())
            {
                //下载完毕,将新版本号写入patchIndex,并保存
                _patchIndexLocal.Version      = VersionCode;
                _patchIndexLocal.PatchVersion = _patchIndexNew.PatchVersion;
                _patchIndexLocal.WriteToFile();
                ResManager.Ins.ReadDownloadDir();
                if (Listener != null)
                {
                    Listener.OnPatchDownloadFinish();
                }
                CheckUpdate();                //补丁更新结束后再次检查更新
                return;
            }
            i++;
            var patchKV = enumerator.Current;
            var name    = patchKV.Key;
            var patch   = patchKV.Value;

            if (patch.NeedDelete)              //如果这个补丁是删除更新,则删除
            {
                FileInfo file = UResources.ReqFile(name);
                if (file != null)
                {
                    File.Delete(file.FullName);
                }
                if (Listener != null)
                {
                    Listener.OnPatchDownloadProgress(name, 1, i, totalToDownload);
                }
                _patchIndexLocal.UpdatePatch(name, patch);
                _patchIndexLocal.WriteToFile();
                DownloadPatch(enumerator, i, totalToDownload);
                return;
            }
            //下载更新
            string url = String.Format(_patchIndexNew.PatchBaseURL, name, patch.Version);

            HttpManager.Get(url, (error, www) => {
                if (!string.IsNullOrEmpty(error))
                {
                    UpdaterLog.LogError("下载补丁包出错" + name + "  " + www.error + "  url:" + www.url);
                    if (Listener != null)
                    {
                        Listener.OnPatchDownloadFail(error);
                    }
                    return;
                }
                string filePath = ResManager.FullDownloadDir + name;
                File.WriteAllBytes(filePath, www.bytes);
                //下载完一个文件后立刻写入PatchIndex,这样中途断开网络后重新下载可以不下载该文件
                _patchIndexLocal.UpdatePatch(name, patch);
                _patchIndexLocal.WriteToFile();
                DownloadPatch(enumerator, i, totalToDownload);
                return;
            }, progress => {
                if (Listener != null)
                {
                    Listener.OnPatchDownloadProgress(name, progress, i, totalToDownload);
                }
            });
        }
Example #4
0
 public static T Load <T>(string path) where T : Object
 {
     return((T)((object)UResources.Load(path, typeof(T))));
 }
Example #5
0
 /// <summary>
 /// 加载物体
 /// </summary>
 /// <param name="path"></param>
 /// <returns></returns>
 public static Object Load(string path)
 {
     return(UResources.Load(path, typeof(Object)));
 }
Example #6
0
        private void CheckUpdate()
        {
            //检查是否有patchIndex文件,并读取内容
            var indexFile = UResources.ReqFile(INDEX_FILE_NAME);

            if (indexFile != null)
            {
                string fileJson = DESFileUtil.ReadAllText(indexFile.FullName, DESKey.DefaultKey, DESKey.DefaultIV);
                _patchIndexLocal = new PatchIndex(this.VersionCode, JObject.Parse(fileJson));
                if (_patchIndexLocal.Version != this.VersionCode)
                {
                    UpdaterLog.Log("发现patchIndex中的主版本不等于目前的主版本,说明刚经历过主包更新,清空补丁");
                    foreach (var file in ResManager.Ins.DownloadedFiles.Values)
                    {
                        File.Delete(file.FullName);
                    }
                    ResManager.Ins.ReadDownloadDir();
                    _patchIndexLocal = new PatchIndex();
                }
            }
            else
            {
                _patchIndexLocal = new PatchIndex();
                try {
                    //清空下载目录
                    Directory.Delete(ResManager.FullDownloadDir, true);
                } catch { }

                try {
                    // 重新创建目录,并写回PatchIndex
                    Directory.CreateDirectory(ResManager.FullDownloadDir);
                    _patchIndexLocal.WriteToFile();
                } catch (Exception e) {
                    Debug.LogException(e);
                }
            }

            //构建检查更新需要的表单
            JObject json = new JObject();

            json["Version"]      = this.VersionCode;
            json["PatchVersion"] = _patchIndexLocal.PatchVersion;
            if (CustomData != null)
            {
                foreach (var kv in CustomData)
                {
                    json[kv.Key.ToString()] = kv.Value.ToString();
                }
            }
            UpdaterLog.Log(json.ToFormatString());
            WWWForm form = new WWWForm();

            form.AddField("Data", json.ToString());

            HttpManager.Post(CheckUpdateUrl, form, (error, www) => {
                if (!string.IsNullOrEmpty(error))
                {
                    Listener.OnError(ErrorCode.NET_CONNECT_ERROR);
                    return;
                }
                UpdaterLog.Log(www.text);

                JObject responseJObj = JObject.Parse(www.text);
                int code             = responseJObj["code"].OptInt(ErrorCode.DEFAULT.Code);
                if (code == ErrorCode.SUCCESS.Code)
                {
                    _updateType = responseJObj["Type"].OptEnum <UpdateType>(UpdateType.NoUpdate);
                    switch (_updateType)
                    {
                    case UpdateType.NoUpdate:
                        _patchIndexNew = new PatchIndex(this.VersionCode, responseJObj);
                        //没有找到更新,验证文件
                        Listener.OnVerifyStart();
                        if (VerifyPatches())
                        {
                            Listener.OnVerifySuccess();
                            Listener.OnPassUpdate(responseJObj);
                        }
                        else
                        {
                            Listener.OnVerifyFail();
                        }
                        break;

                    case UpdateType.MainPak:
                        _mainPakIndexFromServer = new MainPakIndex(this.VersionCode, responseJObj);
                        Listener.OnFindMainUpdate(_mainPakIndexFromServer.Info);
                        break;

                    case UpdateType.Patch:
                        _patchIndexNew = new PatchIndex(this.VersionCode, responseJObj);
                        if (Listener != null)
                        {
                            Listener.OnFindPatchUpdate(_patchIndexNew.PatchInfo, _patchIndexNew.TotalPatchSize);
                        }
                        break;
                    }
                }
                else
                {
                    Listener.OnError(new ErrorCode(code, responseJObj["error"].OptObject()));
                }
            }, null, 15);
        }
Example #7
0
        private void CheckUpdate()
        {
            //检查是否有patchIndex文件,并读取内容
            var indexFile = UResources.ReqFile(INDEX_FILE_NAME);

            if (indexFile != null)
            {
                string fileJson = DESFileUtil.ReadAllText(indexFile.FullName, DESKey.DefaultKey, DESKey.DefaultIV);
                _patchIndexLocal = new PatchIndex(this.VersionCode, JObject.Parse(fileJson));
                if (_patchIndexLocal.Version != this.VersionCode)
                {
                    UpdaterLog.Log("发现patchIndex中的主版本不等于目前的主版本,说明刚经历过主包更新,清空补丁");
                    foreach (var file in ResManager.Ins.DownloadedFiles.Values)
                    {
                        File.Delete(file.FullName);
                    }
                    ResManager.Ins.ReadDownloadDir();
                    _patchIndexLocal = new PatchIndex();
                }
            }
            else
            {
                _patchIndexLocal = new PatchIndex();
            }

            //构建检查更新需要的表单
            JObject json = new JObject();

            json["Version"]      = this.VersionCode;
            json["PatchVersion"] = _patchIndexLocal.PatchVersion;
            if (CustomData != null)
            {
                foreach (var kv in CustomData)
                {
                    json[kv.Key.ToString()] = kv.Value.ToString();
                }
            }
            WWWForm form = new WWWForm();

            form.AddField("Data", json.ToString());

            HttpManager.Post(CheckUpdateUrl, form, (error, www) => {
                if (!string.IsNullOrEmpty(error))
                {
                    Listener.OnError(error);
                    return;
                }
                UpdaterLog.Log(www.text);

                JObject responseJObj = JObject.Parse(www.text);
                _updateType          = (UpdateType)(responseJObj["Type"].OptInt(0));

                switch (_updateType)
                {
                case UpdateType.NoUpdate:
                    _patchIndexNew = new PatchIndex(this.VersionCode, responseJObj);
                    //没有找到更新,验证文件
                    Listener.OnVerifyStart();
                    if (VerifyPatches())
                    {
                        Listener.OnVerifySuccess();
                        Listener.OnPassUpdate();
                    }
                    else
                    {
                        Listener.OnVerifyFail();
                    }
                    break;

                case UpdateType.MainPak:
                    _mainPakIndexFromServer = new MainPakIndex(this.VersionCode, responseJObj);
                    Listener.OnFindMainUpdate(_mainPakIndexFromServer.Info);
                    break;

                case UpdateType.Patch:
                    _patchIndexNew = new PatchIndex(this.VersionCode, responseJObj);
                    if (Listener != null)
                    {
                        Listener.OnFindPatchUpdate(_patchIndexNew.PatchInfo, _patchIndexNew.TotalPatchSize);
                    }
                    break;
                }
            });
        }