Skip to content

Commit

Permalink
Enhancement: Made installing and uninstalling possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceiridge committed Apr 3, 2021
1 parent 2f6c1d6 commit 5a65cfd
Show file tree
Hide file tree
Showing 11 changed files with 550 additions and 14 deletions.
21 changes: 20 additions & 1 deletion ChromeDevExtWarningPatcher/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
using System.Windows;
using System;
using System.Windows;
using ChromeDevExtWarningPatcher.Patches;

namespace ChromeDevExtWarningPatcher {
public partial class App : Application {
public static BytePatchManager? BytePatchManager;

public App() {
string[] args = Environment.GetCommandLineArgs(); // First element is the program's name

if (args.Length <= 1) { // Start gui
return;
}

BytePatchManager = new BytePatchManager(CustomConsoleWrite);
Environment.Exit(0); // Exit to prevent the UI from starting
}

private static MessageBoxResult CustomConsoleWrite(string str, string? title = null) {
Console.WriteLine(str);
return MessageBoxResult.OK;
}
}
}
18 changes: 17 additions & 1 deletion ChromeDevExtWarningPatcher/ChromeDevExtWarningPatcher.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
Expand All @@ -26,6 +26,22 @@
<ItemGroup>
<PackageReference Include="MaterialDesignThemes" Version="4.0.0" />
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
<PackageReference Include="TaskScheduler" Version="2.9.1" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
Expand Down
11 changes: 11 additions & 0 deletions ChromeDevExtWarningPatcher/ComponentModels/InstallationElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using ChromeDevExtWarningPatcher.InstallationFinder;

namespace ChromeDevExtWarningPatcher.ComponentModels {
public class InstallationElement : SelectionListElement {
public InstallationPaths Paths { get; set; }

public InstallationElement(string name, InstallationPaths paths) : base(name) {
this.Paths = paths;
}
}
}
12 changes: 12 additions & 0 deletions ChromeDevExtWarningPatcher/ComponentModels/PatchGroupElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations;

namespace ChromeDevExtWarningPatcher.ComponentModels {
public class PatchGroupElement : SelectionListElement {
[Required]
public int Group { get; set; }

public PatchGroupElement(string name, int group) : base(name) {
this.Group = group;
}
}
}
3 changes: 3 additions & 0 deletions ChromeDevExtWarningPatcher/InstallationFinder/Installation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@ internal abstract class Installation {
public static double GetUnixTime(DateTime date) {
return (date.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
}
public static double GetUnixTime() {
return GetUnixTime(DateTime.UtcNow);
}
}
}
8 changes: 4 additions & 4 deletions ChromeDevExtWarningPatcher/MainView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<Expander x:Name="BrowserExpander" Header="Select Browsers" IsExpanded="True" Expanded="OnExpanderExpand">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="85*" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

Expand All @@ -35,15 +35,15 @@
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="75*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0, 3, 0, 3">
<Button x:Name="InstallButton" Content="(Re-)Install" Click="OnInstall" />
<Button x:Name="UninstallButton" Content="Uninstall" Style="{DynamicResource MaterialDesignRaisedLightButton}" Margin="5, 0, 0, 0" />
<Button x:Name="UninstallButton" Content="Uninstall" Style="{DynamicResource MaterialDesignRaisedLightButton}" Margin="5, 0, 0, 0" Cursor="Hand" Click="OnUninstall" />
</StackPanel>

<RichTextBox Grid.Row="1" x:Name="ConsoleBox" Margin="0, 0, 0, 5" IsUndoEnabled="False" IsReadOnly="True" IsReadOnlyCaretVisible="True">
<RichTextBox Grid.Row="1" x:Name="ConsoleBox" Margin="5, 0, 5, 5" IsUndoEnabled="False" IsReadOnly="True" IsReadOnlyCaretVisible="True">
<FlowDocument LineHeight="1">
<Paragraph>
<Run Text="Console" />
Expand Down
74 changes: 70 additions & 4 deletions ChromeDevExtWarningPatcher/MainView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using ChromeDevExtWarningPatcher.ComponentModels;
using ChromeDevExtWarningPatcher.InstallationFinder;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
Expand All @@ -11,12 +14,12 @@
using ChromeDevExtWarningPatcher.InstallationFinder.Defaults;
using ChromeDevExtWarningPatcher.Patches;
using Brush = System.Windows.Media.Brush;
using Brushes = System.Windows.Media.Brushes;

