Skip to content

Commit

Permalink
Enhancement: Added installation finders, improved gui
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceiridge committed Apr 3, 2021
1 parent 7742191 commit 732ff91
Show file tree
Hide file tree
Showing 15 changed files with 322 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<ItemGroup>
<PackageReference Include="MaterialDesignThemes" Version="4.0.0" />
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
Expand Down
4 changes: 2 additions & 2 deletions ChromeDevExtWarningPatcher/ComponentModels/MainModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace ChromeDevExtWarningPatcher.ComponentModels {
public class MainModel {
public SelectionListModel BrowserListModel => new SelectionListModel();
public SelectionListModel PatchListModel => new SelectionListModel();
public SelectionListModel BrowserListModel { get; set; } = new SelectionListModel();
public SelectionListModel PatchListModel { get; set; } = new SelectionListModel();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
using System.Windows.Media;

namespace ChromeDevExtWarningPatcher.ComponentModels {
public class SelectionListElement : INotifyPropertyChanged {
[Required]
public string Name { get; set; }
public string? Tooltip { get; set; }
public string? Description { get; set; }
public Image? IconImage { get; set; }
public ImageSource? IconImage { get; set; }

private bool isSelected;
public bool IsSelected {
Expand Down
10 changes: 0 additions & 10 deletions ChromeDevExtWarningPatcher/ComponentModels/SelectionListModel.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;

namespace ChromeDevExtWarningPatcher.ComponentModels {
public class SelectionListModel {
[Required]
public ObservableCollection<SelectionListElement> ElementList { get; set; } = new ObservableCollection<SelectionListElement>();

public SelectionListModel() {
Task.Run(async () => {
await Task.Delay(100);
this.ElementList.Add(new SelectionListElement("Edge") {
Description = "Very edgy"
});
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
<ListView ItemsSource="{Binding ElementList}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid ToolTip="{Binding Tooltip}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" MaxWidth="150" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<CheckBox IsChecked="{Binding IsSelected}" />
<CheckBox IsChecked="{Binding IsSelected}">
<CheckBox.LayoutTransform>
<ScaleTransform ScaleX="1.5" ScaleY="1.5" />
</CheckBox.LayoutTransform>
</CheckBox>
<Image Grid.Column="1" Source="{Binding IconImage}" Margin="5, 0, 5, 0" />
<StackPanel Grid.Column="2" Orientation="Vertical">
<TextBlock FontSize="16" Text="{Binding Name}" />
Expand Down
17 changes: 17 additions & 0 deletions ChromeDevExtWarningPatcher/InstallationFinder/Defaults/Brave.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.IO;

namespace ChromeDevExtWarningPatcher.InstallationFinder.Defaults {
internal class Brave : Installation {
public Brave() : base("Brave") { }

public override List<InstallationPaths> FindInstallationPaths() {
List<InstallationPaths> dllFiles = new List<InstallationPaths>();

AddDllAndExeToList(dllFiles, this.GetLatestDllAndExe(new DirectoryInfo(@"C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application"), "chrome.dll", "brave.exe"));
AddDllAndExeToList(dllFiles, this.GetLatestDllAndExe(new DirectoryInfo(@"C:\Program Files\BraveSoftware\Brave-Browser\Application"), "chrome.dll", "brave.exe"));

return dllFiles;
}
}
}
20 changes: 20 additions & 0 deletions ChromeDevExtWarningPatcher/InstallationFinder/Defaults/Chrome.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.IO;

namespace ChromeDevExtWarningPatcher.InstallationFinder.Defaults {
internal class Chrome : Installation {
public Chrome() : base("Chrome") { }

public override List<InstallationPaths> FindInstallationPaths() {
List<InstallationPaths> dllFiles = new List<InstallationPaths>();

AddDllAndExeToList(dllFiles, this.GetLatestDllAndExe(new DirectoryInfo(@"C:\Program Files (x86)\Google\Chrome\Application"), "chrome.dll", "chrome.exe"));
AddDllAndExeToList(dllFiles, this.GetLatestDllAndExe(new DirectoryInfo(@"C:\Program Files (x86)\Google\Chrome Beta\Application"), "chrome.dll", "chrome.exe"));

AddDllAndExeToList(dllFiles, this.GetLatestDllAndExe(new DirectoryInfo(@"C:\Program Files\Google\Chrome\Application"), "chrome.dll", "chrome.exe"));
AddDllAndExeToList(dllFiles, this.GetLatestDllAndExe(new DirectoryInfo(@"C:\Program Files\Google\Chrome Beta\Application"), "chrome.dll", "chrome.exe"));

return dllFiles;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.IO;

namespace ChromeDevExtWarningPatcher.InstallationFinder.Defaults {
internal class CustomPath : Installation {
private readonly string path;

public CustomPath(string path) : base("CustomPath") {
this.path = path;
}

public override List<InstallationPaths> FindInstallationPaths() {
List<InstallationPaths> dllFiles = new List<InstallationPaths>();

AddDllAndExeToList(dllFiles, this.GetLatestDllAndExe(new DirectoryInfo(this.path), "chrome.dll", "chrome.exe"));
AddDllAndExeToList(dllFiles, this.GetLatestDllAndExe(new DirectoryInfo(this.path), "msedge.dll", "msedge.exe"));
AddDllAndExeToList(dllFiles, this.GetLatestDllAndExe(new DirectoryInfo(this.path), "chrome.dll", "brave.exe"));
AddDllAndExeToList(dllFiles, this.GetLatestDllAndExe(new DirectoryInfo(this.path), "browser.dll", "browser.exe"));

return dllFiles;
}
}
}
17 changes: 17 additions & 0 deletions ChromeDevExtWarningPatcher/InstallationFinder/Defaults/Edge.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.IO;

namespace ChromeDevExtWarningPatcher.InstallationFinder.Defaults {
internal class Edge : Installation {
public Edge() : base("Edge") { }

public override List<InstallationPaths> FindInstallationPaths() {
List<InstallationPaths> dllFiles = new List<InstallationPaths>();

AddDllAndExeToList(dllFiles, this.GetLatestDllAndExe(new DirectoryInfo(@"C:\Program Files (x86)\Microsoft\Edge\Application"), "msedge.dll", "msedge.exe"));
AddDllAndExeToList(dllFiles, this.GetLatestDllAndExe(new DirectoryInfo(@"C:\Program Files\Microsoft\Edge\Application"), "msedge.dll", "msedge.exe"));

return dllFiles;
}
}
}
18 changes: 18 additions & 0 deletions ChromeDevExtWarningPatcher/InstallationFinder/Defaults/Yandex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace ChromeDevExtWarningPatcher.InstallationFinder.Defaults {
internal class Yandex : Installation {
public Yandex() : base("Yandex") { }

public override List<InstallationPaths> FindInstallationPaths() {
List<InstallationPaths> dllFiles = new List<InstallationPaths>();

string appDataLocal = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
AddDllAndExeToList(dllFiles, this.GetLatestDllAndExe(new DirectoryInfo(Path.Combine(appDataLocal, @"Yandex\YandexBrowser\Application")), "browser.dll", "browser.exe"));

return dllFiles;
}
}
}
56 changes: 56 additions & 0 deletions ChromeDevExtWarningPatcher/InstallationFinder/Installation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ChromeDevExtWarningPatcher.InstallationFinder {
internal abstract class Installation {
protected string Name;

protected Installation(string name) {
this.Name = name;
}

public abstract List<InstallationPaths> FindInstallationPaths();

protected InstallationPaths GetLatestDllAndExe(DirectoryInfo versionsFolder, string dllName, string exeName) {
if (!versionsFolder.Exists) {
return new InstallationPaths(this.Name);
}

InstallationPaths paths = new InstallationPaths(this.Name);

List<DirectoryInfo> chromeVersions = new List<DirectoryInfo>(versionsFolder.EnumerateDirectories());
chromeVersions = chromeVersions.OrderByDescending(dirInfo => GetUnixTime(dirInfo.LastWriteTime)).ToList();

foreach (DirectoryInfo chromeVersion in chromeVersions) {
if (chromeVersion.Name.Contains(".")) {
foreach (FileInfo file in chromeVersion.EnumerateFiles()) {
if (file.Name.Equals(dllName)) {
paths.ChromeDllPath = file.FullName;
break;
}
}
}
}

FileInfo chromeExe = new FileInfo(Path.Combine(versionsFolder.FullName, exeName));
if (chromeExe.Exists && paths.ChromeDllPath != null) { // Every installation path also has to have a chrome.exe, otherwise the entire patcher won't work
paths.ChromeExePath = chromeExe.FullName;
return paths;
}

return new InstallationPaths(this.Name);
}

protected static void AddDllAndExeToList(List<InstallationPaths> pathList, InstallationPaths latestDllAndExe) {
if (latestDllAndExe.ChromeDllPath != null && latestDllAndExe.ChromeExePath != null) {
pathList.Add(latestDllAndExe);
}
}

public static double GetUnixTime(DateTime date) {
return (date.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using ChromeDevExtWarningPatcher.InstallationFinder.Defaults;
using System;
using System.Collections.Generic;
using System.IO;

namespace ChromeDevExtWarningPatcher.InstallationFinder {
public class InstallationManager {
private readonly List<Installation> installationFinders = new List<Installation>();

public InstallationManager() {
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<InstallationPaths> FindAllChromiumInstallations() {
List<InstallationPaths> installations = new List<InstallationPaths>();

foreach (Installation installation in this.installationFinders) {
foreach (InstallationPaths paths in installation.FindInstallationPaths()) {
if (paths.Is64Bit()) { // Force x64 installations
installations.Add(paths);
}
}
}

return installations;
}

// 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 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) {
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) {
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);
ushort magicByte = reader.ReadUInt16();
return magicByte == 0x20B;
}
}
}
30 changes: 30 additions & 0 deletions ChromeDevExtWarningPatcher/InstallationFinder/InstallationPaths.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.ComponentModel.DataAnnotations;
using System.IO;

namespace ChromeDevExtWarningPatcher.InstallationFinder {
public class InstallationPaths {
[Required]
public string Name;
public string? ChromeDllPath, ChromeExePath;

public InstallationPaths(string name, string chromeDllPath, string chromeExePath) {
this.Name = name;
this.ChromeDllPath = chromeDllPath;
this.ChromeExePath = chromeExePath;
}

public InstallationPaths(string name, FileInfo chromeDll, FileInfo chromeExe) : this(name, chromeDll.FullName, chromeExe.FullName) { }

public InstallationPaths(string name) {
this.Name = name;
}

public bool Is64Bit() {
if(this.ChromeDllPath == null || this.ChromeExePath == null) {
return false;
}

return InstallationManager.IsImageX64(this.ChromeDllPath) && InstallationManager.IsImageX64(this.ChromeExePath);
}
}
}
18 changes: 13 additions & 5 deletions ChromeDevExtWarningPatcher/MainView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Expander Header="Select Browsers" IsExpanded="True">
<cv:SelectionListView DataContext="{Binding BrowserListModel}" />
<Expander x:Name="BrowserExpander" Header="Select Browsers" IsExpanded="True" Expanded="OnExpanderExpand">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="85*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<cv:SelectionListView DataContext="{Binding BrowserListModel}" />
<Button Grid.Row="1" Content="Add a custom path" Style="{DynamicResource MaterialDesignRaisedLightButton}" HorizontalAlignment="Center" Width="Auto" Margin="0, 3, 0, 0" />
</Grid>
</Expander>

<Expander Grid.Row="1" Header="Select Patches">
<Expander x:Name="PatchExpander" Grid.Row="1" Header="Select Patches" Expanded="OnExpanderExpand">
<cv:SelectionListView DataContext="{Binding PatchListModel}" />
</Expander>

<Expander Grid.Row="2" Header="Install Patches">
<Expander x:Name="InstallExpander" Grid.Row="2" Header="Install Patches" Expanded="OnExpanderExpand">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
Expand All @@ -31,7 +39,7 @@

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0, 3, 0, 3">
<Button Content="(Re-)Install" />
<Button Content="Uninstall" Style="{DynamicResource MaterialDesignRaisedLightButton}" Margin="5, 0, 0, 0" />
<Button Content="Uninstall" Style="{DynamicResource MaterialDesignRaisedLightButton}" Margin="5, 0, 0, 0" Cursor="Hand" />
</StackPanel>

<RichTextBox Grid.Row="1" x:Name="ConsoleBox" Margin="0, 0, 0, 5" IsUndoEnabled="False" IsReadOnly="True" IsReadOnlyCaretVisible="True">
Expand Down
Loading

0 comments on commit 732ff91

Please sign in to comment.