diff --git a/ChromeDevExtWarningPatcher/ChromeDevExtWarningPatcher.csproj b/ChromeDevExtWarningPatcher/ChromeDevExtWarningPatcher.csproj index 7752e04..ef7f553 100644 --- a/ChromeDevExtWarningPatcher/ChromeDevExtWarningPatcher.csproj +++ b/ChromeDevExtWarningPatcher/ChromeDevExtWarningPatcher.csproj @@ -27,6 +27,8 @@ true true + + AnyCPU diff --git a/ChromeDevExtWarningPatcher/CommandLineOptions.cs b/ChromeDevExtWarningPatcher/CommandLineOptions.cs index 7332c5c..0447dfe 100644 --- a/ChromeDevExtWarningPatcher/CommandLineOptions.cs +++ b/ChromeDevExtWarningPatcher/CommandLineOptions.cs @@ -3,7 +3,7 @@ namespace ChromeDevExtWarningPatcher { public class CommandLineOptions { - [Option("groups", Required = false, HelpText = "Set what patch groups you want to use. See patterns.xml to get the group ids (comma-seperated: 0,1,2,etc.)", Separator = ',')] + [Option("groups", Required = false, HelpText = "Set what patch groups you want to use. See patterns.xml to get the group ids (comma-separated: 0,1,2,etc.)", Separator = ',')] public IEnumerable Groups { get; set; } [Option('w', "noWait", Required = false, HelpText = "Disable the almost-pointless wait after finishing")] diff --git a/ChromeDevExtWarningPatcher/CustomCheckBox.cs b/ChromeDevExtWarningPatcher/CustomCheckBox.cs index 6de7d34..0864bef 100644 --- a/ChromeDevExtWarningPatcher/CustomCheckBox.cs +++ b/ChromeDevExtWarningPatcher/CustomCheckBox.cs @@ -1,29 +1,30 @@ -using System.Windows.Controls; -using System.Windows.Media; - -namespace ChromeDevExtWarningPatcher { - class CustomCheckBox : CheckBox { - public int Group; - - public CustomCheckBox(string text, string tooltip, int group) : base() { - Content = text; - Foreground = BorderBrush = new SolidColorBrush(Color.FromRgb(202, 62, 71)); - Group = group; - - if (tooltip != null) - ToolTip = tooltip; - } - - public CustomCheckBox(GuiPatchGroupData patchGroupData) : this(patchGroupData.Name, patchGroupData.Tooltip, patchGroupData.Group) { - IsChecked = patchGroupData.Default; - } - - public CustomCheckBox(string text) : this(text, null, -1) { } - } - - struct GuiPatchGroupData { - public string Name, Tooltip; - public int Group; - public bool Default; - } -} +using System.Windows.Controls; +using System.Windows.Media; + +namespace ChromeDevExtWarningPatcher { + class CustomCheckBox : CheckBox { + public int Group; + + public CustomCheckBox(string text, string tooltip, int group) { + this.Content = text; + this.Foreground = this.BorderBrush = new SolidColorBrush(Color.FromRgb(202, 62, 71)); + this.Group = group; + + if (tooltip != null) { + this.ToolTip = tooltip; + } + } + + public CustomCheckBox(GuiPatchGroupData patchGroupData) : this(patchGroupData.Name, patchGroupData.Tooltip, patchGroupData.Group) { + this.IsChecked = patchGroupData.Default; + } + + public CustomCheckBox(string text) : this(text, null, -1) { } + } + + struct GuiPatchGroupData { + public string Name, Tooltip; + public int Group; + public bool Default; + } +} diff --git a/ChromeDevExtWarningPatcher/InstallationFinder/Defaults/CustomPath.cs b/ChromeDevExtWarningPatcher/InstallationFinder/Defaults/CustomPath.cs index 051bd8a..dff24ee 100644 --- a/ChromeDevExtWarningPatcher/InstallationFinder/Defaults/CustomPath.cs +++ b/ChromeDevExtWarningPatcher/InstallationFinder/Defaults/CustomPath.cs @@ -3,19 +3,19 @@ namespace ChromeDevExtWarningPatcher.InstallationFinder.Defaults { class CustomPath : Installation { - private string Path; + private readonly string path; public CustomPath(string path) : base("CustomPath") { - Path = path; + this.path = path; } public override List FindInstallationPaths() { List dllFiles = new List(); - AddDllAndExeToList(dllFiles, GetLatestDllAndExe(new DirectoryInfo(Path), "chrome.dll", "chrome.exe")); - AddDllAndExeToList(dllFiles, GetLatestDllAndExe(new DirectoryInfo(Path), "msedge.dll", "msedge.exe")); - AddDllAndExeToList(dllFiles, GetLatestDllAndExe(new DirectoryInfo(Path), "chrome.dll", "brave.exe")); - AddDllAndExeToList(dllFiles, GetLatestDllAndExe(new DirectoryInfo(Path), "browser.dll", "browser.exe")); + AddDllAndExeToList(dllFiles, GetLatestDllAndExe(new DirectoryInfo(this.path), "chrome.dll", "chrome.exe")); + AddDllAndExeToList(dllFiles, GetLatestDllAndExe(new DirectoryInfo(this.path), "msedge.dll", "msedge.exe")); + AddDllAndExeToList(dllFiles, GetLatestDllAndExe(new DirectoryInfo(this.path), "chrome.dll", "brave.exe")); + AddDllAndExeToList(dllFiles, GetLatestDllAndExe(new DirectoryInfo(this.path), "browser.dll", "browser.exe")); return dllFiles; } diff --git a/ChromeDevExtWarningPatcher/InstallationFinder/Installation.cs b/ChromeDevExtWarningPatcher/InstallationFinder/Installation.cs index ff78d53..4325e7c 100644 --- a/ChromeDevExtWarningPatcher/InstallationFinder/Installation.cs +++ b/ChromeDevExtWarningPatcher/InstallationFinder/Installation.cs @@ -7,15 +7,17 @@ namespace ChromeDevExtWarningPatcher.InstallationFinder { abstract class Installation { protected string Name; - public Installation(string Name) { - this.Name = Name; + protected Installation(string name) { + this.Name = name; } public abstract List FindInstallationPaths(); protected static InstallationPaths GetLatestDllAndExe(DirectoryInfo versionsFolder, string dllName, string exeName) { - if (!versionsFolder.Exists) + if (!versionsFolder.Exists) { return new InstallationPaths(); + } + InstallationPaths paths = new InstallationPaths(); List chromeVersions = new List(versionsFolder.EnumerateDirectories()); @@ -42,8 +44,10 @@ protected static InstallationPaths GetLatestDllAndExe(DirectoryInfo versionsFold } protected static void AddDllAndExeToList(List pathList, InstallationPaths latestDllAndExe) { - if (latestDllAndExe.ChromeDllPath == null || latestDllAndExe.ChromeExePath == null) + if (latestDllAndExe.ChromeDllPath == null || latestDllAndExe.ChromeExePath == null) { return; + } + pathList.Add(latestDllAndExe); } diff --git a/ChromeDevExtWarningPatcher/InstallationFinder/InstallationManager.cs b/ChromeDevExtWarningPatcher/InstallationFinder/InstallationManager.cs index 1015272..cea0728 100644 --- a/ChromeDevExtWarningPatcher/InstallationFinder/InstallationManager.cs +++ b/ChromeDevExtWarningPatcher/InstallationFinder/InstallationManager.cs @@ -5,20 +5,20 @@ namespace ChromeDevExtWarningPatcher.InstallationFinder { class InstallationManager { - private List installationFinders = new List(); + private readonly List installationFinders = new List(); public InstallationManager() { - installationFinders.Clear(); - installationFinders.Add(new Chrome()); - installationFinders.Add(new Brave()); - installationFinders.Add(new Edge()); - installationFinders.Add(new Yandex()); + this.installationFinders.Clear(); + this.installationFinders.Add(new Chrome()); + this.installationFinders.Add(new Brave()); + this.installationFinders.Add(new Edge()); + this.installationFinders.Add(new Yandex()); } public List FindAllChromiumInstallations() { List installations = new List(); - foreach (Installation installation in installationFinders) { + foreach (Installation installation in this.installationFinders) { foreach (InstallationPaths paths in installation.FindInstallationPaths()) { if (paths.Is64Bit()) { // force x64 installations.Add(paths); @@ -31,19 +31,21 @@ public List FindAllChromiumInstallations() { // Taken from https://stackoverflow.com/questions/480696/how-to-find-if-a-native-dll-file-is-compiled-as-x64-or-x86 public static bool IsImageX64(string peFilePath) { - using (var stream = new FileStream(peFilePath, FileMode.Open, FileAccess.Read)) - using (var reader = new BinaryReader(stream)) { + using (FileStream stream = new FileStream(peFilePath, FileMode.Open, FileAccess.Read)) + using (BinaryReader reader = new BinaryReader(stream)) { //check the MZ signature to ensure it's a valid Portable Executable image - if (reader.ReadUInt16() != 23117) + if (reader.ReadUInt16() != 23117) { throw new BadImageFormatException("Not a valid Portable Executable image", peFilePath); + } // seek to, and read, e_lfanew then advance the stream to there (start of NT header) stream.Seek(0x3A, SeekOrigin.Current); stream.Seek(reader.ReadUInt32(), SeekOrigin.Begin); // Ensure the NT header is valid by checking the "PE\0\0" signature - if (reader.ReadUInt32() != 17744) + if (reader.ReadUInt32() != 17744) { throw new BadImageFormatException("Not a valid Portable Executable image", peFilePath); + } // seek past the file header, then read the magic number from the optional header stream.Seek(20, SeekOrigin.Current); diff --git a/ChromeDevExtWarningPatcher/InstallationFinder/InstallationPaths.cs b/ChromeDevExtWarningPatcher/InstallationFinder/InstallationPaths.cs index f875959..be5470f 100644 --- a/ChromeDevExtWarningPatcher/InstallationFinder/InstallationPaths.cs +++ b/ChromeDevExtWarningPatcher/InstallationFinder/InstallationPaths.cs @@ -5,17 +5,17 @@ struct InstallationPaths { public string ChromeDllPath, ChromeExePath; public InstallationPaths(string chromeDllPath, string chromeExePath) { - ChromeDllPath = chromeDllPath; - ChromeExePath = chromeExePath; + this.ChromeDllPath = chromeDllPath; + this.ChromeExePath = chromeExePath; } public InstallationPaths(FileInfo chromeDll, FileInfo chromeExe) { - ChromeDllPath = chromeDll.FullName; - ChromeExePath = chromeExe.FullName; + this.ChromeDllPath = chromeDll.FullName; + this.ChromeExePath = chromeExe.FullName; } public bool Is64Bit() { - return InstallationManager.IsImageX64(ChromeDllPath) && InstallationManager.IsImageX64(ChromeExePath); + return InstallationManager.IsImageX64(this.ChromeDllPath) && InstallationManager.IsImageX64(this.ChromeExePath); } } } diff --git a/ChromeDevExtWarningPatcher/PatcherGui.xaml.cs b/ChromeDevExtWarningPatcher/PatcherGui.xaml.cs index 5f78588..8b02570 100644 --- a/ChromeDevExtWarningPatcher/PatcherGui.xaml.cs +++ b/ChromeDevExtWarningPatcher/PatcherGui.xaml.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; @@ -13,123 +12,130 @@ namespace ChromeDevExtWarningPatcher { public partial class PatcherGui : Window { public PatcherGui() { - InitializeComponent(); + this.InitializeComponent(); } private void SelectFolderBtn_Click(object sender, RoutedEventArgs e) { - OpenFileDialog openFile = new OpenFileDialog(); - openFile.Title = "Select a chrome.dll"; - openFile.Filter = "chrome.dll/msedge.dll file (chrome.dll;msedge.dll)|chrome.dll;msedge.dll|Alternative chrome.dll file (*.dll)|*.dll|All files (*.*)|*.*"; - openFile.FilterIndex = 1; + OpenFileDialog openFile = new OpenFileDialog { + Title = "Select a chrome.dll", + Filter = "chrome.dll/msedge.dll file (chrome.dll;msedge.dll)|chrome.dll;msedge.dll|Alternative chrome.dll file (*.dll)|*.dll|All files (*.*)|*.*", + FilterIndex = 1 + }; openFile.CheckFileExists = openFile.CheckPathExists = openFile.AddExtension = true; - if (openFile.ShowDialog(this) == true) { // No, I'm not a noob, I have to do it like this and further below - string chromeDllPath = openFile.FileName; + if (openFile.ShowDialog(this) != true) { // No, I'm not a noob, I have to do it like this and further below + return; + } - openFile = new OpenFileDialog(); - openFile.Title = "Select a chrome.exe"; - openFile.Filter = "Browser executable file (*.exe)|*.exe|All files (*.*)|*.*"; - openFile.FilterIndex = 1; - openFile.CheckFileExists = openFile.CheckPathExists = openFile.AddExtension = true; - openFile.InitialDirectory = Directory.GetParent(Path.GetDirectoryName(chromeDllPath)).FullName; + string chromeDllPath = openFile.FileName; - if (openFile.ShowDialog(this) == true) { - string chromeExePath = openFile.FileName; - AddChromiumInstallation(new InstallationPaths(chromeDllPath, chromeExePath), false); - } + openFile = new OpenFileDialog { + Title = "Select a chrome.exe", + Filter = "Browser executable file (*.exe)|*.exe|All files (*.*)|*.*", + FilterIndex = 1 + }; + openFile.CheckFileExists = openFile.CheckPathExists = openFile.AddExtension = true; + openFile.InitialDirectory = Directory.GetParent(Path.GetDirectoryName(chromeDllPath)).FullName; + + if (openFile.ShowDialog(this) != true) { + return; } + + string chromeExePath = openFile.FileName; + this.AddChromiumInstallation(new InstallationPaths(chromeDllPath, chromeExePath), false); } private void PatchBtn_Click(object sender, RoutedEventArgs e) { - PatchBtn.IsEnabled = UnPatchBtn.IsEnabled = false; + this.PatchBtn.IsEnabled = this.UnPatchBtn.IsEnabled = false; - Program.bytePatchManager.DisabledGroups.Clear(); - foreach (CustomCheckBox patchBox in PatchGroupList.Items) { - if (patchBox.IsChecked == false) - Program.bytePatchManager.DisabledGroups.Add(patchBox.Group); + Program.BytePatchManager.DisabledGroups.Clear(); + foreach (CustomCheckBox patchBox in this.PatchGroupList.Items) { + if (patchBox.IsChecked == false) { + Program.BytePatchManager.DisabledGroups.Add(patchBox.Group); + } } try { List installPaths = new List(); - foreach (CustomCheckBox installBox in InstallationList.Items) { + foreach (CustomCheckBox installBox in this.InstallationList.Items) { if (installBox.IsChecked == true) { installPaths.Add(new InstallationPaths(installBox.Content.ToString(), installBox.ToolTip.ToString().Split(new string[] { " & " }, StringSplitOptions.None)[1])); } } PatcherInstaller installer = new PatcherInstaller(installPaths); - if (installer.Install(Log)) { - foreach (CustomCheckBox installBox in InstallationList.Items) { + if (installer.Install(this.Log)) { + foreach (CustomCheckBox installBox in this.InstallationList.Items) { if (installBox.IsChecked == true) { installBox.Foreground = installBox.BorderBrush = new SolidColorBrush(Color.FromRgb(72, 207, 133)); } } } } catch (Exception ex) { - Log("Error while installing:" + ex.Message); + this.Log("Error while installing:" + ex.Message); } - PatchBtn.IsEnabled = UnPatchBtn.IsEnabled = true; + this.PatchBtn.IsEnabled = this.UnPatchBtn.IsEnabled = true; } private void UnPatchBtn_Click(object sender, RoutedEventArgs e) { - PatchBtn.IsEnabled = UnPatchBtn.IsEnabled = false; + this.PatchBtn.IsEnabled = this.UnPatchBtn.IsEnabled = false; try { List installPaths = new List(); - foreach (CustomCheckBox installBox in InstallationList.Items) { + foreach (CustomCheckBox installBox in this.InstallationList.Items) { installPaths.Add(new InstallationPaths(installBox.Content.ToString(), installBox.ToolTip.ToString().Split(new string[] { " & " }, StringSplitOptions.None)[1])); // chromeExePath is always in the ToolTip after " & " } PatcherInstaller installer = new PatcherInstaller(installPaths); - if (installer.UninstallAll(Log)) { - foreach (CustomCheckBox installBox in InstallationList.Items) { + if (installer.UninstallAll(this.Log)) { + foreach (CustomCheckBox installBox in this.InstallationList.Items) { if (installBox.IsChecked == true) { installBox.Foreground = installBox.BorderBrush = new SolidColorBrush(Color.FromRgb(72, 207, 133)); } } } } catch (Exception ex) { - Log("Error while uninstalling:" + ex.Message); + this.Log("Error while uninstalling:" + ex.Message); } - PatchBtn.IsEnabled = UnPatchBtn.IsEnabled = true; + this.PatchBtn.IsEnabled = this.UnPatchBtn.IsEnabled = true; } private void CopyBtn_Click(object sender, RoutedEventArgs e) { - Clipboard.SetText(ConsoleBox.GetTotalTextRange().Text); + Clipboard.SetText(this.ConsoleBox.GetTotalTextRange().Text); } protected override void OnInitialized(EventArgs e) { base.OnInitialized(e); - ConsoleBox.GetTotalTextRange().Text = ""; + this.ConsoleBox.GetTotalTextRange().Text = ""; - Log("Patcher gui initialized"); - Log("Searching for Chromium installations..."); + this.Log("Patcher gui initialized"); + this.Log("Searching for Chromium installations..."); - foreach (InstallationPaths paths in new InstallationFinder.InstallationManager().FindAllChromiumInstallations()) { - AddChromiumInstallation(paths); + foreach (InstallationPaths paths in new InstallationManager().FindAllChromiumInstallations()) { + this.AddChromiumInstallation(paths); } - foreach (GuiPatchGroupData patchGroup in Program.bytePatchManager.PatchGroups) { - PatchGroupList.Items.Add(new CustomCheckBox(patchGroup)); + foreach (GuiPatchGroupData patchGroup in Program.BytePatchManager.PatchGroups) { + this.PatchGroupList.Items.Add(new CustomCheckBox(patchGroup)); } } public void Log(string str) { - ConsoleBox.Dispatcher.Invoke(new Action(() => { + this.ConsoleBox.Dispatcher.Invoke(new Action(() => { Paragraph logParagraph = new Paragraph(); logParagraph.Inlines.Add(str); - ConsoleBox.Document.Blocks.Add(logParagraph); - ConsoleBox.ScrollToEnd(); + this.ConsoleBox.Document.Blocks.Add(logParagraph); + this.ConsoleBox.ScrollToEnd(); })); } private void AddChromiumInstallation(InstallationPaths chromePaths, bool suppressErrors = true) { - if(!chromePaths.Is64Bit()) { - if(!suppressErrors) { + if (!chromePaths.Is64Bit()) { + if (!suppressErrors) { MessageBox.Show("A 64-bit Chromium installation is required", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } return; @@ -139,8 +145,8 @@ private void AddChromiumInstallation(InstallationPaths chromePaths, bool suppres installationBox.IsChecked = true; installationBox.ToolTip = chromePaths.ChromeDllPath + " & " + chromePaths.ChromeExePath; - InstallationList.Items.Add(installationBox); - Log("Added Chromium installation at " + chromePaths.ChromeDllPath); + this.InstallationList.Items.Add(installationBox); + this.Log("Added Chromium installation at " + chromePaths.ChromeDllPath); } } diff --git a/ChromeDevExtWarningPatcher/PatcherInstaller.cs b/ChromeDevExtWarningPatcher/PatcherInstaller.cs index 5e30172..e11da96 100644 --- a/ChromeDevExtWarningPatcher/PatcherInstaller.cs +++ b/ChromeDevExtWarningPatcher/PatcherInstaller.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.IO.Compression; using System.Linq; using System.Runtime.InteropServices; using System.Security.Principal; @@ -14,50 +13,50 @@ namespace ChromeDevExtWarningPatcher { class PatcherInstaller { - private static uint FILE_HEADER = BitConverter.ToUInt32(BitConverter.GetBytes(0xCE161D6E).Reverse().ToArray(), 0); - private static uint PATCH_HEADER = BitConverter.ToUInt32(BitConverter.GetBytes(0x8A7C5000).Reverse().ToArray(), 0); // fix for wrong endianess + private static readonly uint fileHeader = BitConverter.ToUInt32(BitConverter.GetBytes(0xCE161D6E).Reverse().ToArray(), 0); + private static readonly uint patchHeader = BitConverter.ToUInt32(BitConverter.GetBytes(0x8A7C5000).Reverse().ToArray(), 0); // fix for wrong endianess [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] - private static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg, UIntPtr wParam, + private static extern bool SendNotifyMessage(IntPtr hWnd, uint msg, UIntPtr wParam, IntPtr lParam); - private List InstallationPaths; + private readonly List installationPaths; public PatcherInstaller(List installationPaths) { - InstallationPaths = installationPaths; + this.installationPaths = installationPaths; } private static byte[] GetPatchFileBinary(InstallationPaths paths) { MemoryStream stream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(stream); - writer.Write(FILE_HEADER); + writer.Write(fileHeader); writer.Write(paths.ChromeDllPath.Length); // Needed, so it takes 4 bytes writer.Write(Encoding.ASCII.GetBytes(paths.ChromeDllPath)); - foreach (BytePatch patch in Program.bytePatchManager.BytePatches) { - if (Program.bytePatchManager.DisabledGroups.Contains(patch.group)) { + foreach (BytePatch patch in Program.BytePatchManager.BytePatches) { + if (Program.BytePatchManager.DisabledGroups.Contains(patch.Group)) { continue; } - writer.Write(PATCH_HEADER); - writer.Write(patch.pattern.AlternativePatternsX64.Count); + writer.Write(patchHeader); + writer.Write(patch.Pattern.AlternativePatternsX64.Count); - foreach (byte[] pattern in patch.pattern.AlternativePatternsX64) { + foreach (byte[] pattern in patch.Pattern.AlternativePatternsX64) { writer.Write(pattern.Length); writer.Write(pattern); } - writer.Write(patch.offsets.Count); - foreach (int offset in patch.offsets) { + writer.Write(patch.Offsets.Count); + foreach (int offset in patch.Offsets) { writer.Write(offset); } - writer.Write(patch.origByte); - writer.Write(patch.patchByte); - writer.Write(patch.isSig); - writer.Write(patch.sigOffset); + writer.Write(patch.OrigByte); + writer.Write(patch.PatchByte); + writer.Write(patch.IsSig); + writer.Write(patch.SigOffset); } writer.Close(); @@ -88,15 +87,15 @@ private static void TryDeletePatcherDlls(DirectoryInfo folder) { // This is needed to ignore deletion errors private static void DeleteFolderRecursively(DirectoryInfo folder) { - foreach(FileInfo file in folder.EnumerateFiles()) { + foreach (FileInfo file in folder.EnumerateFiles()) { try { file.Delete(); - } catch(Exception) { + } catch (Exception) { // Ignore } } - foreach(DirectoryInfo directory in folder.EnumerateDirectories()) { + foreach (DirectoryInfo directory in folder.EnumerateDirectories()) { DeleteFolderRecursively(directory); } @@ -130,7 +129,7 @@ public bool Install(WriteToLog log) { log("Cleared ChromeExes registry"); int i = 0; - foreach (InstallationPaths paths in InstallationPaths) { + foreach (InstallationPaths paths in this.installationPaths) { string appDir = Path.GetDirectoryName(paths.ChromeExePath); // Write patch data info file @@ -153,8 +152,8 @@ public bool Install(WriteToLog log) { Directory.CreateDirectory(programsFolder.FullName); // Also creates all subdirectories } - using(ZipFile zip = ZipFile.Read(new MemoryStream(Properties.Resources.ChromeDllInjector))) { - foreach(ZipEntry entry in zip) { + using (ZipFile zip = ZipFile.Read(new MemoryStream(Properties.Resources.ChromeDllInjector))) { + foreach (ZipEntry entry in zip) { entry.Extract(programsFolder.FullName, ExtractExistingFileAction.OverwriteSilently); } } @@ -220,7 +219,7 @@ public bool UninstallAll(WriteToLog log) { log("Cleared ChromeExes registry"); } - foreach (InstallationPaths paths in InstallationPaths) { + foreach (InstallationPaths paths in this.installationPaths) { string appDir = Path.GetDirectoryName(paths.ChromeExePath); string patchesFile = Path.Combine(appDir, "ChromePatches.bin"); diff --git a/ChromeDevExtWarningPatcher/Patches/BytePatch.cs b/ChromeDevExtWarningPatcher/Patches/BytePatch.cs index b29f897..2bf4668 100644 --- a/ChromeDevExtWarningPatcher/Patches/BytePatch.cs +++ b/ChromeDevExtWarningPatcher/Patches/BytePatch.cs @@ -3,22 +3,22 @@ namespace ChromeDevExtWarningPatcher { public class BytePatch { - public byte origByte, patchByte; - public List offsets; - public int sigOffset; - public bool isSig; - public BytePatchPattern pattern; + public byte OrigByte, PatchByte; + public List Offsets; + public int SigOffset; + public bool IsSig; + public BytePatchPattern Pattern; - public int group; + public int Group; public BytePatch(BytePatchPattern pattern, byte origByte, byte patchByte, List offsets, int group, bool isSig = false, int sigOffset = 0) { - this.pattern = pattern; - this.origByte = origByte; - this.patchByte = patchByte; - this.offsets = offsets; - this.isSig = isSig; - this.sigOffset = sigOffset; - this.group = group; + this.Pattern = pattern; + this.OrigByte = origByte; + this.PatchByte = patchByte; + this.Offsets = offsets; + this.IsSig = isSig; + this.SigOffset = sigOffset; + this.Group = group; } } } diff --git a/ChromeDevExtWarningPatcher/Patches/BytePatchManager.cs b/ChromeDevExtWarningPatcher/Patches/BytePatchManager.cs index f4326cb..38f021b 100644 --- a/ChromeDevExtWarningPatcher/Patches/BytePatchManager.cs +++ b/ChromeDevExtWarningPatcher/Patches/BytePatchManager.cs @@ -12,24 +12,27 @@ namespace ChromeDevExtWarningPatcher.Patches { class BytePatchManager { public List BytePatches = new List(); - private Dictionary BytePatterns = new Dictionary(); + private readonly Dictionary bytePatterns = new Dictionary(); public List DisabledGroups = new List(); public List PatchGroups = new List(); public delegate MessageBoxResult WriteLineOrMessageBox(string str, string title); public BytePatchManager(WriteLineOrMessageBox log) { - BytePatches.Clear(); - BytePatterns.Clear(); + this.BytePatches.Clear(); + this.bytePatterns.Clear(); XDocument xmlDoc = null; string xmlFile = Program.DEBUG ? @"..\..\..\patterns.xml" : (Path.GetTempPath() + "chrome_patcher_patterns.xml"); try { - if (Program.DEBUG) + if (Program.DEBUG) { throw new Exception("Forcing to use local patterns.xml"); + } using (WebClient web = new WebClient()) { + ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; // TLS 1.2, which is required for Github + 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 @@ -46,68 +49,67 @@ public BytePatchManager(WriteLineOrMessageBox log) { } - if (xmlDoc != null) { - // Comma culture 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; + // Comma culture 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); - Version myVersion = Assembly.GetCallingAssembly().GetName().Version; + float newVersion = float.Parse(xmlDoc.Root.Attribute("version").Value); + Version myVersion = Assembly.GetCallingAssembly().GetName().Version; - if (newVersion > float.Parse(myVersion.Major + "." + myVersion.Minor)) { - log("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"); - } + if (newVersion > float.Parse(myVersion.Major + "." + myVersion.Minor)) { + log("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 pattern in xmlDoc.Root.Element("Patterns").Elements("Pattern")) { + BytePatchPattern patternClass = new BytePatchPattern(pattern.Attribute("name").Value); - foreach (XElement bytePattern in pattern.Elements("BytePattern")) { - string[] unparsedBytes = bytePattern.Value.Split(' '); - byte[] patternBytesArr = new byte[unparsedBytes.Length]; + foreach (XElement bytePattern in pattern.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); - } - patternClass.AlternativePatternsX64.Add(patternBytesArr); + for (int i = 0; i < unparsedBytes.Length; i++) { + string unparsedByte = unparsedBytes[i].Equals("?") ? "FF" : unparsedBytes[i]; + patternBytesArr[i] = Convert.ToByte(unparsedByte, 16); } - BytePatterns.Add(patternClass.Name, patternClass); + patternClass.AlternativePatternsX64.Add(patternBytesArr); } - 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, patchX64 = 0; - List offsetsX64 = new List(); - int sigOffset = 0; - bool sig = false; - - foreach (XElement patchData in patch.Elements("PatchData")) { - foreach (XElement offsetElement in patchData.Elements("Offset")) { - offsetsX64.Add(Convert.ToInt32(offsetElement.Value.Replace("0x", ""), 16)); - } - - origX64 = Convert.ToByte(patchData.Attribute("orig").Value.Replace("0x", ""), 16); - patchX64 = Convert.ToByte(patchData.Attribute("patch").Value.Replace("0x", ""), 16); - sig = Convert.ToBoolean(patchData.Attribute("sig").Value); - if(patchData.Attributes("sigOffset").Any()) { - sigOffset = Convert.ToInt32(patchData.Attribute("sigOffset").Value.Replace("0x", ""), 16); - } - break; + this.bytePatterns.Add(patternClass.Name, patternClass); + } + + foreach (XElement patch in xmlDoc.Root.Element("Patches").Elements("Patch")) { + BytePatchPattern pattern = this.bytePatterns[patch.Attribute("pattern").Value]; + int group = int.Parse(patch.Attribute("group").Value); + + byte origX64 = 0, patchX64 = 0; + List offsetsX64 = new List(); + int sigOffset = 0; + bool sig = false; + + foreach (XElement patchData in patch.Elements("PatchData")) { + foreach (XElement offsetElement in patchData.Elements("Offset")) { + offsetsX64.Add(Convert.ToInt32(offsetElement.Value.Replace("0x", ""), 16)); } - BytePatches.Add(new BytePatch(pattern, origX64, patchX64, offsetsX64, group, sig, sigOffset)); + origX64 = Convert.ToByte(patchData.Attribute("orig").Value.Replace("0x", ""), 16); + patchX64 = Convert.ToByte(patchData.Attribute("patch").Value.Replace("0x", ""), 16); + sig = Convert.ToBoolean(patchData.Attribute("sig").Value); + if (patchData.Attributes("sigOffset").Any()) { + sigOffset = Convert.ToInt32(patchData.Attribute("sigOffset").Value.Replace("0x", ""), 16); + } + break; } - foreach (XElement patchGroup in xmlDoc.Root.Element("GroupedPatches").Elements("GroupedPatch")) { - PatchGroups.Add(new GuiPatchGroupData { - Group = int.Parse(patchGroup.Attribute("group").Value), - Default = bool.Parse(patchGroup.Attribute("default").Value), - Name = patchGroup.Element("Name").Value, - Tooltip = patchGroup.Element("Tooltip").Value - }); - } + this.BytePatches.Add(new BytePatch(pattern, origX64, patchX64, offsetsX64, @group, sig, sigOffset)); + } + + foreach (XElement patchGroup in xmlDoc.Root.Element("GroupedPatches").Elements("GroupedPatch")) { + this.PatchGroups.Add(new GuiPatchGroupData { + Group = int.Parse(patchGroup.Attribute("group").Value), + Default = bool.Parse(patchGroup.Attribute("default").Value), + Name = patchGroup.Element("Name").Value, + Tooltip = patchGroup.Element("Tooltip").Value + }); } } } diff --git a/ChromeDevExtWarningPatcher/Patches/BytePatchPattern.cs b/ChromeDevExtWarningPatcher/Patches/BytePatchPattern.cs index 1d7dac7..0742895 100644 --- a/ChromeDevExtWarningPatcher/Patches/BytePatchPattern.cs +++ b/ChromeDevExtWarningPatcher/Patches/BytePatchPattern.cs @@ -6,7 +6,7 @@ public class BytePatchPattern { public List AlternativePatternsX64 = new List(); public BytePatchPattern(string name) { - Name = name; + this.Name = name; } } } diff --git a/ChromeDevExtWarningPatcher/Program.cs b/ChromeDevExtWarningPatcher/Program.cs index 7acf4db..93e2e8f 100644 --- a/ChromeDevExtWarningPatcher/Program.cs +++ b/ChromeDevExtWarningPatcher/Program.cs @@ -15,32 +15,32 @@ namespace ChromeDevExtWarningPatcher { class Program { private static Application guiApp; private static Window guiWindow; - public static BytePatchManager bytePatchManager; + public static BytePatchManager BytePatchManager; public const bool DEBUG = false; [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] private static extern bool FreeConsole(); - private const string archError = "A 64-bit operating system is required. 32-bit is not supported and won't be in the future."; + private const string ARCH_ERROR = "A 64-bit operating system is required. 32-bit is not supported and won't be in the future."; [STAThread] public static void Main(string[] args) { bool incompatibleArchitecture = !Environment.Is64BitOperatingSystem; if (args.Length == 0) { FreeConsole(); - - if(incompatibleArchitecture) { - MessageBox.Show(archError, "Error", MessageBoxButton.OK, MessageBoxImage.Error); + + if (incompatibleArchitecture) { + MessageBox.Show(ARCH_ERROR, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } - bytePatchManager = new BytePatchManager(MessageBox.Show); + BytePatchManager = new BytePatchManager(MessageBox.Show); guiApp = new Application(); guiApp.Run(guiWindow = new PatcherGui()); } else { - if(incompatibleArchitecture) { - Console.WriteLine(archError); + if (incompatibleArchitecture) { + Console.WriteLine(ARCH_ERROR); return; } @@ -59,9 +59,11 @@ public static void MainCmd(string[] args) { return; } - if (clOptions == null) + if (clOptions == null) { return; - bytePatchManager = new BytePatchManager(CustomConsoleWrite); + } + + BytePatchManager = new BytePatchManager(CustomConsoleWrite); List applicationPaths = new List(); List groups = new List(clOptions.Groups); @@ -71,7 +73,7 @@ public static void MainCmd(string[] args) { return; } - if (clOptions.CustomPath != null && clOptions.CustomPath.Length > 0) { + if (!string.IsNullOrEmpty(clOptions.CustomPath)) { if (!Directory.Exists(clOptions.CustomPath)) { Console.WriteLine("CustomPath not found"); return; @@ -79,16 +81,18 @@ public static void MainCmd(string[] args) { applicationPaths.AddRange(new CustomPath(clOptions.CustomPath).FindInstallationPaths()); } else { - applicationPaths.AddRange(new InstallationFinder.InstallationManager().FindAllChromiumInstallations()); + applicationPaths.AddRange(new InstallationManager().FindAllChromiumInstallations()); } - foreach (GuiPatchGroupData patchData in bytePatchManager.PatchGroups) { - if (!groups.Contains(patchData.Group)) - bytePatchManager.DisabledGroups.Add(patchData.Group); + foreach (GuiPatchGroupData patchData in BytePatchManager.PatchGroups) { + if (!groups.Contains(patchData.Group)) { + BytePatchManager.DisabledGroups.Add(patchData.Group); + } } - if (applicationPaths.Count == 0) + if (applicationPaths.Count == 0) { Console.WriteLine("Error: No patchable browser files found!"); + } try { PatcherInstaller installer = new PatcherInstaller(applicationPaths); @@ -97,8 +101,9 @@ public static void MainCmd(string[] args) { Console.WriteLine("Error while installing patches: " + ex.Message); } - if (!clOptions.NoWait) + if (!clOptions.NoWait) { Thread.Sleep(5000); // Wait a bit to let the user see the result + } } private static MessageBoxResult CustomConsoleWrite(string str, string arg2 = "") { diff --git a/ChromeDllInjector/Properties/AssemblyInfo.cs b/ChromeDllInjector/Properties/AssemblyInfo.cs index f9c7ca5..97c70db 100644 --- a/ChromeDllInjector/Properties/AssemblyInfo.cs +++ b/ChromeDllInjector/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // Allgemeine Informationen über eine Assembly werden über die folgenden diff --git a/ChromeDllInjectorBuildZipper/Properties/AssemblyInfo.cs b/ChromeDllInjectorBuildZipper/Properties/AssemblyInfo.cs index 38a747f..694158b 100644 --- a/ChromeDllInjectorBuildZipper/Properties/AssemblyInfo.cs +++ b/ChromeDllInjectorBuildZipper/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // Allgemeine Informationen über eine Assembly werden über die folgenden