-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event.cs
177 lines (138 loc) · 5.54 KB
/
Event.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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BetterEventSystem.Exceptions;
namespace BetterEventSystem {
public class EventArgs {
public object data;
// for getters, setters
private bool _cancelled;
public bool cancelled {
get => _cancelled;
set => cancel(value); // this doesnt do anything special, but maybe in future the cancel method will do something other than just set the value.
}
public void cancel(bool cancel = true) {
_cancelled = cancel;
}
public EventArgs(object data) {
this.data = data;
}
}
[Serializable]
public class Event {
public bool AllowAsync;
public string Name;
private List<Action<EventArgs, Action<EventArgs>>> _preprocessors = new List<Action<EventArgs, Action<EventArgs>>>();
private List<Action<EventArgs>> _listeners = new List<Action<EventArgs>>();
private List<Action<EventArgs, Action<EventArgs>>> _postprocessors = new List<Action<EventArgs, Action<EventArgs>>>();
public Event(string name, bool allowAsync = true, bool register = true) {
this.Name = name;
this.AllowAsync = allowAsync;
if (register) { EventSystem.Register(this); }
}
#region Add Items
public void AddPreprocessor(Action<EventArgs, Action<EventArgs>> preprocessor) {
_preprocessors.Add(preprocessor);
}
public void AddListener(Action<EventArgs> listener) {
_listeners.Add(listener);
}
public void AddPostprocessor(Action<EventArgs, Action<EventArgs>> postprocessor) {
_postprocessors.Add(postprocessor);
}
#endregion
#region Remove Items
private void RemovePreprocessor(Action<EventArgs, Action<EventArgs>> preprocessor) {
_preprocessors.Remove(preprocessor);
}
public void RemoveListener(Action<EventArgs> listener) {
_listeners.Remove(listener);
}
private void RemovePostprocessor(Action<EventArgs, Action<EventArgs>> postprocessor) {
_postprocessors.Remove(postprocessor);
}
public void RemoveAllPreprocessors() {
_preprocessors.Clear();
}
public void RemoveAllListeners() {
_listeners.Clear();
}
public void RemoveAllPostprocessors() {
_postprocessors.Clear();
}
public void RemoveAll() {
RemoveAllPreprocessors();
RemoveAllListeners();
RemoveAllPostprocessors();
}
#endregion
private EventArgs RunPreprocessors(EventArgs args) {
List<Action<EventArgs, Action<EventArgs>>> _preprocessor_temp = _preprocessors;
// iterate through each preprocessor and run it, passing the args to the next preprocessor
Action<EventArgs> next = new Action<EventArgs>(eArgs => {
if (_preprocessor_temp.Count > 0) {
_preprocessor_temp.RemoveAt(0);
}
args = eArgs;
});
while (_preprocessor_temp.Count > 0) {
_preprocessor_temp[0](args, next);
}
return args;
}
private void RunListeners(EventArgs args, bool async) {
if (async) {
foreach (var item in _listeners) {
if (AllowAsync) {
Task.Run(() => item(args));
} else {
throw new BroadcastException("async not allowed in event");
}
}
} else {
foreach (var item in _listeners) {
item.Invoke(args);
}
}
}
private EventArgs RunPostprocessors(EventArgs args) {
List<Action<EventArgs, Action<EventArgs>>> _postprocessor_temp = _postprocessors;
// iterate through each postprocessor and run it, passing the args to the next postprocessor
Action<EventArgs> next = new Action<EventArgs>(eArgs => {
if (_postprocessor_temp.Count > 0) {
_postprocessor_temp.RemoveAt(0);
}
args = eArgs;
});
while (_postprocessor_temp.Count > 0) {
_postprocessor_temp[0](args, next);
}
return args;
}
public EventArgs BroadcastAsync(object data = null) {
EventArgs args = new EventArgs(data);
args = RunPreprocessors(args);
if (args.cancelled) { return args; }
RunListeners(args, true);
if (args.cancelled) { return args; }
return RunPostprocessors(args);
}
public EventArgs BroadcastSync(object data = null) {
EventArgs args = new EventArgs(data);
args = RunPreprocessors(args);
if (args.cancelled) { return args; }
RunListeners(args, false);
if (args.cancelled) { return args; }
return RunPostprocessors(args);
}
public EventArgs Broadcast(object data = null) {
EventArgs result;
if (AllowAsync) {
result = BroadcastAsync(data);
} else {
result = BroadcastSync(data);
}
return result;
}
}
}