From af0c0d633aa8c0d0ea7912e72c1d5835f9b1555e Mon Sep 17 00:00:00 2001 From: Ivan Murashka Date: Thu, 7 Aug 2025 14:54:54 +0200 Subject: [PATCH] fix duplication of serializer TypeName --- Runtime/BinaryStorage.Builder.cs | 35 ++++++++++--------- .../DuplicateTypeSerializerException.cs | 9 +++-- .../DuplicateTypeSerializerNameException.cs | 18 ++++++++++ ...plicateTypeSerializerNameException.cs.meta | 3 ++ .../Serialization/UnityTypesSerializers.cs | 2 +- 5 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 Runtime/Exceptions/DuplicateTypeSerializerNameException.cs create mode 100644 Runtime/Exceptions/DuplicateTypeSerializerNameException.cs.meta diff --git a/Runtime/BinaryStorage.Builder.cs b/Runtime/BinaryStorage.Builder.cs index 7f75180..5628635 100644 --- a/Runtime/BinaryStorage.Builder.cs +++ b/Runtime/BinaryStorage.Builder.cs @@ -28,7 +28,6 @@ public static BinaryStorage Get(string filePath) public static Builder Construct(string filePath) { ThrowIfFilePathLocked(filePath); - LockFilePathInEditor(filePath); return new Builder(filePath); } @@ -114,12 +113,21 @@ public Builder AddPrimitiveTypes() /// The serializer for the specified type. /// The current instance for method chaining. /// Thrown if a serializer for the specified type already exists. + /// Thrown if a serializer with the same type name already exists. public Builder AddTypeSerializer(TypeSerializer typeSerializer) { - if (_serializers.Any(c => c is TypedBinarySection)) + var otherSerializer = _serializers.FirstOrDefault(c => c is TypedBinarySection)?.Serializer; + if (otherSerializer != null) { - throw new DuplicateTypeSerializerException(typeof(T), typeSerializer.TypeName, _filePath); + throw new DuplicateTypeSerializerException(typeSerializer, otherSerializer, _filePath); } + + otherSerializer = _serializers.FirstOrDefault(c => c.Serializer.TypeName == typeSerializer.TypeName)?.Serializer; + if (otherSerializer != null) + { + throw new DuplicateTypeSerializerNameException(typeSerializer, otherSerializer, _filePath); + } + _serializers.Add(new TypedBinarySection(typeSerializer)); return this; } @@ -217,20 +225,13 @@ public Builder SupportDictionariesOf() /// An I/O error occurred public BinaryStorage Build(KeyLoadFailedBehaviour keyLoadFailedBehaviour = KeyLoadFailedBehaviour.IgnoreWithWarning) { - try - { - var storage = new BinaryStorage(_filePath, _serializers); - storage.AutoSave = _autoSave; - storage.MissingKeyBehavior = _missingKeyBehavior; - storage.TypeMismatchBehaviour = _typeMismatchBehaviour; - storage.LoadDataFromDisk(keyLoadFailedBehaviour); - return storage; - } - catch - { - UnlockFilePathInEditor(_filePath); - throw; - } + var storage = new BinaryStorage(_filePath, _serializers); + storage.AutoSave = _autoSave; + storage.MissingKeyBehavior = _missingKeyBehavior; + storage.TypeMismatchBehaviour = _typeMismatchBehaviour; + storage.LoadDataFromDisk(keyLoadFailedBehaviour); + LockFilePathInEditor(_filePath); + return storage; } } } diff --git a/Runtime/Exceptions/DuplicateTypeSerializerException.cs b/Runtime/Exceptions/DuplicateTypeSerializerException.cs index ee80688..7754616 100644 --- a/Runtime/Exceptions/DuplicateTypeSerializerException.cs +++ b/Runtime/Exceptions/DuplicateTypeSerializerException.cs @@ -3,10 +3,15 @@ namespace Appegy.Storage { + /// + /// Thrown when a second serializer for the same value type is being registered. + /// public class DuplicateTypeSerializerException : Exception { - public DuplicateTypeSerializerException(Type type, string typeNameCode, string storagePath) - : base($"You're trying to add second serializer for type {type.Name} ({typeNameCode}) to storage {Path.GetFileName(storagePath)}. This is not allowed") + public DuplicateTypeSerializerException(TypeSerializer newSerializer, TypeSerializer existingSerializer, string storagePath) + : base($"Duplicate serializer detected in '{Path.GetFileName(storagePath)}'. " + + $"Attempted: {newSerializer.GetType().Name} (TypeName: '{newSerializer.TypeName}'); " + + $"Already registered: {existingSerializer.GetType().Name} (TypeName: '{existingSerializer.TypeName}').") { } } diff --git a/Runtime/Exceptions/DuplicateTypeSerializerNameException.cs b/Runtime/Exceptions/DuplicateTypeSerializerNameException.cs new file mode 100644 index 0000000..de1de38 --- /dev/null +++ b/Runtime/Exceptions/DuplicateTypeSerializerNameException.cs @@ -0,0 +1,18 @@ +using System; +using System.IO; + +namespace Appegy.Storage +{ + /// + /// Thrown when a serializer with a TypeName that is already in use is being registered. + /// + public class DuplicateTypeSerializerNameException : Exception + { + public DuplicateTypeSerializerNameException(TypeSerializer newSerializer, TypeSerializer existingSerializer, string storagePath) + : base($"TypeName collision detected in '{Path.GetFileName(storagePath)}'. " + + $"Attempted: {newSerializer.GetType().Name} (TypeName: '{newSerializer.TypeName}'); " + + $"Conflicting: {existingSerializer.GetType().Name} (TypeName: '{existingSerializer.TypeName}').") + { + } + } +} \ No newline at end of file diff --git a/Runtime/Exceptions/DuplicateTypeSerializerNameException.cs.meta b/Runtime/Exceptions/DuplicateTypeSerializerNameException.cs.meta new file mode 100644 index 0000000..64c27a0 --- /dev/null +++ b/Runtime/Exceptions/DuplicateTypeSerializerNameException.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1100d7553afb4580a2cdb1b773162f71 +timeCreated: 1754569969 \ No newline at end of file diff --git a/Runtime/Serialization/UnityTypesSerializers.cs b/Runtime/Serialization/UnityTypesSerializers.cs index 0bc80f7..bf7d450 100644 --- a/Runtime/Serialization/UnityTypesSerializers.cs +++ b/Runtime/Serialization/UnityTypesSerializers.cs @@ -6,7 +6,7 @@ namespace Appegy.Storage internal class QuaternionSerializer : EquatableTypeSerializer { public static QuaternionSerializer Shared { get; } = new(); - public override string TypeName => "vector2f"; + public override string TypeName => "quaternion"; public override void WriteTo(BinaryWriter writer, Quaternion value) {