/// <summary> /// b.验证文件完整性,寻找对应的补丁信息 /// </summary> public void ValidateAndFindModifyInfo() { // 寻找对应文件版本与SHA1的修改信息 foreach (FileHexEditor editor in editors) // 多种文件 { // 通过SHA1和文件版本判断是否可以打补丁 根据不同结果返回不同的提示 ModifyInfo matchingSHA1Before = null, matchingSHA1After = null, matchingVersion = null; foreach (ModifyInfo modifyInfo in config.FileModifyInfos[editor.FileName]) // 多个版本信息 { if (modifyInfo.Name == editor.FileName) // 保险用的无用判断 { if (editor.FileSHA1 == modifyInfo.SHA1After) { matchingSHA1After = modifyInfo; } else if (editor.FileSHA1 == modifyInfo.SHA1Before) { matchingSHA1Before = modifyInfo; } if (editor.FileVersion == modifyInfo.Version) { matchingVersion = modifyInfo; } } } // 补丁前SHA1匹配上,肯定是正确的dll if (matchingSHA1Before != null) { editor.FileModifyInfo = matchingSHA1Before; editor.TargetChanges = matchingSHA1Before.Changes; continue; } // 补丁后SHA1匹配上,肯定已经打过补丁 if (matchingSHA1After != null) { throw new BusinessException("installed", $"你已经安装过此补丁!"); } // SHA1不匹配说明精准替换肯定不支持 if (matchingSHA1Before == null && matchingSHA1After == null) { // 多个版本范围,匹配出对应版本可以使用的特征 if (config.FileCommonModifyInfos != null) { editor.FileCommonModifyInfo = FindCommonModifyInfo(editor); } // 存在对应的特征时不报错 if (editor.FileCommonModifyInfo != null && editor.FileCommonModifyInfo.ReplacePatterns != null) { // 如果能顺利得到 TargetChanges 不报错则可以使用特征替换方式 editor.TargetChanges = ModifyFinder.FindChanges(editor.FilePath, editor.FileCommonModifyInfo.ReplacePatterns); continue; } else { // SHA1不匹配,连版本也不匹配,说明完全不支持 if (matchingVersion == null) { throw new BusinessException("not_support", $"不支持此版本:{editor.FileVersion}!"); } // SHA1不匹配,但是版本匹配,可能dll已经被其他补丁程序修改过 if (matchingVersion != null) { throw new BusinessException("maybe_modified", $"程序支持此版本:{editor.FileVersion}。但是文件校验不通过,请确认是否使用过其他补丁程序!"); } } } } }
/// <summary> /// 操作版本号显示控件的内容和样式 /// </summary> /// <param name="label">显示版本的控件</param> public void SetVersionLabelAndCategoryCategories(Label label, Panel panel) { string version = GetVersion(); // 补丁信息中是否都有对应的版本 int i = 0, j = 0; // 精确版本匹配 foreach (FileHexEditor editor in editors) // 多种文件 { // 精确版本匹配 bool haven = false; foreach (ModifyInfo modifyInfo in config.FileModifyInfos[editor.FileName]) // 多个版本信息 { if (editor.FileVersion == modifyInfo.Version) { haven = true; break; } } if (haven) { i++; } } if (i == editors.Count) { label.Text = version + "(已支持)"; label.ForeColor = Color.Green; UIController.AddMsgToPanel(panel, "只有基于特征的补丁才能选择功能"); return; } // 模糊版本匹配(特征码) // 特征码匹配的时候的可选功能项 SortedSet <string> categories = new SortedSet <string>(); SortedSet <string> installed = new SortedSet <string>(); foreach (FileHexEditor editor in editors) // 多种文件 { // 匹配出对应版本是否有可以使用的特征 if (config.FileCommonModifyInfos != null) { bool inRange = false; foreach (CommonModifyInfo commonModifyInfo in config.FileCommonModifyInfos[editor.FileName]) { // editor.FileVersion 在 StartVersion 和 EndVersion 之间 if (IsInVersionRange(editor.FileVersion, commonModifyInfo.StartVersion, commonModifyInfo.EndVersion)) { // 取出特征码的功能类型 foreach (string c in commonModifyInfo.GetCategories()) { if (c != null) { categories.Add(c); } } // 获取已经安装过的功能类型 SortedSet <string> replaced = ModifyFinder.FindReplacedFunction(editor.FilePath, commonModifyInfo.ReplacePatterns); foreach (string c in replaced) { installed.Add(c); } inRange = true; break; } } if (inRange) { j++; } } } // 全部都有对应匹配的版本 if (j == editors.Count) { label.Text = version + "(支持特征防撤回)"; label.ForeColor = Color.LimeGreen; UIController.AddCategoryCheckBoxToPanel(panel, categories.ToArray(), installed.ToArray()); } else { label.Text = version + "(不支持)"; label.ForeColor = Color.Red; UIController.AddMsgToPanel(panel, "无功能选项"); } }