예제 #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();
            }
        }
예제 #2
0
        /// <summary>
        /// 比较两个IndexJson中的Patch,过滤出需要更新的Patch
        /// </summary>
        /// <param name="local"></param>
        /// <param name="server"></param>
        /// <returns></returns>
        public static Dictionary <string, Patch> FilterUpdatePatch(PatchIndex local, PatchIndex server)
        {
            var ret = new Dictionary <string, Patch>();

            //比较本地和服务器patch,过滤需要更新的patch
            foreach (var serverPatchKV in server.Patches)
            {
                var serverPatchName = serverPatchKV.Key;
                var serverPatch     = serverPatchKV.Value;
                if (!local.Patches.ContainsKey(serverPatchName))
                {
                    ret.Add(serverPatchName, serverPatch);
                }
                else                            //本地已经有这个补丁
                {
                    if (serverPatch.NeedDelete) //如果是一个删除补丁,则需要更新(删除)
                    {
                        ret.Add(serverPatchName, serverPatch);
                    }
                    else                        //不是删除补丁,则比较版本号
                    {
                        var localPatch = local.Patches[serverPatchName];
                        if (localPatch.Version < serverPatch.Version)                          //版本较低,则需要更新
                        {
                            ret.Add(serverPatchName, serverPatch);
                        }
                    }
                }
            }
            return(ret);
        }
예제 #3
0
        /// <summary>
        /// 开始下载补丁包
        /// </summary>
        public void GotoPatchUpdate()
        {
            if (_patchIndexNew != null && _patchIndexLocal != null)
            {
                //比较服务器patchIndex和本地patchIndex得到需要更新的文件
                Dictionary <string, Patch> patchToDownload;
                if (_updateType == UpdateType.NoUpdate)                  //如果更新结果为,没有更新,表示文件验证失败,直接使用patches
                {
                    patchToDownload = _patchIndexNew.Patches;
                }
                else
                {
                    patchToDownload = PatchIndex.FilterUpdatePatch(_patchIndexLocal, _patchIndexNew);
                }

                var enumerator = patchToDownload.GetEnumerator();
                DownloadPatch(enumerator, 0, patchToDownload.Count);
            }
        }
예제 #4
0
        private void CheckUpdate()
        {
            //检查是否有patchIndex文件,并读取内容
            Init(this.VersionCode);

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

            json["version"]      = this.VersionCode;
            json["patchVersion"] = _patchIndexLocal.PatchVersion;
            if (CustomData != null)
            {
                foreach (var kv in CustomData)
                {
                    json[kv.Name] = kv;
                }
            }
            WWWForm form = new WWWForm();

            form.AddField("P", 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);
                if (responseJObj["E"].GetString() != null)
                {
                    Listener?.OnError(responseJObj["E"].AsString());
                    return;
                }
                responseJObj = responseJObj["P"].OptObject();
                _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;
                }
            });
        }
예제 #5
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);
        }
예제 #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();
            }

            //构建检查更新需要的表单
            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;
                }
            });
        }