Unity3D doesn't reload a Cecil-modified assembly with added custom attribute

384 views
Skip to first unread message

Andriy Tylychko

unread,
Sep 17, 2014, 1:21:22 PM9/17/14
to mono-...@googlegroups.com
I don't know if it's Unity3D or Cecil problem. It took some time to resolve this issue, hopefully this description can help somebody else.

I'm using Cecil to generate code in a Unity3D assembly: Unity calls my class static constructor after building the assembly, I modify it by generating some code and adding an assembly level custom attribute to flag the assembly as "generated" one. Then I ask Unity to reload the assembly by call to `UnityEditorInternal.InternalEditorUtility.RequestScriptReload()`. Unity reloads the assembly, calls my static constructor again, I find my custom attribute and don't touch the assembly this time.

This worked fine until I upgraded Unity3D to v. 4.5.3 (from 4.2).

It looks like Unity reloads the assembly the first time but after that it doesn't reload it even on normal refresh, I mean even unmodified version, until I restart it. At least this definitely looks like a bug in Unity.

I narrowed the problem to the custom attribute I add to the modified assembly. Comparing modified and not-modified assemblies manifests I noticed that the custom attribute causes the assembly to reference itself, looks like:

...
.assembly extern 'Assembly-CSharp'
{
 
.ver 0:0:0:0
}
.assembly 'Assembly-CSharp'
{
 
.custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
 
.custom instance void ['Assembly-CSharp']PropertySystem.PropertyGenerator/GeneratedAttribute::.ctor() = ( 01 00 00 00 )
 
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
                                                                                                             
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.
 
.hash algorithm 0x00008004
 
.ver 0:0:0:0
}
.module 'Assembly-CSharp.dll'
...

Can this be a problem?

Jonathan Pryor

unread,
Sep 22, 2014, 9:22:48 AM9/22/14
to mono-cecil
On Sep 17, 2014, at 1:21 PM, Andriy Tylychko <andriy....@gmail.com> wrote:
> This worked fine until I upgraded Unity3D to v. 4.5.3 (from 4.2).
>
> It looks like Unity reloads the assembly the first time but after that it doesn't reload it even on normal refresh, I mean even unmodified version, until I restart it. At least this definitely looks like a bug in Unity.

I suspect that a Unity-specific forum may be a better place to ask this question. Offhand, I don't see how this is related to Cecil.

- Jon

Jb Evain

unread,
Sep 23, 2014, 8:32:51 AM9/23/14
to mono-...@googlegroups.com
Hey,

Agreed it looks like a bug in Unity. What do you mean by «I narrowed
the problem to the custom attribute I add to the modified assembly»?
If you use another mechanism to tag the assembly, it will be properly
reloaded?

Jb
> --
> --
> --
> mono-cecil
> ---
> You received this message because you are subscribed to the Google Groups
> "mono-cecil" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mono-cecil+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Andreia Gaita

unread,
Sep 23, 2014, 9:59:33 AM9/23/14
to mono-...@googlegroups.com
In another life I rewrote parts of the assembly loading mechanisms in Unity for 4.3, so you might have hit a bug where it could be leaking assembly references in such a way that makes it so Unity's mono never fully releases the physical assembly image when it unloads/reloads the domain, so you never get the new version loaded (which is my favourite side effect ever). Might be a corner case from self-modifying the assembly with a reference to itself. You should file a bug with Unity with a small repro test case.
Reply all
Reply to author
Forward
0 new messages