Browse Source

Added helpers to deal with hardware info & A1111 ui settings. also removed unused usings

pull/5/head
JT 2 years ago
parent
commit
cc63c1e239
  1. 15
      StabilityMatrix/App.xaml.cs
  2. 29
      StabilityMatrix/Helper/A1111Helper.cs
  3. 34
      StabilityMatrix/Helper/HardwareHelper.cs
  4. 10
      StabilityMatrix/Helper/ProcessRunner.cs
  5. 1
      StabilityMatrix/InstallPage.xaml
  6. 14
      StabilityMatrix/InstallPage.xaml.cs
  7. 11
      StabilityMatrix/LaunchPage.xaml.cs
  8. 11
      StabilityMatrix/MainWindow.xaml.cs
  9. 34
      StabilityMatrix/ViewModels/InstallerViewModel.cs

15
StabilityMatrix/App.xaml.cs

@ -1,22 +1,7 @@
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Xaml.Shapes;
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Core; using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
namespace StabilityMatrix namespace StabilityMatrix
{ {

29
StabilityMatrix/Helper/A1111Helper.cs

@ -0,0 +1,29 @@
using System;
namespace StabilityMatrix.Helper;
public static class A1111Helper
{
public static string GetVramOption()
{
var vramGb = HardwareHelper.GetGpuMemoryBytes() / 1024 / 1024 / 1024;
return vramGb switch
{
< 4 => "--lowvram",
< 8 => "--medvram",
_ => string.Empty
};
}
public static string GetXformersOption()
{
var gpuName = HardwareHelper.GetGpuChipName();
if (gpuName.Contains("NVIDIA", StringComparison.OrdinalIgnoreCase))
{
return "--xformers";
}
return string.Empty;
}
}

34
StabilityMatrix/Helper/HardwareHelper.cs

@ -0,0 +1,34 @@
using System;
using Microsoft.Win32;
namespace StabilityMatrix.Helper;
public class HardwareHelper
{
public static ulong GetGpuMemoryBytes()
{
var registry = Registry.LocalMachine;
var key = registry.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0000", false);
if (key == null)
{
return 0;
}
var vram = key.GetValue("HardwareInformation.qwMemorySize");
var vramLong = Convert.ToUInt64(vram);
return vramLong;
}
public static string GetGpuChipName()
{
var registry = Registry.LocalMachine;
var key = registry.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0000", false);
if (key == null)
{
return "Unknown";
}
var gpuName = key.GetValue("HardwareInformation.ChipType");
return gpuName?.ToString() ?? "Unknown";
}
}

10
StabilityMatrix/Helper/ProcessRunner.cs

@ -1,13 +1,11 @@
using System; using System.Diagnostics;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StabilityMatrix.Helper; namespace StabilityMatrix.Helper;
public static class ProcessRunner public static class ProcessRunner
{ {
public static async Task<string> RunProcessAsync(string fileName, string arguments) public static async Task<string> GetProcessOutputAsync(string fileName, string arguments)
{ {
using var process = new Process(); using var process = new Process();
process.StartInfo.FileName = fileName; process.StartInfo.FileName = fileName;
@ -23,10 +21,10 @@ public static class ProcessRunner
return output; return output;
} }
public static Process RunProcess(string fileName, string arguments) public static Process StartProcess(string fileName, string arguments)
{ {
using var process = new Process(); var process = new Process();
process.StartInfo.FileName = fileName; process.StartInfo.FileName = fileName;
process.StartInfo.Arguments = arguments; process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false; process.StartInfo.UseShellExecute = false;

1
StabilityMatrix/InstallPage.xaml

@ -43,6 +43,7 @@
<Button Content="Install" Command="{Binding InstallCommand}" Width="100" Height="50" HorizontalAlignment="Center"/> <Button Content="Install" Command="{Binding InstallCommand}" Width="100" Height="50" HorizontalAlignment="Center"/>
<TextBlock Text="{Binding InstalledText}" HorizontalAlignment="Center" Padding="8"/> <TextBlock Text="{Binding InstalledText}" HorizontalAlignment="Center" Padding="8"/>
<ProgressBar Visibility="{Binding ProgressBarVisibility}" Value="{Binding ProgressValue, FallbackValue=10}" <ProgressBar Visibility="{Binding ProgressBarVisibility}" Value="{Binding ProgressValue, FallbackValue=10}"
IsIndeterminate="{Binding IsIndeterminate, FallbackValue=True}"
Maximum="100" Width="500" Style="{ThemeResource DefaultProgressBarStyle}"/> Maximum="100" Width="500" Style="{ThemeResource DefaultProgressBarStyle}"/>
</StackPanel> </StackPanel>
</Grid> </Grid>

14
StabilityMatrix/InstallPage.xaml.cs

@ -1,21 +1,7 @@
// Copyright (c) Microsoft Corporation and Contributors. // Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License. // Licensed under the MIT License.
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using StabilityMatrix.ViewModels; using StabilityMatrix.ViewModels;
// To learn more about WinUI, the WinUI project structure, // To learn more about WinUI, the WinUI project structure,

11
StabilityMatrix/LaunchPage.xaml.cs

@ -1,17 +1,6 @@
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System; using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
// To learn more about WinUI, the WinUI project structure, // To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info. // and more about our project templates, see: http://aka.ms/winui-project-info.

11
StabilityMatrix/MainWindow.xaml.cs

@ -1,18 +1,7 @@
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System; using System;
using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using WinRT.Interop;
namespace StabilityMatrix namespace StabilityMatrix
{ {

34
StabilityMatrix/ViewModels/InstallerViewModel.cs

@ -1,15 +1,10 @@
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Threading.Tasks; using System.Threading.Tasks;
using Windows.UI.Popups;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using StabilityMatrix.Helper; using StabilityMatrix.Helper;
using StabilityMatrix.Models; using StabilityMatrix.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
namespace StabilityMatrix.ViewModels; namespace StabilityMatrix.ViewModels;
@ -18,6 +13,7 @@ internal class InstallerViewModel : INotifyPropertyChanged
{ {
private string installedText; private string installedText;
private int progressValue; private int progressValue;
private bool isIndeterminate;
public InstallerViewModel() public InstallerViewModel()
{ {
@ -53,31 +49,47 @@ internal class InstallerViewModel : INotifyPropertyChanged
} }
} }
public bool IsIndeterminate
{
get => isIndeterminate;
set
{
if (value == isIndeterminate) return;
isIndeterminate = value;
OnPropertyChanged();
}
}
public Visibility ProgressBarVisibility => ProgressValue > 0 ? Visibility.Visible : Visibility.Collapsed; public Visibility ProgressBarVisibility => ProgressValue > 0 ? Visibility.Visible : Visibility.Collapsed;
public AsyncRelayCommand InstallCommand => new(InstallGitIfNecessary); public AsyncRelayCommand InstallCommand => new(InstallGitIfNecessary);
private async Task InstallGitIfNecessary() private async Task InstallGitIfNecessary()
{ {
var gitOutput = await ProcessRunner.RunProcessAsync("git", "--version"); var gitOutput = await ProcessRunner.GetProcessOutputAsync("git", "--version");
if (gitOutput.Contains("git version 2")) if (gitOutput.Contains("git version 2"))
{ {
InstalledText = "Installed"; InstalledText = "Installed";
IsIndeterminate = false;
ProgressValue = 100; ProgressValue = 100;
return; return;
} }
InstalledText = "Not Installed"; InstalledText = "Not Installed";
var installProcess = ProcessRunner.RunProcess("Assets\\Git-2.40.1-64-bit.exe", "/SILENT /NORESTART"); IsIndeterminate = true;
while (!installProcess.StandardOutput.EndOfStream && !installProcess.HasExited) using var installProcess = ProcessRunner.StartProcess("Assets\\Git-2.40.1-64-bit.exe", "/VERYSILENT /NORESTART");
{ await installProcess.WaitForExitAsync();
Debug.WriteLine(await installProcess.StandardOutput.ReadLineAsync());
}
if (installProcess.ExitCode == 0) if (installProcess.ExitCode == 0)
{ {
InstalledText = "Git successfully installed"; InstalledText = "Git successfully installed";
ProgressValue = 100; ProgressValue = 100;
} }
else
{
InstalledText = $"There was an error installing Git: {installProcess.ExitCode}";
ProgressValue = 0;
}
IsIndeterminate = false;
} }
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;

Loading…
Cancel
Save