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.
Merge pull request #100 from Nexus-Mods/value-handlers
Fix tuple 3 handling, and add an API for value remapping from user code
- Loading branch information
Showing
9 changed files
with
145 additions
and
79 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
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,66 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using NexusMods.MnemonicDB.Abstractions.ElementComparers; | ||
using NexusMods.MnemonicDB.Abstractions.Internals; | ||
using Reloaded.Memory.Extensions; | ||
|
||
namespace NexusMods.MnemonicDB.Abstractions; | ||
|
||
/// <summary> | ||
/// Static methods that help with reading, writing and formatting values | ||
/// </summary> | ||
public static class ValueHelpers | ||
{ | ||
/// <summary> | ||
/// Remaps the values of this attribute, if required. | ||
/// </summary> | ||
public static void Remap(Func<EntityId, EntityId> remapFn, in KeyPrefix prefix, Span<byte> valueSpan) | ||
{ | ||
switch (prefix.ValueTag) | ||
{ | ||
case ValueTags.Reference: | ||
var oldId = MemoryMarshal.Read<EntityId>(valueSpan); | ||
var newId = remapFn(oldId); | ||
MemoryMarshal.Write(valueSpan, newId); | ||
break; | ||
case ValueTags.Tuple2: | ||
{ | ||
var tag1 = (ValueTags)valueSpan[0]; | ||
var tag2 = (ValueTags)valueSpan[1]; | ||
if (tag1 == ValueTags.Reference) | ||
{ | ||
var entityId = MemoryMarshal.Read<EntityId>(valueSpan.SliceFast(2)); | ||
var newEntityId = remapFn(entityId); | ||
MemoryMarshal.Write(valueSpan.SliceFast(2), newEntityId); | ||
} | ||
if (tag2 == ValueTags.Reference) | ||
{ | ||
throw new NotSupportedException("This attribute does not support remapping of the second element."); | ||
} | ||
break; | ||
} | ||
case ValueTags.Tuple3: | ||
{ | ||
var tag1 = (ValueTags)valueSpan[0]; | ||
var tag2 = (ValueTags)valueSpan[1]; | ||
var tag3 = (ValueTags)valueSpan[2]; | ||
if (tag1 == ValueTags.Reference) | ||
{ | ||
var entityId = MemoryMarshal.Read<EntityId>(valueSpan.SliceFast(3)); | ||
var newEntityId = remapFn(entityId); | ||
MemoryMarshal.Write(valueSpan.SliceFast(3), newEntityId); | ||
} | ||
if (tag2 == ValueTags.Reference) | ||
{ | ||
throw new NotSupportedException("This attribute does not support remapping of the second element."); | ||
} | ||
if (tag3 == ValueTags.Reference) | ||
{ | ||
throw new NotSupportedException("This attribute does not support remapping of the third element."); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
|
||
} |
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
18 changes: 0 additions & 18 deletions
18
tests/NexusMods.MnemonicDB.TestModel/Attributes/Int3Attribute.cs
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
tests/NexusMods.MnemonicDB.TestModel/Attributes/Tuple3TestAttribute.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,18 @@ | ||
using NexusMods.MnemonicDB.Abstractions; | ||
using NexusMods.MnemonicDB.Abstractions.Attributes; | ||
using NexusMods.MnemonicDB.Abstractions.ElementComparers; | ||
|
||
namespace NexusMods.MnemonicDB.TestModel.Attributes; | ||
|
||
public class Tuple3TestAttribute(string ns, string name) : TupleAttribute<EntityId, ulong, int, int, string, string>(ValueTags.Reference, ValueTags.Int32, ValueTags.Ascii, ns, name) | ||
{ | ||
protected override (EntityId, int, string) FromLowLevel((ulong, int, string) value) | ||
{ | ||
return (EntityId.From(value.Item1), value.Item2, value.Item3); | ||
} | ||
|
||
protected override (ulong, int, string) ToLowLevel((EntityId, int, string) value) | ||
{ | ||
return (value.Item1.Value, value.Item2, value.Item3); | ||
} | ||
} |
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
54 changes: 27 additions & 27 deletions
54
tests/NexusMods.MnemonicDB.Tests/DbTests.CanUseTuple3Attributes.verified.txt
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 |
---|---|---|
@@ -1,27 +1,27 @@ | ||
+ | 0200000000000001 | TupleTest | (1, 1, 1) | 0100000000000003 | ||
+ | 0200000000000002 | TupleTest | (1, 1, 2) | 0100000000000003 | ||
+ | 0200000000000003 | TupleTest | (1, 1, 3) | 0100000000000003 | ||
+ | 0200000000000004 | TupleTest | (1, 2, 1) | 0100000000000003 | ||
+ | 0200000000000005 | TupleTest | (1, 2, 2) | 0100000000000003 | ||
+ | 0200000000000006 | TupleTest | (1, 2, 3) | 0100000000000003 | ||
+ | 0200000000000007 | TupleTest | (1, 3, 1) | 0100000000000003 | ||
+ | 0200000000000008 | TupleTest | (1, 3, 2) | 0100000000000003 | ||
+ | 0200000000000009 | TupleTest | (1, 3, 3) | 0100000000000003 | ||
+ | 020000000000000A | TupleTest | (2, 1, 1) | 0100000000000003 | ||
+ | 020000000000000B | TupleTest | (2, 1, 2) | 0100000000000003 | ||
+ | 020000000000000C | TupleTest | (2, 1, 3) | 0100000000000003 | ||
+ | 020000000000000D | TupleTest | (2, 2, 1) | 0100000000000003 | ||
+ | 020000000000000E | TupleTest | (2, 2, 2) | 0100000000000003 | ||
+ | 020000000000000F | TupleTest | (2, 2, 3) | 0100000000000003 | ||
+ | 0200000000000010 | TupleTest | (2, 3, 1) | 0100000000000003 | ||
+ | 0200000000000011 | TupleTest | (2, 3, 2) | 0100000000000003 | ||
+ | 0200000000000012 | TupleTest | (2, 3, 3) | 0100000000000003 | ||
+ | 0200000000000013 | TupleTest | (3, 1, 1) | 0100000000000003 | ||
+ | 0200000000000014 | TupleTest | (3, 1, 2) | 0100000000000003 | ||
+ | 0200000000000015 | TupleTest | (3, 1, 3) | 0100000000000003 | ||
+ | 0200000000000016 | TupleTest | (3, 2, 1) | 0100000000000003 | ||
+ | 0200000000000017 | TupleTest | (3, 2, 2) | 0100000000000003 | ||
+ | 0200000000000018 | TupleTest | (3, 2, 3) | 0100000000000003 | ||
+ | 0200000000000019 | TupleTest | (3, 3, 1) | 0100000000000003 | ||
+ | 020000000000001A | TupleTest | (3, 3, 2) | 0100000000000003 | ||
+ | 020000000000001B | TupleTest | (3, 3, 3) | 0100000000000003 | ||
+ | 0200000000000001 | TupleTest | (EId:200000000000001, 1, 1) | 0100000000000003 | ||
+ | 0200000000000002 | TupleTest | (EId:200000000000002, 1, 2) | 0100000000000003 | ||
+ | 0200000000000003 | TupleTest | (EId:200000000000003, 1, 3) | 0100000000000003 | ||
+ | 0200000000000004 | TupleTest | (EId:200000000000004, 2, 1) | 0100000000000003 | ||
+ | 0200000000000005 | TupleTest | (EId:200000000000005, 2, 2) | 0100000000000003 | ||
+ | 0200000000000006 | TupleTest | (EId:200000000000006, 2, 3) | 0100000000000003 | ||
+ | 0200000000000007 | TupleTest | (EId:200000000000007, 3, 1) | 0100000000000003 | ||
+ | 0200000000000008 | TupleTest | (EId:200000000000008, 3, 2) | 0100000000000003 | ||
+ | 0200000000000009 | TupleTest | (EId:200000000000009, 3, 3) | 0100000000000003 | ||
+ | 020000000000000A | TupleTest | (EId:20000000000000A, 1, 1) | 0100000000000003 | ||
+ | 020000000000000B | TupleTest | (EId:20000000000000B, 1, 2) | 0100000000000003 | ||
+ | 020000000000000C | TupleTest | (EId:20000000000000C, 1, 3) | 0100000000000003 | ||
+ | 020000000000000D | TupleTest | (EId:20000000000000D, 2, 1) | 0100000000000003 | ||
+ | 020000000000000E | TupleTest | (EId:20000000000000E, 2, 2) | 0100000000000003 | ||
+ | 020000000000000F | TupleTest | (EId:20000000000000F, 2, 3) | 0100000000000003 | ||
+ | 0200000000000010 | TupleTest | (EId:200000000000010, 3, 1) | 0100000000000003 | ||
+ | 0200000000000011 | TupleTest | (EId:200000000000011, 3, 2) | 0100000000000003 | ||
+ | 0200000000000012 | TupleTest | (EId:200000000000012, 3, 3) | 0100000000000003 | ||
+ | 0200000000000013 | TupleTest | (EId:200000000000013, 1, 1) | 0100000000000003 | ||
+ | 0200000000000014 | TupleTest | (EId:200000000000014, 1, 2) | 0100000000000003 | ||
+ | 0200000000000015 | TupleTest | (EId:200000000000015, 1, 3) | 0100000000000003 | ||
+ | 0200000000000016 | TupleTest | (EId:200000000000016, 2, 1) | 0100000000000003 | ||
+ | 0200000000000017 | TupleTest | (EId:200000000000017, 2, 2) | 0100000000000003 | ||
+ | 0200000000000018 | TupleTest | (EId:200000000000018, 2, 3) | 0100000000000003 | ||
+ | 0200000000000019 | TupleTest | (EId:200000000000019, 3, 1) | 0100000000000003 | ||
+ | 020000000000001A | TupleTest | (EId:20000000000001A, 3, 2) | 0100000000000003 | ||
+ | 020000000000001B | TupleTest | (EId:20000000000001B, 3, 3) | 0100000000000003 |
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