Esempio n. 1
0
        private void btnPatch_Click(object sender, EventArgs e)
        {
            if (!modifier.IsAllFilesExist(txtPath.Text))
            {
                MessageBox.Show("请选择正确的安装路径!");
                return;
            }

            // 记录点了什么应用的防撤回
            string enName  = GetCheckedRadioButtonNameEn(); // 应用英文名
            string version = modifier.GetVersion();         // 应用版本

            ga.RequestPageView($"{enName}/{version}/patch", "点击防撤回");

            EnableAllButton(false);
            PatchType type = PatchType.Accurate; // 两种打补丁的方式:精准(指定位置替换)、通用(特征码替换)

            // a.重新初始化编辑器
            modifier.InitEditors(txtPath.Text);
            // b.计算SHA1,验证文件完整性,寻找对应的补丁信息(精确版本、通用特征码两种补丁信息)
            try
            {
                modifier.ValidateAndFindModifyInfo();
            }
            catch (BusinessException ex)
            {
                if ((ex.ErrorCode == "not_support" || ex.ErrorCode == "maybe_modified") && modifier.EditorsHasCommonModifyInfos())
                {
                    // 存在特征码修改替换信息的情况下,尝试使用特征码替换
                    type = PatchType.Common;
                }
                else
                {
                    ga.RequestPageView($"{enName}/{version}/patch/sha1/ex/{ex.ErrorCode}", ex.Message);
                    MessageBox.Show(ex.Message);
                    EnableAllButton(true);
                    btnRestore.Enabled = modifier.BackupExists();
                    return;
                }
            }
            catch (IOException ex)
            {
                ga.RequestPageView($"{enName}/{version}/patch/sha1/ex/{ex.HResult.ToString("x4")}", ex.Message);
                MessageBox.Show(ex.Message + " 请以管理员权限启动本程序,并确认当前应用(微信/QQ/TIM)处于关闭状态。");
                EnableAllButton(true);
                btnRestore.Enabled = modifier.BackupExists();
                return;
            }
            catch (Exception ex)
            {
                ga.RequestPageView($"{enName}/{version}/patch/sha1/ex/{ex.HResult.ToString("x4")}", ex.Message);
                MessageBox.Show(ex.Message);
                EnableAllButton(true);
                btnRestore.Enabled = modifier.BackupExists();
                return;
            }

            // c.打补丁
            try
            {
                modifier.Patch(type);
                ga.RequestPageView($"{enName}/{version}/patch/succ", "防撤回成功");
                MessageBox.Show("补丁安装成功!");
            }
            catch (BusinessException ex)
            {
                Console.WriteLine(ex.Message);
                ga.RequestPageView($"{enName}/{version}/patch/ex/{ex.ErrorCode}", ex.Message);
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                ga.RequestPageView($"{enName}/{version}/patch/ex/{ex.HResult.ToString("x4")}", ex.Message);
                MessageBox.Show(ex.Message + " 请以管理员权限启动本程序,并确认当前应用(微信/QQ/TIM)处于关闭状态。");
            }
            finally
            {
                EnableAllButton(true);
                btnRestore.Enabled = modifier.BackupExists();
            }
        }