Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions Runtime/BinaryStorage.Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public static BinaryStorage Get(string filePath)
public static Builder Construct(string filePath)
{
ThrowIfFilePathLocked(filePath);
LockFilePathInEditor(filePath);
return new Builder(filePath);
}

Expand Down Expand Up @@ -114,12 +113,21 @@ public Builder AddPrimitiveTypes()
/// <param name="typeSerializer">The serializer for the specified type.</param>
/// <returns>The current <see cref="Builder"/> instance for method chaining.</returns>
/// <exception cref="DuplicateTypeSerializerException">Thrown if a serializer for the specified type already exists.</exception>
/// <exception cref="DuplicateTypeSerializerNameException">Thrown if a serializer with the same type name already exists.</exception>
public Builder AddTypeSerializer<T>(TypeSerializer<T> typeSerializer)
{
if (_serializers.Any(c => c is TypedBinarySection<T>))
var otherSerializer = _serializers.FirstOrDefault(c => c is TypedBinarySection<T>)?.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<T>(typeSerializer));
return this;
}
Expand Down Expand Up @@ -217,20 +225,13 @@ public Builder SupportDictionariesOf<TKey, TValue>()
/// <exception cref="IOException"> An I/O error occurred </exception>
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;
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions Runtime/Exceptions/DuplicateTypeSerializerException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@

namespace Appegy.Storage
{
/// <summary>
/// Thrown when a second serializer for the same value type is being registered.
/// </summary>
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}').")
{
}
}
Expand Down
18 changes: 18 additions & 0 deletions Runtime/Exceptions/DuplicateTypeSerializerNameException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.IO;

namespace Appegy.Storage
{
/// <summary>
/// Thrown when a serializer with a TypeName that is already in use is being registered.
/// </summary>
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}').")
{
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Runtime/Serialization/UnityTypesSerializers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Appegy.Storage
internal class QuaternionSerializer : EquatableTypeSerializer<Quaternion>
{
public static QuaternionSerializer Shared { get; } = new();
public override string TypeName => "vector2f";
public override string TypeName => "quaternion";

public override void WriteTo(BinaryWriter writer, Quaternion value)
{
Expand Down
Loading