Browse Source

Improved system info gathering, fixed crash report dialog caption, remove debug crash from resize doc dialog and added debug crash in debug menu

CPKreuz 3 years ago
parent
commit
7488142fac

+ 33 - 68
PixiEditor/Helpers/CrashHelper.cs

@@ -1,18 +1,16 @@
-using PixiEditor.Helpers.Extensions;
+using ByteSizeLib;
+using Hardware.Info;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
-using PixiEditor.Parser;
-using PixiEditor.ViewModels;
 using System;
 using System;
-using System.IO;
-using System.IO.Compression;
-using System.Management;
-using System.Runtime.InteropServices;
+using System.Globalization;
 using System.Text;
 using System.Text;
 
 
 namespace PixiEditor.Helpers
 namespace PixiEditor.Helpers
 {
 {
-    public static class CrashHelper
+    public class CrashHelper
     {
     {
+        private readonly IHardwareInfo hwInfo;
+
         public static void SaveCrashInfo(Exception exception)
         public static void SaveCrashInfo(Exception exception)
         {
         {
             CrashReport report = CrashReport.Generate(exception);
             CrashReport report = CrashReport.Generate(exception);
@@ -20,56 +18,54 @@ namespace PixiEditor.Helpers
             report.RestartToCrashReport();
             report.RestartToCrashReport();
         }
         }
 
 
-        public static void GetCPUInformation(StringBuilder builder)
+        public CrashHelper()
         {
         {
-            builder.AppendLine("CPU:");
+            hwInfo = new HardwareInfo();
+        }
 
 
-            ManagementClass processorClass = new("Win32_Processor");
-            ManagementObjectCollection processorsCollection = processorClass.GetInstances();
+        public void GetCPUInformation(StringBuilder builder)
+        {
+            builder.AppendLine("CPU:");
+            hwInfo.RefreshCPUList(false);
 
 
-            foreach (var processor in processorsCollection)
+            foreach (var processor in hwInfo.CpuList)
             {
             {
                 builder
                 builder
-                    .AppendLine($"  ID: {processor.Properties["DeviceID"].Value}")
-                    .AppendLine($"  Name: {processor.Properties["Name"].Value}");
+                    .AppendLine($"  Name: {processor.Name}")
+                    .AppendLine($"  Speed: {(processor.CurrentClockSpeed / 1000f).ToString("F2", CultureInfo.InvariantCulture)} GHz")
+                    .AppendLine($"  Max Speed: {(processor.MaxClockSpeed / 1000f).ToString("F2", CultureInfo.InvariantCulture)} GHz")
+                    .AppendLine();
             }
             }
         }
         }
 
 
-        public static void GetGPUInformation(StringBuilder builder)
+        public void GetGPUInformation(StringBuilder builder)
         {
         {
-            builder.AppendLine("\nGPU:");
-
-            ManagementClass gpuClass = new("Win32_VideoController");
-            ManagementObjectCollection gpuCollection = gpuClass.GetInstances();
+            builder.AppendLine("GPU:");
+            hwInfo.RefreshVideoControllerList();
 
 
-            foreach (var gpu in gpuCollection)
+            foreach (var gpu in hwInfo.VideoControllerList)
             {
             {
                 builder
                 builder
-                    .AppendLine($"  ID: {gpu.Properties["DeviceID"].Value}")
-                    .AppendLine($"  Name: {gpu.Properties["Name"].Value}");
+                    .AppendLine($"  Name: {gpu.Name}")
+                    .AppendLine($"  Driver: {gpu.DriverVersion}")
+                    .AppendLine();
             }
             }
         }
         }
 
 
-        public static void GetMemoryInformation(StringBuilder builder)
+        public void GetMemoryInformation(StringBuilder builder)
         {
         {
-            builder.AppendLine("\nMemory:");
+            builder.AppendLine("Memory:");
+            hwInfo.RefreshMemoryStatus();
 
 
-            // TODO: Make this work
-            if (TryGetMemoryStatus(out MemoryStatus status))
-            {
-                builder.AppendLine($"  Usage: {status.dwMemoryLoad}%");
-                builder.AppendLine($"  Available Memory: {status.ullAvailPhys}");
-                builder.AppendLine($"  Total Memory: {status.ullTotalPhys}");
-            }
-            else
-            {
-                throw new InvalidOperationException($"Getting memory failed: {Marshal.GetLastWin32Error()}");
-            }
+            var memInfo = hwInfo.MemoryStatus;
+
+            builder
+                .AppendLine($"  Available: {new ByteSize(memInfo.AvailablePhysical).ToString("", CultureInfo.InvariantCulture)}")
+                .AppendLine($"  Total: {new ByteSize(memInfo.TotalPhysical).ToString("", CultureInfo.InvariantCulture)}");
         }
         }
 
 
         public static void AddExceptionMessage(StringBuilder builder, Exception e)
         public static void AddExceptionMessage(StringBuilder builder, Exception e)
         {
         {
-
             builder
             builder
                 .AppendLine("\n-------Crash message-------")
                 .AppendLine("\n-------Crash message-------")
                 .Append(e.GetType().ToString())
                 .Append(e.GetType().ToString())
@@ -102,36 +98,5 @@ namespace PixiEditor.Helpers
                 }
                 }
             }
             }
         }
         }
