Skip to content

Commit

Permalink
Enhancement: Added command line
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceiridge committed Apr 4, 2021
1 parent 5a65cfd commit b3f21f3
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 34 deletions.
20 changes: 1 addition & 19 deletions ChromeDevExtWarningPatcher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,5 @@
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;
}
}
public partial class App : Application { }
}
24 changes: 15 additions & 9 deletions ChromeDevExtWarningPatcher/ChromeDevExtWarningPatcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/Ceiridge/Chrome-Developer-Mode-Extension-Warning-Patcher</PackageProjectUrl>
<RepositoryUrl>https://github.com/Ceiridge/Chrome-Developer-Mode-Extension-Warning-Patcher</RepositoryUrl>

<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
<EnableDefaultApplicationDefinition>false</EnableDefaultApplicationDefinition>
<StartupObject>ChromeDevExtWarningPatcher.MainClass</StartupObject>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

<ItemGroup>
Expand All @@ -24,24 +29,25 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<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>
<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>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
Expand Down
15 changes: 15 additions & 0 deletions ChromeDevExtWarningPatcher/CommandLineOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using CommandLine;

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-separated: 0,1,2,etc.)", Separator = ',')]
public IEnumerable<int> Groups { get; set; } = new List<int>();

[Option('w', "noWait", Required = false, HelpText = "Disable the almost-pointless wait after finishing")]
public bool NoWait { get; set; }

[Option("customPath", Required = false, HelpText = "Instead of automatically detecting and patching all chrome.dll files, define a custom Application-folder path (see README) (string in quotes is recommended)")]
public string? CustomPath { get; set; }
}
}
113 changes: 113 additions & 0 deletions ChromeDevExtWarningPatcher/MainClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using ChromeDevExtWarningPatcher.ComponentModels;
using ChromeDevExtWarningPatcher.InstallationFinder;
using ChromeDevExtWarningPatcher.InstallationFinder.Defaults;
using ChromeDevExtWarningPatcher.Patches;
using CommandLine;
using CommandLine.Text;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;

namespace ChromeDevExtWarningPatcher {
public class MainClass {
public static BytePatchManager? BytePatchManager;

private const string ARCH_ERROR = "A 64-bit operating system is required. 32-bit is not supported and won't be in the future.";
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
private static extern bool FreeConsole();

[STAThread]
public static void Main(string[] args) {
bool incompatibleArchitecture = !Environment.Is64BitOperatingSystem;

if (args.Length == 0) { // No command line arguments given => launch the GUI
FreeConsole(); // Hide the console to not interfere with the GUI

if (incompatibleArchitecture) {
MessageBox.Show(ARCH_ERROR, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

App app = new App();
app.InitializeComponent();
app.Run();
} else {
if (incompatibleArchitecture) {
Console.Error.WriteLine(incompatibleArchitecture);
return;
}

MainCmd(args); // Parse the command line and work with it
}
}

private static void MainCmd(string[] args) {
CommandLineOptions? clOptions = null;
ParserResult<CommandLineOptions> result = Parser.Default.ParseArguments<CommandLineOptions>(args).WithParsed(options => {
clOptions = options;
});

if (result.Tag == ParserResultType.NotParsed) { // Show help if wrong/no arguments were given
HelpText.AutoBuild(result);
return;
}

if (clOptions == null) {
return;
}

SelectionListModel groupModel = new SelectionListModel(); // Required to store the grouped patches
BytePatchManager = new BytePatchManager(CustomConsoleWrite, groupModel);

List<InstallationPaths> applicationPaths = new List<InstallationPaths>();
List<int> groups = new List<int>(clOptions.Groups);
List<int> disabledGroups = new List<int>();

if (groups.Count == 0) {
Console.WriteLine("Groups need to be defined. Use --help for help.");
return;
}

if (!string.IsNullOrEmpty(clOptions.CustomPath)) {
if (!Directory.Exists(clOptions.CustomPath)) {
Console.WriteLine("CustomPath not found");
return;
}

applicationPaths.AddRange(new CustomPath(clOptions.CustomPath).FindInstallationPaths());
} else {
applicationPaths.AddRange(new InstallationManager().FindAllChromiumInstallations());
}

foreach (SelectionListElement element in groupModel.ElementList) {
if (element is PatchGroupElement patchGroup && !groups.Contains(patchGroup.Group)) {
disabledGroups.Add(patchGroup.Group);
}
}

if (applicationPaths.Count == 0) {
Console.WriteLine("Error: No patchable browser files found!");
return;
}

try {
PatcherInstaller installer = new PatcherInstaller(applicationPaths);
installer.Install(Console.WriteLine, disabledGroups);
} catch (Exception ex) {
Console.WriteLine("Error while installing patches: " + ex.Message);
}

if (!clOptions.NoWait) {
Thread.Sleep(5000); // Wait a bit to let the user see the result
}
}

private static MessageBoxResult CustomConsoleWrite(string str, string? title = null) {
Console.WriteLine(str);
return MessageBoxResult.OK;
}
}
}
9 changes: 4 additions & 5 deletions ChromeDevExtWarningPatcher/MainView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using ChromeDevExtWarningPatcher.ComponentModels;
using ChromeDevExtWarningPatcher.InstallationFinder;
using ChromeDevExtWarningPatcher.InstallationFinder.Defaults;
using ChromeDevExtWarningPatcher.Patches;
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;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ChromeDevExtWarningPatcher.InstallationFinder.Defaults;
using ChromeDevExtWarningPatcher.Patches;
using Brush = System.Windows.Media.Brush;
using Brushes = System.Windows.Media.Brushes;

Expand Down Expand Up @@ -54,7 +53,7 @@ public partial class MainView : Window {
this.AddInstallationPath(paths);
}

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

private void AddInstallationPath(InstallationPaths paths) {
Expand Down Expand Up @@ -107,7 +106,7 @@ public partial class MainView : Window {

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) {
if (element is PatchGroupElement { IsSelected: true } patchGroup) {
disabledGroups.Add(patchGroup.Group);
}
}
Expand Down
2 changes: 1 addition & 1 deletion ChromeDevExtWarningPatcher/PatcherInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal class PatcherInstaller {
writer.Write(paths.ChromeDllPath!.Length); // Needed, so it takes 4 bytes
writer.Write(Encoding.ASCII.GetBytes(paths.ChromeDllPath));

foreach (BytePatch patch in App.BytePatchManager!.BytePatches) {
foreach (BytePatch patch in MainClass.BytePatchManager!.BytePatches) {
if (disabledGroups.Contains(patch.Group)) {
continue;
}
Expand Down
15 changes: 15 additions & 0 deletions ChromeDevExtWarningPatcher/app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application></application>
</compatibility>
</assembly>

0 comments on commit b3f21f3

Please sign in to comment.