-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cs
342 lines (311 loc) · 11.8 KB
/
Util.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;
using Rainmeter;
namespace PluginRainForms
{
internal static class Util
{
internal static Type TypeFromString(string typename)
{
Type type;
try
{
type = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(t => t.GetTypes())
.Where(t => string.Equals(t.FullName, typename, StringComparison.OrdinalIgnoreCase))
.First();
}
catch
{
try
{
type = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(t => t.GetTypes())
.Where(t => string.Equals(t.Name, typename, StringComparison.OrdinalIgnoreCase))
.First();
}
catch
{
return null;
}
}
return type;
}
internal static void PrintTypeInfo(LogHelper log, Type type)
{
if (type.IsEnum)
{
foreach (string enumname in Enum.GetNames(type))
{
log.LogNotice(type.Name + "." + enumname);
}
}
int camount = 0;
var con = type.GetConstructors(BindingFlags.Public);
if (con != null)
{
foreach (ConstructorInfo cinfo in type.GetConstructors())
{
var por = cinfo.GetParameters();
bool unsupported = cinfo.GetParameters()
.Any(x =>
x.ParameterType != typeof(string) &&
x.ParameterType != typeof(int) &&
x.ParameterType != typeof(bool) &&
x.ParameterType != typeof(float) &&
x.ParameterType != typeof(double) &&
!x.ParameterType.IsEnum);
if (unsupported) continue;
camount++;
string loginfo = type.Name + "(";
foreach (ParameterInfo pinfo in cinfo.GetParameters())
{
loginfo += pinfo.ParameterType + " " + pinfo.Name + ", ";
}
loginfo = loginfo.Remove(loginfo.Length - 2, 2);
loginfo += ")\n";
log.LogNotice(loginfo);
}
}
var meth = type.GetMethods(BindingFlags.Public | BindingFlags.Static);
if (meth != null)
{
foreach (MethodInfo minfo in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
{
if (minfo.ReturnType != type) continue;
bool unsupported = minfo.GetParameters()
.Any(x =>
x.ParameterType != typeof(string) &&
x.ParameterType != typeof(int) &&
x.ParameterType != typeof(bool) &&
x.ParameterType != typeof(float) &&
x.ParameterType != typeof(double) &&
!x.ParameterType.IsEnum);
if (unsupported) continue;
camount++;
string loginfo = type.Name + "." + minfo.Name + "(";
foreach (ParameterInfo pinfo in minfo.GetParameters())
{
loginfo += pinfo.ParameterType + " " + pinfo.Name + ", ";
}
loginfo = loginfo.Remove(loginfo.Length - 2, 2);
loginfo += ")";
log.LogNotice(loginfo);
}
}
}
internal static object EnumFromString(Type type, string str, bool flag = true)
{
if (Enum.GetNames(type).Any(x => x.ToUpper() == str.ToUpper()))
{
return Enum.Parse(type, str, true);
}
else
{
// if the enum is used as a flag
if (flag)
{
var enumflags = str.Split('|');
if (enumflags.Except(Enum.GetNames(type), StringComparer.OrdinalIgnoreCase).Count() > 0)
{
// found invalid enums
return null;
}
int fEnum = 0;
foreach (string s in enumflags)
{
fEnum = fEnum | (int)Convert.ChangeType(Enum.Parse(type, s), typeof(int));
}
return Enum.ToObject(type, fEnum);
}
}
return null;
}
internal static object ObjectFromString(Rainmeter.API api, Type type, string str)
{
string[] strparams = str.Split(',');
// search and invoke constructors that match the parameters
var constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
foreach (ConstructorInfo cinfo in constructors)
{
object instance = TryInvokeMethod(null, cinfo, strparams);
if (instance != null) return instance;
}
// no constructor matched, lets search for static methods that return an instance
var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Static).Where(x => x.ReturnType == type);
foreach (MethodInfo minfo in methods)
{
object instance = TryInvokeMethod(null, minfo, strparams);
if (instance != null) return instance;
}
return null;
}
internal static object TryInvokeMethod(object instance, MethodBase method, string[] strparams)
{
bool b;
return TryInvokeMethod(instance, method, strparams, out b);
}
// Trys to invoke a method with the supplied parameters. Returns null of return type is void or if not possible.
internal static object TryInvokeMethod(object instance, MethodBase method, string[] strparams, out bool succesfull)
{
succesfull = false;
var parameters = method.GetParameters();
if (strparams.Length >= parameters.Count(x => !x.IsOptional) && strparams.Length <= parameters.Length)
{
try
{
object[] invokeparams = new object[strparams.Length];
for (int i = 0; i < strparams.Length; i++)
{
Type paramtype = parameters[i].ParameterType;
//api.Log(Rainmeter.API.LogType.Debug, "paramtype: " + paramtype);
if (paramtype == typeof(string))
{
invokeparams[i] = strparams[i];
}
else if (paramtype == typeof(int))
{
int p;
if (!int.TryParse(strparams[i], out p)) goto ParamsNotMatching;
invokeparams[i] = p;
}
else if (paramtype == typeof(double))
{
double p;
if (!double.TryParse(strparams[i], out p)) goto ParamsNotMatching;
invokeparams[i] = p;
}
else if (paramtype == typeof(float))
{
float p;
if (!float.TryParse(strparams[i], out p)) goto ParamsNotMatching;
invokeparams[i] = p;
}
else if (paramtype == typeof(bool))
{
bool p;
if (!bool.TryParse(strparams[i], out p)) goto ParamsNotMatching;
invokeparams[i] = p;
}
else
{
if (paramtype.IsEnum)
{
var fEnum = Util.EnumFromString(paramtype, strparams[i]);
if (fEnum != null)
{
invokeparams[i] = fEnum;
continue;
}
}
// non-basic types in constructors are not supported
goto ParamsNotMatching;
}
}
if (method is ConstructorInfo cinfo)
{
object ret = cinfo.Invoke(invokeparams);
if (ret != null)
{
succesfull = true;
return ret;
}
}
else if (method is MethodInfo minfo)
{
object ret = minfo.Invoke(instance, invokeparams);
succesfull = true;
return ret;
}
}
catch (Exception e)
{
//api.Log(Rainmeter.API.LogType.Debug, "Ex: " + e.Message);
}
}
ParamsNotMatching:
return null;
}
internal static Color? ColorFromString(string color)
{
if (color == "")
{
return null;
}
string[] rgba = color.Split(',');
try
{
if (rgba.Length == 4)
{
return Color.FromArgb(
int.Parse(rgba[3]),
int.Parse(rgba[0]),
int.Parse(rgba[1]),
int.Parse(rgba[2]));
}
else if (rgba.Length == 3)
{
return Color.FromArgb(
int.Parse(rgba[0]),
int.Parse(rgba[1]),
int.Parse(rgba[2]));
}
}
catch { }
return null;
}
}
internal class LogHelper
{
private Rainmeter.API api;
internal LogHelper(API api)
{
this.api = api;
}
internal void LogDebug(string message)
{
#if DEBUG
api.Log(API.LogType.Debug, message);
#endif
}
internal void LogError(string message)
{
api.Log(API.LogType.Error, message);
}
internal void LogNotice(string message)
{
api.Log(API.LogType.Notice, message);
}
internal void LogWarning(string message)
{
api.Log(API.LogType.Warning, message);
}
internal void LogParameterNotValid(string param, string paramname)
{
LogError(
"RainForms.dll: \"" +
param +
"\" is not a valid " +
paramname);
}
internal void LogPropertyValueNotValid(string value, PropertyInfo prop)
{
LogError(
"RainForms.dll: The value " +
value +
" for property " +
prop.Name +
" is not valid, it needs to be of type " +
prop.PropertyType);
}
}
}