-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHelper.cs
More file actions
150 lines (130 loc) · 5.49 KB
/
Helper.cs
File metadata and controls
150 lines (130 loc) · 5.49 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
using Il2CppInterop.Runtime;
using ProjectM;
using ProjectM.Shared;
using Stunlock.Core;
using System.Linq;
using Unity.Collections;
using Unity.Entities;
namespace KindredExtract;
// This is an anti-pattern, move stuff away from Helper not into it
internal static partial class Helper
{
public static AdminAuthSystem adminAuthSystem = Core.TheWorld.GetExistingSystemManaged<AdminAuthSystem>();
public static PrefabGUID GetPrefabGUID(Entity entity)
{
var entityManager = Core.EntityManager;
PrefabGUID guid;
try
{
guid = entityManager.GetComponentData<PrefabGUID>(entity);
}
catch
{
guid = new PrefabGUID(0);
}
return guid;
}
public static Entity AddItemToInventory(Entity recipient, PrefabGUID guid, int amount)
{
try
{
var gameData = Core.TheWorld.GetExistingSystemManaged<GameDataSystem>();
var itemSettings = AddItemSettings.Create(Core.EntityManager, gameData.ItemHashLookupMap);
var inventoryResponse = InventoryUtilitiesServer.TryAddItem(itemSettings, recipient, guid, amount);
return inventoryResponse.NewEntity;
}
catch (System.Exception e)
{
Core.LogException(e);
}
return new Entity();
}
public static NativeArray<Entity> GetEntitiesByComponentType<T1>(bool includeAll = false, bool includeDisabled = false, bool includeSpawn = false, bool includePrefab = false, bool includeDestroyed = false)
{
EntityQueryOptions options = EntityQueryOptions.Default;
if (includeAll) options |= EntityQueryOptions.IncludeAll;
if (includeDisabled) options |= EntityQueryOptions.IncludeDisabled;
if (includeSpawn) options |= EntityQueryOptions.IncludeSpawnTag;
if (includePrefab) options |= EntityQueryOptions.IncludePrefab;
if (includeDestroyed) options |= EntityQueryOptions.IncludeDestroyTag;
EntityQueryBuilder entityQueryBuilder = new(Allocator.Temp);
entityQueryBuilder.AddAll(new(Il2CppType.Of<T1>(), ComponentType.AccessMode.ReadWrite));
entityQueryBuilder.WithOptions(options);
var query = Core.EntityManager.CreateEntityQuery(ref entityQueryBuilder);
var entities = query.ToEntityArray(Allocator.Temp);
return entities;
}
public static NativeArray<Entity> GetEntitiesByComponentTypes<T1, T2>(bool includeAll = false, bool includeDisabled = false, bool includeSpawn = false, bool includePrefab = false, bool includeDestroyed = false)
{
EntityQueryOptions options = EntityQueryOptions.Default;
if (includeAll) options |= EntityQueryOptions.IncludeAll;
if (includeDisabled) options |= EntityQueryOptions.IncludeDisabled;
if (includeSpawn) options |= EntityQueryOptions.IncludeSpawnTag;
if (includePrefab) options |= EntityQueryOptions.IncludePrefab;
if (includeDestroyed) options |= EntityQueryOptions.IncludeDestroyTag;
EntityQueryBuilder entityQueryBuilder = new(Allocator.Temp);
entityQueryBuilder.AddAll(new(Il2CppType.Of<T1>(), ComponentType.AccessMode.ReadWrite));
entityQueryBuilder.AddAll(new(Il2CppType.Of<T2>(), ComponentType.AccessMode.ReadWrite));
entityQueryBuilder.WithOptions(options);
var query = Core.EntityManager.CreateEntityQuery(ref entityQueryBuilder);
var entities = query.ToEntityArray(Allocator.Temp);
return entities;
}
public static void RepairGear(Entity Character, bool repair = true)
{
Equipment equipment = Character.Read<Equipment>();
NativeList<Entity> equippedItems = new(Allocator.Temp);
equipment.GetAllEquipmentEntities(equippedItems);
foreach (var equippedItem in equippedItems)
{
if (equippedItem.Has<Durability>())
{
var durability = equippedItem.Read<Durability>();
if (repair)
{
durability.Value = durability.MaxDurability;
}
else
{
durability.Value = 0;
}
equippedItem.Write(durability);
}
}
equippedItems.Dispose();
for (int i = 0; i < 36; i++)
{
if (InventoryUtilities.TryGetItemAtSlot(Core.EntityManager, Character, i, out InventoryBuffer item))
{
var itemEntity = item.ItemEntity._Entity;
if (itemEntity.Has<Durability>())
{
var durability = itemEntity.Read<Durability>();
if (repair)
{
durability.Value = durability.MaxDurability;
}
else
{
durability.Value = 0;
}
itemEntity.Write(durability);
}
}
}
}
public static bool FindClan(string clanName, out Entity clanEntity)
{
var clans = Helper.GetEntitiesByComponentType<ClanTeam>().ToArray();
var matchedClans = clans.Where(x => x.Read<ClanTeam>().Name.ToString().ToLower() == clanName.ToLower());
foreach (var clan in matchedClans)
{
var members = Core.EntityManager.GetBuffer<ClanMemberStatus>(clan);
if (members.Length == 0) continue;
clanEntity = clan;
return true;
}
clanEntity = new Entity();
return false;
}
}