-
Notifications
You must be signed in to change notification settings - Fork 9
/
MyBaseHookable.cs
70 lines (68 loc) · 2.28 KB
/
MyBaseHookable.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
/*
* Created by SharpDevelop.
* User: JordivdMolen
* Date: 2/14/2018
* Time: 9:39 PM
*
* Using this file for commercial purposes can result
* in violating the license!
*/
using SFSML.Exceptions;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace SFSML.HookSystem
{
/// <summary>
/// Description of MyBaseHookable.
/// </summary>
public class MyBaseHookable
{
private List<MyInitialHook> hooks = new List<MyInitialHook>();
public MyBaseHookable()
{
}
public void registerListener<T>(MyBaseHook<T> e)
{
if (!e.isListener())
{
throw new MyCoreException("This hook is not a listener! @ RegisterListener","registerListener<?>()");
}
this.hooks.Add(e);
}
public void removeListener(MyInitialHook e)
{
this.hooks.Remove(e);
}
public T castHook<T>(T e)
{
T usedCaller = (T) ((MyInitialHook)(object)e).Clone();
MyBaseHook<T> convertedBase = (Object) e as MyBaseHook<T>;
Dictionary<String, FieldInfo> initialFields = convertedBase.getEventArgumets();
foreach (MyInitialHook initHook in this.hooks)
{
T ih = (T) (object) initHook;
MyBaseHook<T> convertedHook = (MyBaseHook<T>) initHook;
T afterInvoke = convertedHook.invoke(usedCaller);
convertedHook = (object) afterInvoke as MyBaseHook<T>;
Dictionary<String, FieldInfo> initHookFields = convertedHook.getEventArgumets();
if (convertedHook.isCanceled())
{
convertedBase.forceCanceled(true);
}
foreach (String fieldName in initialFields.Keys)
{
FieldInfo orginField = initialFields[fieldName];
FieldInfo newField = initHookFields[fieldName];
object orginValue = orginField.GetValue(e);
object newValue = newField.GetValue(afterInvoke);
if (orginValue != newValue)
{
orginField.SetValue(e, newValue);
}
}
}
return (T) (object) convertedBase;
}
}
}