diff options
Diffstat (limited to 'OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs')
-rw-r--r-- | OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs | 324 |
1 files changed, 0 insertions, 324 deletions
diff --git a/OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs b/OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs deleted file mode 100644 index c5c96a9..0000000 --- a/OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs +++ /dev/null | |||
@@ -1,324 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Reflection; | ||
30 | using System.Collections.Generic; | ||
31 | using Nini.Config; | ||
32 | using log4net; | ||
33 | using OpenSim.Framework; | ||
34 | using OpenSim.Region.Framework.Interfaces; | ||
35 | using OpenSim.Region.Framework.Scenes; | ||
36 | using Mono.Addins; | ||
37 | using OpenMetaverse; | ||
38 | using System.Linq; | ||
39 | using System.Linq.Expressions; | ||
40 | |||
41 | namespace OpenSim.Region.OptionalModules.Scripting.ScriptModuleComms | ||
42 | { | ||
43 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "ScriptModuleCommsModule")] | ||
44 | class ScriptModuleCommsModule : INonSharedRegionModule, IScriptModuleComms | ||
45 | { | ||
46 | private static readonly ILog m_log = | ||
47 | LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
48 | |||
49 | private Dictionary<string,object> m_constants = new Dictionary<string,object>(); | ||
50 | |||
51 | #region ScriptInvocation | ||
52 | protected class ScriptInvocationData | ||
53 | { | ||
54 | public Delegate ScriptInvocationDelegate { get; private set; } | ||
55 | public string FunctionName { get; private set; } | ||
56 | public Type[] TypeSignature { get; private set; } | ||
57 | public Type ReturnType { get; private set; } | ||
58 | |||
59 | public ScriptInvocationData(string fname, Delegate fn, Type[] callsig, Type returnsig) | ||
60 | { | ||
61 | FunctionName = fname; | ||
62 | ScriptInvocationDelegate = fn; | ||
63 | TypeSignature = callsig; | ||
64 | ReturnType = returnsig; | ||
65 | } | ||
66 | } | ||
67 | |||
68 | private Dictionary<string,ScriptInvocationData> m_scriptInvocation = new Dictionary<string,ScriptInvocationData>(); | ||
69 | #endregion | ||
70 | |||
71 | private IScriptModule m_scriptModule = null; | ||
72 | public event ScriptCommand OnScriptCommand; | ||
73 | |||
74 | #region RegionModuleInterface | ||
75 | public void Initialise(IConfigSource config) | ||
76 | { | ||
77 | } | ||
78 | |||
79 | public void AddRegion(Scene scene) | ||
80 | { | ||
81 | scene.RegisterModuleInterface<IScriptModuleComms>(this); | ||
82 | } | ||
83 | |||
84 | public void RemoveRegion(Scene scene) | ||
85 | { | ||
86 | } | ||
87 | |||
88 | public void RegionLoaded(Scene scene) | ||
89 | { | ||
90 | m_scriptModule = scene.RequestModuleInterface<IScriptModule>(); | ||
91 | |||
92 | if (m_scriptModule != null) | ||
93 | m_log.Info("[MODULE COMMANDS]: Script engine found, module active"); | ||
94 | } | ||
95 | |||
96 | public string Name | ||
97 | { | ||
98 | get { return "ScriptModuleCommsModule"; } | ||
99 | } | ||
100 | |||
101 | public Type ReplaceableInterface | ||
102 | { | ||
103 | get { return null; } | ||
104 | } | ||
105 | |||
106 | public void Close() | ||
107 | { | ||
108 | } | ||
109 | #endregion | ||
110 | |||
111 | #region ScriptModuleComms | ||
112 | |||
113 | public void RaiseEvent(UUID script, string id, string module, string command, string k) | ||
114 | { | ||
115 | ScriptCommand c = OnScriptCommand; | ||
116 | |||
117 | if (c == null) | ||
118 | return; | ||
119 | |||
120 | c(script, id, module, command, k); | ||
121 | } | ||
122 | |||
123 | public void DispatchReply(UUID script, int code, string text, string k) | ||
124 | { | ||
125 | if (m_scriptModule == null) | ||
126 | return; | ||
127 | |||
128 | Object[] args = new Object[] {-1, code, text, k}; | ||
129 | |||
130 | m_scriptModule.PostScriptEvent(script, "link_message", args); | ||
131 | } | ||
132 | |||
133 | public void RegisterScriptInvocation(object target, string meth) | ||
134 | { | ||
135 | MethodInfo mi = target.GetType().GetMethod(meth, | ||
136 | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); | ||
137 | if (mi == null) | ||
138 | { | ||
139 | m_log.WarnFormat("[MODULE COMMANDS] Failed to register method {0}",meth); | ||
140 | return; | ||
141 | } | ||
142 | |||
143 | RegisterScriptInvocation(target, mi); | ||
144 | } | ||
145 | |||
146 | public void RegisterScriptInvocation(object target, string[] meth) | ||
147 | { | ||
148 | foreach (string m in meth) | ||
149 | RegisterScriptInvocation(target, m); | ||
150 | } | ||
151 | |||
152 | public void RegisterScriptInvocation(object target, MethodInfo mi) | ||
153 | { | ||
154 | m_log.DebugFormat("[MODULE COMMANDS] Register method {0} from type {1}", mi.Name, target.GetType().Name); | ||
155 | |||
156 | Type delegateType; | ||
157 | var typeArgs = mi.GetParameters() | ||
158 | .Select(p => p.ParameterType) | ||
159 | .ToList(); | ||
160 | |||
161 | if (mi.ReturnType == typeof(void)) | ||
162 | { | ||
163 | delegateType = Expression.GetActionType(typeArgs.ToArray()); | ||
164 | } | ||
165 | else | ||
166 | { | ||
167 | typeArgs.Add(mi.ReturnType); | ||
168 | delegateType = Expression.GetFuncType(typeArgs.ToArray()); | ||
169 | } | ||
170 | |||
171 | Delegate fcall = Delegate.CreateDelegate(delegateType, target, mi); | ||
172 | |||
173 | lock (m_scriptInvocation) | ||
174 | { | ||
175 | ParameterInfo[] parameters = fcall.Method.GetParameters (); | ||
176 | if (parameters.Length < 2) // Must have two UUID params | ||
177 | return; | ||
178 | |||
179 | // Hide the first two parameters | ||
180 | Type[] parmTypes = new Type[parameters.Length - 2]; | ||
181 | for (int i = 2 ; i < parameters.Length ; i++) | ||
182 | parmTypes[i - 2] = parameters[i].ParameterType; | ||
183 | m_scriptInvocation[fcall.Method.Name] = new ScriptInvocationData(fcall.Method.Name, fcall, parmTypes, fcall.Method.ReturnType); | ||
184 | } | ||
185 | } | ||
186 | |||
187 | public Delegate[] GetScriptInvocationList() | ||
188 | { | ||
189 | List<Delegate> ret = new List<Delegate>(); | ||
190 | |||
191 | lock (m_scriptInvocation) | ||
192 | { | ||
193 | foreach (ScriptInvocationData d in m_scriptInvocation.Values) | ||
194 | ret.Add(d.ScriptInvocationDelegate); | ||
195 | } | ||
196 | return ret.ToArray(); | ||
197 | } | ||
198 | |||
199 | public string LookupModInvocation(string fname) | ||
200 | { | ||
201 | lock (m_scriptInvocation) | ||
202 | { | ||
203 | ScriptInvocationData sid; | ||
204 | if (m_scriptInvocation.TryGetValue(fname,out sid)) | ||
205 | { | ||
206 | if (sid.ReturnType == typeof(string)) | ||
207 | return "modInvokeS"; | ||
208 | else if (sid.ReturnType == typeof(int)) | ||
209 | return "modInvokeI"; | ||
210 | else if (sid.ReturnType == typeof(float)) | ||
211 | return "modInvokeF"; | ||
212 | else if (sid.ReturnType == typeof(UUID)) | ||
213 | return "modInvokeK"; | ||
214 | else if (sid.ReturnType == typeof(OpenMetaverse.Vector3)) | ||
215 | return "modInvokeV"; | ||
216 | else if (sid.ReturnType == typeof(OpenMetaverse.Quaternion)) | ||
217 | return "modInvokeR"; | ||
218 | else if (sid.ReturnType == typeof(object[])) | ||
219 | return "modInvokeL"; | ||
220 | |||
221 | m_log.WarnFormat("[MODULE COMMANDS] failed to find match for {0} with return type {1}",fname,sid.ReturnType.Name); | ||
222 | } | ||
223 | } | ||
224 | |||
225 | return null; | ||
226 | } | ||
227 | |||
228 | public Delegate LookupScriptInvocation(string fname) | ||
229 | { | ||
230 | lock (m_scriptInvocation) | ||
231 | { | ||
232 | ScriptInvocationData sid; | ||
233 | if (m_scriptInvocation.TryGetValue(fname,out sid)) | ||
234 | return sid.ScriptInvocationDelegate; | ||
235 | } | ||
236 | |||
237 | return null; | ||
238 | } | ||
239 | |||
240 | public Type[] LookupTypeSignature(string fname) | ||
241 | { | ||
242 | lock (m_scriptInvocation) | ||
243 | { | ||
244 | ScriptInvocationData sid; | ||
245 | if (m_scriptInvocation.TryGetValue(fname,out sid)) | ||
246 | return sid.TypeSignature; | ||
247 | } | ||
248 | |||
249 | return null; | ||
250 | } | ||
251 | |||
252 | public Type LookupReturnType(string fname) | ||
253 | { | ||
254 | lock (m_scriptInvocation) | ||
255 | { | ||
256 | ScriptInvocationData sid; | ||
257 | if (m_scriptInvocation.TryGetValue(fname,out sid)) | ||
258 | return sid.ReturnType; | ||
259 | } | ||
260 | |||
261 | return null; | ||
262 | } | ||
263 | |||
264 | public object InvokeOperation(UUID hostid, UUID scriptid, string fname, params object[] parms) | ||
265 | { | ||
266 | List<object> olist = new List<object>(); | ||
267 | olist.Add(hostid); | ||
268 | olist.Add(scriptid); | ||
269 | foreach (object o in parms) | ||
270 | olist.Add(o); | ||
271 | Delegate fn = LookupScriptInvocation(fname); | ||
272 | return fn.DynamicInvoke(olist.ToArray()); | ||
273 | } | ||
274 | |||
275 | /// <summary> | ||
276 | /// Operation to for a region module to register a constant to be used | ||
277 | /// by the script engine | ||
278 | /// </summary> | ||
279 | public void RegisterConstant(string cname, object value) | ||
280 | { | ||
281 | m_log.DebugFormat("[MODULE COMMANDS] register constant <{0}> with value {1}",cname,value.ToString()); | ||
282 | lock (m_constants) | ||
283 | { | ||
284 | m_constants.Add(cname,value); | ||
285 | } | ||
286 | } | ||
287 | |||
288 | /// <summary> | ||
289 | /// Operation to check for a registered constant | ||
290 | /// </summary> | ||
291 | public object LookupModConstant(string cname) | ||
292 | { | ||
293 | // m_log.DebugFormat("[MODULE COMMANDS] lookup constant <{0}>",cname); | ||
294 | |||
295 | lock (m_constants) | ||
296 | { | ||
297 | object value = null; | ||
298 | if (m_constants.TryGetValue(cname,out value)) | ||
299 | return value; | ||
300 | } | ||
301 | |||
302 | return null; | ||
303 | } | ||
304 | |||
305 | /// <summary> | ||
306 | /// Get all registered constants | ||
307 | /// </summary> | ||
308 | public Dictionary<string, object> GetConstants() | ||
309 | { | ||
310 | Dictionary<string, object> ret = new Dictionary<string, object>(); | ||
311 | |||
312 | lock (m_constants) | ||
313 | { | ||
314 | foreach (KeyValuePair<string, object> kvp in m_constants) | ||
315 | ret[kvp.Key] = kvp.Value; | ||
316 | } | ||
317 | |||
318 | return ret; | ||
319 | } | ||
320 | |||
321 | #endregion | ||
322 | |||
323 | } | ||
324 | } | ||