Skip to content

Commit

Permalink
Disable CS0436 for generated attributes, closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
js6pak committed Mar 10, 2024
1 parent aa48d8d commit 75b77ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

<Target Name="Publicize" AfterTargets="ResolveReferences" BeforeTargets="FindReferenceAssembliesForReferences">
<Error Condition="'$(UsingMicrosoftNETSdk)' != 'true'" Text="BepInEx.AssemblyPublicizer.MSBuild only works in SDK-style projects" />

<PropertyGroup>
<GeneratedIgnoresAccessChecksToFile Condition="'$(GeneratedIgnoresAccessChecksToFile)' == ''">$(IntermediateOutputPath)$(MSBuildProjectName).IgnoresAccessChecksTo.cs</GeneratedIgnoresAccessChecksToFile>
</PropertyGroup>

<PublicizeTask IntermediateOutputPath="$(IntermediateOutputPath)" ReferencePath="@(ReferencePath)" PackageReference="@(PackageReference)" Publicize="@(Publicize)">
<PublicizeTask IntermediateOutputPath="$(IntermediateOutputPath)" GeneratedIgnoresAccessChecksToFile="$(GeneratedIgnoresAccessChecksToFile)" ReferencePath="@(ReferencePath)" PackageReference="@(PackageReference)" Publicize="@(Publicize)">
<Output TaskParameter="RemovedReferences" ItemName="_RemovedReferences" />
<Output TaskParameter="PublicizedReferences" ItemName="_PublicizedReferences" />
</PublicizeTask>
Expand All @@ -19,9 +23,8 @@
<ReferencePath Remove="@(_RemovedReferences)" />
<ReferencePath Include="@(_PublicizedReferences)" />

<AssemblyAttribute Include="System.Runtime.CompilerServices.IgnoresAccessChecksToAttribute" Condition="'@(_PublicizedReferences)' != ''">
<_Parameter1>%(_PublicizedReferences.Filename)</_Parameter1>
</AssemblyAttribute>
<Compile Include="$(GeneratedIgnoresAccessChecksToFile)" />
<FileWrites Include="$(GeneratedIgnoresAccessChecksToFile)" />
</ItemGroup>

<PropertyGroup>
Expand Down
22 changes: 22 additions & 0 deletions BepInEx.AssemblyPublicizer.MSBuild/PublicizeTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down Expand Up @@ -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<ITaskItem> publicizedReferences)
{
var stringBuilder = new StringBuilder();

stringBuilder.AppendLine("// <auto-generated />");
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)
Expand Down

0 comments on commit 75b77ea

Please sign in to comment.