TeamSirenix / odin-serializer

Fast, robust, powerful and extendible .NET serializer built for Unity
http://www.odininspector.com
Apache License 2.0
1.66k stars 190 forks source link

[Suggestion] TypeGuidAttribute instead of BindTypeNameToTypeAttribute #7

Closed c0ffeeartc closed 4 years ago

c0ffeeartc commented 5 years ago

Hello Would you please add TypeGuidAttribute, same as BindTypeNameToTypeAttribute but required to add once instead of per each rename refactoring

TorVestergaard commented 4 years ago

Sorry for taking so long to reply - I was never quite sure about the details of this proposal, and asking for clarifications slipped my mind way back. I'm going back through old issues now, so I'll ask at this time: I'm not sure how exactly this would work, and what you would gain from this over the current approach. Could you please add some details to clarify what exactly it is you are proposing, and how it would work? Perhaps with an example PR?

TorVestergaard commented 4 years ago

Whoops, didn't mean to close the issue :D

c0ffeeartc commented 4 years ago

Hi, thanks for answering and asking = )

Current approach BindTypeNameToTypeAttribute uses string for old type name

[assembly: OdinSerializer.BindTypeNameToType("Namespace.OldTypeName", typeof(Namespace.NewTypeName))]
//[assembly: OdinSerializer.BindTypeNameToType("Namespace.OldTypeName, OldFullAssemblyName", typeof(Namespace.NewTypeName))]

namespace Namespace
{
    public class SomeComponent : SerializedMonoBehaviour
    {
        public IInterface test; // Contains an instance of OldTypeName;
    }

    public interface IInterface { }

    public class NewTypeName : IInterface { }

    //public class OldTypeName : IInterface { }
}

It requires editing OldTypeName string on each type refactoring.

I'm asking if it's possible to add TypeGuidAttribute

namespace Namespace
{
    public class SomeComponent : SerializedMonoBehaviour
    {
        public IInterface test; // Contains an instance of OldTypeName;
    }

    public interface IInterface { }

    [TypeGuid("06d4dbae-35b6-44b1-a3c0-32bb889c90a0")]
    public class NewTypeName /*OldTypeName*/: IInterface { }
}

Which would allow renaming to NewTypeName without changing anything besides name itself.

I'm not sure if it's possible, can't provide PR but looking forward for your answer.

Thanks

TorVestergaard commented 4 years ago

Hmm. I understand now :)

This is certainly possible in theory, but I don't believe it's something that should come as standard in the library as it's a bit of an edge case and it would look slightly strange in the serialized data, as we'd have to serialize just the Guid, and not the type name, and I try to avoid feature bloat.

It is something that could be implemented easily by extending and providing your own TwoWaySerializationBinder to the SerializationContext and DeserializationContext you provide to serialization calls - this is an abstraction explicitly meant for enabling the user to control exactly how type names are bound and resolved, and we simply set the default behaviour when nothing else is specified.