-
Notifications
You must be signed in to change notification settings - Fork 5
/
ECSExtensions.cs
51 lines (44 loc) · 1.63 KB
/
ECSExtensions.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
using System;
using Bloodstone.API;
using Il2CppInterop.Runtime;
using Unity.Entities;
/// <remarks>
/// Copying these around is an anti-pattern - they should live in Bloodstone. I've put them here for now to uncouple things.
/// CS8500
/// </remarks>
#pragma warning disable CS8500
internal static class ECSExtensions
{
internal static void With<T>(this Entity entity, VExtensions.ActionRef<T> action) where T : struct
{
T item = entity.RW<T>();
action(ref item);
VWorld.Game.EntityManager.SetComponentData(entity, item);
}
internal static bool Has<T>(this Entity entity) where T : struct
{
int typeIndex = TypeManager.GetTypeIndex(Il2CppType.Of<T>());
return VWorld.Game.EntityManager.HasComponentRaw(entity, typeIndex);
}
internal unsafe static T RW<T>(this Entity entity) where T : struct
{
int typeIndex = TypeManager.GetTypeIndex(Il2CppType.Of<T>());
T* componentDataRawRW = (T*)VWorld.Game.EntityManager.GetComponentDataRawRW(entity, typeIndex);
if (componentDataRawRW == null)
{
throw new InvalidOperationException($"Failure to access ReadWrite <{typeof(T).Name}> typeIndex({typeIndex}) on entity({entity}).");
}
return *componentDataRawRW;
}
internal unsafe static T Read<T>(this Entity entity) where T : struct
{
int typeIndex = TypeManager.GetTypeIndex(Il2CppType.Of<T>());
T* componentDataRawRO = (T*)VWorld.Game.EntityManager.GetComponentDataRawRO(entity, typeIndex);
if (componentDataRawRO == null)
{
throw new InvalidOperationException($"Failure to access ReadOnly <{typeof(T).Name}> typeIndex({typeIndex}) on entity({entity}).");
}
return *componentDataRawRO;
}
}
#pragma warning restore CS8500