forked from Fronkln/HActLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflection.cs
163 lines (135 loc) · 4.85 KB
/
Reflection.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.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HActLib.Internal
{
public class Reflection
{
public static bool Done = false;
public static Dictionary<Game, Dictionary<uint, Type>> ElementNodes = new Dictionary<Game, Dictionary<uint, Type>>();
public static Dictionary<Game, List<string>> GamePrefixes = new Dictionary<Game, List<string>>();
public static Type GetElementEnumFromGame(Game game)
{
switch(game)
{
default:
return typeof(LADIWNodeIDs);
case Game.LADIW:
return typeof(LADIWNodeIDs);
case Game.LAD7Gaiden:
return typeof(GaidenNodeIDs);
case Game.LJ:
return typeof(LJNodeIDs);
case Game.YLAD:
return typeof(YLADNodeIDs);
case Game.YK2:
return typeof(YK2NodeIDs);
case Game.JE:
return typeof(JENodeIDs);
case Game.Y6:
return typeof(Y6NodeIDs);
case Game.Y6Demo:
return typeof(Y6NodeIDs);
case Game.Y5:
return typeof(Y5NodeIDs);
case Game.Ishin:
return typeof(IshinNodeIDs);
case Game.Y0:
return typeof(Y0NodeIDs);
case Game.YK1:
return typeof(Y0NodeIDs);
}
}
public static string[] GetGamePrefixes(Game game)
{
if (!Done)
Process();
return GamePrefixes[game].ToArray();
}
public static uint GetElementIDByName(string name, Game game)
{
if (!Done)
Process();
Type elementEnum = GetElementEnumFromGame(game);
string[] names = Enum.GetNames(elementEnum);
Array values = Enum.GetValues(elementEnum);
int idx = Array.IndexOf(names, name);
if (idx > -1)
return (uint)values.GetValue(idx);
else
return 0;
}
public static string GetElementNameByID(uint id, Game game)
{
if(id >= 1337)
{
switch(id)
{
default:
return "UNKNOWN_EX_AUTH_NODE_" + id;
case 1337:
return "System Speed (EX Auth)";
case 60010:
return "Transit HAct (Like a Brawler)";
}
}
Type elementEnum = GetElementEnumFromGame(game);
try
{
string name = Enum.GetName(elementEnum, id);
if (string.IsNullOrEmpty(name))
return "UNKNOWN_NODE_" + id;
else
return name;
}
catch
{
return "UNKNOWN_NODE_" + id;
}
}
public static Game GetGameFromString(string str)
{
if (!Done)
Process();
str = str.ToLowerInvariant();
foreach (var kv in GamePrefixes)
foreach (string prefix in kv.Value)
if (prefix == str)
return kv.Key;
return (Game)9999;
}
public static void Process()
{
ElementNodes.Clear();
Type[] types = Assembly.GetExecutingAssembly().GetTypes();
Type baseType = typeof(NodeElement);
for (int i = 0; i < Enum.GetValues(typeof(Game)).Length; i++)
{
ElementNodes.Add((Game)i, new Dictionary<uint, Type>());
GamePrefixes.Add((Game)i, new List<string>());
}
foreach(Type type in types)
{
if (!type.IsSubclassOf(baseType))
{
if(type.IsEnum)
{
GamePrefixAttribute[] prefixes = type.GetCustomAttributes<GamePrefixAttribute>().ToArray();
foreach (GamePrefixAttribute attrib in prefixes)
GamePrefixes[attrib.Game].AddRange(attrib.Prefixes);
}
continue;
}
ElementIDAttribute[] elementIDs = type.GetCustomAttributes<ElementIDAttribute>().ToArray();
foreach(ElementIDAttribute attrib in elementIDs)
ElementNodes[attrib.Game][attrib.ID] = type;
}
ElementNodes[Game.Y6Demo] = ElementNodes[Game.Y6];
ElementNodes[Game.YK1] = ElementNodes[Game.Y0];
Done = true;
}
}
}