From 5ed38573dee6d166d97ce0d07005528fe0dd52de Mon Sep 17 00:00:00 2001 From: huiyadanli Date: Sun, 19 Jul 2020 17:06:32 +0800 Subject: [PATCH] =?UTF-8?q?[#]=20=E4=BF=AE=E6=94=B9=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E5=A4=87=E4=BB=BD=E9=80=BB=E8=BE=91=EF=BC=8C=E7=9B=B8=E5=90=8C?= =?UTF-8?q?=E7=89=88=E6=9C=ACdll=E5=8F=AA=E5=81=9A=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E5=A4=87=E4=BB=BD=EF=BC=81=20=E5=8E=9F=E5=9B=A0:=20=E5=8F=AF?= =?UTF-8?q?=E9=80=89=E5=8A=9F=E8=83=BD=E8=A1=A5=E4=B8=81=E5=85=81=E8=AE=B8?= =?UTF-8?q?=E5=9C=A8=E9=9D=9E=E5=8E=9F=E7=89=88dll=E4=B8=8A=E5=81=9A?= =?UTF-8?q?=E5=A4=9A=E6=AC=A1=E5=AE=89=E8=A3=85=E8=A1=A5=E4=B8=81=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RevokeMsgPatcher/FormMain.Designer.cs | 2 +- RevokeMsgPatcher/Matcher/ModifyFinder.cs | 38 +++++++++++++--------- RevokeMsgPatcher/Modifier/AppModifier.cs | 3 +- RevokeMsgPatcher/Modifier/FileHexEditor.cs | 14 +++++++- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/RevokeMsgPatcher/FormMain.Designer.cs b/RevokeMsgPatcher/FormMain.Designer.cs index b638844..66d009e 100644 --- a/RevokeMsgPatcher/FormMain.Designer.cs +++ b/RevokeMsgPatcher/FormMain.Designer.cs @@ -78,7 +78,7 @@ this.btnPatch.Name = "btnPatch"; this.btnPatch.Size = new System.Drawing.Size(98, 23); this.btnPatch.TabIndex = 3; - this.btnPatch.Text = "安装补丁"; + this.btnPatch.Text = "安装补丁!"; this.btnPatch.UseVisualStyleBackColor = true; this.btnPatch.Click += new System.EventHandler(this.btnPatch_Click); // diff --git a/RevokeMsgPatcher/Matcher/ModifyFinder.cs b/RevokeMsgPatcher/Matcher/ModifyFinder.cs index dc4d8fc..4bb75cd 100644 --- a/RevokeMsgPatcher/Matcher/ModifyFinder.cs +++ b/RevokeMsgPatcher/Matcher/ModifyFinder.cs @@ -1,4 +1,5 @@ using RevokeMsgPatcher.Model; +using System; using System.Collections.Generic; using System.IO; @@ -6,15 +7,16 @@ namespace RevokeMsgPatcher.Matcher { public class ModifyFinder { + // TODO 该逻辑需要优化! public static List FindChanges(string path, List replacePatterns) { // 读取整个文件(dll) byte[] fileByteArray = File.ReadAllBytes(path); - List changes = new List(); + List changes = new List(); // 匹配且需要替换的地方 // 查找所有替换点 - int matchNum = 0; + int matchNum = 0; // 匹配数量 foreach (ReplacePattern pattern in replacePatterns) { // 所有的匹配点位 @@ -33,13 +35,24 @@ namespace RevokeMsgPatcher.Matcher // 匹配数和期望的匹配数不一致时报错(当前一个特征只会出现一次) if (matchNum != replacePatterns.Count) { - if (IsAllReplaced(fileByteArray, replacePatterns)) + Tuple> res = IsAllReplaced(fileByteArray, replacePatterns); + if (res.Item1) { throw new BusinessException("match_already_replace", "特征比对:当前应用已经安装了对应功能的补丁!"); } else { - throw new BusinessException("match_inconformity", $"特征比对:当前特征码匹配数[{matchNum}]和期望的匹配数[{replacePatterns.Count}]不一致,如果当前版本为新版本,特征码可能出现变化,请联系作者处理!"); + if (res.Item2.Count > 0) + { + throw new BusinessException("match_inconformity", $"特征比对:以下功能补丁已经安装,请取消勾选!\n已安装功能:【{string.Join("、", res.Item2)}】"); + } + else + { + throw new BusinessException("match_inconformity", $"特征比对:当前特征码匹配数[{matchNum}]和期望的匹配数[{replacePatterns.Count}]不一致。\n" + + $"出现此种情况的一般有如下可能:\n" + + $"1. 你可能已经安装了某个功能的补丁,请选择未安装功能进行安装。\n" + + $"2. 如果当前版本为最新版本,特征码可能出现变化(可能性比较低),请联系作者处理。"); + } } } else @@ -50,11 +63,11 @@ namespace RevokeMsgPatcher.Matcher // 此逻辑在当前特征配置下不会进入,因为查找串和替换串当前全部都是不相同的 if (changes.Count == 0) { - throw new BusinessException("match_already_replace", "特征比对:当前应用已经安装了防撤回补丁!"); + throw new BusinessException("match_already_replace", "特征比对:当前应用已经安装了所选功能补丁!"); } else { - throw new BusinessException("match_part_replace", "特征比对:部分特征已经被替换,请确认是否有使用过其他防撤回补丁!"); + throw new BusinessException("match_part_replace", "特征比对:部分特征已经被替换,请确认是否有使用过其他防撤回/多开补丁!"); } } @@ -66,9 +79,10 @@ namespace RevokeMsgPatcher.Matcher } } - private static bool IsAllReplaced(byte[] fileByteArray, List replacePatterns) + private static Tuple> IsAllReplaced(byte[] fileByteArray, List replacePatterns) { int matchNum = 0; + SortedSet alreadyReplaced = new SortedSet(); // 已经被替换特征的功能 foreach (ReplacePattern pattern in replacePatterns) { // 所有的匹配点位 @@ -76,16 +90,10 @@ namespace RevokeMsgPatcher.Matcher if (matchIndexs.Length == 1) { matchNum++; + alreadyReplaced.Add(pattern.Category); } } - if (matchNum == replacePatterns.Count) - { - return true; - } - else - { - return false; - } + return new Tuple>(matchNum == replacePatterns.Count, alreadyReplaced); } } } diff --git a/RevokeMsgPatcher/Modifier/AppModifier.cs b/RevokeMsgPatcher/Modifier/AppModifier.cs index 1b6c06f..699bf0f 100644 --- a/RevokeMsgPatcher/Modifier/AppModifier.cs +++ b/RevokeMsgPatcher/Modifier/AppModifier.cs @@ -345,6 +345,7 @@ namespace RevokeMsgPatcher.Modifier bool success = editor.Patch(); if (!success) { + // 此处还原逻辑不可能进入 editor.Restore(); } else @@ -369,7 +370,7 @@ namespace RevokeMsgPatcher.Modifier { foreach (FileHexEditor editor in editors) { - if (!File.Exists(editor.FileBakPath)) + if (!File.Exists(editor.FileBakPath) || editor.FileVersion != editor.BackupFileVersion) { return false; } diff --git a/RevokeMsgPatcher/Modifier/FileHexEditor.cs b/RevokeMsgPatcher/Modifier/FileHexEditor.cs index 40be54c..6dbea11 100644 --- a/RevokeMsgPatcher/Modifier/FileHexEditor.cs +++ b/RevokeMsgPatcher/Modifier/FileHexEditor.cs @@ -80,7 +80,19 @@ namespace RevokeMsgPatcher.Modifier /// public void Backup() { - File.Copy(FilePath, FileBakPath, true); + // 不覆盖同版本的备份文件 + if (File.Exists(FileBakPath)) + { + if (FileVersion != BackupFileVersion) + { + File.Copy(FilePath, FileBakPath, true); + } + } + else + { + File.Copy(FilePath, FileBakPath, true); + } + } ///