generated from Nexus-Mods/NexusMods.App.Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
351 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/NexusMods.EventSourcing.Abstractions/EntityAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
|
||
namespace NexusMods.EventSourcing.Abstractions; | ||
|
||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] | ||
public class EntityAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Defines the UID of the entity, and what revision it is. Incrementing | ||
/// the revision will cause the entity snapshots to be discarded, and regenerated | ||
/// during the next load. | ||
/// </summary> | ||
/// <param name="uid"></param> | ||
/// <param name="revision"></param> | ||
public EntityAttribute(string guid, ushort revision) | ||
{ | ||
Span<byte> span = stackalloc byte[16]; | ||
Guid.Parse(guid).TryWriteBytes(span); | ||
UUID = BinaryPrimitives.ReadUInt128BigEndian(span); | ||
Revision = revision; | ||
} | ||
|
||
/// <summary> | ||
/// The revision of the entity *Type*. | ||
/// </summary> | ||
public ushort Revision { get; } | ||
|
||
/// <summary> | ||
/// The unique identifier of the entity *Type*. | ||
/// </summary> | ||
public UInt128 UUID { get; } | ||
} |
11 changes: 11 additions & 0 deletions
11
src/NexusMods.EventSourcing.Abstractions/EntityDefinition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace NexusMods.EventSourcing.Abstractions; | ||
|
||
/// <summary> | ||
/// Records the entity type, UUID and revision, for use in the DI container. | ||
/// </summary> | ||
/// <param name="Type"></param> | ||
/// <param name="UUID"></param> | ||
/// <param name="Revision"></param> | ||
public record EntityDefinition(Type Type, UInt128 UUID, ushort Revision); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/NexusMods.EventSourcing.Abstractions/ISnapshotEventIngester.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace NexusMods.EventSourcing.Abstractions; | ||
|
||
/// <summary> | ||
/// An event ingester that supports entity snapshots | ||
/// </summary> | ||
public interface ISnapshotEventIngester : IEventIngester | ||
{ | ||
/// <summary> | ||
/// This method will be called for each attribute snapshotted, before the normal event ingestion is called | ||
/// </summary> | ||
/// <param name="attributeName"></param> | ||
/// <param name="attribute"></param> | ||
public void IngestSnapshotAttribute(string attributeName, IAttribute attribute); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/NexusMods.EventSourcing/Serialization/EntityDefinitionSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
using NexusMods.EventSourcing.Abstractions; | ||
using NexusMods.EventSourcing.Abstractions.Serialization; | ||
using Reloaded.Memory.Extensions; | ||
|
||
namespace NexusMods.EventSourcing.Serialization; | ||
|
||
/// <summary> | ||
/// Serializes an entity definition, while intentionally not serializing the type of the entity. | ||
/// </summary> | ||
public sealed class EntityDefinitionSerializer : IFixedSizeSerializer<EntityDefinition> | ||
{ | ||
/// <inheritdoc /> | ||
public bool CanSerialize(Type valueType) | ||
{ | ||
return valueType == typeof(EntityDefinition); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool TryGetFixedSize(Type valueType, out int size) | ||
{ | ||
size = 16 + sizeof(ushort); | ||
return true; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Serialize(EntityDefinition value, Span<byte> output) | ||
{ | ||
BinaryPrimitives.WriteUInt128BigEndian(output, value.UUID); | ||
BinaryPrimitives.WriteUInt16BigEndian(output.SliceFast(16), value.Revision); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public EntityDefinition Deserialize(ReadOnlySpan<byte> from) | ||
{ | ||
var uuid = BinaryPrimitives.ReadUInt128BigEndian(from); | ||
var revision = BinaryPrimitives.ReadUInt16BigEndian(from.SliceFast(16)); | ||
|
||
var existing = EntityStructureRegistry.GetDefinitionByUUID(uuid); | ||
|
||
if (existing.Revision != revision) | ||
return new EntityDefinition(existing.Type, uuid, revision); | ||
|
||
return existing; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.