Skip to content

Commit d9a967e

Browse files
authored
A collection of bugfixes and performance improvements (#4)
2 parents 678bab3 + 917d548 commit d9a967e

File tree

8 files changed

+448
-235
lines changed

8 files changed

+448
-235
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Install .NET SDK
2323
uses: actions/setup-dotnet@v1
2424
with:
25-
dotnet-version: "5.0.x"
25+
dotnet-version: "6.0.x"
2626

2727
- name: Add MSBuild to PATH
2828
uses: microsoft/setup-msbuild@v1.0.2

Windows Build Identifier/Identification/Common.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public static WindowsVersion GetGreaterVersion(WindowsVersion version1, WindowsV
164164

165165
CultureInfo provider = CultureInfo.InvariantCulture;
166166

167-
string format = "yyMMDD-HHmm";
167+
const string format = "yyMMdd-HHmm";
168168

169169
DateTime date1 = DateTime.ParseExact(version1.CompileDate, format, provider);
170170
DateTime date2 = DateTime.ParseExact(version2.CompileDate, format, provider);
@@ -249,6 +249,7 @@ public static void DisplayReport(WindowsImage report)
249249
Console.WriteLine("Architecture : " + report.Architecture);
250250
Console.WriteLine("BuildType : " + report.BuildType);
251251
Console.WriteLine("Types : " + typedisp);
252+
Console.WriteLine("Base Sku : " + report.BaseSku);
252253
Console.WriteLine("Sku : " + report.Sku);
253254
Console.WriteLine("Editions : " + editiondisp);
254255
Console.WriteLine("Licensing : " + report.Licensing);

Windows Build Identifier/Identification/InstalledImage/DetectionHandler.cs

Lines changed: 168 additions & 58 deletions
Large diffs are not rendered by default.

Windows Build Identifier/Identification/MediaHandler.cs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,27 @@ public class UnsupportedWIMException : Exception { }
2121

2222
public class UnsupportedWIMXmlException : Exception { }
2323

24-
private static string ExtractWIMXml(Stream wimstream)
24+
private static string ExtractWIMXml(ArchiveFile archiveFile)
2525
{
2626
try
2727
{
28-
using (ArchiveFile archiveFile = new ArchiveFile(wimstream, SevenZipFormat.Wim))
28+
if (archiveFile.Entries.Any(x => x.FileName == "[1].xml"))
2929
{
30-
if (archiveFile.Entries.Any(x => x.FileName == "[1].xml"))
31-
{
32-
Entry wimXmlEntry = archiveFile.Entries.First(x => x.FileName == "[1].xml");
33-
34-
string xml;
35-
using (MemoryStream memoryStream = new MemoryStream())
36-
{
37-
wimXmlEntry.Extract(memoryStream);
30+
Entry wimXmlEntry = archiveFile.Entries.First(x => x.FileName == "[1].xml");
3831

39-
xml = Encoding.Unicode.GetString(memoryStream.ToArray(), 2, (int)memoryStream.Length - 2);
40-
}
41-
42-
return xml;
43-
}
44-
else if (archiveFile.Entries.Any(x => x.FileName == "Windows"))
32+
string xml;
33+
using (MemoryStream memoryStream = new MemoryStream())
4534
{
46-
return archiveFile.GetArchiveComment();
35+
wimXmlEntry.Extract(memoryStream);
36+
37+
xml = Encoding.Unicode.GetString(memoryStream.ToArray(), 2, (int)memoryStream.Length - 2);
4738
}
39+
40+
return xml;
41+
}
42+
else if (archiveFile.Entries.Any(x => x.FileName == "Windows"))
43+
{
44+
return archiveFile.GetArchiveComment();
4845
}
4946

5047
throw new UnsupportedWIMException();
@@ -80,22 +77,26 @@ private static WindowsImageIndex[] IdentifyWindowsNTFromWIM(Stream wimstream, bo
8077

8178
Console.WriteLine("Gathering WIM information XML file");
8279

83-
string xml = ExtractWIMXml(wimstream);
80+
81+
using ArchiveFile archiveFile = new ArchiveFile(wimstream, SevenZipFormat.Wim);
82+
string xml = ExtractWIMXml(archiveFile);
8483

8584
Console.WriteLine("Parsing WIM information XML file");
8685
XmlFormats.WIMXml.WIM wim = GetWIMClassFromXml(xml);
8786

8887
Console.WriteLine($"Found {wim.IMAGE.Length} images in the wim according to the XML");
8988

9089
Console.WriteLine("Evaluating relevant images in the WIM according to the XML");
91-
int irelevantcount2 = (wim.IMAGE.Any(x => x.DESCRIPTION.Contains("winpe", StringComparison.InvariantCultureIgnoreCase)) ? 1 : 0) +
92-
(wim.IMAGE.Any(x => x.DESCRIPTION.Contains("setup", StringComparison.InvariantCultureIgnoreCase)) ? 1 : 0) +
93-
(wim.IMAGE.Any(x => x.DESCRIPTION.Contains("preinstallation", StringComparison.InvariantCultureIgnoreCase)) ? 1 : 0) +
94-
(wim.IMAGE.Any(x => x.DESCRIPTION.Contains("winre", StringComparison.InvariantCultureIgnoreCase)) ? 1 : 0) +
95-
(wim.IMAGE.Any(x => x.DESCRIPTION.Contains("recovery", StringComparison.InvariantCultureIgnoreCase)) ? 1 : 0);
90+
int irelevantcount2 = (wim.IMAGE.Any(x => x.DESCRIPTION != null && x.DESCRIPTION.Contains("winpe", StringComparison.InvariantCultureIgnoreCase)) ? 1 : 0) +
91+
(wim.IMAGE.Any(x => x.DESCRIPTION != null && x.DESCRIPTION.Contains("setup", StringComparison.InvariantCultureIgnoreCase)) ? 1 : 0) +
92+
(wim.IMAGE.Any(x => x.DESCRIPTION != null && x.DESCRIPTION.Contains("preinstallation", StringComparison.InvariantCultureIgnoreCase)) ? 1 : 0) +
93+
(wim.IMAGE.Any(x => x.DESCRIPTION != null && x.DESCRIPTION.Contains("winre", StringComparison.InvariantCultureIgnoreCase)) ? 1 : 0) +
94+
(wim.IMAGE.Any(x => x.DESCRIPTION != null && x.DESCRIPTION.Contains("recovery", StringComparison.InvariantCultureIgnoreCase)) ? 1 : 0);
9695

9796
Console.WriteLine($"Found {irelevantcount2} irrelevant images in the wim according to the XML");
9897

98+
var provider = new WIMInstallProviderInterface(archiveFile);
99+
99100
foreach (var image in wim.IMAGE)
100101
{
101102
Console.WriteLine();
@@ -105,11 +106,11 @@ private static WindowsImageIndex[] IdentifyWindowsNTFromWIM(Stream wimstream, bo
105106
// If what we're trying to identify isn't just a winpe, and we are accessing a winpe image
106107
// skip the image
107108
//
108-
int irelevantcount = (image.DESCRIPTION.Contains("winpe", StringComparison.InvariantCultureIgnoreCase) ? 1 : 0) +
109-
(image.DESCRIPTION.Contains("setup", StringComparison.InvariantCultureIgnoreCase) ? 1 : 0) +
110-
(image.DESCRIPTION.Contains("preinstallation", StringComparison.InvariantCultureIgnoreCase) ? 1 : 0) +
111-
(image.DESCRIPTION.Contains("winre", StringComparison.InvariantCultureIgnoreCase) ? 1 : 0) +
112-
(image.DESCRIPTION.Contains("recovery", StringComparison.InvariantCultureIgnoreCase) ? 1 : 0);
109+
int irelevantcount = (image.DESCRIPTION != null && image.DESCRIPTION.Contains("winpe", StringComparison.InvariantCultureIgnoreCase) ? 1 : 0) +
110+
(image.DESCRIPTION != null && image.DESCRIPTION.Contains("setup", StringComparison.InvariantCultureIgnoreCase) ? 1 : 0) +
111+
(image.DESCRIPTION != null && image.DESCRIPTION.Contains("preinstallation", StringComparison.InvariantCultureIgnoreCase) ? 1 : 0) +
112+
(image.DESCRIPTION != null && image.DESCRIPTION.Contains("winre", StringComparison.InvariantCultureIgnoreCase) ? 1 : 0) +
113+
(image.DESCRIPTION != null && image.DESCRIPTION.Contains("recovery", StringComparison.InvariantCultureIgnoreCase) ? 1 : 0);
113114

114115
Console.WriteLine($"Index contains {irelevantcount} flags indicating this is a preinstallation environment");
115116

@@ -125,7 +126,6 @@ private static WindowsImageIndex[] IdentifyWindowsNTFromWIM(Stream wimstream, bo
125126

126127
if (index != null && wim.IMAGE[0].INDEX == "0")
127128
{
128-
using ArchiveFile archiveFile = new ArchiveFile(wimstream, SevenZipFormat.Wim);
129129
if (!archiveFile.Entries.Any(x => x.FileName.StartsWith("0\\")))
130130
{
131131
WorkaroundForWIMFormatBug = true;
@@ -140,12 +140,10 @@ private static WindowsImageIndex[] IdentifyWindowsNTFromWIM(Stream wimstream, bo
140140

141141
Console.WriteLine($"Index value: {index}");
142142

143-
var provider = new WIMInstallProviderInterface(wimstream, index);
143+
provider.SetIndex(index);
144144

145145
var report = InstalledImage.DetectionHandler.IdentifyWindowsNT(provider);
146146

147-
provider.Close();
148-
149147
// fallback
150148
if ((string.IsNullOrEmpty(report.Sku) || report.Sku == "TerminalServer") && !string.IsNullOrEmpty(image.FLAGS))
151149
{
@@ -205,7 +203,7 @@ private static WindowsImageIndex[] IdentifyWindowsNTFromWIM(Stream wimstream, bo
205203
results.Add(imageIndex);
206204
}
207205

208-
wimstream.Dispose();
206+
provider.Close();
209207

210208
return results.ToArray();
211209
}
@@ -575,7 +573,7 @@ public static FileItem[] IdentifyWindowsFromISO(string isopath, bool deep, bool
575573
using FileStream isoStream = File.Open(isopath, FileMode.Open, FileAccess.Read);
576574

577575
VfsFileSystemFacade cd = new CDReader(isoStream, true);
578-
if (cd.FileExists(@"README.TXT"))
576+
if (cd.FileExists(@"README.TXT") || cd.Root.GetDirectories().Length == 0)
579577
{
580578
cd = new UdfReader(isoStream);
581579
}
@@ -646,6 +644,7 @@ public static WindowsImageIndex[] IdentifyWindowsFromWIM(Stream wim, bool includ
646644
Console.WriteLine(ex.ToString());
647645
}
648646

647+
wim.Dispose();
649648
return result;
650649
}
651650

0 commit comments

Comments
 (0)