-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathScript.cs
More file actions
197 lines (168 loc) · 6.1 KB
/
Copy pathScript.cs
File metadata and controls
197 lines (168 loc) · 6.1 KB
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
namespace Squalr.Engine.Scripting
{
using Squalr.Engine.Common.Logging;
using System;
using System.Reflection;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// Instance of a single script.
/// </summary>
public abstract class Script
{
/// <summary>
/// Time to wait for the update loop to finish on deactivation.
/// </summary>
private const Int32 AbortTime = 500;
/// <summary>
/// Update time in milliseconds.
/// </summary>
private const Int32 UpdateTime = 1000 / 15;
/// <summary>
/// Initializes a new instance of the <see cref="Script" /> class.
/// </summary>
protected Script()
{
}
/// <summary>
/// Gets or sets the text of this script.
/// </summary>
public String Text { get; set; }
/// <summary>
/// Gets or sets the name of this script.
/// </summary>
public String Name { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this script is active.
/// </summary>
public Boolean IsActivated { get; set; }
/// <summary>
/// Gets the compiled script assembly.
/// </summary>
public String CompiledAssembly { get; private set; }
/// <summary>
/// Gets or sets the task for the update loops.
/// </summary>
private Task Task { get; set; }
/// <summary>
/// Gets or sets a cancelation request for the update loop.
/// </summary>
private CancellationTokenSource CancelRequest { get; set; }
/// <summary>
/// Gets or sets the compiled assembly object of a script.
/// </summary>
private dynamic ScriptObject { get; set; }
/// <summary>
/// Creates a script object from the given assembly.
/// </summary>
/// <param name="assembly">The assembly from which to create the script.</param>
/// <returns>The script created from the assembly.</returns>
public static Script FromAssembly(Assembly assembly)
{
dynamic script = assembly?.CreateInstance("Squalr.Engine.Scripting.Script");
return script;
}
/// <summary>
/// Runs the activation function in the script.
/// </summary>
/// <returns>Returns true if the activation function successfully ran, otherwise false.</returns>
public Boolean RunActivationFunction()
{
try
{
// Bind the deactivation function such that scripts can deactivate themselves
this.ScriptObject.Deactivate = new Action(() => this.IsActivated = false);
// Call OnActivate function in the script
this.ScriptObject.OnActivate();
Logger.Log(LogLevel.Info, "Script activated: " + this.Name);
}
catch (SecurityException ex)
{
Logger.Log(LogLevel.Error, "Invalid operation in sandbox environment", ex);
return false;
}
catch (Exception ex)
{
Logger.Log(LogLevel.Error, "Unable to activate script", ex);
return false;
}
return true;
}
/// <summary>
/// Continously runs the update function in the script.
/// </summary>
public void RunUpdateFunction()
{
this.CancelRequest = new CancellationTokenSource();
try
{
this.Task = Task.Run(
async () =>
{
TimeSpan elapsedTime;
DateTime previousTime = DateTime.Now;
while (true)
{
DateTime currentTime = DateTime.Now;
elapsedTime = currentTime - previousTime;
try
{
// Call the update function, giving the elapsed milliseconds since the previous call
ScriptObject.OnUpdate((Single)elapsedTime.TotalMilliseconds);
}
catch (Exception ex)
{
String exception = ex.ToString();
if (exception.ToString().Contains("does not contain a definition for 'OnUpdate'"))
{
Logger.Log(LogLevel.Warn, "Optional update function not executed");
}
else
{
Logger.Log(LogLevel.Error, "Error running update function: ", ex);
}
return;
}
previousTime = currentTime;
// Await with cancellation
await Task.Delay(Script.UpdateTime, this.CancelRequest.Token);
}
},
this.CancelRequest.Token);
return;
}
catch
{
Logger.Log(LogLevel.Error, "Error executing update loop.");
}
}
/// <summary>
/// Runs the deactivation function in the script.
/// </summary>
public void RunDeactivationFunction()
{
// Abort the update loop
try
{
this.ScriptObject.OnDeactivate();
Logger.Log(LogLevel.Info, "Script deactivated: " + this.Name);
try
{
this.CancelRequest?.Cancel();
this.Task?.Wait(Script.AbortTime);
}
catch
{
}
}
catch (Exception ex)
{
Logger.Log(LogLevel.Error, "Error when deactivating script", ex);
}
return;
}
}
//// End class
}
//// End namespace