forked from mfoltz/Bloodcraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
219 lines (212 loc) · 7.18 KB
/
Extensions.cs
File metadata and controls
219 lines (212 loc) · 7.18 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using Bloodcraft.Services;
using Il2CppInterop.Runtime;
using ProjectM;
using ProjectM.Gameplay.Systems;
using ProjectM.Network;
using ProjectM.Scripting;
using Stunlock.Core;
using System.Runtime.InteropServices;
using Unity.Collections;
using Unity.Entities;
using static Bloodcraft.Services.PlayerService;
namespace Bloodcraft;
internal static class Extensions
{
static EntityManager EntityManager => Core.EntityManager;
static ServerGameManager ServerGameManager => Core.ServerGameManager;
static SystemService SystemService => Core.SystemService;
static PrefabCollectionSystem PrefabCollectionSystem => SystemService.PrefabCollectionSystem;
public delegate void ActionRef<T>(ref T item);
public static void With<T>(this Entity entity, ActionRef<T> action) where T : struct
{
T item = entity.ReadRW<T>();
action(ref item);
EntityManager.SetComponentData(entity, item);
}
public unsafe static void Write<T>(this Entity entity, T componentData) where T : struct
{
// Get the ComponentType for T
var ct = new ComponentType(Il2CppType.Of<T>());
// Marshal the component data to a byte array
byte[] byteArray = StructureToByteArray(componentData);
// Get the size of T
int size = Marshal.SizeOf<T>();
// Create a pointer to the byte array
fixed (byte* p = byteArray)
{
// Set the component data
EntityManager.SetComponentDataRaw(entity, ct.TypeIndex, p, size);
}
}
public static byte[] StructureToByteArray<T>(T structure) where T : struct
{
int size = Marshal.SizeOf(structure);
byte[] byteArray = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(structure, ptr, true);
Marshal.Copy(ptr, byteArray, 0, size);
Marshal.FreeHGlobal(ptr);
return byteArray;
}
public unsafe static T ReadRW<T>(this Entity entity) where T : struct
{
var ct = new ComponentType(Il2CppType.Of<T>());
void* componentDataRawRW = EntityManager.GetComponentDataRawRW(entity, ct.TypeIndex);
T componentData = Marshal.PtrToStructure<T>(new IntPtr(componentDataRawRW));
return componentData;
}
public unsafe static T Read<T>(this Entity entity) where T : struct
{
var ct = new ComponentType(Il2CppType.Of<T>());
void* rawPointer = EntityManager.GetComponentDataRawRO(entity, ct.TypeIndex);
T componentData = Marshal.PtrToStructure<T>(new IntPtr(rawPointer));
return componentData;
}
public static DynamicBuffer<T> ReadBuffer<T>(this Entity entity) where T : struct
{
return EntityManager.GetBuffer<T>(entity);
}
public static bool TryGetComponent<T>(this Entity entity, out T componentData) where T : struct
{
componentData = default;
if (entity.Has<T>())
{
componentData = entity.Read<T>();
return true;
}
return false;
}
public static bool Has<T>(this Entity entity)
{
var ct = new ComponentType(Il2CppType.Of<T>());
return EntityManager.HasComponent(entity, ct);
}
public static string LookupName(this PrefabGUID prefabGUID)
{
return (PrefabCollectionSystem.PrefabGuidToNameDictionary.ContainsKey(prefabGUID)
? PrefabCollectionSystem.PrefabGuidToNameDictionary[prefabGUID] + " " + prefabGUID : "Guid Not Found").ToString();
}
public static string GetPrefabName(this PrefabGUID itemPrefabGUID)
{
return LocalizationService.GetPrefabName(itemPrefabGUID);
}
public static void LogComponentTypes(this Entity entity)
{
NativeArray<ComponentType>.Enumerator enumerator = EntityManager.GetComponentTypes(entity).GetEnumerator();
Core.Log.LogInfo("===");
while (enumerator.MoveNext())
{
ComponentType current = enumerator.Current;
Core.Log.LogInfo($"{current}");
}
Core.Log.LogInfo("===");
}
public static void Add<T>(this Entity entity)
{
var ct = new ComponentType(Il2CppType.Of<T>());
EntityManager.AddComponent(entity, ct);
}
public static void Remove<T>(this Entity entity)
{
var ct = new ComponentType(Il2CppType.Of<T>());
EntityManager.RemoveComponent(entity, ct);
}
public static Entity GetOwner(this Entity entity)
{
return ServerGameManager.GetOwner(entity);
}
public static bool TryGetFollowedPlayer(this Entity entity, out Entity player)
{
player = Entity.Null;
if (entity.Has<Follower>())
{
Follower follower = entity.Read<Follower>();
Entity followed = follower.Followed._Value;
if (followed.IsPlayer())
{
player = followed;
return true;
}
}
return false;
}
public static bool TryGetPlayer(this Entity entity, out Entity player)
{
player = Entity.Null;
if (entity.Has<PlayerCharacter>())
{
player = entity;
return true;
}
return false;
}
public static bool IsPlayer(this Entity entity)
{
if (entity.Has<VampireTag>())
{
return true;
}
return false;
}
public static bool IsFollowingPlayer(this Entity entity)
{
if (entity.Has<Follower>())
{
Follower follower = entity.Read<Follower>();
if (follower.Followed._Value.IsPlayer())
{
return true;
}
}
return false;
}
public static Entity GetBuffTarget(this Entity entity)
{
return CreateGameplayEventServerUtility.GetBuffTarget(EntityManager, entity);
}
public static Entity GetPrefabEntity(this Entity entity)
{
return ServerGameManager.GetPrefabEntity(entity.Read<PrefabGUID>());
}
public static Entity GetSpellTarget(this Entity entity)
{
return CreateGameplayEventServerUtility.GetSpellTarget(EntityManager, entity);
}
public static Entity GetTeamEntity(this Entity entity)
{
if (entity.Has<TeamReference>())
{
return entity.Read<TeamReference>().Value._Value;
}
return Entity.Null;
}
public static bool Exists(this Entity entity)
{
return EntityManager.Exists(entity);
}
public static bool Disabled(this Entity entity)
{
return entity.Has<Disabled>();
}
public static ulong GetSteamId(this Entity entity)
{
return entity.Read<PlayerCharacter>().UserEntity.Read<User>().PlatformId;
}
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var item in source)
{
action(item);
}
}
public static bool TryGetPlayerInfo(this ulong steamId, out PlayerInfo playerInfo)
{
if (PlayerCache.TryGetValue(steamId.ToString(), out playerInfo)) return true;
return false;
}
public static bool TryGetPlayerInfo(this string playerName, out PlayerInfo playerInfo)
{
if (PlayerCache.TryGetValue(playerName, out playerInfo)) return true;
return false;
}
}