-
-        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
-        private struct MemoryStatus
-        {
-            public uint dwLength;
-            public uint dwMemoryLoad;
-            public ulong ullTotalPhys;
-            public ulong ullAvailPhys;
-            public ulong ullTotalPageFile;
-            public ulong ullAvailPageFile;
-            public ulong ullTotalVirtual;
-            public ulong ullAvailVirtual;
-            public ulong ullAvailExtendedVirtual;
-        }
-
-        private static unsafe bool TryGetMemoryStatus(out MemoryStatus status)
-        {
-            MemoryStatus memoryStatus = new();
-            memoryStatus.dwLength = (uint)sizeof(MemoryStatus);
-
-            bool success = GlobalMemoryStatusEx(memoryStatus);
-
-            status = memoryStatus;
-
-            return success;
-        }
-
-        // https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-globalmemorystatusex
-        [return: MarshalAs(UnmanagedType.Bool)]
-        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
-        private static extern bool GlobalMemoryStatusEx([In, Out] MemoryStatus lpBuffer);
     }
     }
 }
 }

+ 5 - 3
PixiEditor/Models/DataHolders/CrashReport.cs

@@ -27,9 +27,11 @@ namespace PixiEditor.Models.DataHolders
                 .AppendLine($"  OS: {Environment.OSVersion.VersionString}")
                 .AppendLine($"  OS: {Environment.OSVersion.VersionString}")
                 .AppendLine();
                 .AppendLine();
 
 
