-
Notifications
You must be signed in to change notification settings - Fork 244
/
ObjectHandler.cs
198 lines (169 loc) · 5.73 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
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
namespace LeagueSharp.Common
{
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// Provides cached game objects.
/// </summary>
[Obsolete("Cache enforced in LeagueSharp Core, class obsolete and prone to delete.", false)]
public class ObjectHandler
{
#region Static Fields
/// <summary>
/// The game objects
/// </summary>
private static readonly Dictionary<Type, Dictionary<int, GameObject>> gameObjects =
new Dictionary<Type, Dictionary<int, GameObject>>();
#endregion
#region Constructors and Destructors
/// <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;
}
#endregion
#region Public Properties
/// <summary>
/// Gets the player.
/// </summary>
/// <value>The player.</value>
public static Obj_AI_Hero Player
{
get
{
return ObjectManager.Player;
}
}
#endregion
#region Public Methods and Operators
/// <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.Where(o => o.IsValid<T>()).ToList().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;
}
#endregion
#region Methods
/// <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);
}
}
#endregion
/// <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()
{
#region Public Properties
/// <summary>
/// Gets the allies.
/// </summary>
/// <value>The allies.</value>
public List<T> Allies
{
get
{
return this.FindAll(o => o.IsValid<T>() && o.IsAlly);
}
}
/// <summary>
/// Gets the enemies.
/// </summary>
/// <value>The enemies.</value>
public List<T> Enemies
{
get
{
return this.FindAll(o => o.IsValid<T>() && o.IsEnemy);
}
}
/// <summary>
/// Gets the neutrals.
/// </summary>
/// <value>The neutrals.</value>
public List<T> Neutrals
{
get
{
return this.FindAll(o => o.IsValid<T>() && o.Team == GameObjectTeam.Neutral);
}
}
#endregion
}
}
}