This repository has been archived by the owner on Nov 18, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Plugin.cs
276 lines (244 loc) · 8.96 KB
/
Plugin.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using NLua;
using NLua.Exceptions;
namespace Oxide
{
/// <summary>
/// Represents a plugin that modifies server behaviour in some way
/// </summary>
public class Plugin
{
public string Name { get; private set; }
public string Title { get; private set; }
public string Description { get; private set; }
public string Author { get; private set; }
public string Version { get; private set; }
public string Filename { get; private set; }
public string ShortFilename { get; private set; }
public Lua LuaInstance { get; private set; }
private LuaTable table;
private Dictionary<string, LuaFunction> functionmap;
private bool incall;
/// <summary>
/// Gets the currently active plugin (NOT THREADSAFE)
/// </summary>
public static Plugin CurrentPlugin { get; private set; }
/// <summary>
/// Returns the Lua table associated with this plugin
/// </summary>
public LuaTable Table
{
get
{
return table;
}
}
public Plugin(Lua lua)
{
// Store the lua instance
LuaInstance = lua;
}
/// <summary>
/// Loads this plugin from file
/// </summary>
/// <param name="filename"></param>
public bool Load(string filename)
{
// Store filename
Filename = filename;
ShortFilename = Path.GetFileName(filename);
Name = Path.GetFileNameWithoutExtension(filename);
// Check it exists
if (!File.Exists(filename))
{
Logger.Error(string.Format("Failed to load plugin {0} (file not found)", Name));
return false;
}
// Load it
string script = File.ReadAllText(filename);
// Attempt to compile
LuaFunction func;
try
{
func = LuaInstance.LoadString(script, ShortFilename);
}
catch (LuaScriptException ex)
{
Logger.Error(string.Format("Failed to load plugin {0}", Name), ex);
return false;
}
// Create the plugin table
LuaFunction createplugin = LuaInstance["createplugin"] as LuaFunction;
if (createplugin == null) return false;
table = createplugin.Call()[0] as LuaTable;
table["Name"] = Name;
table["Filename"] = filename;
LuaInstance["PLUGIN"] = table;
// Attempt to call
try
{
func.Call();
}
catch (LuaScriptException ex)
{
Logger.Error(string.Format("Failed to load plugin {0}", Name), ex);
return false;
}
LuaInstance["PLUGIN"] = null;
// Get all functions
functionmap = new Dictionary<string, LuaFunction>();
foreach (var key in table.Keys)
{
if (key is string)
{
var value = table[key];
if (value is LuaFunction)
{
try
{
functionmap.Add((string)key, value as LuaFunction);
}
catch (Exception e)
{
//Logger.Error(string.Format("Failed to map function {0} in {1} table.Keys", (string)key, Name));
}
}
}
}
// Get base functions
LuaTable metatable = (LuaInstance["getmetatable"] as LuaFunction).Call(table)[0] as LuaTable;
if (metatable != null)
{
LuaTable basetable = metatable["__index"] as LuaTable;
foreach (var key in basetable.Keys)
{
if (key is string)
{
var value = basetable[key];
if (value is LuaFunction)
{
try
{
functionmap.Add((string)key, value as LuaFunction);
}
catch (Exception e)
{
//Logger.Error(string.Format("Failed to map function {0} in {1} metatable", (string)key, Name));
}
}
}
}
}
// Check plugin descriptors
if (!(table["Title"] is string))
{
Logger.Error(string.Format("Failed to load plugin {0} (invalid 'Title')", Name));
return false;
}
if (!(table["Description"] is string))
{
Logger.Error(string.Format("Failed to load plugin {0} (invalid 'Description')", Name));
return false;
}
if (!(table["Version"] is string))
{
Logger.Error(string.Format("Failed to load plugin {0} (invalid 'Version')", Name));
return false;
}
if (!(table["Author"] is string))
{
Logger.Error(string.Format("Failed to load plugin {0} (invalid 'Author')", Name));
return false;
}
// Load plugin descriptors
Title = (string)table["Title"];
Description = (string)table["Description"];
Version = (string)table["Version"];
Author = (string)table["Author"];
// Success
return true;
}
/// <summary>
/// Returns all hooks that this plugin implements
/// </summary>
/// <returns></returns>
public IEnumerable<string> GetHooks()
{
return functionmap.Keys;
}
private static MethodBase LuaCallFunction;
private static readonly Type[] LuaCallFunctionSig = new Type[] { typeof(object), typeof(object[]), typeof(Type[]) };
private static readonly object[] LuaCallFunctionArgs = new object[3];
private object CallFunction(LuaFunction func, object[] args)
{
// Check the method is loaded
if (LuaCallFunction == null) LuaCallFunction = typeof(Lua).GetMethod("CallFunction", BindingFlags.NonPublic | BindingFlags.Instance, null, LuaCallFunctionSig, null);
// Setup args
LuaCallFunctionArgs[0] = func;
LuaCallFunctionArgs[1] = args;
LuaCallFunctionArgs[2] = null;
// Call it
return LuaCallFunction.Invoke(LuaInstance, LuaCallFunctionArgs);
}
/// <summary>
/// Calls a hook on this plugin with a given set of arguments
/// </summary>
/// <param name="hookname"></param>
/// <param name="args"></param>
/// <returns></returns>
public object Call(string hookname, object[] args)
{
// Check incall
//if (incall)
//{
// Logger.Error(string.Format("Failed to call hook {0} on plugin {1} (coming from {2}) (recursive hook calls are forbidden)", hookname, Name, CurrentPlugin));
// return null;
//}
// Check that the function exists
LuaFunction func;
if (!functionmap.TryGetValue(hookname, out func)) return null;
// Setup the args
object[] luaargs;
if (args != null)
{
luaargs = Main.Array(args.Length + 1);
for (int i = 0; i < args.Length; i++)
luaargs[i + 1] = args[i];
}
else
luaargs = Main.Array(1);
luaargs[0] = table;
// Make the call
incall = true;
Plugin oldcaller = CurrentPlugin;
CurrentPlugin = this;
object[] result;
try
{
result = CallFunction(func, luaargs) as object[];
}
catch (LuaScriptException ex)
{
Logger.Error(string.Format("Failed to call hook {0} on plugin {1} (coming from {2})", hookname, Name, CurrentPlugin), ex);
return null;
}
finally
{
CurrentPlugin = oldcaller;
incall = false;
}
// Return the result
if (result != null && result.Length > 0)
return result[0];
else
return null;
}
public override string ToString()
{
return Name;
}
}
}