-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExtensions.cs
63 lines (54 loc) · 2.17 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
using System;
using System.Collections.Generic;
using Il2CppAssets.Scripts.Models;
using Il2CppAssets.Scripts.Models.Bloons;
using Il2CppAssets.Scripts.Unity.UI_New.InGame;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSystem.Reflection;
namespace BloonsClicker;
public static class Extensions
{
public static void UpdateCollisionPass(this ProjectileModel projectile, int collisionPass)
{
projectile.collisionPasses ??= Array.Empty<int>();
if (!projectile.collisionPasses.Contains(collisionPass))
{
projectile.collisionPasses = projectile.collisionPasses.Concat([collisionPass]).ToArray();
}
}
private static readonly Dictionary<string, FieldInfo> BehaviorsFieldCache = new();
public static bool HasBehaviorWithName(this Model model, string name, bool throwIfNotFound = true)
{
var type = model.GetIl2CppType();
if (!BehaviorsFieldCache.TryGetValue(type.FullName, out var behaviorsField))
{
behaviorsField = type.GetField("behaviors");
if (behaviorsField == null)
{
if (throwIfNotFound)
throw new InvalidOperationException($"Model {model.name} does not have a behaviors field");
return false;
}
BehaviorsFieldCache.Add(type.FullName, behaviorsField);
}
var behaviors = behaviorsField.GetValue(model).Cast<Il2CppReferenceArray<Model>>();
return Enumerable.Any(behaviors, behavior => behavior.name == name);
}
private static readonly Dictionary<string, float> MaxHealthCache = new();
public static float GetMaxHealth(this BloonModel bloonModel)
{
if (MaxHealthCache.TryGetValue(bloonModel.id, out var maxHealth))
{
return maxHealth;
}
var totalHealth = 0;
bloonModel.UpdateChildBloonModels();
foreach (var child in bloonModel.GetChildBloonModels(InGame.instance.bridge.Simulation))
{
totalHealth += child.maxHealth;
}
totalHealth += bloonModel.maxHealth;
MaxHealthCache[bloonModel.id] = totalHealth;
return totalHealth;
}
}