namespace ChromeDevExtWarningPatcher {
public partial class MainView : Window {
private readonly MainModel mainModel = new MainModel();
private readonly InstallationManager installationManager = new InstallationManager();
private BytePatchManager? bytePatchManager;

public MainView() {
this.InitializeComponent();
Expand All @@ -36,6 +39,10 @@ public partial class MainView : Window {
});
}

private void Log(string str) { // For delegates
this.Log(str, null);
}

protected override void OnInitialized(EventArgs e) {
base.OnInitialized(e);
this.ConsoleBox.Document.Blocks.Clear();
Expand All @@ -47,14 +54,14 @@ public partial class MainView : Window {
this.AddInstallationPath(paths);
}

this.bytePatchManager = new BytePatchManager(MessageBox.Show, this.mainModel.PatchListModel);
App.BytePatchManager = new BytePatchManager(MessageBox.Show, this.mainModel.PatchListModel);
}

private void AddInstallationPath(InstallationPaths paths) {
Icon? icon = System.Drawing.Icon.ExtractAssociatedIcon(paths.ChromeExePath!);
ImageSource? source = icon != null ? Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()) : null; // Turn the icon into an ImageSource for the WPF gui

this.mainModel.BrowserListModel.ElementList.Add(new SelectionListElement(paths.Name) {
this.mainModel.BrowserListModel.ElementList.Add(new InstallationElement(paths.Name, paths) {
Description = paths.ChromeDllPath,
IconImage = source,
Tooltip = paths.ChromeExePath + " & " + paths.ChromeDllPath,
Expand All @@ -79,10 +86,69 @@ public partial class MainView : Window {
}
}

private void DisableButtons(bool disable) {
this.InstallButton.Dispatcher.Invoke(() => {
this.InstallButton.IsEnabled = this.UninstallButton.IsEnabled = !disable;
});
}

private List<InstallationPaths> GetEnabledInstallationPaths() {
List<InstallationPaths> installPaths = new List<InstallationPaths>();
foreach (SelectionListElement element in this.mainModel.BrowserListModel.ElementList) {
if (element is InstallationElement { IsSelected: true } installation) {
installPaths.Add(installation.Paths);
}
}
return installPaths;
}

private void OnInstall(object sender, RoutedEventArgs e) {
this.InstallButton.IsEnabled = this.UninstallButton.IsEnabled = false;
this.DisableButtons(true);

List<int> disabledGroups = new List<int>(); // Get all disabled patch groups from the UI
foreach (SelectionListElement element in this.mainModel.PatchListModel.ElementList) {
if (element is PatchGroupElement {IsSelected: true} patchGroup) {
disabledGroups.Add(patchGroup.Group);
}
}

new Thread((() => {
try {
List<InstallationPaths> installPaths = this.GetEnabledInstallationPaths();
PatcherInstaller installer = new PatcherInstaller(installPaths);
if (installer.Install(this.Log, disabledGroups)) {
foreach (InstallationPaths paths in installPaths) {
this.Log($"Successfully installed to {paths.ChromeExePath}", Brushes.Green);
}
}
} catch (Exception exception) {
this.Log("Error while installing: " + exception.Message, Brushes.Red);
}
this.DisableButtons(false);
})).Start();
}

private void OnUninstall(object sender, RoutedEventArgs e) {
this.DisableButtons(true);

new Thread((() => {
try {
List<InstallationPaths> installPaths = this.GetEnabledInstallationPaths();
PatcherInstaller installer = new PatcherInstaller(installPaths);
if (installer.UninstallAll(this.Log)) {
foreach (InstallationPaths paths in installPaths) {
this.Log($"Successfully uninstalled from {paths.ChromeExePath}", Brushes.Green);
}
}
} catch (Exception exception) {
this.Log("Error while uninstalling: " + exception.Message, Brushes.Red);
}
this.DisableButtons(false);
})).Start();
}
}
}

0 comments on commit 5a65cfd

Please sign in to comment.