FastScriptReload icon indicating copy to clipboard operation
FastScriptReload copied to clipboard

Could Attributes be supported ?

Open builder-main opened this issue 2 years ago • 6 comments
trafficstars

What are the current limitations around Attributes ?

I'm very often adding / removing attributes at editor / runtime in order to debug or more clearly see my data structures (with odin inspector for instance).

I also use them for dependency injection.

Is it doable, easy or hard ?

builder-main avatar Apr 04 '23 09:04 builder-main

I think that may be not an possible, technically after hot reload you end up with additional assembly and calls from old methods are detoured/reditected to new (via MonoMod / Harmony).

Assembly metadata can't be adjusted in that way I'm afraid and will still need full recompile and reload

handzlikchris avatar Apr 04 '23 10:04 handzlikchris

Assuming that you're correct that modifying the metadata is impossible, this might be doable if you could modify the code responsible for reading the metadata...

Could we patch reflection methods to allow returning modified data? Or does Unity have some patchable method or cache it uses to look up attributes?

However, even if that's possible, I don't know whether Unity uses C# reflection to read these things or whether that's all happening in C++ land out of reach of detouring.

SamPruden avatar Apr 04 '23 13:04 SamPruden

Okay, fair enough. Thanks for the answer.

However for those who wonder, methods dynamically looked up do get updated. So if one is named in an attribute you still can update it (to iterate in editor for instance)

builder-main avatar Apr 04 '23 13:04 builder-main

That's good idea - I think that could work if you patched those methods and kept a list of what has been added. (btw. I always read custom attributes via reflection methods and I don't know of any other method so likely patchable)

It's more of a Harmony world and I'm not sure if patching core method like that would be good for everyone but defo great for additional feature.

I'll get the issue opened and will put it on the backlog later.

handzlikchris avatar Apr 04 '23 13:04 handzlikchris

I've not tried the experimental adding fields support. Are new fields currently visible to reflection? If not, potentially we have a larger feature request here; patch reflection to always pickup changes, which could include metadata only changes like attributes.

I'm not convinced this will work though. Even if Unity does go via the standard reflection API to read attributes, I suspect it reads them once and caches the result, rather than rechecking each frame, so patching reflection might not actually be reflected in the editor. Hopefully I'm wrong.

SamPruden avatar Apr 04 '23 13:04 SamPruden

The wider stuff on how Unity works with attributes - we'll probably not sort that out.

But for anything on managed side, it'll probably use reflection.

New fields are very simple - your managed code is rewritten to access / modify entries in a dictionary so they are not fields as such. And using reflection on them would not work.

Good for discussion but patching reflection to this level is probably quite big and nasty.

Whenever possible I try to keep detours and patches to minimum. They are really difficult to maintain and if you patch stuff that's used so extensively there's a good chance something will break quite badly.

handzlikchris avatar Apr 04 '23 14:04 handzlikchris