[#] 修改当前备份逻辑,相同版本dll只做一次备份!

原因: 可选功能补丁允许在非原版dll上做多次安装补丁操作。
This commit is contained in:
huiyadanli 2020-07-19 17:06:32 +08:00
parent fed166fa2b
commit 5ed38573de
4 changed files with 39 additions and 18 deletions

View File

@ -78,7 +78,7 @@
this.btnPatch.Name = "btnPatch"; this.btnPatch.Name = "btnPatch";
this.btnPatch.Size = new System.Drawing.Size(98, 23); this.btnPatch.Size = new System.Drawing.Size(98, 23);
this.btnPatch.TabIndex = 3; this.btnPatch.TabIndex = 3;
this.btnPatch.Text = "安装补丁"; this.btnPatch.Text = "安装补丁";
this.btnPatch.UseVisualStyleBackColor = true; this.btnPatch.UseVisualStyleBackColor = true;
this.btnPatch.Click += new System.EventHandler(this.btnPatch_Click); this.btnPatch.Click += new System.EventHandler(this.btnPatch_Click);
// //

View File

@ -1,4 +1,5 @@
using RevokeMsgPatcher.Model; using RevokeMsgPatcher.Model;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -6,15 +7,16 @@ namespace RevokeMsgPatcher.Matcher
{ {
public class ModifyFinder public class ModifyFinder
{ {
// TODO 该逻辑需要优化!
public static List<Change> FindChanges(string path, List<ReplacePattern> replacePatterns) public static List<Change> FindChanges(string path, List<ReplacePattern> replacePatterns)
{ {
// 读取整个文件(dll) // 读取整个文件(dll)
byte[] fileByteArray = File.ReadAllBytes(path); byte[] fileByteArray = File.ReadAllBytes(path);
List<Change> changes = new List<Change>(); List<Change> changes = new List<Change>(); // 匹配且需要替换的地方
// 查找所有替换点 // 查找所有替换点
int matchNum = 0; int matchNum = 0; // 匹配数量
foreach (ReplacePattern pattern in replacePatterns) foreach (ReplacePattern pattern in replacePatterns)
{ {
// 所有的匹配点位 // 所有的匹配点位
@ -33,13 +35,24 @@ namespace RevokeMsgPatcher.Matcher
// 匹配数和期望的匹配数不一致时报错(当前一个特征只会出现一次) // 匹配数和期望的匹配数不一致时报错(当前一个特征只会出现一次)
if (matchNum != replacePatterns.Count) if (matchNum != replacePatterns.Count)
{ {
if (IsAllReplaced(fileByteArray, replacePatterns)) Tuple<bool, SortedSet<string>> res = IsAllReplaced(fileByteArray, replacePatterns);
if (res.Item1)
{ {
throw new BusinessException("match_already_replace", "特征比对:当前应用已经安装了对应功能的补丁!"); throw new BusinessException("match_already_replace", "特征比对:当前应用已经安装了对应功能的补丁!");
} }
else 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 else
@ -50,11 +63,11 @@ namespace RevokeMsgPatcher.Matcher
// 此逻辑在当前特征配置下不会进入,因为查找串和替换串当前全部都是不相同的 // 此逻辑在当前特征配置下不会进入,因为查找串和替换串当前全部都是不相同的
if (changes.Count == 0) if (changes.Count == 0)
{ {
throw new BusinessException("match_already_replace", "特征比对:当前应用已经安装了防撤回补丁!"); throw new BusinessException("match_already_replace", "特征比对:当前应用已经安装了所选功能补丁!");
} }
else 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<ReplacePattern> replacePatterns) private static Tuple<bool, SortedSet<string>> IsAllReplaced(byte[] fileByteArray, List<ReplacePattern> replacePatterns)
{ {
int matchNum = 0; int matchNum = 0;
SortedSet<string> alreadyReplaced = new SortedSet<string>(); // 已经被替换特征的功能
foreach (ReplacePattern pattern in replacePatterns) foreach (ReplacePattern pattern in replacePatterns)
{ {
// 所有的匹配点位 // 所有的匹配点位
@ -76,16 +90,10 @@ namespace RevokeMsgPatcher.Matcher
if (matchIndexs.Length == 1) if (matchIndexs.Length == 1)
{ {
matchNum++; matchNum++;
alreadyReplaced.Add(pattern.Category);
} }
} }
if (matchNum == replacePatterns.Count) return new Tuple<bool, SortedSet<string>>(matchNum == replacePatterns.Count, alreadyReplaced);
{
return true;
}
else
{
return false;
}
} }
} }
} }

View File

@ -345,6 +345,7 @@ namespace RevokeMsgPatcher.Modifier
bool success = editor.Patch(); bool success = editor.Patch();
if (!success) if (!success)
{ {
// 此处还原逻辑不可能进入
editor.Restore(); editor.Restore();
} }
else else
@ -369,7 +370,7 @@ namespace RevokeMsgPatcher.Modifier
{ {
foreach (FileHexEditor editor in editors) foreach (FileHexEditor editor in editors)
{ {
if (!File.Exists(editor.FileBakPath)) if (!File.Exists(editor.FileBakPath) || editor.FileVersion != editor.BackupFileVersion)
{ {
return false; return false;
} }

View File

@ -80,7 +80,19 @@ namespace RevokeMsgPatcher.Modifier
/// </summary> /// </summary>
public void Backup() 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);
}
} }
/// <summary> /// <summary>