-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathObjectHandler.cs
163 lines (146 loc) · 5.2 KB
/
ObjectHandler.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
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace LeagueSharp.Common
{
/// <summary>
/// Provides cached game objects.
/// </summary>
[Obsolete(
"This will most likely not be needed anymore when Jodus adds it to LeagueSharp.dll and chewy fixes cache," +
"only use it if you know what you are doing!", false)]
public class ObjectHandler
{
/// <summary>
/// The game objects
/// </summary>
private static readonly Dictionary<Type, Dictionary<int, GameObject>> gameObjects =
new Dictionary<Type, Dictionary<int, GameObject>>();
/// <summary>
/// Initializes static members of the <see cref="ObjectHandler"/> class.
/// </summary>
static ObjectHandler()
{
// All existing objects
var i = 0;
foreach (var obj in ObjectManager.Get<GameObject>())
{
var type = obj.GetType();
if (!gameObjects.ContainsKey(type))
{
gameObjects.Add(type, new Dictionary<int, GameObject>());
}
var index = obj.NetworkId;
if (index == 0)
{
index = i;
i++;
}
gameObjects[type][index] = obj;
}
// Listen to events
GameObject.OnCreate += Obj_AI_Base_OnCreate;
GameObject.OnDelete += Obj_AI_Base_OnDelete;
}
/// <summary>
/// Gets the player.
/// </summary>
/// <value>The player.</value>
public static Obj_AI_Hero Player
{
get { return ObjectManager.Player; }
}
/// <summary>
/// Fired when a <see cref="Obj_AI_Base"/> is created.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="args">The <see cref="EventArgs"/> instance containing the event data.</param>
private static void Obj_AI_Base_OnCreate(GameObject sender, EventArgs args)
{
var type = sender.GetType();
if (!gameObjects.ContainsKey(type))
{
gameObjects.Add(type, new Dictionary<int, GameObject>());
}
gameObjects[type][sender.NetworkId] = sender;
}
/// <summary>
/// Fired when a <see cref="Obj_AI_Base"/> is deleted.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="args">The <see cref="EventArgs"/> instance containing the event data.</param>
private static void Obj_AI_Base_OnDelete(GameObject sender, EventArgs args)
{
foreach (var dictionary in gameObjects.Values)
{
dictionary.Remove(sender.NetworkId);
}
}
/// <summary>
/// Gets all of the game objects of the specified type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>GameObjectWrapper<T>.</returns>
public static GameObjectWrapper<T> Get<T>() where T : GameObject, new()
{
var type = typeof(T);
var found = new GameObjectWrapper<T>();
foreach (var key in gameObjects.Keys)
{
if (type.IsAssignableFrom(key))
{
found.AddRange(gameObjects[key].Values.FindAll(o => o.IsValid<T>()).ConvertAll(o => (T)o));
}
}
return found;
}
/// <summary>
/// Gets the unit by network identifier.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="networkId">The network identifier.</param>
/// <returns>T.</returns>
public static T GetUnitByNetworkId<T>(int networkId) where T : GameObject, new()
{
foreach (var dict in gameObjects.Values)
{
if (dict.ContainsKey(networkId) && dict[networkId].IsValid)
{
return dict[networkId] as T;
}
}
return null;
}
/// <summary>
/// A wrapper around a <see cref="List{T}"/> that provides extensions.
/// </summary>
/// <typeparam name="T"></typeparam>
public class GameObjectWrapper<T> : List<T> where T : GameObject, new()
{
/// <summary>
/// Gets the allies.
/// </summary>
/// <value>The allies.</value>
public List<T> Allies
{
get { return FindAll(o => o.IsValid<T>() && o.IsAlly); }
}
/// <summary>
/// Gets the enemies.
/// </summary>
/// <value>The enemies.</value>
public List<T> Enemies
{
get { return FindAll(o => o.IsValid<T>() && o.IsEnemy); }
}
/// <summary>
/// Gets the neutrals.
/// </summary>
/// <value>The neutrals.</value>
public List<T> Neutrals
{
get { return FindAll(o => o.IsValid<T>() && o.Team == GameObjectTeam.Neutral); }
}
}
}
}