-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathExtensions.cs
132 lines (120 loc) · 6.27 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using EntitiesBT.Core;
using JetBrains.Annotations;
using Unity.Assertions;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
namespace EntitiesBT.Variant
{
public static class BlobVariantExtension
{
[Pure]
public static unsafe ref TValue As<TValue>(this ref BlobVariant blobVariant) where TValue : unmanaged =>
ref UnsafeUtility.AsRef<TValue>(blobVariant.AsPointer());
[Pure]
public static unsafe void* AsPointer(this ref BlobVariant blobVariant)
{
fixed (int* thisPtr = &blobVariant.MetaDataOffsetPtr)
{
return (byte*)thisPtr + blobVariant.MetaDataOffsetPtr;
}
}
[Pure]
public static unsafe object AsObject(this ref BlobVariant blobVariant, Type valueType) =>
Marshal.PtrToStructure(new IntPtr(blobVariant.AsPointer()), valueType);
[Pure]
public static IEnumerable<ComponentType> GetComponentAccessList(this ref BlobVariant blobVariant)
{
var @delegate = DelegateRegistry<AccessorMethodAttribute.Delegate>.TryGetValue(blobVariant.VariantId);
return @delegate == null ? Enumerable.Empty<ComponentType>() : @delegate(ref blobVariant);
}
public static T Read<T, TNodeBlob, TBlackboard>(this ref BlobVariant blobVariant, int index, ref TNodeBlob blob, ref TBlackboard bb)
where T : unmanaged
where TNodeBlob : struct, INodeBlob
where TBlackboard : struct, IBlackboard
{
var @delegate = DelegateRegistry<ReaderMethodAttribute.Delegate<T, TNodeBlob, TBlackboard>>
.TryGetValue(blobVariant.VariantId)
;
Assert.IsNotNull(@delegate, nameof(@delegate) + " != null");
return @delegate.Invoke(ref blobVariant, index, ref blob, ref bb);
}
public static IntPtr ReadOnlyPtr<TNodeBlob, TBlackboard>(this ref BlobVariant blobVariant, int index, ref TNodeBlob blob, ref TBlackboard bb)
where TNodeBlob : struct, INodeBlob
where TBlackboard : struct, IBlackboard
{
var @delegate = DelegateRegistry<ReadOnlyPointerMethodAttribute.Delegate<TNodeBlob, TBlackboard>>
.TryGetValue(blobVariant.VariantId)
;
Assert.IsNotNull(@delegate, nameof(@delegate) + " != null");
return @delegate.Invoke(ref blobVariant, index, ref blob, ref bb);
}
public static IntPtr ReadWritePtr<TNodeBlob, TBlackboard>(this ref BlobVariant blobVariant, int index, ref TNodeBlob blob, ref TBlackboard bb)
where TNodeBlob : struct, INodeBlob
where TBlackboard : struct, IBlackboard
{
var @delegate = DelegateRegistry<ReadWritePointerMethodAttribute.Delegate<TNodeBlob, TBlackboard>>
.TryGetValue(blobVariant.VariantId)
;
Assert.IsNotNull(@delegate, nameof(@delegate) + " != null");
return @delegate.Invoke(ref blobVariant, index, ref blob, ref bb);
}
public static IntPtr ReadOnlyPtrWithReadWriteFallback<TNodeBlob, TBlackboard>(this ref BlobVariant blobVariant, int index, ref TNodeBlob blob, ref TBlackboard bb)
where TNodeBlob : struct, INodeBlob
where TBlackboard : struct, IBlackboard
{
var @delegate = DelegateRegistry<ReadOnlyPointerMethodAttribute.Delegate<TNodeBlob, TBlackboard>>
.TryGetValue(blobVariant.VariantId)
;
if (@delegate != null) return @delegate.Invoke(ref blobVariant, index, ref blob, ref bb);
return ReadWritePtr(ref blobVariant, index, ref blob, ref bb);
}
public static ref T ReadRef<T, TNodeBlob, TBlackboard>(this ref BlobVariant blobVariant, int index, ref TNodeBlob blob, ref TBlackboard bb)
where T : unmanaged
where TNodeBlob : struct, INodeBlob
where TBlackboard : struct, IBlackboard
{
var @delegate = DelegateRegistry<RefReaderMethodAttribute.Delegate<T, TNodeBlob, TBlackboard>>
.TryGetValue(blobVariant.VariantId)
;
Assert.IsNotNull(@delegate, nameof(@delegate) + " != null");
return ref @delegate(ref blobVariant, index, ref blob, ref bb);
}
public static T ReadWithRefFallback<T, TNodeBlob, TBlackboard>(this ref BlobVariant blobVariant, int index, ref TNodeBlob blob, ref TBlackboard bb)
where T : unmanaged
where TNodeBlob : struct, INodeBlob
where TBlackboard : struct, IBlackboard
{
var reader = DelegateRegistry<ReaderMethodAttribute.Delegate<T, TNodeBlob, TBlackboard>>
.TryGetValue(blobVariant.VariantId)
;
if (reader != null) return reader(ref blobVariant, index, ref blob, ref bb);
return ReadRef<T, TNodeBlob, TBlackboard>(ref blobVariant, index, ref blob, ref bb);
}
public static void Write<T, TNodeBlob, TBlackboard>(this ref BlobVariant blobVariant, int index, ref TNodeBlob blob, ref TBlackboard bb, T value)
where T : unmanaged
where TNodeBlob : struct, INodeBlob
where TBlackboard : struct, IBlackboard
{
var @delegate = DelegateRegistry<WriterMethodAttribute.Delegate<T, TNodeBlob, TBlackboard>>
.TryGetValue(blobVariant.VariantId)
;
Assert.IsNotNull(@delegate, nameof(@delegate) + " != null");
@delegate(ref blobVariant, index, ref blob, ref bb, value);
}
public static void WriteWithRefFallback<T, TNodeBlob, TBlackboard>(this ref BlobVariant blobVariant, int index, ref TNodeBlob blob, ref TBlackboard bb, T value)
where T : unmanaged
where TNodeBlob : struct, INodeBlob
where TBlackboard : struct, IBlackboard
{
var writer = DelegateRegistry<WriterMethodAttribute.Delegate<T, TNodeBlob, TBlackboard>>
.TryGetValue(blobVariant.VariantId)
;
if (writer != null) writer(ref blobVariant, index, ref blob, ref bb, value);
else ReadRef<T, TNodeBlob, TBlackboard>(ref blobVariant, index, ref blob, ref bb) = value;
}
}
}