PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/OpenSim/Region/CoreModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs

https://bitbucket.org/KyanhaLLC/opensim
C# | 383 lines | 287 code | 58 blank | 38 comment | 44 complexity | 542e32286185bcd79900b13ec5726eea MD5 | raw file
Possible License(s): Unlicense, MIT, BSD-3-Clause
  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. using System;
  28. using System.Reflection;
  29. using System.Collections.Generic;
  30. using Nini.Config;
  31. using log4net;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Framework.Interfaces;
  34. using OpenSim.Region.Framework.Scenes;
  35. using Mono.Addins;
  36. using OpenMetaverse;
  37. using System.Linq;
  38. using System.Linq.Expressions;
  39. namespace OpenSim.Region.CoreModules.Scripting.ScriptModuleComms
  40. {
  41. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "ScriptModuleCommsModule")]
  42. public class ScriptModuleCommsModule : INonSharedRegionModule, IScriptModuleComms
  43. {
  44. private static readonly ILog m_log =
  45. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private Dictionary<string,object> m_constants = new Dictionary<string,object>();
  47. #region ScriptInvocation
  48. protected class ScriptInvocationData
  49. {
  50. public Delegate ScriptInvocationDelegate { get; private set; }
  51. public string FunctionName { get; private set; }
  52. public Type[] TypeSignature { get; private set; }
  53. public Type ReturnType { get; private set; }
  54. public ScriptInvocationData(string fname, Delegate fn, Type[] callsig, Type returnsig)
  55. {
  56. FunctionName = fname;
  57. ScriptInvocationDelegate = fn;
  58. TypeSignature = callsig;
  59. ReturnType = returnsig;
  60. }
  61. }
  62. private Dictionary<string,ScriptInvocationData> m_scriptInvocation = new Dictionary<string,ScriptInvocationData>();
  63. #endregion
  64. private IScriptModule m_scriptModule = null;
  65. public event ScriptCommand OnScriptCommand;
  66. #region RegionModuleInterface
  67. public void Initialise(IConfigSource config)
  68. {
  69. }
  70. public void AddRegion(Scene scene)
  71. {
  72. scene.RegisterModuleInterface<IScriptModuleComms>(this);
  73. }
  74. public void RemoveRegion(Scene scene)
  75. {
  76. }
  77. public void RegionLoaded(Scene scene)
  78. {
  79. m_scriptModule = scene.RequestModuleInterface<IScriptModule>();
  80. if (m_scriptModule != null)
  81. m_log.Info("[MODULE COMMANDS]: Script engine found, module active");
  82. }
  83. public string Name
  84. {
  85. get { return "ScriptModuleCommsModule"; }
  86. }
  87. public Type ReplaceableInterface
  88. {
  89. get { return null; }
  90. }
  91. public void Close()
  92. {
  93. }
  94. #endregion
  95. #region ScriptModuleComms
  96. public void RaiseEvent(UUID script, string id, string module, string command, string k)
  97. {
  98. ScriptCommand c = OnScriptCommand;
  99. if (c == null)
  100. return;
  101. c(script, id, module, command, k);
  102. }
  103. public void DispatchReply(UUID script, int code, string text, string k)
  104. {
  105. if (m_scriptModule == null)
  106. return;
  107. Object[] args = new Object[] {-1, code, text, k};
  108. m_scriptModule.PostScriptEvent(script, "link_message", args);
  109. }
  110. private static MethodInfo GetMethodInfoFromType(Type target, string meth, bool searchInstanceMethods)
  111. {
  112. BindingFlags getMethodFlags =
  113. BindingFlags.NonPublic | BindingFlags.Public;
  114. if (searchInstanceMethods)
  115. getMethodFlags |= BindingFlags.Instance;
  116. else
  117. getMethodFlags |= BindingFlags.Static;
  118. return target.GetMethod(meth, getMethodFlags);
  119. }
  120. public void RegisterScriptInvocation(object target, string meth)
  121. {
  122. MethodInfo mi = GetMethodInfoFromType(target.GetType(), meth, true);
  123. if (mi == null)
  124. {
  125. m_log.WarnFormat("[MODULE COMMANDS] Failed to register method {0}", meth);
  126. return;
  127. }
  128. RegisterScriptInvocation(target, mi);
  129. }
  130. public void RegisterScriptInvocation(object target, string[] meth)
  131. {
  132. foreach (string m in meth)
  133. RegisterScriptInvocation(target, m);
  134. }
  135. public void RegisterScriptInvocation(object target, MethodInfo mi)
  136. {
  137. m_log.DebugFormat("[MODULE COMMANDS] Register method {0} from type {1}", mi.Name, (target is Type) ? ((Type)target).Name : target.GetType().Name);
  138. Type delegateType;
  139. List<Type> typeArgs = mi.GetParameters()
  140. .Select(p => p.ParameterType)
  141. .ToList();
  142. if (mi.ReturnType == typeof(void))
  143. {
  144. delegateType = Expression.GetActionType(typeArgs.ToArray());
  145. }
  146. else
  147. {
  148. typeArgs.Add(mi.ReturnType);
  149. delegateType = Expression.GetFuncType(typeArgs.ToArray());
  150. }
  151. Delegate fcall;
  152. if (!(target is Type))
  153. fcall = Delegate.CreateDelegate(delegateType, target, mi);
  154. else
  155. fcall = Delegate.CreateDelegate(delegateType, (Type)target, mi.Name);
  156. lock (m_scriptInvocation)
  157. {
  158. ParameterInfo[] parameters = fcall.Method.GetParameters();
  159. if (parameters.Length < 2) // Must have two UUID params
  160. return;
  161. // Hide the first two parameters
  162. Type[] parmTypes = new Type[parameters.Length - 2];
  163. for (int i = 2; i < parameters.Length; i++)
  164. parmTypes[i - 2] = parameters[i].ParameterType;
  165. m_scriptInvocation[fcall.Method.Name] = new ScriptInvocationData(fcall.Method.Name, fcall, parmTypes, fcall.Method.ReturnType);
  166. }
  167. }
  168. public void RegisterScriptInvocation(Type target, string[] methods)
  169. {
  170. foreach (string method in methods)
  171. {
  172. MethodInfo mi = GetMethodInfoFromType(target, method, false);
  173. if (mi == null)
  174. m_log.WarnFormat("[MODULE COMMANDS] Failed to register method {0}", method);
  175. else
  176. RegisterScriptInvocation(target, mi);
  177. }
  178. }
  179. public void RegisterScriptInvocations(IRegionModuleBase target)
  180. {
  181. foreach(MethodInfo method in target.GetType().GetMethods(
  182. BindingFlags.Public | BindingFlags.Instance |
  183. BindingFlags.Static))
  184. {
  185. if(method.GetCustomAttributes(
  186. typeof(ScriptInvocationAttribute), true).Any())
  187. {
  188. if(method.IsStatic)
  189. RegisterScriptInvocation(target.GetType(), method);
  190. else
  191. RegisterScriptInvocation(target, method);
  192. }
  193. }
  194. }
  195. public Delegate[] GetScriptInvocationList()
  196. {
  197. List<Delegate> ret = new List<Delegate>();
  198. lock (m_scriptInvocation)
  199. {
  200. foreach (ScriptInvocationData d in m_scriptInvocation.Values)
  201. ret.Add(d.ScriptInvocationDelegate);
  202. }
  203. return ret.ToArray();
  204. }
  205. public string LookupModInvocation(string fname)
  206. {
  207. lock (m_scriptInvocation)
  208. {
  209. ScriptInvocationData sid;
  210. if (m_scriptInvocation.TryGetValue(fname,out sid))
  211. {
  212. if (sid.ReturnType == typeof(string))
  213. return "modInvokeS";
  214. else if (sid.ReturnType == typeof(int))
  215. return "modInvokeI";
  216. else if (sid.ReturnType == typeof(float))
  217. return "modInvokeF";
  218. else if (sid.ReturnType == typeof(UUID))
  219. return "modInvokeK";
  220. else if (sid.ReturnType == typeof(OpenMetaverse.Vector3))
  221. return "modInvokeV";
  222. else if (sid.ReturnType == typeof(OpenMetaverse.Quaternion))
  223. return "modInvokeR";
  224. else if (sid.ReturnType == typeof(object[]))
  225. return "modInvokeL";
  226. m_log.WarnFormat("[MODULE COMMANDS] failed to find match for {0} with return type {1}",fname,sid.ReturnType.Name);
  227. }
  228. }
  229. return null;
  230. }
  231. public Delegate LookupScriptInvocation(string fname)
  232. {
  233. lock (m_scriptInvocation)
  234. {
  235. ScriptInvocationData sid;
  236. if (m_scriptInvocation.TryGetValue(fname,out sid))
  237. return sid.ScriptInvocationDelegate;
  238. }
  239. return null;
  240. }
  241. public Type[] LookupTypeSignature(string fname)
  242. {
  243. lock (m_scriptInvocation)
  244. {
  245. ScriptInvocationData sid;
  246. if (m_scriptInvocation.TryGetValue(fname,out sid))
  247. return sid.TypeSignature;
  248. }
  249. return null;
  250. }
  251. public Type LookupReturnType(string fname)
  252. {
  253. lock (m_scriptInvocation)
  254. {
  255. ScriptInvocationData sid;
  256. if (m_scriptInvocation.TryGetValue(fname,out sid))
  257. return sid.ReturnType;
  258. }
  259. return null;
  260. }
  261. public object InvokeOperation(UUID hostid, UUID scriptid, string fname, params object[] parms)
  262. {
  263. List<object> olist = new List<object>();
  264. olist.Add(hostid);
  265. olist.Add(scriptid);
  266. foreach (object o in parms)
  267. olist.Add(o);
  268. Delegate fn = LookupScriptInvocation(fname);
  269. return fn.DynamicInvoke(olist.ToArray());
  270. }
  271. /// <summary>
  272. /// Operation to for a region module to register a constant to be used
  273. /// by the script engine
  274. /// </summary>
  275. public void RegisterConstant(string cname, object value)
  276. {
  277. m_log.DebugFormat("[MODULE COMMANDS] register constant <{0}> with value {1}",cname,value.ToString());
  278. lock (m_constants)
  279. {
  280. m_constants.Add(cname,value);
  281. }
  282. }
  283. public void RegisterConstants(IRegionModuleBase target)
  284. {
  285. foreach (FieldInfo field in target.GetType().GetFields(
  286. BindingFlags.Public | BindingFlags.Static |
  287. BindingFlags.Instance))
  288. {
  289. if (field.GetCustomAttributes(
  290. typeof(ScriptConstantAttribute), true).Any())
  291. {
  292. RegisterConstant(field.Name, field.GetValue(target));
  293. }
  294. }
  295. }
  296. /// <summary>
  297. /// Operation to check for a registered constant
  298. /// </summary>
  299. public object LookupModConstant(string cname)
  300. {
  301. // m_log.DebugFormat("[MODULE COMMANDS] lookup constant <{0}>",cname);
  302. lock (m_constants)
  303. {
  304. object value = null;
  305. if (m_constants.TryGetValue(cname,out value))
  306. return value;
  307. }
  308. return null;
  309. }
  310. /// <summary>
  311. /// Get all registered constants
  312. /// </summary>
  313. public Dictionary<string, object> GetConstants()
  314. {
  315. Dictionary<string, object> ret = new Dictionary<string, object>();
  316. lock (m_constants)
  317. {
  318. foreach (KeyValuePair<string, object> kvp in m_constants)
  319. ret[kvp.Key] = kvp.Value;
  320. }
  321. return ret;
  322. }
  323. #endregion
  324. }
  325. }