马上注册,结交更多易友,享用更多功能,让你轻松玩转觅风论坛。
您需要 登录 才可以下载或查看,没有账号?立即注册
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;
}
}
|
|