导读

[c#] 万能程序补丁工具

[复制链接]

微信扫一扫 分享朋友圈

Ss渔歌123 发表于 2026-1-14 16:09:14 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

马上注册,结交更多易友,享用更多功能,让你轻松玩转觅风论坛。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
//附件源码里的代码是全的,这上面代码不全,要求在10000字以内
namespace 万能程序补丁工具
{
    public partial class Form1 : Form
    {
        string TargetFileName = null;
        string TargetFilePathName = null;
        OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
        string[] SourceHex = null;
        string[] PatchHex = null;
        bool ConfBool = false;
        public Form1()
        {
            InitializeComponent();
        }
        private byte[] ReplaceBytes(byte[] original, byte[] toReplace, byte[] replacement)
        {
            if (original == null) throw new ArgumentNullException(nameof(original));
            if (toReplace == null) throw new ArgumentNullException(nameof(toReplace));
            if (replacement == null) throw new ArgumentNullException(nameof(replacement));
            using (var memoryStream = new MemoryStream())
            {
                int index = 0;
                int searchLength = toReplace.Length;
                while ((index = Array.IndexOf(original, toReplace[0], index)) >= 0)
                {
                    // 寻找匹配项
                    if (searchLength <= original.Length - index)
                    {
                        bool match = true;
                        for (int i = 1; i < searchLength; i++)
                        {
                            if (original[index + i] != toReplace[i])
                            {
                                match = false;
                                break;
                            }
                        }

                        if (match)
                        {                     
                            memoryStream.Write(original, index, index - (int)memoryStream.Position);                        
                            memoryStream.Write(replacement, 0, replacement.Length);                        
                            index += searchLength;
                            continue;
                        }
                    }         
                    memoryStream.WriteByte(original[index++]);
                }        
                if (index < original.Length)
                {
                    memoryStream.Write(original, index, original.Length - index);
                }

                return memoryStream.ToArray();
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            string ConfigFile = Application.StartupPath + "\\config.prg";//dynamic ConfigFile = Application.StartupPath + "\\config.prg";        
            OpenConfigFile(ConfigFile);
        }
        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (!ConfBool)
                {
                    MessageBox.Show("没有默认配置文件 config.prg\r\n鼠标右键加载 *.prg 配置文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                OpenFileDialog1.Filter = "可执行文件(*.exe)|*.exe|库文件(*.dll)|*.dll|所有文件(*.*)|*.*";
                OpenFileDialog1.FilterIndex = 1;
                OpenFileDialog1.Title = "打开目标文件";
                OpenFileDialog1.FileName = TargetFileName;
                OpenFileDialog1.InitialDirectory = System.IO.Directory.GetCurrentDirectory(); //首选当前目录
                if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        TargetFilePathName = OpenFileDialog1.FileName; //完整目标文件名
                        int Pos = TargetFilePathName.LastIndexOf(".");
                        string CrackFilePathName = TargetFilePathName.Substring(0, Pos) + "-PJ" + TargetFilePathName.Substring(Pos); //完整备份文件名

                        byte[] FSData = File.ReadAllBytes(TargetFilePathName); //目标文件读入字节数组

                        //Rename(TargetFilePathName, BakFilePathName) '目标文件改名备份
                        bool BytesChange = false;
                        int PatchNum = SourceHex.Length - 1;
                        byte[] PatchByte = null;
                        for (int i = 1; i <= PatchNum; i++)
                        {
                            string[] HexStr = SourceHex[i].Split(' ');
                            int HexNum = HexStr.Length - 1;
                            Array.Resize(ref SourceByte, HexNum + 1);
                            Array.Resize(ref PatchByte, HexNum + 1);
                            for (int j = 0; j <= HexNum; j++)
                            {
                                // SourceByte[j] = byte.Parse("&H" + HexStr[j]);
                                SourceByte[j] = Convert.ToByte(HexStr[j], 16);
                            }
                            HexStr = PatchHex[i].Split(' ');
                            for (int k = 0; k <= HexNum; k++)
                            {
                                //PatchByte[k] = byte.Parse("&H" + HexStr[k]);
                                PatchByte[k] = Convert.ToByte(HexStr[k], 16);
                            }
                            int Index = IndexOf(FSData, SourceByte);
                                                            //Console.WriteLine(i)
                            if (Index != -1) //特征码匹配
                            {
                                int P = 0;
                                int Indexs = Index + PatchByte.Length - 1;
                                for (int m = Index; m <= Indexs; m++)
                                {
                                    FSData[m] = PatchByte[P]; //替换特征码
                                    P += 1;
                                }
                                BytesChange = true;
                            }
                            //Console.WriteLine("2=" & i)
                        }
                        if (BytesChange)
                        {
                            File.WriteAllBytes(CrackFilePathName, FSData);
                            MessageBox.Show("程序保存在相同目录下,文件为:\r\n" + Path.GetFileName(CrackFilePathName), "完成程序补丁", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("prg文件特征码补丁,不符合本程序!", "程序补丁失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("配置文件中错误:\r\n1、原始特征码和补丁特征码不匹配,不符合设定!\r\n2、特征码十六进制空格或位数不符合设定!\r\n\r\n" + ex.StackTrace, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                }
            }
            if (e.Button == MouseButtons.Middle)
            {
                Form About = new About();
                About.Show();
            }
            if (e.Button == MouseButtons.Right)
            {
                //鼠标右键打开或更改配置文件
                //定义打开对话框属性
                OpenFileDialog1.Filter = "目标配置文件(*.prg)|*.prg";
                OpenFileDialog1.FilterIndex = 1;
                OpenFileDialog1.Title = "更改目标配置文件";
                OpenFileDialog1.InitialDirectory = Environment.CurrentDirectory;
                //首选当前目录
                if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    TargetFilePathName = OpenFileDialog1.FileName;
                    OpenConfigFile(TargetFilePathName);
                }
            }
        }
        /// <summary>打开或更改配置文件</summary>
        /// <param name="ConfigFilePathName">配置文件含完整目录全称</param>
        private void OpenConfigFile(string ConfigFilePathName)
        {
            if (File.Exists(ConfigFilePathName))
            {
                try
                {
                    StreamReader TargetFile = new StreamReader(ConfigFilePathName, Encoding.UTF8);
                    int i = 0;
                    SourceHex = null;
                    PatchHex = null;
                    while (TargetFile.Peek() > 0)
                    {
                        Array.Resize(ref SourceHex, i + 1);
                        Array.Resize(ref PatchHex, i + 1);
                        SourceHex[i] = TargetFile.ReadLine().Trim();
                        //读行
                        PatchHex[i] = TargetFile.ReadLine().Trim();
                        i += 1;
                    }
                    TargetFile.Dispose();
                    //注销文件流
                    TargetFileName = SourceHex[0];
                    //目标文件名
                    string TargetFileVer = PatchHex[0];
                    //目标版本
                    this.Text = "万用特征码补丁器 [" + TargetFileName + " " + TargetFileVer + "]";
                    ConfBool = true;
                }
                catch (Exception ex)
                {
                    ConfBool = false;
                    MessageBox.Show("配置文件错误:\r\n配置文件非法或不符合设定!\r\n\r\n" + ex.StackTrace, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                this.Text = "万用特征码补丁器 [无配置文件 Config.prg]";
                ConfBool = false;
            }
        }
     
回复

使用道具 举报

精彩评论4

 楼主| Ss渔歌123 发表于 2026-1-14 16:11:51 | 显示全部楼层
附件里的源码是全的,上面的代码由于字数限制篇幅不全

万能程序补丁和谐特征码文件.rar

119.55 KB, 下载积分: 下载币 -1 , 觅币 -2

售价: 7 觅币  [购买]

回复 支持 反对

使用道具 举报

q13129841235 发表于 4 天前 | 显示全部楼层
支持一下,期待更多东西
回复 支持 反对

使用道具 举报

呜呜呜 发表于 4 天前 | 显示全部楼层
好好好好的我要下载看看看
回复 支持 反对

使用道具 举报

lijianan12300 发表于 4 天前 | 显示全部楼层
我要下载试试,我要下载试试...
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注我们:觅风论坛与你快乐分享

收藏本站

用心服务做个非盈利公益编程网站

www.eyyba.com

服务人:觅风论坛

Email:eyyba@foxmail.com

Powered by WWW.EYYBA.COM X3.4© 2001-2023 Inc.   版权所有   

觅风论坛  疆ICP备15020893号-1