Skip to content

Commit

Permalink
Update tests, refactor code, update library
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingWonders committed Nov 24, 2024
1 parent 1fa4028 commit 2a3d3ee
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 62 deletions.
5 changes: 3 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ static async Task Main(string[] args)

List<SystemComponent> defaultComponents = new List<SystemComponent>();
// Add Microsoft-Windows-Shell-Setup in oobeSystem pass. It's already filled in, but add it anyway
SystemComponent defaultComponent = new SystemComponent("Microsoft-Windows-Shell-Setup");
defaultComponent.Passes.Add(new SystemPass("oobeSystem"));
List<SystemPass> defaultPasses = new List<SystemPass>();
defaultPasses.Add(new SystemPass("oobeSystem"));
SystemComponent defaultComponent = new SystemComponent("Microsoft-Windows-Shell-Setup", defaultPasses);
defaultComponents.Add(defaultComponent);

Console.WriteLine($"UnattendGen{(File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DT")) ? " for DISMTools" : "")}, version {Assembly.GetEntryAssembly().GetName().Version.ToString()}");
Expand Down
2 changes: 2 additions & 0 deletions Tests/userAccounts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
<UserAccount Enabled="1" Name="Mads Kristensen" Password="Test4" Group="Admins" />
<UserAccount Enabled="1" Name="Bill Gates" Password="Test5" Group="Admins" />
<UserAccount Enabled="1" Name="Steve Ballmer" Password="Test6" Group="Admins" />
<UserAccount Enabled="1" Name="Satya Nadella" Password="Test7" Group="Users" />
<UserAccount Enabled="1" Name="David William Plummer" Password="Test8" Group="Users" />
</root>
55 changes: 55 additions & 0 deletions UserSettings/PartitionMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,64 @@

