TeamSirenix / odin-serializer

Fast, robust, powerful and extendible .NET serializer built for Unity

Home Page:http://www.odininspector.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Suggestion] TypeGuidAttribute instead of BindTypeNameToTypeAttribute

c0ffeeartc opened this issue · comments

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

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?

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

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

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.