Skip to content

Commit

Permalink
Zip embedded Opc.Ua.NodeSet2.xml to reduce assembly size (#2232)
Browse files Browse the repository at this point in the history
- Add only the zipped NodeSet2.xml as embedded resource
- Reduces assembly size by at least 2MB
- Breaking Change: Embedded resource Opc.Ua.NodeSet2.xml is now Opc.Ua.NodeSet2.xml.zip
  • Loading branch information
mregen committed Jul 27, 2023
1 parent 9256c2e commit 04cd9bc
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,4 @@ OPC\ Foundation/
/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.xml
/SampleApplications/Samples/OPCOutput
!**/Assets/*.*
*.Nodeset2.xml.zip
21 changes: 19 additions & 2 deletions Stack/Opc.Ua.Core/Opc.Ua.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</PropertyGroup>

<ItemGroup>
<None Remove="Schema\Opc.Ua.NodeSet2.xml" />
<None Remove="Schema\Opc.Ua.NodeSet2.xml.zip" />
<None Remove="Schema\Opc.Ua.Types.bsd" />
<None Remove="Schema\Opc.Ua.Types.xsd" />
<None Remove="Schema\UANodeSet.xsd" />
Expand All @@ -29,7 +29,6 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Schema\Opc.Ua.NodeSet2.xml" />
<EmbeddedResource Include="Schema\Opc.Ua.Types.bsd" />
<EmbeddedResource Include="Schema\Opc.Ua.Types.xsd" />
<EmbeddedResource Include="Schema\UANodeSet.xsd" />
Expand Down Expand Up @@ -73,4 +72,22 @@

<Target Name="GetPackagingOutputs" />

<!-- Produces a zipped version of the Nodeset 2 to reduce assembly size by 2.7MB ! -->
<ItemGroup>
<EmbeddedResource Include="Schema\Opc.Ua.NodeSet2.xml.zip" />
</ItemGroup>

<PropertyGroup>
<ZipTmp>$(BaseIntermediateOutputPath)/zipnodeset2</ZipTmp>
<ZipNodeSet2XML>Schema/Opc.Ua.NodeSet2.xml</ZipNodeSet2XML>
</PropertyGroup>

<Target Name="ZipNodeSet2" BeforeTargets="PrepareForBuild" Inputs="$(ZipNodeSet2XML)" Outputs="$(ZipNodeSet2XML).zip">
<Message Text="Zip $(ZipNodeSet2XML) in $(ZipTmp)." Importance="high"/>
<Copy SourceFiles="$(ZipNodeSet2XML)" DestinationFolder="$(ZipTmp)" />
<ZipDirectory SourceDirectory="$(ZipTmp)" DestinationFile="$(ZipNodeSet2XML).zip" Overwrite="true" />
<RemoveDir Directories="$(ZipTmp)" />
<Message Text="Zip NodeSet2 completed, $(ZipTmp) removed." Importance="high"/>
</Target>

</Project>
2 changes: 1 addition & 1 deletion Stack/Opc.Ua.Core/Stack/State/NodeState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public AccessRestrictionType AccessRestrictions
}

/// <summary>
/// Gets or sets the extensions of the node set. Property used when importing nodeset2.xml files.
/// Gets or sets the extensions of the node set. Property used when importing NodeSet2.xml files.
/// </summary>
/// <value>
/// The extensions.
Expand Down
1 change: 0 additions & 1 deletion Stack/Opc.Ua.Core/Stack/State/NodeStateCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ public void LoadFromXml(ISystemContext context, Stream istrm, bool updateTables)

using (XmlReader reader = XmlReader.Create(istrm, Utils.DefaultXmlReaderSettings()))
{
XmlQualifiedName root = new XmlQualifiedName("ListOfNodeState", Namespaces.OpcUaXsd);
XmlDecoder decoder = new XmlDecoder(null, reader, messageContext);

NamespaceTable namespaceUris = new NamespaceTable();
Expand Down
1 change: 1 addition & 0 deletions Tests/Opc.Ua.Core.Tests/Opc.Ua.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.IO.Compression" Version="4.3.0" />
</ItemGroup>

<PropertyGroup Condition="'$(SignAssembly)' == 'true'">
Expand Down
16 changes: 8 additions & 8 deletions Tests/Opc.Ua.Core.Tests/Stack/Schema/UANodeSetHelpersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class UANodeSetHelpersTests
{
#region DataPointSource
[DatapointSource]
public static readonly Nodeset2Asset[] Nodeset2AssetArray = new AssetCollection<Nodeset2Asset>(Ua.Tests.TestUtils.EnumerateTestAssets("*.NodeSet2.xml")).ToArray();
public static readonly NodeSet2Asset[] NodeSet2AssetArray = new AssetCollection<NodeSet2Asset>(Ua.Tests.TestUtils.EnumerateTestAssets("*.NodeSet2.xml")).ToArray();
#endregion

#region Test Setup
Expand Down Expand Up @@ -209,14 +209,14 @@ public void ArrayDimensionsValidationTest()
}

/// <summary>
/// Test Nodeset2 import.
/// Test NodeSet2 import.
/// </summary>
[Test]
[TestCase("Stack/Opc.Ua.Core/Schema/Opc.Ua.NodeSet2.xml")]
[TestCase("Applications/Quickstarts.Servers/TestData/TestData.NodeSet2.xml")]
[TestCase("Applications/Quickstarts.Servers/MemoryBuffer/MemoryBuffer.NodeSet2.xml")]
[TestCase("Applications/Quickstarts.Servers/Boiler/Boiler.NodeSet2.xml")]
public void Nodeset2ValidationTest(string nodeset2File)
public void NodeSet2ValidationTest(string nodeset2File)
{
var assetPath = Utils.GetAbsoluteFilePath("../../../../../" + nodeset2File, true, false, false);
using (var importStream = new FileStream(assetPath, FileMode.Open))
Expand All @@ -239,10 +239,10 @@ public void Nodeset2ValidationTest(string nodeset2File)
}

/// <summary>
/// Test Nodeset2 import. Requires test assets to be in the 'Assets' folder.
/// Test NodeSet2 import. Requires test assets to be in the 'Assets' folder.
/// </summary>
[Theory]
public void Nodeset2ValidationTest(Nodeset2Asset nodeset2Asset)
public void NodeSet2ValidationTest(NodeSet2Asset nodeset2Asset)
{
using (var importStream = new MemoryStream(nodeset2Asset.Xml))
{
Expand All @@ -267,11 +267,11 @@ public void Nodeset2ValidationTest(Nodeset2Asset nodeset2Asset)

#region Asset helpers
/// <summary>
/// A Nodeset2 as test asset.
/// A NodeSet2 as test asset.
/// </summary>
public class Nodeset2Asset : IAsset, IFormattable
public class NodeSet2Asset : IAsset, IFormattable
{
public Nodeset2Asset() { }
public NodeSet2Asset() { }

public string Path { get; private set; }
public byte[] Xml { get; private set; }
Expand Down
53 changes: 52 additions & 1 deletion Tests/Opc.Ua.Core.Tests/Types/Schemas/XmlSchemaWellKnownTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Runtime.Serialization;
using System.Xml;
using Castle.Components.DictionaryAdapter.Xml;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using Opc.Ua.Core.Tests.Stack.Schema;
using Opc.Ua.Schema.Xml;

namespace Opc.Ua.Core.Tests.Types.Schemas
Expand All @@ -43,7 +50,7 @@ public class XmlSchemaWellKnownTests : XmlSchemaValidator
{
#region DataPointSources
[DatapointSource]
public string[][] WellKnownSchemaData = WellKnownDictionaries;
public string[][] WellKnownSchemaData => WellKnownDictionaries;
#endregion

#region Test Methods
Expand Down Expand Up @@ -78,6 +85,50 @@ public void ValidateResources(string[] schemaData)
Assert.AreEqual(schemaData[0], schema.TargetSchema.TargetNamespace);
}
}

/// <summary>
/// Load zipped NodeSet2 resource.
/// </summary>
[Test]
[TestCase("Opc.Ua.NodeSet2.xml")]
public void LoadZipNodeSet2Resources(string resource)
{
const string schemaPrefix = "Opc.Ua.Schema.";
const string zipExtension = ".zip";
var assembly = typeof(XmlSchemaValidator).GetTypeInfo().Assembly;

using (var stream = assembly.GetManifestResourceStream(schemaPrefix + resource + zipExtension))
{
Assert.IsNotNull(stream);
using (ZipArchive zipArchive = new ZipArchive(stream, ZipArchiveMode.Read))
{
Assert.NotNull(zipArchive);
Assert.AreEqual(1, zipArchive.Entries.Count);
var zipEntry = zipArchive.GetEntry(zipArchive.Entries[0].Name);
Assert.AreEqual(resource, zipEntry.Name);
var zipStream = zipEntry.Open();
Assert.IsNotNull(zipStream);

XmlReaderSettings settings = Utils.DefaultXmlReaderSettings();
settings.CloseInput = true;

var localContext = new SystemContext {
NamespaceUris = new NamespaceTable()
};

var exportedNodeSet = Export.UANodeSet.Read(zipStream);
Assert.IsNotNull(exportedNodeSet);
exportedNodeSet.NamespaceUris = exportedNodeSet.NamespaceUris ?? System.Array.Empty<string>();
foreach (var namespaceUri in exportedNodeSet.NamespaceUris)
{
localContext.NamespaceUris.Append(namespaceUri);
}
var nodeStates = new NodeStateCollection();
exportedNodeSet.Import(localContext, nodeStates);
Assert.Greater(nodeStates.Count, 0);
}
}
}
#endregion
}
}
16 changes: 16 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#---------------------------------#
# general configuration #
#---------------------------------#

# you can use {branch} name in version format too
version: 1.0.{build}-{branch}

# branches to build
branches:
# whitelist
only:
- appveyor

# blacklist
except:
- gh-pages

0 comments on commit 04cd9bc

Please sign in to comment.