From 4f34b3da7cb57eb3094e2818b8168124f81a2cf9 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Fri, 26 Jun 2020 14:06:58 -0400 Subject: [PATCH] feat: Added attributes for controlling obfuscation --- CHANGELOG.md | 3 + .../ICD.Common.Utils_SimplSharp.csproj | 1 + .../Properties/ObfuscationAttributes.cs | 143 ++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 ICD.Common.Utils/Properties/ObfuscationAttributes.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dad4cd..e603e83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + - Added attributes for controlling obfuscation + ## [12.0.0] - 2020-06-18 ### Added - Added ToCollection extension method for copying an enumerable to a new collection diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index 0baaf3b..dc7949b 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -141,6 +141,7 @@ + diff --git a/ICD.Common.Utils/Properties/ObfuscationAttributes.cs b/ICD.Common.Utils/Properties/ObfuscationAttributes.cs new file mode 100644 index 0000000..d615303 --- /dev/null +++ b/ICD.Common.Utils/Properties/ObfuscationAttributes.cs @@ -0,0 +1,143 @@ +// ============================================================================= +// Definition of Custom Attributes for Declarative Obfuscation +// ============================================================================= +// +// This file is only necessary for the projects targeting one of the following: +// +// - .NET Core version 1.1 or lower +// - Universal Windows Platform (UWP) version 10.0.15063 or lower +// - Portable Class Library (PCL) +// - WinRT +// - Silverlight +// - .NET Compact Framework +// +// ============================================================================= + +using System; + +// ReSharper disable once CheckNamespace + +namespace ICD.Common.Properties +{ + /// + /// Instructs obfuscation tools to use their standard obfuscation rules for the appropriate assembly type. + /// + [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false, Inherited = false)] + public sealed class ObfuscateAssemblyAttribute : Attribute + { + /// + /// Initializes a new instance of the class, + /// specifying whether the assembly to be obfuscated is public or private. + /// + /// true if the assembly is used within the scope of one application; otherwise, false. + public ObfuscateAssemblyAttribute(bool assemblyIsPrivate) + { + m_AssemblyIsPrivate = assemblyIsPrivate; + m_StripAfterObfuscation = true; + } + + readonly bool m_AssemblyIsPrivate; + + /// + /// Gets a value indicating whether the assembly was marked private. + /// + /// + /// true if the assembly was marked private; otherwise, false. + /// + public bool AssemblyIsPrivate + { + get { return m_AssemblyIsPrivate; } + } + + bool m_StripAfterObfuscation; + + /// + /// Gets or sets a value indicating whether the obfuscation tool should remove the attribute after processing. + /// + /// + /// true if the obfuscation tool should remove the attribute after processing; otherwise, false. + /// The default value for this property is true. + /// + public bool StripAfterObfuscation + { + get { return m_StripAfterObfuscation; } + set { m_StripAfterObfuscation = value; } + } + } + + /// + /// Instructs obfuscation tools to take the specified actions for an assembly, type, or member. + /// + [AttributeUsage(AttributeTargets.Delegate | AttributeTargets.Parameter | AttributeTargets.Interface | AttributeTargets.Event | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Enum | AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)] + public sealed class ObfuscationAttribute : Attribute + { + /// + /// Initializes a new instance of the class. + /// + public ObfuscationAttribute() + { + m_ApplyToMembers = true; + m_Exclude = true; + m_Feature = "all"; + m_StripAfterObfuscation = true; + } + + bool m_ApplyToMembers; + + /// + /// Gets or sets a value indicating whether the attribute of a type is to apply to the members of the type. + /// + /// + /// true if the attribute is to apply to the members of the type; otherwise, false. The default is true. + /// + public bool ApplyToMembers + { + get { return m_ApplyToMembers; } + set { m_ApplyToMembers = value; } + } + + bool m_Exclude; + + /// + /// Gets or sets a value indicating whether the obfuscation tool should exclude the type or member from obfuscation. + /// + /// + /// true if the type or member to which this attribute is applied should be excluded from obfuscation; otherwise, false. + /// The default is true. + /// + public bool Exclude + { + get { return m_Exclude; } + set { m_Exclude = value; } + } + + string m_Feature; + + /// + /// Gets or sets a string value that is recognized by the obfuscation tool, and which specifies processing options. + /// + /// + /// A string value that is recognized by the obfuscation tool, and which specifies processing options. The default is "all". + /// + public string Feature + { + get { return m_Feature; } + set { m_Feature = value; } + } + + bool m_StripAfterObfuscation; + + /// + /// Gets or sets a value indicating whether the obfuscation tool should remove the attribute after processing. + /// + /// + /// true if the obfuscation tool should remove the attribute after processing; otherwise, false. + /// The default value for this property is true. + /// + public bool StripAfterObfuscation + { + get { return m_StripAfterObfuscation; } + set { m_StripAfterObfuscation = value; } + } + } +}