-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginRainForms.cs
409 lines (355 loc) · 14.4 KB
/
PluginRainForms.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using Rainmeter;
namespace PluginRainForms
{
class Measure
{
internal static List<Measure> Measures = new List<Measure>();
internal Measure Parent;
internal IntPtr Skin;
internal string Name;
internal Type Type;
internal bool Invalid = false;
internal API api;
internal LogHelper log;
internal Control Control;
internal string TabName;
public Measure()
{
Measures.Add(this);
}
internal void Dispose()
{
Measures.Remove(this);
}
internal void AddControlToParent()
{
Control.ControlCollection ctrlCollection;
if (Parent.Control is TabControl tabControl)
{
if (TabName == null || TabName == "")
{
log.LogError(Name + " requires a TabName to be in a TabControl");
Invalid = true;
return;
}
if (!tabControl.TabPages.ContainsKey(TabName))
{
tabControl.TabPages.Add(TabName, TabName);
}
ctrlCollection = tabControl.TabPages[TabName].Controls;
}
else
{
ctrlCollection = Parent.Control.Controls;
}
ctrlCollection.Add(Control);
}
internal void ParseMeasureProps(API api)
{
this.api = api;
this.log = new LogHelper(api);
this.Skin = api.GetSkin();
this.Name = api.GetMeasureName();
}
static public implicit operator Measure(IntPtr data)
{
return (Measure)GCHandle.FromIntPtr(data).Target;
}
public IntPtr buffer = IntPtr.Zero;
}
internal static class PropertyParser
{
internal static void ParseProperties(Rainmeter.API api, object obj)
{
LogHelper log = new LogHelper(api);
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
EventInfo[] events = type.GetEvents(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo pinfo in properties)
{
if (pinfo.CanWrite)
{
if (pinfo.PropertyType == typeof(string))
{
//log.LogDebug("Found string Property: " + pinfo.Name);
string propval = api.ReadString(pinfo.Name, "");
if (propval != "")
{
log.LogDebug("Setting " + pinfo.PropertyType.Name + " Property " + pinfo.Name + " to " + propval);
pinfo.SetValue(obj, propval, null);
}
}
else if (pinfo.PropertyType == typeof(int))
{
int val = (int)pinfo.GetValue(obj, null);
//log.LogDebug("Found int Property: " + pinfo.Name + " with value: " + val);
int propval = api.ReadInt(pinfo.Name, val);
if (propval != val)
{
log.LogDebug("Setting " + pinfo.PropertyType.Name + " Property " + pinfo.Name + " to " + propval);
pinfo.SetValue(obj, propval, null);
}
}
else if (pinfo.PropertyType == typeof(bool))
{
bool val = (bool)pinfo.GetValue(obj, null);
//log.LogDebug("Found " + pinfo.PropertyType + " Property: " + pinfo.Name + " with value: " + val);
bool propval = api.ReadInt(pinfo.Name, Convert.ToInt32(val)) > 0;
if (propval != val)
{
log.LogDebug("Setting " + pinfo.PropertyType.Name + " Property " + pinfo.Name + " to " + propval);
pinfo.SetValue(obj, propval, null);
}
}
else if (pinfo.PropertyType == typeof(float))
{
float val = (float)pinfo.GetValue(obj, null);
//log.LogDebug("Found float Property: " + pinfo.Name + " with value: " + val);
float propval = (float)api.ReadDouble(pinfo.Name, val);
if (propval != val)
{
log.LogDebug("Setting " + pinfo.PropertyType.Name + " Property " + pinfo.Name + " to " + propval);
pinfo.SetValue(obj, propval, null);
}
}
else if (pinfo.PropertyType == typeof(double))
{
double val = (double)pinfo.GetValue(obj, null);
//log.LogDebug("Found double Property: " + pinfo.Name + " with value: " + val);
double propval = api.ReadDouble(pinfo.Name, val);
if (propval != val)
{
log.LogDebug("Setting " + pinfo.PropertyType.Name + " Property " + pinfo.Name + " to " + propval);
pinfo.SetValue(obj, propval, null);
}
}
else
{
string propval = api.ReadString(pinfo.Name, "");
if (propval != "")
{
object val = null;
if (pinfo.PropertyType == typeof(Color))
{
val = Util.ColorFromString(propval);
}
else
{
val = pinfo.PropertyType.IsEnum ?
Util.EnumFromString(pinfo.PropertyType, propval) :
Util.ObjectFromString(api, pinfo.PropertyType, propval);
}
if (val != null)
{
pinfo.SetValue(obj, val, null);
}
else
{
log.LogPropertyValueNotValid(propval, pinfo);
}
}
}
}
}
foreach (EventInfo einfo in events)
{
// winforms syntax for events
string evntval = api.ReadString(einfo.Name, "");
if (evntval == "")
{
// rainmeter syntax for events
evntval = api.ReadString("On" + einfo.Name, "");
if (evntval == "")
{
continue;
}
}
// remove existing event
cEventHelper.RemoveEventHandler(obj, einfo.Name);
// create new event
EventHandler untypedHandler = delegate (object sender, EventArgs args)
{
api.Execute(evntval);
};
Delegate typedHandler = Delegate.CreateDelegate(einfo.EventHandlerType,
untypedHandler.Target, untypedHandler.Method);
einfo.AddEventHandler(obj, typedHandler);
}
}
}
public class Plugin
{
[DllExport]
public static void Initialize(ref IntPtr data, IntPtr rm)
{
Measure measure = new Measure();
data = GCHandle.ToIntPtr(GCHandle.Alloc(measure));
Rainmeter.API api = (Rainmeter.API)rm;
measure.ParseMeasureProps(api);
}
[DllExport]
public static void Finalize(IntPtr data)
{
Measure measure = (Measure)data;
if (measure.buffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(measure.buffer);
}
GCHandle.FromIntPtr(data).Free();
if (measure.Control != null && !measure.Control.IsDisposed)
{
measure.Control.Dispose();
measure.Control = null;
}
measure.Dispose();
}
[DllExport]
public static void Reload(IntPtr data, IntPtr rm, ref double maxValue)
{
Measure measure = (Measure)data;
Rainmeter.API api = (Rainmeter.API)rm;
string type = api.ReadString("Type","");
measure.Type = typeof(Control)
.Assembly
.GetTypes()
.Where(x => x.GetBaseTypes().Contains(typeof(Control)))
.FirstOrDefault(x => x.Name == type);
if (measure.Type == default(Type))
{
measure.log.LogParameterNotValid(type, "Type");
measure.Invalid = true;
return;
}
if (measure.Control == null || measure.Type.GetType() != measure.Type || measure.Invalid)
{
measure.Control = (Control)measure.Type.GetConstructor(Type.EmptyTypes).Invoke(null);
}
PropertyParser.ParseProperties(api, measure.Control);
measure.TabName = api.ReadString("TabName", "");
if (measure.Control.GetType() != typeof(Form))
{
// Find parent using name AND the skin handle to be sure that it's the right one.
string parentName = api.ReadString("ParentName", "");
foreach (Measure parentMeasure in Measure.Measures)
{
if (parentMeasure.Skin.Equals(measure.Skin) && parentMeasure.Name.Equals(parentName))
{
measure.Parent = parentMeasure;
}
}
if (measure.Parent == null)
{
measure.log.LogError("RainForms.dll: " + measure.Type.ToString() + " needs a parent.");
measure.Invalid = true;
return;
}
measure.AddControlToParent();
}
}
[DllExport]
public static double Update(IntPtr data)
{
Measure measure = (Measure)data;
if (measure.Invalid) return 0.0d;
if (measure.Control is CheckBox cb)
{
return Convert.ToDouble(cb.Checked);
}
return 0.0d;
}
[DllExport]
public static IntPtr GetString(IntPtr data)
{
Measure measure = (Measure)data;
if (measure.buffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(measure.buffer);
measure.buffer = IntPtr.Zero;
}
if (measure.Invalid)
{
measure.buffer = Marshal.StringToHGlobalUni("");
return measure.buffer;
}
string returnval = "";
if (measure.Control is TextBox tb)
{
returnval = tb.Text;
}
measure.buffer = Marshal.StringToHGlobalUni(returnval);
return measure.buffer;
}
[DllExport]
public static void ExecuteBang(IntPtr data, [MarshalAs(UnmanagedType.LPWStr)]String arg)
{
Measure measure = (Measure)data;
if (arg == "" || measure.Invalid) return;
var args = arg.Split(' ');
if (args[0] == "RFTypeInfo")
{
Type type = Util.TypeFromString(args[1]);
if (type == null)
{
measure.log.LogError("The type \"" + args[1] + "\" was not found");
return;
}
measure.log.LogNotice("--------------------------");
Util.PrintTypeInfo(measure.log, type);
measure.log.LogNotice("Supported constructors:");
if (!type.IsEnum)
{
measure.log.LogNotice("Supported constructors:");
}
else
{
measure.log.LogNotice("Enum values:");
}
measure.log.LogNotice("--------------------------");
measure.log.LogNotice("RFTypeInfo for [" + type + "]");
return;
}
else if (args[0] == "RFPropertyInfo")
{
var prop = measure.Type.GetProperty(args[1]);
if (prop == null)
{
measure.log.LogError("The property \"" + args[1] + "\" was not found");
return;
}
measure.log.LogNotice("--------------------------");
Util.PrintTypeInfo(measure.log, prop.PropertyType);
if (!prop.PropertyType.IsEnum)
{
measure.log.LogNotice("The type has following supported constructors:");
}
else
{
measure.log.LogNotice("The type has following enum values:");
}
measure.log.LogNotice("The property is of type " + prop.PropertyType);
measure.log.LogNotice("--------------------------");
measure.log.LogNotice("RFPropertyInfo for [" + prop.DeclaringType.Name + "." + prop.Name + "]");
return;
}
var methods = measure.Type.GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(x => x.IsPublic && x.Name == args[0]).ToArray();
bool successful = false;
for (int i = 0; i < methods.Length && successful == false; i++)
{
Util.TryInvokeMethod(measure.Control, methods[i], args.Skip(1).ToArray(), out successful);
}
if (successful == false)
{
measure.log.LogError("RainForms.dll: " + args[0] + " is not a valid method in type " + measure.Type);
}
measure.log.LogDebug("Received Bang: " + args[0]);
}
}
}