Ss渔歌123 发表于 2026-1-14 16:09:14

万能程序补丁工具

//附件源码里的代码是全的,这上面代码不全,要求在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, index)) >= 0)
                {
                  // 寻找匹配项
                  if (searchLength <= original.Length - index)
                  {
                        bool match = true;
                        for (int i = 1; i < searchLength; i++)
                        {
                            if (original != toReplace)
                            {
                              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);
                }      
                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.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 = byte.Parse("&H" + HexStr);
                              SourceByte = Convert.ToByte(HexStr, 16);
                            }
                            HexStr = PatchHex.Split(' ');
                            for (int k = 0; k <= HexNum; k++)
                            {
                              //PatchByte = byte.Parse("&H" + HexStr);
                              PatchByte = Convert.ToByte(HexStr, 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 = PatchByte; //替换特征码
                                    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 = TargetFile.ReadLine().Trim();
                        //读行
                        PatchHex = TargetFile.ReadLine().Trim();
                        i += 1;
                  }
                  TargetFile.Dispose();
                  //注销文件流
                  TargetFileName = SourceHex;
                  //目标文件名
                  string TargetFileVer = PatchHex;
                  //目标版本
                  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;
            }
      }
   

Ss渔歌123 发表于 2026-1-14 16:11:51

附件里的源码是全的,上面的代码由于字数限制篇幅不全

q13129841235 发表于 2026-1-19 09:40:12

支持一下,期待更多东西

呜呜呜 发表于 2026-1-19 10:35:13

好好好好的我要下载看看看

lijianan12300 发表于 2026-1-19 10:42:22

我要下载试试,我要下载试试...
页: [1]
查看完整版本: 万能程序补丁工具