Skip to content

Commit

Permalink
Update patch algoritm. It's pretty bad, but at least it works
Browse files Browse the repository at this point in the history
  • Loading branch information
bugworm committed Nov 10, 2019
1 parent 438475d commit b288c17
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 24 deletions.
20 changes: 20 additions & 0 deletions BorderlandsPatcher/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 69 additions & 24 deletions BorderlandsPatcher/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public partial class Form1 : Form
private bool isBorderlands2 = true;
private string b2il = String.Empty;
private string btpsil = String.Empty;
private string filepath;

public Form1()
{
Expand Down Expand Up @@ -71,12 +72,12 @@ private void Form1_Shown(object sender, EventArgs e)
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 49520");
if (key != null)
{
b2il = key.GetValue("InstallLocation") as string;
b2il = key.GetValue("InstallLocation") as string;
}
else
{
key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Steam App 49520");
if(key != null)
if (key != null)
{
b2il = key.GetValue("InstallLocation") as string;
}
Expand All @@ -92,7 +93,7 @@ private void Form1_Shown(object sender, EventArgs e)
else
{
key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Steam App 261640");
if(key != null)
if (key != null)
{
btpsil = key.GetValue("InstallLocation") as string;
}
Expand All @@ -102,7 +103,7 @@ private void Form1_Shown(object sender, EventArgs e)

private void BtnPatchGame_Click(object sender, EventArgs e)
{
string filepath;
//string filepath;
try
{
if (isBorderlands2)
Expand Down Expand Up @@ -154,34 +155,73 @@ private void PatchExe(string file)
{
MessageBox.Show("You already have a backup. Skipping.");
}
if (isBorderlands2)
MessageBox.Show("This would take couple minutes. Please be patient...");

backgroundWorker1.RunWorkerAsync();
progressBar1.Value = 0;
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//progressBar1.Value = 20;
backgroundWorker1.ReportProgress(15);
byte[] dataArraySay = new byte[32] { 0x61, 0x00, 0x77, 0x00, 0x20, 0x00, 0x5B, 0x00, 0x47, 0x00, 0x54, 0x00, 0x5D, 0x00, 0x00, 0x00, 0x73, 0x00, 0x61, 0x00, 0x79, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6D, 0x73, 0x67, 0x20 };
byte[] dataArraySayChanged = new byte[32] { 0x61, 0x00, 0x77, 0x00, 0x20, 0x00, 0x5B, 0x00, 0x47, 0x00, 0x54, 0x00, 0x5D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6D, 0x73, 0x67, 0x20 };
byte[] dataArraySet = new byte[8] { 0x83, 0xC4, 0x0C, 0x85, 0xC0, 0x75, 0x1A, 0x6A };
byte[] dataArraySetChanged = new byte[8] { 0x83, 0xC4, 0x0C, 0x85, 0xFF, 0x75, 0x1A, 0x6A };
long positions = 0;
long positions2 = 0;

positions = FindBytes(filepath, dataArraySay);
if (positions == 0)
{
var stream = new FileStream(file, FileMode.Open, FileAccess.ReadWrite);
stream.Position = 0x004F2590;
stream.WriteByte(0xff);
for (long i = 0x01B94B0C; i <= 0x01B94B10; i++)
{
stream.Position = i;
stream.WriteByte(0x00);
}
stream.Close();
MessageBox.Show("Done!");
MessageBox.Show("Looks like binary file is already patched or it's a bug. Check if it's working", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
backgroundWorker1.ReportProgress(100);
backgroundWorker1.CancelAsync();
}
else
backgroundWorker1.ReportProgress(55);
positions2 = FindBytes(filepath, dataArraySet);
backgroundWorker1.ReportProgress(95);

if (positions2 == 0)
{
var stream = new FileStream(file, FileMode.Open, FileAccess.ReadWrite);
stream.Position = 0x00D8BD1F;
stream.WriteByte(0xff);
for (long i = 0x01982A00; i <= 0x01982A05; i++)
MessageBox.Show("Looks like binary file is already patched or it's a bug. Check if it's working", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
backgroundWorker1.ReportProgress(100);
backgroundWorker1.CancelAsync();
}
var stream = new FileStream(filepath, FileMode.Open, FileAccess.ReadWrite);
stream.Position = positions;
stream.Write(dataArraySayChanged, 0, 32);
stream.Position = positions2;
stream.Write(dataArraySetChanged, 0, 8);
stream.Close();
backgroundWorker1.ReportProgress(100);
MessageBox.Show("Done!");
}

private long FindBytes(string fileName, byte[] bytes)
{
long i;
int j, k = 0;
using (FileStream fs = File.OpenRead(fileName))
{
for (i = 0; i < fs.Length - bytes.Length; i++)
{
stream.Position = i;
stream.WriteByte(0x00);
fs.Seek(i, SeekOrigin.Begin);
for (j = 0; j < bytes.Length; j++)
{
if (fs.ReadByte() != bytes[j])
break;
}
if (j == bytes.Length)
return i;
}
stream.Close();
MessageBox.Show("Done!");
fs.Close();
}
return 0;
}


private bool VerifyExe(string file)
{
bool isExeExtension = false;
Expand Down Expand Up @@ -346,5 +386,10 @@ private void LblCommunityMods_LinkClicked(object sender, LinkLabelLinkClickedEve
LblCommunityMods.LinkVisited = true;
System.Diagnostics.Process.Start("https://github.com/BLCM/ModCabinet/wiki");
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
}
}
3 changes: 3 additions & 0 deletions BorderlandsPatcher/Form1.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
<metadata name="folderBrowserDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>157, 17</value>
</metadata>
<metadata name="backgroundWorker1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>326, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
Expand Down

0 comments on commit b288c17

Please sign in to comment.