Skip to content

Commit

Permalink
Removed hardcoded patterns and added autoupdating xml file and fixed …
Browse files Browse the repository at this point in the history
…Edge x64 v81
  • Loading branch information
Ceiridge committed Apr 14, 2020
1 parent fa5110e commit e569af7
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 81 deletions.
2 changes: 0 additions & 2 deletions ChromeDevExtWarningPatcher/ChromeDevExtWarningPatcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@
</Compile>
<Compile Include="Patches\BytePatchManager.cs" />
<Compile Include="Patches\BytePatchPattern.cs" />
<Compile Include="Patches\Defaults\Patches.cs" />
<Compile Include="Patches\Defaults\Patterns.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
3 changes: 0 additions & 3 deletions ChromeDevExtWarningPatcher/DllPatcher.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using ChromeDevExtWarningPatcher.Patches;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ChromeDevExtWarningPatcher {
class DllPatcher {
Expand Down
25 changes: 7 additions & 18 deletions ChromeDevExtWarningPatcher/PatcherGui.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
using ChromeDevExtWarningPatcher.Patches.Defaults;
using Microsoft.Win32;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ChromeDevExtWarningPatcher {
public partial class PatcherGui : Window {
Expand All @@ -35,15 +26,13 @@ private void SelectFolderBtn_Click(object sender, RoutedEventArgs e) {
}

private void PatchBtn_Click(object sender, RoutedEventArgs e) {
Program.bytePatchManager.disabledTypes.Clear();
if (RemoveExtWarning.IsChecked == false) {
Program.bytePatchManager.disabledTypes.Add(typeof(RemoveExtensionWarningPatch1));
Program.bytePatchManager.disabledTypes.Add(typeof(RemoveExtensionWarningPatch2));
}
Program.bytePatchManager.DisabledGroups.Clear();
if (RemoveExtWarning.IsChecked == false)
Program.bytePatchManager.disabledTypes.Add(typeof(RemoveDebugWarningPatch));
Program.bytePatchManager.DisabledGroups.Add(0);
if (RemoveDebugWarning.IsChecked == false)
Program.bytePatchManager.DisabledGroups.Add(1);
if (RemoveElision.IsChecked == false)
Program.bytePatchManager.disabledTypes.Add(typeof(RemoveElisionPatch));
Program.bytePatchManager.DisabledGroups.Add(2);

foreach (CheckBox installationBox in InstallationList.Items) {
if (installationBox.IsChecked == true) {
Expand Down Expand Up @@ -87,7 +76,7 @@ private void AddChromiumInstallation(string chromeDll) {
CheckBox installationBox = new CheckBox();
installationBox.Content = chromeDll;
installationBox.IsChecked = true;
installationBox.Foreground = new SolidColorBrush(Color.FromRgb(202, 62, 71));
installationBox.Foreground = installationBox.BorderBrush = new SolidColorBrush(Color.FromRgb(202, 62, 71));

InstallationList.Items.Add(installationBox);
Log("Added Chromium installation at " + chromeDll);
Expand Down
12 changes: 9 additions & 3 deletions ChromeDevExtWarningPatcher/Patches/BytePatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ namespace ChromeDevExtWarningPatcher
public class BytePatch
{
public byte origByteX64, patchByteX64;
public int offsetX64;
public int offsetX64, aoffsetX64;
public BytePatchPattern pattern;

public byte origByteX86, patchByteX86;
public int offsetX86;
public int offsetX86, aoffsetX86;

public BytePatch(BytePatchPattern pattern, byte origByteX64, byte patchByteX64, int offsetX64, byte origByteX86, byte patchByteX86, int offsetX86)
public int group;

public BytePatch(BytePatchPattern pattern, byte origByteX64, byte patchByteX64, int offsetX64, int aoffsetX64, byte origByteX86, byte patchByteX86, int offsetX86, int aoffsetX86, int group)
{
this.pattern = pattern;
this.origByteX64 = origByteX64;
this.patchByteX64 = patchByteX64;
this.offsetX64 = offsetX64;
this.aoffsetX64 = aoffsetX64;

this.origByteX86 = origByteX86;
this.patchByteX86 = patchByteX86;
this.offsetX86 = offsetX86;
this.aoffsetX86 = aoffsetX86;

this.group = group;
}
}
}
111 changes: 102 additions & 9 deletions ChromeDevExtWarningPatcher/Patches/BytePatchManager.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,111 @@
using ChromeDevExtWarningPatcher.Patches.Defaults;
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows;
using System.Xml.Linq;

namespace ChromeDevExtWarningPatcher.Patches {
class BytePatchManager {
private List<BytePatch> BytePatches = new List<BytePatch>();
private Dictionary<string, BytePatchPattern> BytePatterns = new Dictionary<string, BytePatchPattern>();

public List<Type> disabledTypes = new List<Type>();
public List<int> DisabledGroups = new List<int>();

public BytePatchManager() {
BytePatches.Clear();
BytePatches.Add(new RemoveExtensionWarningPatch1());
BytePatches.Add(new RemoveExtensionWarningPatch2());
BytePatches.Add(new RemoveDebugWarningPatch());
BytePatches.Add(new RemoveElisionPatch());
BytePatterns.Clear();

XDocument xmlDoc = null;
string xmlFile = Path.GetTempPath() + "chrome_patcher_patterns.xml";

try {
using (WebClient web = new WebClient()) {
string xmlStr;
xmlDoc = XDocument.Parse(xmlStr = web.DownloadString("https://raw.githubusercontent.com/Ceiridge/Chrome-Developer-Mode-Extension-Warning-Patcher/master/patterns.xml")); // Hardcoded defaults xml file; This makes quick fixes possible

File.WriteAllText(xmlFile, xmlStr);
}
} catch (Exception ex) {
if(File.Exists(xmlFile)) {
xmlDoc = XDocument.Parse(File.ReadAllText(xmlFile));
MessageBox.Show("An error occurred trying to fetch the new patterns. The old cached version will be used instead. Expect patch errors.\n\n" + ex.Message, "Warning");
} else {
MessageBox.Show("An error occurred trying to fetch the new patterns. The program has to exit, as no cached version of this file has been found.\n\n" + ex.Message, "Error");
Environment.Exit(1);
}
}


if(xmlDoc != null) {
// Comma cultuer setter from https://stackoverflow.com/questions/9160059/set-up-dot-instead-of-comma-in-numeric-values
CultureInfo customCulture = (CultureInfo) Thread.CurrentThread.CurrentCulture.Clone(); customCulture.NumberFormat.NumberDecimalSeparator = ".";
Thread.CurrentThread.CurrentCulture = customCulture;

float newVersion = float.Parse(xmlDoc.Root.Attribute("version").Value);
if(newVersion > Program.VERSION) {
MessageBox.Show("A new version of this patcher has been found.\nDownload it at:\nhttps://github.com/Ceiridge/Chrome-Developer-Mode-Extension-Warning-Patcher/releases", "New update available");
}

foreach(XElement pattern in xmlDoc.Root.Element("Patterns").Elements("Pattern")) {
BytePatchPattern patternClass = new BytePatchPattern(pattern.Attribute("name").Value);

foreach (XElement patternList in pattern.Elements("BytePatternList")) {
bool isX64 = patternList.Attribute("type").Value.Equals("x64");

foreach(XElement bytePattern in patternList.Elements("BytePattern")) {
string[] unparsedBytes = bytePattern.Value.Split(' ');
byte[] patternBytesArr = new byte[unparsedBytes.Length];

for(int i = 0; i < unparsedBytes.Length; i++) {
string unparsedByte = unparsedBytes[i].Equals("?") ? "FF" : unparsedBytes[i];
patternBytesArr[i] = Convert.ToByte(unparsedByte, 16);
}
(isX64 ? patternClass.AlternativePatternsX64 : patternClass.AlternativePatternsX86).Add(patternBytesArr);
}
}
BytePatterns.Add(patternClass.Name, patternClass);
}

foreach(XElement patch in xmlDoc.Root.Element("Patches").Elements("Patch")) {
BytePatchPattern pattern = BytePatterns[patch.Attribute("pattern").Value];
int group = int.Parse(patch.Attribute("group").Value);

byte origX64 = 0, origX86 = 0, patchX64 = 0, patchX86 = 0;
int offsetX64 = 0, offsetX86 = 0, aoffsetX64 = -1, aoffsetX86 = -1;

foreach(XElement patchData in patch.Elements("PatchData")) {
byte orig = Convert.ToByte(patchData.Attribute("orig").Value.Replace("0x", ""), 16);
byte patchB = Convert.ToByte(patchData.Attribute("patch").Value.Replace("0x", ""), 16);
int offset = Convert.ToInt32(patchData.Attribute("offset").Value.Replace("0x", ""), 16);

XAttribute alternativeOffsetAttr = patchData.Attribute("alternativeOffset");
int aoffset = alternativeOffsetAttr == null ? -1 : Convert.ToInt32(alternativeOffsetAttr.Value.Replace("0x", ""), 16);

if (patchData.Attribute("type").Value.Equals("x64")) {
origX64 = orig;
patchX64 = patchB;
offsetX64 = offset;
aoffsetX64 = aoffset;
} else {
origX86 = orig;
patchX86 = patchB;
offsetX86 = offset;
aoffsetX86 = aoffset;
}
}
BytePatches.Add(new BytePatch(pattern, origX64, patchX64, offsetX64, aoffsetX64, origX86, patchX86, offsetX86, aoffsetX86, group));
}
}
}

public bool PatchBytes(ref byte[] raw, bool x64, BytePatchPattern.WriteToLog log) {
int patches = 0;

foreach(BytePatch patch in BytePatches) {
if (disabledTypes.Contains(patch.GetType())) {
if (DisabledGroups.Contains(patch.group)) {
patches++;
continue;
}
Expand All @@ -30,6 +115,7 @@ public bool PatchBytes(ref byte[] raw, bool x64, BytePatchPattern.WriteToLog log
byte patchPatchByte = x64 ? patch.patchByteX64 : patch.patchByteX86;

if(addr != -1) {
REDO_CHECKS:
long index = addr + patchOffset;
byte sourceByte = raw[index];

Expand All @@ -38,8 +124,15 @@ public bool PatchBytes(ref byte[] raw, bool x64, BytePatchPattern.WriteToLog log
raw[index] = patchPatchByte;
log(index + " => " + patchPatchByte);
patches++;
} else
} else {
int patchAlternativeOffset = x64 ? patch.aoffsetX64 : patch.aoffsetX86;
if(patchOffset != patchAlternativeOffset && patchAlternativeOffset != -1) { // if the first offset didn't work, try the next one
patchOffset = patchAlternativeOffset;
goto REDO_CHECKS;
}

log("Source byte unexpected, should be " + patchOrigByte + "!");
}
} else {
log("Couldn't find offset for a patch " + patch.pattern.Name);
}
Expand Down
6 changes: 3 additions & 3 deletions ChromeDevExtWarningPatcher/Patches/BytePatchPattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
using System.Text;

namespace ChromeDevExtWarningPatcher.Patches {
public abstract class BytePatchPattern {
public class BytePatchPattern {
public string Name;
protected List<byte[]> AlternativePatternsX86 = new List<byte[]>();
protected List<byte[]> AlternativePatternsX64 = new List<byte[]>();
public List<byte[]> AlternativePatternsX86 = new List<byte[]>();
public List<byte[]> AlternativePatternsX64 = new List<byte[]>();

public BytePatchPattern(string name) {
Name = name;
Expand Down
17 changes: 0 additions & 17 deletions ChromeDevExtWarningPatcher/Patches/Defaults/Patches.cs

This file was deleted.

26 changes: 0 additions & 26 deletions ChromeDevExtWarningPatcher/Patches/Defaults/Patterns.cs

This file was deleted.

2 changes: 2 additions & 0 deletions ChromeDevExtWarningPatcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Program
private static Window guiWindow;
public static BytePatchManager bytePatchManager;

public const float VERSION = 3.2F;

[STAThread]
public static void Main(string[] args)
{
Expand Down
Loading

0 comments on commit e569af7

Please sign in to comment.