I am making mods for a Unity game where non-public members are exposed using BepInEx.AssemblyPublicizer
. Previously, I use their MSBuild
package like this and it works fine:
<!-- Publicizer -->
<PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<!-- Game Assemblies -->
<Reference Include="$(AssemblyPath)\Unity*.dll" Publicize="true">
<DestinationSubDirectory>libs/</DestinationSubDirectory>
<Private>False</Private>
</Reference>
However because the number of projects increases, performing Publicizing for each project takes too much time, so I thought about publicizing them once and just refer to the dlls:
<!-- Game publicized Assemblies. Run GameAssemblyPublicizer to publicize them -->
<Reference Include="$(GameSolutionFolder)GameAssemblyPublicizer\out\**\*.dll">
<Private>false</Private>
</Reference>
However, any code that accesses something that it should not be able to now has this runtime error (the code can compile just fine):
FieldAccessException: Field `GameClassName:GameMemberName' is inaccessible from method `MyCLassName:tor ()'
This is very strange because IIRC, Mono runtime does not validate member access. Looking at the BepInEx.AssemblyPublicizer.MSBuild
source code I am not very sure what it does differently.
Is it possible to achieve what BepInEx.AssemblyPublicizer.MSBuild
is doing without using it and instead refer to the publicized DLLs (I am in control of the publicizer script, I can modify anything I need).
Note: in both cases, I am not copying any of the game's files with the mod. The game already contains them (without its members publicized obviously) but only the MSBuild package works and exception is thrown for my own publicized DLLs.
Update: surely BepInEx.AssemblyPublicizer.MSBuild
was doing something beside publicizing the members because even when I use that (successful) method, then copying the obj\Debug\netstandard2.1\publicized
files out and refer to them, the error still happens.
I am making mods for a Unity game where non-public members are exposed using BepInEx.AssemblyPublicizer
. Previously, I use their MSBuild
package like this and it works fine:
<!-- Publicizer -->
<PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<!-- Game Assemblies -->
<Reference Include="$(AssemblyPath)\Unity*.dll" Publicize="true">
<DestinationSubDirectory>libs/</DestinationSubDirectory>
<Private>False</Private>
</Reference>
However because the number of projects increases, performing Publicizing for each project takes too much time, so I thought about publicizing them once and just refer to the dlls:
<!-- Game publicized Assemblies. Run GameAssemblyPublicizer to publicize them -->
<Reference Include="$(GameSolutionFolder)GameAssemblyPublicizer\out\**\*.dll">
<Private>false</Private>
</Reference>
However, any code that accesses something that it should not be able to now has this runtime error (the code can compile just fine):
FieldAccessException: Field `GameClassName:GameMemberName' is inaccessible from method `MyCLassName:tor ()'
This is very strange because IIRC, Mono runtime does not validate member access. Looking at the BepInEx.AssemblyPublicizer.MSBuild
source code I am not very sure what it does differently.
Is it possible to achieve what BepInEx.AssemblyPublicizer.MSBuild
is doing without using it and instead refer to the publicized DLLs (I am in control of the publicizer script, I can modify anything I need).
Note: in both cases, I am not copying any of the game's files with the mod. The game already contains them (without its members publicized obviously) but only the MSBuild package works and exception is thrown for my own publicized DLLs.
Update: surely BepInEx.AssemblyPublicizer.MSBuild
was doing something beside publicizing the members because even when I use that (successful) method, then copying the obj\Debug\netstandard2.1\publicized
files out and refer to them, the error still happens.
2 Answers
Reset to default 1AssemblyPublicizer also adds AllowUnsafeBlocks
to the project, according to this comment.
So maybe try add something like this to your project:
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
I finally found out what BepInEx.AssemblyPublicizer.MSBuild
did, it create a bunch of assembly:IgnoresAccessChecksToAttribute
like this:
// <auto-generated />
#pragma warning disable CS0436 // Type conflicts with imported type
[assembly: System.Runtime.CompilerServices.IgnoresAccessChecksToAttribute("Assembly1")]
[assembly: System.Runtime.CompilerServices.IgnoresAccessChecksToAttribute("Assembly2")]
// ...
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
internal sealed class IgnoresAccessChecksToAttribute : Attribute
{
public IgnoresAccessChecksToAttribute(string assemblyName)
{
}
}
}
From their GenerateIgnoresAccessChecksToFile source code.
So if I want to reuse the pre-publicized DLLs, I need to grab that file and make a shortcut in every project.
Update: I also need to keep BepInEx.AssemblyPublicizer.MSBuild
refered when built or else it still does not work, but at least for me, I don't need to publicize all the files every time I build anymore.