+            CrashHelper helper = new();
+
             try
             try
             {
             {
-                CrashHelper.GetCPUInformation(builder);
+                helper.GetCPUInformation(builder);
             }
             }
             catch (Exception cpuE)
             catch (Exception cpuE)
             {
             {
@@ -38,7 +40,7 @@ namespace PixiEditor.Models.DataHolders
 
 
             try
             try
             {
             {
-                CrashHelper.GetGPUInformation(builder);
+                helper.GetGPUInformation(builder);
             }
             }
             catch (Exception gpuE)
             catch (Exception gpuE)
             {
             {
@@ -47,7 +49,7 @@ namespace PixiEditor.Models.DataHolders
 
 
             try
             try
             {
             {
-                CrashHelper.GetMemoryInformation(builder);
+                helper.GetMemoryInformation(builder);
             }
             }
             catch (Exception memE)
             catch (Exception memE)
             {
             {

+ 0 - 3
PixiEditor/Models/Dialogs/ResizeDocumentDialog.cs

@@ -10,9 +10,6 @@ namespace PixiEditor.Models.Dialogs
 
 
         public ResizeDocumentDialog(int currentWidth, int currentHeight, bool openResizeCanvas = false)
         public ResizeDocumentDialog(int currentWidth, int currentHeight, bool openResizeCanvas = false)
         {
         {
-            // Remove this
-            throw new System.Exception("Just doing some testing stuff");
-
             Width = currentWidth;
             Width = currentWidth;
             Height = currentHeight;
             Height = currentHeight;
             OpenResizeCanvas = openResizeCanvas;
             OpenResizeCanvas = openResizeCanvas;

+ 2 - 0
PixiEditor/PixiEditor.csproj

@@ -183,11 +183,13 @@
 	</ItemGroup>
 	</ItemGroup>
 	<ItemGroup>
 	<ItemGroup>
 		<PackageReference Include="Dirkster.AvalonDock" Version="4.60.1" />
 		<PackageReference Include="Dirkster.AvalonDock" Version="4.60.1" />
+		<PackageReference Include="ByteSize" Version="2.1.1" />
 		<PackageReference Include="DiscordRichPresence" Version="1.0.175" />
 		<PackageReference Include="DiscordRichPresence" Version="1.0.175" />
 		<PackageReference Include="Expression.Blend.Sdk">
 		<PackageReference Include="Expression.Blend.Sdk">
 			<Version>1.0.2</Version>
 			<Version>1.0.2</Version>
 			<NoWarn>NU1701</NoWarn>
 			<NoWarn>NU1701</NoWarn>
 		</PackageReference>
 		</PackageReference>
+		<PackageReference Include="Hardware.Info" Version="1.1.1.1" />
 		<PackageReference Include="MessagePack" Version="2.3.85" />
 		<PackageReference Include="MessagePack" Version="2.3.85" />
 		<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
 		<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
 		<PackageReference Include="MvvmLightLibs" Version="5.4.1.1">
 		<PackageReference Include="MvvmLightLibs" Version="5.4.1.1">

+ 3 - 0
PixiEditor/ViewModels/SubViewModels/Main/DebugViewModel.cs

@@ -12,11 +12,14 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
 
         public RelayCommand OpenInstallLocationCommand { get; set; }
         public RelayCommand OpenInstallLocationCommand { get; set; }
 
 
+        public RelayCommand CrashCommand { get; set; }
+
         public DebugViewModel(ViewModelMain owner)
         public DebugViewModel(ViewModelMain owner)
             : base(owner)
             : base(owner)
         {
         {
             OpenFolderCommand = new RelayCommand(OpenFolder);
             OpenFolderCommand = new RelayCommand(OpenFolder);
             OpenInstallLocationCommand = new RelayCommand(OpenInstallLocation);
             OpenInstallLocationCommand = new RelayCommand(OpenInstallLocation);
+            CrashCommand = new RelayCommand(_ => throw new InvalidOperationException("Debug Crash"));
         }
         }
 
 
         public static void OpenFolder(object parameter)
         public static void OpenFolder(object parameter)

+ 1 - 1
PixiEditor/Views/Dialogs/CrashReportDialog.xaml

@@ -31,7 +31,7 @@
 
 
     <Grid>
     <Grid>
         <Grid.RowDefinitions>
         <Grid.RowDefinitions>
-            <RowDefinition Height="50"/>
+            <RowDefinition Height="35"/>
             <RowDefinition/>
             <RowDefinition/>
         </Grid.RowDefinitions>
         </Grid.RowDefinitions>
         <DockPanel Grid.Row="0" Background="{StaticResource MainColor}">
         <DockPanel Grid.Row="0" Background="{StaticResource MainColor}">

+ 13 - 0
PixiEditor/Views/Dialogs/CrashReportDialog.xaml.cs

@@ -1,6 +1,7 @@
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.ViewModels;
 using PixiEditor.ViewModels;
 using System.Windows;
 using System.Windows;
+using System.Windows.Input;
 
 
 namespace PixiEditor.Views.Dialogs
 namespace PixiEditor.Views.Dialogs
 {
 {
@@ -14,5 +15,17 @@ namespace PixiEditor.Views.Dialogs
             DataContext = new CrashReportViewModel(report);
             DataContext = new CrashReportViewModel(report);
             InitializeComponent();
             InitializeComponent();
         }
         }
+
+        private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
+        {
+            e.CanExecute = true;
+        }
+
+
+        private void CommandBinding_Executed_Close(object sender, ExecutedRoutedEventArgs e)
+        {
+            SystemCommands.CloseWindow(this);
+        }
+
     }
     }
 }
 }

+ 4 - 3
PixiEditor/Views/MainWindow.xaml

@@ -177,11 +177,12 @@
                               CommandParameter="https://pixieditor.net/docs/Third-party-licenses"/>
                               CommandParameter="https://pixieditor.net/docs/Third-party-licenses"/>
                 </MenuItem>
                 </MenuItem>
                 <MenuItem Header="_Debug" Visibility="{Binding IsDebug, Converter={StaticResource BoolToVisibilityConverter}}">
                 <MenuItem Header="_Debug" Visibility="{Binding IsDebug, Converter={StaticResource BoolToVisibilityConverter}}">
-                    <MenuItem Header="_Open Local App Data" Command="{Binding DebugSubViewModel.OpenFolderCommand}"
+                    <MenuItem Header="Open _Local App Data" Command="{Binding DebugSubViewModel.OpenFolderCommand}"
                               CommandParameter="%LocalAppData%/PixiEditor"/>
                               CommandParameter="%LocalAppData%/PixiEditor"/>
-                    <MenuItem Header="_Open Roaming App Data" Command="{Binding DebugSubViewModel.OpenFolderCommand}"
+                    <MenuItem Header="Open _Roaming App Data" Command="{Binding DebugSubViewModel.OpenFolderCommand}"
                               CommandParameter="%AppData%/PixiEditor"/>
                               CommandParameter="%AppData%/PixiEditor"/>
-                    <MenuItem Header="_Open Install Location"  Command="{Binding DebugSubViewModel.OpenInstallLocationCommand}"/>
+                    <MenuItem Header="Open _Install Location"  Command="{Binding DebugSubViewModel.OpenInstallLocationCommand}"/>
+                    <MenuItem Header="_Crash"  Command="{Binding DebugSubViewModel.CrashCommand}"/>
                 </MenuItem>
                 </MenuItem>
             </Menu>
             </Menu>
             <StackPanel DockPanel.Dock="Right" VerticalAlignment="Top" Orientation="Horizontal" Margin="0,-5,-5,0"
             <StackPanel DockPanel.Dock="Right" VerticalAlignment="Top" Orientation="Horizontal" Margin="0,-5,-5,0"

+ 12 - 3
PixiEditor/Views/MainWindow.xaml.cs

@@ -1,10 +1,12 @@
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.DependencyInjection;
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Helpers.Extensions;
+using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.UserPreferences;
 using PixiEditor.Models.UserPreferences;
 using PixiEditor.ViewModels;
 using PixiEditor.ViewModels;
 using PixiEditor.Views.Dialogs;
 using PixiEditor.Views.Dialogs;
 using System;
 using System;
+using System.Collections.Generic;
 using System.ComponentModel;
 using System.ComponentModel;
 using System.Diagnostics;
 using System.Diagnostics;
 using System.Linq;
 using System.Linq;
@@ -24,13 +26,15 @@ namespace PixiEditor
 
 
         private readonly IPreferences preferences;
         private readonly IPreferences preferences;
 
 
+        private readonly IServiceProvider services;
+
         public static MainWindow Current { get; private set; }
         public static MainWindow Current { get; private set; }
 
 
         public new ViewModelMain DataContext { get => (ViewModelMain)base.DataContext; set => base.DataContext = value; }
         public new ViewModelMain DataContext { get => (ViewModelMain)base.DataContext; set => base.DataContext = value; }
 
 
         public MainWindow()
         public MainWindow()
         {
         {
-            IServiceProvider services = new ServiceCollection()
+            services = new ServiceCollection()
                 .AddPixiEditor()
                 .AddPixiEditor()
                 .BuildServiceProvider();
                 .BuildServiceProvider();
 
 
@@ -69,8 +73,13 @@ namespace PixiEditor
         {
         {
             MainWindow window = new();
             MainWindow window = new();
 
 
-            BitmapManager bitmapManager = window.DataContext.BitmapManager;
-            bitmapManager.Documents.AddRange(documents);
+            BitmapManager bitmapManager = window.services.GetRequiredService<BitmapManager>();
+
+            foreach (Document document in documents)
+            {
+                bitmapManager.Documents.Add(document);
+            }
+
             bitmapManager.ActiveDocument = bitmapManager.Documents.FirstOrDefault();
             bitmapManager.ActiveDocument = bitmapManager.Documents.FirstOrDefault();
 
 
             return window;
             return window;