namespace UnattendGen.UserSettings
{
/// <summary>
/// Partition settings for Disk 0
/// </summary>
public class DiskZeroSettings
{
public enum PartitionStyle
{
/// <summary>
/// MBR partition table, used by default on systems that use BIOS instead of UEFI
/// </summary>
MBR,
/// <summary>
/// GUID Partition Table, used by default on newer systems with UEFI firmware
/// </summary>
GPT
}

public enum RecoveryEnvironmentMode
{
/// <summary>
/// The Recovery Environment will not be present
/// </summary>
None,
/// <summary>
/// The Recovery Environment will be installed on a separate disk partition
/// </summary>
Partition,
/// <summary>
/// The Recovery Environment will be installed on the Windows installation
/// </summary>
Windows
}

/// <summary>
/// The style of the partition table
/// </summary>
public PartitionStyle partStyle;

/// <summary>
/// The size of the EFI System Partition, in MB
/// </summary>
public int ESPSize;

/// <summary>
/// The availability and location of the Recovery Environment (WinRE)
/// </summary>
public RecoveryEnvironmentMode recoveryEnvironment;

/// <summary>
/// The size of the Recovery Environment partition, in MB
/// </summary>
public int recEnvSize;

/// <summary>
/// Loads the disk settings specified by the user in a configuration file
/// </summary>
/// <param name="filePath">The path of the configuration file</param>
/// <returns>The disk settings</returns>
public static DiskZeroSettings? LoadDiskSettings(string filePath)
{
DiskZeroSettings diskZero = new DiskZeroSettings();
Expand Down Expand Up @@ -99,16 +134,36 @@ public enum RecoveryEnvironmentMode

}

/// <summary>
/// Partition settings for DiskPart scripts
/// </summary>
public class DiskPartSettings
{
/// <summary>
/// The path of the specified DiskPart script
/// </summary>
public string? scriptFile;

/// <summary>
/// Determine whether or not to install Windows to the first available partition that has enough space and does not already contain an installation of Windows, after DiskPart configuration has finished
/// </summary>
public bool automaticInstall;

/// <summary>
/// Disk number for OS installation. Only used when <see cref="automaticInstall"/> is set to false
/// </summary>
public int diskNum;

/// <summary>
/// Partition number for OS installation. Only used when <see cref="automaticInstall"/> is set to false
/// </summary>
public int partNum;

/// <summary>
/// Loads the DiskPart settings specified by the user in a configuration file
/// </summary>
/// <param name="filePath">The path of the configuration file</param>
/// <returns>The DiskPart configuration</returns>
public static DiskPartSettings? LoadDiskSettings(string filePath)
{
DiskPartSettings diskPart = new DiskPartSettings();
Expand Down
30 changes: 11 additions & 19 deletions UserSettings/RegionFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ public GeoIds(string id, string displayName)
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "GeoId")
{
GeoIds geo = new GeoIds();
geo.Id = reader.GetAttribute("Id");
geo.DisplayName = reader.GetAttribute("DisplayName");
GeoIds geo = new GeoIds(reader.GetAttribute("Id"), reader.GetAttribute("DisplayName"));
geoList.Add(geo);
}
}
Expand Down Expand Up @@ -115,9 +113,7 @@ public ImageLanguages(string id, string displayName)
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "ImageLanguage")
{
ImageLanguages lang = new ImageLanguages();
lang.Id = reader.GetAttribute("Id");
lang.DisplayName = reader.GetAttribute("DisplayName");
ImageLanguages lang = new ImageLanguages(reader.GetAttribute("Id"), reader.GetAttribute("DisplayName"));
langList.Add(lang);
}
}
Expand Down Expand Up @@ -174,10 +170,9 @@ public KeyboardIdentifiers(string id, string displayName, string type)
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "KeyboardIdentifier")
{
KeyboardIdentifiers keyboard = new KeyboardIdentifiers();
keyboard.Id = reader.GetAttribute("Id");
keyboard.DisplayName = reader.GetAttribute("DisplayName");
keyboard.Type = reader.GetAttribute("Type");
KeyboardIdentifiers keyboard = new KeyboardIdentifiers(reader.GetAttribute("Id"),
reader.GetAttribute("DisplayName"),
reader.GetAttribute("Type"));
keyboardList.Add(keyboard);
}
}
Expand Down Expand Up @@ -232,9 +227,7 @@ public TimeOffsets(string id, string displayName)
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "TimeOffset")
{
TimeOffsets offset = new TimeOffsets();
offset.Id = reader.GetAttribute("Id");
offset.DisplayName = reader.GetAttribute("DisplayName");
TimeOffsets offset = new TimeOffsets(reader.GetAttribute("Id"), reader.GetAttribute("DisplayName"));
offsetList.Add(offset);
}
}
Expand Down Expand Up @@ -299,12 +292,11 @@ public UserLocales(string id, string displayName, string lcid, string keybId, st
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "UserLocale")
{
UserLocales locale = new UserLocales();
locale.Id = reader.GetAttribute("Id");
locale.DisplayName = reader.GetAttribute("DisplayName");
locale.LCID = reader.GetAttribute("LCID");
locale.KeybId = reader.GetAttribute("KeyboardLayout");
locale.GeoLoc = reader.GetAttribute("GeoLocation");
UserLocales locale = new UserLocales(reader.GetAttribute("Id"),
reader.GetAttribute("DisplayName"),
reader.GetAttribute("LCID"),
reader.GetAttribute("KeyboardLayout"),
reader.GetAttribute("GeoLocation"));
localeList.Add(locale);
}
}
Expand Down
15 changes: 12 additions & 3 deletions UserSettings/SystemComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ public class SystemComponent
public SystemComponent() { }

public SystemComponent(string? id)
{
this.Id = id;
}

public SystemComponent(string? id, List<SystemPass>? passes)
{
Id = id;
Passes = passes;
}

public static List<SystemComponent>? LoadComponents(string? filePath)
Expand All @@ -39,12 +45,12 @@ public SystemComponent(string? id)
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "Component")
{
SystemComponent component = new SystemComponent();
component.Id = reader.GetAttribute("Id");
string passList = reader.GetAttribute("Passes");
List<String> passListTemp = new List<String>();
passListTemp = passList.Split(new char[] { ',' }).ToList();

List<SystemPass> passes = new List<SystemPass>();

List<string> knownPasses =
[
.. new string[] { "offlineServicing", "windowsPE", "generalize", "specialize", "auditSystem", "auditUser", "oobeSystem" },
Expand All @@ -57,8 +63,11 @@ public SystemComponent(string? id)
Debug.WriteLine($"Unknown pass \"{pass}\"");
continue;
}
component?.Passes.Add(new SystemPass(pass));
passes.Add(new SystemPass(pass));
}

SystemComponent component = new SystemComponent(reader.GetAttribute("Id"), passes);

