From 977c2ba2a26e55ed92a3f941bfb9dcf4d24988bc Mon Sep 17 00:00:00 2001 From: Joel Ahlgren Date: Thu, 20 May 2021 20:54:31 +0200 Subject: [PATCH] Prepared projects to allow building without self extracting (SFX) functionality, to reduce build size. --- SevenZip.Tests/MiscellaneousTests.cs | 12 +++--- SevenZip.Tests/SevenZip.Tests.csproj | 1 + SevenZip/SevenZip.csproj | 43 ++++++++++---------- SevenZip/SevenZipSfx.cs | 60 +++++++++++++++++----------- 4 files changed, 64 insertions(+), 52 deletions(-) diff --git a/SevenZip.Tests/MiscellaneousTests.cs b/SevenZip.Tests/MiscellaneousTests.cs index 32af147..f5715b3 100644 --- a/SevenZip.Tests/MiscellaneousTests.cs +++ b/SevenZip.Tests/MiscellaneousTests.cs @@ -15,20 +15,21 @@ public class MiscellaneousTests : TestBase [Test] public void SerializationTest() { - var ex = new ArgumentException("blahblah"); - var bf = new BinaryFormatter(); + var argumentException = new ArgumentException("blahblah"); + var binaryFormatter = new BinaryFormatter(); using (var ms = new MemoryStream()) { using (var fileStream = File.Create(TemporaryFile)) { - bf.Serialize(ms, ex); - SevenZipCompressor cmpr = new SevenZipCompressor(); - cmpr.CompressStream(ms, fileStream); + binaryFormatter.Serialize(ms, argumentException); + var compressor = new SevenZipCompressor(); + compressor.CompressStream(ms, fileStream); } } } +#if SFX [Test] public void CreateSfxArchiveTest([Values]SfxModule sfxModule) { @@ -59,6 +60,7 @@ public void CreateSfxArchiveTest([Values]SfxModule sfxModule) process?.Kill(); }); } +#endif [Test] public void LzmaEncodeDecodeTest() diff --git a/SevenZip.Tests/SevenZip.Tests.csproj b/SevenZip.Tests/SevenZip.Tests.csproj index 6219e13..fd06c4c 100644 --- a/SevenZip.Tests/SevenZip.Tests.csproj +++ b/SevenZip.Tests/SevenZip.Tests.csproj @@ -8,6 +8,7 @@ ..\Stage\$(Configuration)\ true SevenZipTests.snk + SFX diff --git a/SevenZip/SevenZip.csproj b/SevenZip/SevenZip.csproj index 6fb29f0..ab7089e 100644 --- a/SevenZip/SevenZip.csproj +++ b/SevenZip/SevenZip.csproj @@ -1,4 +1,4 @@ - + SevenZipSharp netstandard2.0;net45;net5.0-windows @@ -13,21 +13,15 @@ ..\Stage\$(Configuration)\ ..\Stage\$(Configuration)\SevenZipSharp.xml false + $(DefaultItemExcludes);sfx\* - TRACE;DEBUG;UNMANAGED + TRACE;DEBUG;UNMANAGED;SFX - TRACE;UNMANAGED + TRACE;UNMANAGED;SFX true - - - - - - - PreserveNewest @@ -35,15 +29,26 @@ PreserveNewest - - + - + + + + + + + + + + + + + @@ -52,19 +57,11 @@ + - - - - - - - - - - + diff --git a/SevenZip/SevenZipSfx.cs b/SevenZip/SevenZipSfx.cs index 78fcfd0..4526a78 100644 --- a/SevenZip/SevenZipSfx.cs +++ b/SevenZip/SevenZipSfx.cs @@ -1,5 +1,6 @@ namespace SevenZip { +#if SFX using System; using System.Collections.Generic; using System.Globalization; @@ -68,7 +69,6 @@ public class SevenZipSfx } } - private SfxModule _module = SfxModule.Default; private string _moduleFileName; private Dictionary> _sfxCommands; @@ -77,7 +77,7 @@ public class SevenZipSfx /// public SevenZipSfx() { - _module = SfxModule.Default; + SfxModule = SfxModule.Default; CommonInit(); } @@ -92,7 +92,7 @@ public SevenZipSfx(SfxModule module) throw new ArgumentException("You must specify the custom module executable.", nameof(module)); } - _module = module; + SfxModule = module; CommonInit(); } @@ -102,7 +102,7 @@ public SevenZipSfx(SfxModule module) /// public SevenZipSfx(string moduleFileName) { - _module = SfxModule.Custom; + SfxModule = SfxModule.Custom; ModuleFileName = moduleFileName; CommonInit(); } @@ -110,7 +110,7 @@ public SevenZipSfx(string moduleFileName) /// /// Gets the sfx module type. /// - public SfxModule SfxModule => _module; + public SfxModule SfxModule { get; private set; } /// /// Gets or sets the custom sfx module file name @@ -127,14 +127,14 @@ public string ModuleFileName } _moduleFileName = value; - _module = SfxModule.Custom; + SfxModule = SfxModule.Custom; var sfxName = Path.GetFileName(value); foreach (var mod in SfxSupportedModuleNames.Keys) { if (SfxSupportedModuleNames[mod].Contains(sfxName)) { - _module = mod; + SfxModule = mod; } } } @@ -178,7 +178,7 @@ private static SfxModule GetModuleByName(string name) /// The resource name for xml definitions private void LoadCommandsFromResource(string xmlDefinitions) { - using (Stream cfg = Assembly.GetExecutingAssembly().GetManifestResourceStream( + using (var cfg = Assembly.GetExecutingAssembly().GetManifestResourceStream( GetResourceString(xmlDefinitions + ".xml"))) { if (cfg == null) @@ -186,7 +186,7 @@ private void LoadCommandsFromResource(string xmlDefinitions) throw new SevenZipSfxValidationException("The configuration \"" + xmlDefinitions + "\" does not exist."); } - using (Stream schm = Assembly.GetExecutingAssembly().GetManifestResourceStream( + using (var schm = Assembly.GetExecutingAssembly().GetManifestResourceStream( GetResourceString(xmlDefinitions + ".xsd"))) { if (schm == null) @@ -195,18 +195,18 @@ private void LoadCommandsFromResource(string xmlDefinitions) "\" does not exist."); } var sc = new XmlSchemaSet(); - using (XmlReader scr = XmlReader.Create(schm)) + using (var scr = XmlReader.Create(schm)) { sc.Add(null, scr); var settings = new XmlReaderSettings {ValidationType = ValidationType.Schema, Schemas = sc}; - string validationErrors = ""; + var validationErrors = ""; settings.ValidationEventHandler += ((s, t) => { validationErrors += String.Format(CultureInfo.InvariantCulture, "[{0}]: {1}\n", t.Severity.ToString(), t.Message); }); - using (XmlReader rdr = XmlReader.Create(cfg, settings)) + using (var rdr = XmlReader.Create(cfg, settings)) { _sfxCommands = new Dictionary>(); rdr.Read(); @@ -218,7 +218,7 @@ private void LoadCommandsFromResource(string xmlDefinitions) rdr.Read(); do { - SfxModule mod = GetModuleByName(rdr["modules"]); + var mod = GetModuleByName(rdr["modules"]); rdr.ReadStartElement("config"); rdr.Read(); if (rdr.Name == "id") @@ -240,7 +240,7 @@ private void LoadCommandsFromResource(string xmlDefinitions) } } while (rdr.Name == "config"); } - if (!String.IsNullOrEmpty(validationErrors)) + if (!string.IsNullOrEmpty(validationErrors)) { throw new SevenZipSfxValidationException( "\n" + validationErrors.Substring(0, validationErrors.Length - 1)); @@ -257,30 +257,37 @@ private void LoadCommandsFromResource(string xmlDefinitions) /// The sfx settings dictionary to validate. private void ValidateSettings(SfxSettings settings) { - if (_module == SfxModule.Custom) + if (SfxModule == SfxModule.Custom) { return; } - List commands = _sfxCommands[_module]; + + var commands = _sfxCommands[SfxModule]; + if (commands == null) { return; } + var invalidCommands = new List(); - foreach (string command in settings.Keys) + + foreach (var command in settings.Keys) { if (!commands.Contains(command)) { invalidCommands.Add(command); } } + if (invalidCommands.Count > 0) { var invalidText = new StringBuilder("\nInvalid commands:\n"); - foreach (string str in invalidCommands) + + foreach (var str in invalidCommands) { invalidText.Append(str); } + throw new SevenZipSfxValidationException(invalidText.ToString()); } } @@ -293,23 +300,26 @@ private void ValidateSettings(SfxSettings settings) private static Stream GetSettingsStream(SfxSettings settings) { var ms = new MemoryStream(); - byte[] buf = Encoding.UTF8.GetBytes(@";!@Install@!UTF-8!" + '\n'); + var buf = Encoding.UTF8.GetBytes(@";!@Install@!UTF-8!" + '\n'); ms.Write(buf, 0, buf.Length); - foreach (string command in settings.Keys) + + foreach (var command in settings.Keys) { buf = Encoding.UTF8.GetBytes(String.Format(CultureInfo.InvariantCulture, "{0}=\"{1}\"\n", command, settings[command])); ms.Write(buf, 0, buf.Length); } + buf = Encoding.UTF8.GetBytes(@";!@InstallEnd@!"); ms.Write(buf, 0, buf.Length); + return ms; } private SfxSettings GetDefaultSettings() { - switch (_module) + switch (SfxModule) { default: return null; @@ -334,7 +344,7 @@ private SfxSettings GetDefaultSettings() /// Writes the whole to the other one. /// /// The source stream to read from. - /// The destination stream to wrie to. + /// The destination stream to write to. private static void WriteStream(Stream src, Stream dest) { if (src == null) @@ -350,6 +360,7 @@ private static void WriteStream(Stream src, Stream dest) src.Seek(0, SeekOrigin.Begin); var buf = new byte[32768]; int bytesRead; + while ((bytesRead = src.Read(buf, 0, buf.Length)) > 0) { dest.Write(buf, 0, bytesRead); @@ -408,12 +419,12 @@ public void MakeSfx(Stream archive, SfxSettings settings, Stream sfxStream) ValidateSettings(settings); - using (var sfx = Assembly.GetExecutingAssembly().GetManifestResourceStream(GetResourceString(SfxSupportedModuleNames[_module][0]))) + using (var sfx = Assembly.GetExecutingAssembly().GetManifestResourceStream(GetResourceString(SfxSupportedModuleNames[SfxModule][0]))) { WriteStream(sfx, sfxStream); } - if (_module == SfxModule.Custom || _sfxCommands[_module] != null) + if (SfxModule == SfxModule.Custom || _sfxCommands[SfxModule] != null) { using (var set = GetSettingsStream(settings)) { @@ -490,4 +501,5 @@ public void MakeSfx(string archiveFileName, SfxSettings settings, Stream sfxStre } } } +#endif } \ No newline at end of file