Skip to content

Commit

Permalink
Remove unused code for embedding binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
cschneegans committed Jan 24, 2025
1 parent e58b6e3 commit c8ae0aa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 30 deletions.
20 changes: 4 additions & 16 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ string ToPrettyString()
return sw.ToString();
}

AddFile(ToPrettyString(), path, ContentTransformation.Text);
AddFile(ToPrettyString(), path);
}

public string AddXmlFile(string xml, string name)
Expand All @@ -1112,27 +1112,16 @@ public string AddTextFile(string name, string content, Action<StringWriter>? bef
before?.Invoke(writer);
writer.WriteLine(content);
after?.Invoke(writer);
AddFile(writer.ToString(), destination, ContentTransformation.Text);
AddFile(writer.ToString(), destination);
return destination;
}

public string AddTextFile(string resourceName, Action<StringWriter>? before = null, Action<StringWriter>? after = null)
{
return AddTextFile(resourceName, content: Util.StringFromResource(resourceName), before: before, after: after);
}

public void AddBinaryFile(byte[] content, string path)
{
AddFile(Convert.ToBase64String(content, Base64FormattingOptions.InsertLineBreaks), path, ContentTransformation.Base64);
}

private enum ContentTransformation
{
Text,
Base64
}

private void AddFile(string content, string path, ContentTransformation transformation)

private void AddFile(string content, string path)
{
{
XmlNode root = Document.SelectSingleNodeOrThrow("/u:unattend", NamespaceManager);
Expand Down Expand Up @@ -1160,7 +1149,6 @@ private void AddFile(string content, string path, ContentTransformation transfor

XmlElement file = Document.CreateElement("File", Constants.MyNamespaceUri);
file.SetAttribute("path", path);
file.SetAttribute("transformation", transformation.ToString());
extensions.AppendChild(file);
file.AppendChild(Document.CreateTextNode(Util.Indent(content)));
}
Expand Down
22 changes: 8 additions & 14 deletions resource/ExtractScripts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ param(
);

foreach( $file in $Document.unattend.Extensions.File ) {
$path = [System.Environment]::ExpandEnvironmentVariables(
$file.GetAttribute( 'path' )
);
$path = [System.Environment]::ExpandEnvironmentVariables( $file.GetAttribute( 'path' ) );
mkdir -Path( $path | Split-Path -Parent ) -ErrorAction 'SilentlyContinue';
$content = $file.InnerText.Trim();
if( $file.GetAttribute( 'transformation' ) -ieq 'Base64' ) {
[System.IO.File]::WriteAllBytes( $path, [System.Convert]::FromBase64String( $content ) );
} else {
$encoding = switch( [System.IO.Path]::GetExtension( $path ) ) {
{ $_ -in '.ps1', '.xml' } { [System.Text.Encoding]::UTF8; }
{ $_ -in '.reg', '.vbs', '.js' } { [System.Text.UnicodeEncoding]::new( $false, $true ); }
default { [System.Text.Encoding]::Default; }
};
[System.IO.File]::WriteAllBytes( $path, ( $encoding.GetPreamble() + $encoding.GetBytes( $content ) ) );
}
$encoding = switch( [System.IO.Path]::GetExtension( $path ) ) {
{ $_ -in '.ps1', '.xml' } { [System.Text.Encoding]::UTF8; }
{ $_ -in '.reg', '.vbs', '.js' } { [System.Text.UnicodeEncoding]::new( $false, $true ); }
default { [System.Text.Encoding]::Default; }
};
$bytes = $encoding.GetPreamble() + $encoding.GetBytes( $file.InnerText.Trim() );
[System.IO.File]::WriteAllBytes( $path, $bytes );
}

0 comments on commit c8ae0aa

Please sign in to comment.