From 75b77ea42057bbc7d89b318f24138e328ec6817f Mon Sep 17 00:00:00 2001 From: js6pak Date: Sun, 10 Mar 2024 02:12:18 +0100 Subject: [PATCH] Disable CS0436 for generated attributes, closes #5 --- .../BepInEx.AssemblyPublicizer.MSBuild.props | 11 ++++++---- .../PublicizeTask.cs | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/BepInEx.AssemblyPublicizer.MSBuild/BepInEx.AssemblyPublicizer.MSBuild.props b/BepInEx.AssemblyPublicizer.MSBuild/BepInEx.AssemblyPublicizer.MSBuild.props index 68b2068..641eeb4 100644 --- a/BepInEx.AssemblyPublicizer.MSBuild/BepInEx.AssemblyPublicizer.MSBuild.props +++ b/BepInEx.AssemblyPublicizer.MSBuild/BepInEx.AssemblyPublicizer.MSBuild.props @@ -9,8 +9,12 @@ + + + $(IntermediateOutputPath)$(MSBuildProjectName).IgnoresAccessChecksTo.cs + - + @@ -19,9 +23,8 @@ - - <_Parameter1>%(_PublicizedReferences.Filename) - + + diff --git a/BepInEx.AssemblyPublicizer.MSBuild/PublicizeTask.cs b/BepInEx.AssemblyPublicizer.MSBuild/PublicizeTask.cs index 1c2abe3..06adb73 100644 --- a/BepInEx.AssemblyPublicizer.MSBuild/PublicizeTask.cs +++ b/BepInEx.AssemblyPublicizer.MSBuild/PublicizeTask.cs @@ -15,6 +15,9 @@ public class PublicizeTask : Task [Required] public string IntermediateOutputPath { get; set; } + [Required] + public string GeneratedIgnoresAccessChecksToFile { get; set; } + [Required] public ITaskItem[] ReferencePath { get; set; } @@ -123,12 +126,31 @@ public override bool Execute() Log.LogMessage($"Publicized {fileName}"); } + GenerateIgnoresAccessChecksToFile(publicizedReferences); + RemovedReferences = removedReferences.ToArray(); PublicizedReferences = publicizedReferences.ToArray(); return true; } + private void GenerateIgnoresAccessChecksToFile(List publicizedReferences) + { + var stringBuilder = new StringBuilder(); + + stringBuilder.AppendLine("// "); + stringBuilder.AppendLine("#pragma warning disable CS0436 // Type conflicts with imported type"); + stringBuilder.AppendLine(); + + foreach (var publicizedReference in publicizedReferences) + { + var assemblyName = Path.GetFileNameWithoutExtension(publicizedReference.ItemSpec); + stringBuilder.AppendLine($"[assembly: System.Runtime.CompilerServices.IgnoresAccessChecksToAttribute(\"{assemblyName}\")]"); + } + + File.WriteAllText(GeneratedIgnoresAccessChecksToFile, stringBuilder.ToString()); + } + private static string ComputeHash(byte[] bytes, AssemblyPublicizerOptions options) { static void Hash(ICryptoTransform hash, byte[] buffer)