componentList.Add(component);
}
}
Expand Down
6 changes: 3 additions & 3 deletions UserSettings/SystemEdition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public SystemEdition(string? id, string? displayName, string? productKey)
if (reader.NodeType == XmlNodeType.Element && reader.Name == "Edition")
{

edition.Id = reader.GetAttribute("Id");
edition.DisplayName = reader.GetAttribute("DisplayName");
edition.ProductKey = reader.GetAttribute("Key");
edition = new SystemEdition(reader.GetAttribute("Id"),
reader.GetAttribute("DisplayName"),
reader.GetAttribute("Key"));

}
}
Expand Down
56 changes: 38 additions & 18 deletions UserSettings/UserAccounts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ public enum UserGroup

public UserGroup Group;

public UserAccount()
{
Enabled = false;
Name = "";
Password = "";
Group = UserGroup.Users;
}

public UserAccount(bool enabled, string? name, string? password, UserGroup group)
{
Enabled = enabled;
Name = name;
Password = password;
Group = group;
}

public static List<UserAccount>? LoadAccounts(string filePath)
{
List<UserAccount> accountList = new List<UserAccount>();
Expand All @@ -43,26 +59,30 @@ public enum UserGroup
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "UserAccount")
{
UserAccount account = new UserAccount();
account.Enabled = reader.GetAttribute("Enabled") switch
int nameLength = reader.GetAttribute("Name").Length;

UserAccount account = new UserAccount(reader.GetAttribute("Enabled") switch
{
"1" => true,
"0" => false,
_ => false
},
(reader.GetAttribute("Name").Length > 20 ?
reader.GetAttribute("Name").Substring(0, 20) :
reader.GetAttribute("Name")),
reader.GetAttribute("Password"),
reader.GetAttribute("Group") switch
{
"Admins" => UserGroup.Administrators,
"Users" => UserGroup.Users,
_ => UserGroup.Users
});


if (nameLength > 20)
{
"1" => true,
"0" => false,
_ => false
};
account.Name = reader.GetAttribute("Name");
if (account.Name.Length > 20)
{
Console.WriteLine($"WARNING: Account name {account.Name} is over 20 characters long. Truncating to 20 characters...");
account.Name = account.Name.Substring(0, 20);
Console.WriteLine($"WARNING: Account name \"{reader.GetAttribute("Name")}\" has been truncated to 20 characters because its length exceeds the limit.");
}
account.Password = reader.GetAttribute("Password");
account.Group = reader.GetAttribute("Group") switch
{
"Admins" => UserGroup.Administrators,
"Users" => UserGroup.Users,
_ => UserGroup.Users
};

accountList.Add(account);
}
Expand Down
47 changes: 31 additions & 16 deletions UserSettings/WirelessNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ public enum AuthenticationProtocol

public bool NonBroadcast;

public WirelessNetwork()
{
SSID = "";
Password = "";
Authentication = AuthenticationProtocol.WPA2;
NonBroadcast = false;
}

public WirelessNetwork(string? ssid, string? password, AuthenticationProtocol authProtocol, bool nonBroadcast)
{
SSID = ssid;
Password = password;
Authentication = authProtocol;
NonBroadcast = nonBroadcast;
}

public static WirelessNetwork? LoadSettings(string filePath)
{
try
Expand All @@ -40,22 +56,21 @@ public enum AuthenticationProtocol
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "WirelessNetwork")
{
WirelessNetwork network = new WirelessNetwork();
network.SSID = reader.GetAttribute("Name");
network.Password = reader.GetAttribute("Password");
network.Authentication = reader.GetAttribute("AuthMode") switch
{
"Open" => AuthenticationProtocol.Open,
"WPA2" => AuthenticationProtocol.WPA2,
"WPA3" => AuthenticationProtocol.WPA3,
_ => AuthenticationProtocol.WPA2
};
network.NonBroadcast = reader.GetAttribute("NonBroadcast") switch
{
"1" => true,
"0" => false,
_ => false
};
WirelessNetwork network = new WirelessNetwork(reader.GetAttribute("Name"),
reader.GetAttribute("Password"),
reader.GetAttribute("AuthMode") switch
{
"Open" => AuthenticationProtocol.Open,
"WPA2" => AuthenticationProtocol.WPA2,
"WPA3" => AuthenticationProtocol.WPA3,
_ => AuthenticationProtocol.WPA2
},
reader.GetAttribute("NonBroadcast") switch
{
"1" => true,
"0" => false,
_ => false
});

return network;
}
Expand Down

0 comments on commit 2a3d3ee

Please sign in to comment.