Skip to content

Commit 0cc37e3

Browse files
shiroinekotfsVLTA of @TheFlightSimsanhvlttfs
authored
Phase 2 of DDA (#3)
## What's new? - Added new `ExceptionView` to match with the template [Get WMI Basic](https://github.com/tfslabs/get-wmi-basic) (a7684b5) - Updated for `AboutForm` and `ConnectForm` to match with the template [Get WMI Basic](https://github.com/tfslabs/get-wmi-basic) (1e2a6d2 and f10a585) - Fix for better UI, with enough spacing and margin between objects - Fix for disposing resources (8788c5b, 60222a9, and 788d5f3) - For developers: added comments as design documentation, so you can understand what behind the logic ## Assignment Developers: @shiroinekotfs and @anhvlttfs Reviewer: @anhvlttfs Moderator: @TheFlightSimsOfficial --------- Co-authored-by: VLTA of @TheFlightSims <anhvlt.2006@theflightsims.com> Co-authored-by: Vo Luu Tuong Anh <anhvlt.2006@outlook.com> Co-authored-by: anhvlt-2k6 <176574466+anhvlt-2k6@users.noreply.github.com> Co-authored-by: VLTA of @TheFlightSims <176574466+anhvlttfs@users.noreply.github.com>
1 parent 00896d3 commit 0cc37e3

22 files changed

+1729
-697
lines changed

About.xaml.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

AddDevice.xaml.cs

Lines changed: 83 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,70 @@
22
using System.Linq;
33
using System.Management;
44
using System.Windows;
5+
6+
using TheFlightSims.HyperVDPD.DefaultUI;
57
using TheFlightSims.HyperVDPD.WMIProperties;
68

9+
/*
10+
* Primary namespace for HyperV-DPD application
11+
* It contains the main window and all related methods for the core application
12+
*/
713
namespace TheFlightSims.HyperVDPD
814
{
15+
/*
16+
* Add Device Window class
17+
* It is used to add device into the selected VM
18+
*/
919
public partial class AddDevice : Window
1020
{
21+
////////////////////////////////////////////////////////////////
22+
/// Global Properties and Constructors Region
23+
/// This region contains global properties and constructors
24+
/// for the MachineMethods class.
25+
////////////////////////////////////////////////////////////////
26+
27+
/*
28+
* Global properties
29+
* deviceId: Device ID of the selected device
30+
* machine: WMI namespace of the machine
31+
*/
32+
1133
protected string deviceId;
1234
protected MachineMethods machine;
1335

36+
/*
37+
* Constructor of the MainWindow class
38+
* Initializes the components and sets up event handlers
39+
*/
1440
public AddDevice(MachineMethods machine)
1541
{
1642
this.machine = machine;
1743
machine.Connect("root\\cimv2");
1844
InitializeComponent();
1945
}
2046

47+
48+
////////////////////////////////////////////////////////////////
49+
/// User Action Methods Region
50+
/// This region contains methods that handle user actions.
51+
/// For example, button clicks, changes in order.
52+
////////////////////////////////////////////////////////////////
53+
2154
/*
22-
* Button and UI methods behaviour....
55+
* Actions for button "Add Device"
2356
*/
24-
2557
private void AddDeviceButton_Click(object sender, RoutedEventArgs e)
2658
{
59+
// If the selected item is not null (nothing is selected), proceed
2760
if (DeviceList.SelectedItem != null)
2861
{
2962
deviceId = DeviceList.SelectedItem.GetType().GetProperty("DeviceId").GetValue(DeviceList.SelectedItem, null).ToString();
3063
DialogResult = true;
3164
}
3265
else
3366
{
34-
MessageBox.Show(
67+
// Notify user if nothing is selected
68+
_ = MessageBox.Show(
3569
"Please select a device to add.",
3670
"Warning",
3771
MessageBoxButton.OK,
@@ -40,53 +74,78 @@ private void AddDeviceButton_Click(object sender, RoutedEventArgs e)
4074
}
4175
}
4276

77+
/*
78+
* Actions for button "Close"
79+
*/
4380
private void AddDeviceCloseButton_Click(object sender, RoutedEventArgs e)
4481
{
4582
Close();
4683
}
4784

85+
////////////////////////////////////////////////////////////////
86+
/// Non-User Action Methods Region
87+
///
88+
/// This region contains methods that do not handle user actions.
89+
///
90+
/// Think about this is the back-end section.
91+
/// It should not be in a seperated class, because it directly interacts with the UI elements.
92+
////////////////////////////////////////////////////////////////
93+
4894
/*
49-
* Non-button methods
95+
* Start getting the device ID
5096
*/
51-
5297
public string GetDeviceId()
5398
{
99+
54100
UpdateDevices();
55-
ShowDialog();
101+
_ = ShowDialog();
56102

103+
// Return the device ID back to the main windows
57104
return deviceId;
58105
}
59106

60-
private void UpdateDevices()
107+
/*
108+
* Update device list
109+
*/
110+
private async void UpdateDevices()
61111
{
112+
// Clear the device list
62113
DeviceList.Items.Clear();
63114

64115
try
65116
{
66-
foreach (ManagementObject device in machine.GetObjects("Win32_PnPEntity", "Status, PNPClass, Name, DeviceID").Cast<ManagementObject>())
117+
// Loop for devices in Win32_PnPEntity
118+
using (ManagementObjectCollection pnpEntityList = machine.GetObjects("Win32_PnPEntity", "Status, PNPClass, Name, DeviceID"))
67119
{
68-
string deviceStatus = device["Status"]?.ToString() ?? "Unknown";
69-
string deviceType = device["PNPClass"]?.ToString() ?? "Unknown";
70-
string deviceName = device["Name"]?.ToString() ?? "Unknown";
71-
string deviceId = device["DeviceID"]?.ToString() ?? "Unknown";
72-
73-
if (!deviceId.StartsWith("PCI"))
120+
foreach (ManagementObject device in pnpEntityList.Cast<ManagementObject>())
74121
{
75-
continue;
76-
}
122+
string deviceStatus = device["Status"]?.ToString() ?? "Unknown";
123+
string deviceType = device["PNPClass"]?.ToString() ?? "Unknown";
124+
string deviceName = device["Name"]?.ToString() ?? "Unknown";
125+
string deviceId = device["DeviceID"]?.ToString() ?? "Unknown";
77126

78-
DeviceList.Items.Add(new
79-
{
80-
DeviceStatus = deviceStatus,
81-
DeviceType = deviceType,
82-
DeviceName = deviceName,
83-
DeviceId = deviceId
84-
});
127+
// If the device is not PCI, skip to the next device
128+
if (!deviceId.StartsWith("PCI"))
129+
{
130+
device.Dispose();
131+
continue;
132+
}
133+
134+
_ = DeviceList.Items.Add(new
135+
{
136+
DeviceStatus = deviceStatus,
137+
DeviceType = deviceType,
138+
DeviceName = deviceName,
139+
DeviceId = deviceId
140+
});
141+
142+
device.Dispose();
143+
}
85144
}
86145
}
87146
catch (Exception ex)
88147
{
89-
WMIDefaultValues.HandleException(ex, machine.GetComputerName());
148+
(new ExceptionView()).HandleException(ex);
90149
}
91150
}
92151
}

ChangeMemorySpace.xaml.cs

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,60 @@
1-
using System;
2-
using System.Windows;
1+
using System.Windows;
32

3+
/*
4+
* Primary namespace for HyperV-DPD application
5+
* It contains the main window and all related methods for the core application
6+
*/
47
namespace TheFlightSims.HyperVDPD
58
{
9+
/*
10+
* Change Memory Space Window
11+
*/
612
public partial class ChangeMemorySpace : Window
713
{
14+
////////////////////////////////////////////////////////////////
15+
/// Global Properties and Constructors Region
16+
/// This region contains global properties and constructors
17+
/// for the MachineMethods class.
18+
////////////////////////////////////////////////////////////////
19+
820
/*
921
* Global properties
22+
* memRange as tuple of low and high memory
1023
*/
11-
protected (UInt64 lowMem, UInt64 highMem) memRange;
24+
protected (ulong lowMem, ulong highMem) memRange;
1225

26+
// Constructor of the Change Memory space window
1327
public ChangeMemorySpace(string vmName)
1428
{
1529
Title += $" for {vmName}";
1630
InitializeComponent();
1731
}
1832

33+
////////////////////////////////////////////////////////////////
34+
/// User Action Methods Region
35+
/// This region contains methods that handle user actions.
36+
/// For example, button clicks, changes in order.
37+
////////////////////////////////////////////////////////////////
38+
1939
/*
20-
* Button-based methods
40+
* Actions for button "Confirm"
2141
*/
2242
private void Confirm_Button(object sender, RoutedEventArgs e)
2343
{
24-
if (UInt64.TryParse(LowMemory_TextBox.Text, out UInt64 lowMemCompare) && UInt64.TryParse(HighMemory_TextBox.Text, out UInt64 highMemCompare))
44+
// Try to parse number in the box
45+
if (ulong.TryParse(LowMemory_TextBox.Text, out ulong lowMemCompare) && ulong.TryParse(HighMemory_TextBox.Text, out ulong highMemCompare))
2546
{
26-
if ((lowMemCompare >= 128 && lowMemCompare <= 3584) && (highMemCompare >= 4096 && highMemCompare <= (UInt64.MaxValue - 2)))
47+
// Check for low MMIO and high MMIO. If valid, return the value
48+
if ((lowMemCompare >= 128 && lowMemCompare <= 3584) && (highMemCompare >= 4096 && highMemCompare <= (ulong.MaxValue - 2)))
2749
{
2850
memRange.lowMem = lowMemCompare;
2951
memRange.highMem = highMemCompare;
3052
DialogResult = true;
3153
}
3254
else
3355
{
34-
MessageBox.Show(
56+
// Notify user for invalid valid input
57+
_ = MessageBox.Show(
3558
"Make sure the Low MMIO Gap is in range (128, 3584) and High MMIO is larger than 4096",
3659
"Warning",
3760
MessageBoxButton.OK,
@@ -41,7 +64,7 @@ private void Confirm_Button(object sender, RoutedEventArgs e)
4164
}
4265
else
4366
{
44-
MessageBox.Show(
67+
_ = MessageBox.Show(
4568
"Please enter a positive integer in both box.",
4669
"Warning",
4770
MessageBoxButton.OK,
@@ -50,17 +73,29 @@ private void Confirm_Button(object sender, RoutedEventArgs e)
5073
}
5174
}
5275

76+
/*
77+
* Action for button "Cancel"
78+
*/
5379
private void Cancel_Button(object sender, RoutedEventArgs e)
5480
{
5581
Close();
5682
}
5783

84+
////////////////////////////////////////////////////////////////
85+
/// Non-User Action Methods Region
86+
///
87+
/// This region contains methods that do not handle user actions.
88+
///
89+
/// Think about this is the back-end section.
90+
/// It should not be in a seperated class, because it directly interacts with the UI elements.
91+
////////////////////////////////////////////////////////////////
92+
5893
/*
59-
* Non-button methods
94+
* Return Value
6095
*/
61-
public (UInt64, UInt64) ReturnValue()
96+
public (ulong, ulong) ReturnValue()
6297
{
63-
ShowDialog();
98+
_ = ShowDialog();
6499

65100
return memRange;
66101
}

0 commit comments

Comments
 (0)