예제 #1
0
        /// <summary>
        /// 通用(特征码替换)
        /// </summary>
        public void CommonPatch()
        {
            if (FileCommonModifyInfo == null)
            {
                throw new Exception("特征码替换:缺失对应特征码信息");
            }
            // 1. 拷贝一份临时文件
            File.Copy(FilePath, fileReplacedPath, true);
            // 2. 读取整个临时文件
            byte[] fileByteArray = File.ReadAllBytes(fileReplacedPath);
            // 3. 循环查找所有未替换的替换点
            int           needReplaceNum = 0;
            List <Change> changes        = new List <Change>();

            foreach (ReplacePattern pattern in FileCommonModifyInfo.ReplacePatterns)
            {
                int[] indexs = FuzzyMatcher.MatchNotReplaced(fileByteArray, pattern.Search, pattern.Replace);
                if (indexs.Length == 1)
                {
                    needReplaceNum++;
                    changes.Add(new Change(indexs[0], pattern.Replace));
                }
            }
            // 判断是否可以使用特征码替换的方式
            if (needReplaceNum == 0)
            {
                // 查找所有替换点
                int matchNum = 0;
                foreach (ReplacePattern pattern in FileCommonModifyInfo.ReplacePatterns)
                {
                    int[] indexs = FuzzyMatcher.MatchAll(fileByteArray, pattern.Search);
                    if (indexs.Length == 1)
                    {
                        matchNum++;
                    }
                }
                if (matchNum == FileCommonModifyInfo.ReplacePatterns.Count)
                {
                    throw new BusinessException("already_replace", "特征码替换:当前应用已经防撤回");
                }
                else
                {
                    throw new BusinessException("not_found_to_replace", "特征码替换:没有搜索到撤回的相关特征");
                }
            }
            else if (needReplaceNum == FileCommonModifyInfo.ReplacePatterns.Count)
            {
                // 正常情况下每个替换点都能找到
                // 3. 替换所有未替换的替换点
                FileUtil.EditMultiHex(fileReplacedPath, changes);
                // 4. 覆盖特征码替换后的文件
                File.Copy(fileReplacedPath, FilePath, true);
            }
            else
            {
                throw new BusinessException("found_num_err", "特征码替换:可替换的特征数不正确");
            }
        }