-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathObjectHandler.cs
114 lines (96 loc) · 3.21 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
using System;
using System.Collections.Generic;
using System.Linq;
using LeagueSharp;
namespace LeagueSharp.Common
{
[Obsolete(
"This will most likely not be needed anymore when Jodus adds it to LeagueSharp.dll," +
"only use it if you know what you are doing!", false)]
public class ObjectHandler
{
public static Obj_AI_Hero Player
{
get { return ObjectManager.Player; }
}
private static readonly Dictionary<Type, Dictionary<int, GameObject>> gameObjects = new Dictionary<Type, Dictionary<int, GameObject>>();
static ObjectHandler()
{
var i = 0;
// All existing objects
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;
}
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;
}
private static void Obj_AI_Base_OnDelete(GameObject sender, EventArgs args)
{
foreach (var dictionary in gameObjects.Values)
{
dictionary.Remove(sender.NetworkId);
}
}
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.ToList().ConvertAll(o => (T)o));
}
}
return found;
}
public static T GetUnitByNetworkId<T>(int networkId) where T : GameObject, new()
{
foreach(var dict in gameObjects.Values)
{
if (dict.ContainsKey(networkId))
{
return (T)dict[networkId];
}
}
return null;
}
public class GameObjectWrapper<T> : List<T> where T : GameObject, new()
{
public List<T> Allies
{
get { return base.FindAll(o => o.IsAlly); }
}
public List<T> Enemies
{
get { return base.FindAll(o => o.IsEnemy); }
}
public List<T> Neutrals
{
get { return base.FindAll(o => o.Team == GameObjectTeam.Neutral); }
}
}
}
}