EventManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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.Collections;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Framework.Scenes;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.ScriptEngine.Shared;
  36. using OpenSim.Region.ScriptEngine.Interfaces;
  37. using log4net;
  38. namespace OpenSim.Region.ScriptEngine.XEngine
  39. {
  40. /// <summary>
  41. /// Prepares events so they can be directly executed upon a script by EventQueueManager, then queues it.
  42. /// </summary>
  43. public class EventManager
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private XEngine myScriptEngine;
  47. public EventManager(XEngine _ScriptEngine)
  48. {
  49. myScriptEngine = _ScriptEngine;
  50. m_log.Info("[XEngine] Hooking up to server events");
  51. myScriptEngine.World.EventManager.OnAttach += attach;
  52. myScriptEngine.World.EventManager.OnObjectGrab += touch_start;
  53. myScriptEngine.World.EventManager.OnObjectDeGrab += touch_end;
  54. myScriptEngine.World.EventManager.OnScriptChangedEvent += changed;
  55. myScriptEngine.World.EventManager.OnScriptAtTargetEvent += at_target;
  56. myScriptEngine.World.EventManager.OnScriptNotAtTargetEvent += not_at_target;
  57. myScriptEngine.World.EventManager.OnScriptControlEvent += control;
  58. myScriptEngine.World.EventManager.OnScriptColliderStart += collision_start;
  59. myScriptEngine.World.EventManager.OnScriptColliding += collision;
  60. myScriptEngine.World.EventManager.OnScriptCollidingEnd += collision_end;
  61. IMoneyModule money=myScriptEngine.World.RequestModuleInterface<IMoneyModule>();
  62. if (money != null)
  63. {
  64. money.OnObjectPaid+=HandleObjectPaid;
  65. }
  66. }
  67. /// <summary>
  68. /// When an object gets paid by an avatar and generates the paid event,
  69. /// this will pipe it to the script engine
  70. /// </summary>
  71. /// <param name="objectID">Object ID that got paid</param>
  72. /// <param name="agentID">Agent Id that did the paying</param>
  73. /// <param name="amount">Amount paid</param>
  74. private void HandleObjectPaid(UUID objectID, UUID agentID,
  75. int amount)
  76. {
  77. // Since this is an event from a shared module, all scenes will
  78. // get it. But only one has the object in question. The others
  79. // just ignore it.
  80. //
  81. SceneObjectPart part =
  82. myScriptEngine.World.GetSceneObjectPart(objectID);
  83. if (part == null)
  84. return;
  85. m_log.Debug("Paid: " + objectID + " from " + agentID + ", amount " + amount);
  86. if (part.ParentGroup != null)
  87. part = part.ParentGroup.RootPart;
  88. if (part != null)
  89. {
  90. money(part.LocalId, agentID, amount);
  91. }
  92. }
  93. /// <summary>
  94. /// Handles piping the proper stuff to The script engine for touching
  95. /// Including DetectedParams
  96. /// </summary>
  97. /// <param name="localID"></param>
  98. /// <param name="originalID"></param>
  99. /// <param name="offsetPos"></param>
  100. /// <param name="remoteClient"></param>
  101. /// <param name="surfaceArgs"></param>
  102. public void touch_start(uint localID, uint originalID, Vector3 offsetPos,
  103. IClientAPI remoteClient, SurfaceTouchEventArgs surfaceArgs)
  104. {
  105. // Add to queue for all scripts in ObjectID object
  106. DetectParams[] det = new DetectParams[1];
  107. det[0] = new DetectParams();
  108. det[0].Key = remoteClient.AgentId;
  109. det[0].Populate(myScriptEngine.World);
  110. if (originalID == 0)
  111. {
  112. SceneObjectPart part = myScriptEngine.World.GetSceneObjectPart(localID);
  113. if (part == null)
  114. return;
  115. det[0].LinkNum = part.LinkNum;
  116. }
  117. else
  118. {
  119. SceneObjectPart originalPart = myScriptEngine.World.GetSceneObjectPart(originalID);
  120. det[0].LinkNum = originalPart.LinkNum;
  121. }
  122. if (surfaceArgs != null)
  123. {
  124. det[0].SurfaceTouchArgs = surfaceArgs;
  125. }
  126. myScriptEngine.PostObjectEvent(localID, new EventParams(
  127. "touch_start", new Object[] { new LSL_Types.LSLInteger(1) },
  128. det));
  129. }
  130. public void touch(uint localID, uint originalID, Vector3 offsetPos,
  131. IClientAPI remoteClient)
  132. {
  133. // Add to queue for all scripts in ObjectID object
  134. DetectParams[] det = new DetectParams[1];
  135. det[0] = new DetectParams();
  136. det[0].Key = remoteClient.AgentId;
  137. det[0].Populate(myScriptEngine.World);
  138. det[0].OffsetPos = new LSL_Types.Vector3(offsetPos.X,
  139. offsetPos.Y,
  140. offsetPos.Z);
  141. if (originalID == 0)
  142. {
  143. SceneObjectPart part = myScriptEngine.World.GetSceneObjectPart(localID);
  144. if (part == null)
  145. return;
  146. det[0].LinkNum = part.LinkNum;
  147. }
  148. else
  149. {
  150. SceneObjectPart originalPart = myScriptEngine.World.GetSceneObjectPart(originalID);
  151. det[0].LinkNum = originalPart.LinkNum;
  152. }
  153. myScriptEngine.PostObjectEvent(localID, new EventParams(
  154. "touch", new Object[] { new LSL_Types.LSLInteger(1) },
  155. det));
  156. }
  157. public void touch_end(uint localID, uint originalID, IClientAPI remoteClient,
  158. SurfaceTouchEventArgs surfaceArgs)
  159. {
  160. // Add to queue for all scripts in ObjectID object
  161. DetectParams[] det = new DetectParams[1];
  162. det[0] = new DetectParams();
  163. det[0].Key = remoteClient.AgentId;
  164. det[0].Populate(myScriptEngine.World);
  165. if (originalID == 0)
  166. {
  167. SceneObjectPart part = myScriptEngine.World.GetSceneObjectPart(localID);
  168. if (part == null)
  169. return;
  170. det[0].LinkNum = part.LinkNum;
  171. }
  172. else
  173. {
  174. SceneObjectPart originalPart = myScriptEngine.World.GetSceneObjectPart(originalID);
  175. det[0].LinkNum = originalPart.LinkNum;
  176. }
  177. if (surfaceArgs != null)
  178. {
  179. det[0].SurfaceTouchArgs = surfaceArgs;
  180. }
  181. myScriptEngine.PostObjectEvent(localID, new EventParams(
  182. "touch_end", new Object[] { new LSL_Types.LSLInteger(1) },
  183. det));
  184. }
  185. public void changed(uint localID, uint change)
  186. {
  187. // Add to queue for all scripts in localID, Object pass change.
  188. myScriptEngine.PostObjectEvent(localID, new EventParams(
  189. "changed",new object[] { new LSL_Types.LSLInteger(change) },
  190. new DetectParams[0]));
  191. }
  192. // state_entry: not processed here
  193. // state_exit: not processed here
  194. public void money(uint localID, UUID agentID, int amount)
  195. {
  196. myScriptEngine.PostObjectEvent(localID, new EventParams(
  197. "money", new object[] {
  198. new LSL_Types.LSLString(agentID.ToString()),
  199. new LSL_Types.LSLInteger(amount) },
  200. new DetectParams[0]));
  201. }
  202. public void collision_start(uint localID, ColliderArgs col)
  203. {
  204. // Add to queue for all scripts in ObjectID object
  205. List<DetectParams> det = new List<DetectParams>();
  206. foreach (DetectedObject detobj in col.Colliders)
  207. {
  208. DetectParams d = new DetectParams();
  209. d.Key =detobj.keyUUID;
  210. d.Populate(myScriptEngine.World);
  211. det.Add(d);
  212. }
  213. if (det.Count > 0)
  214. myScriptEngine.PostObjectEvent(localID, new EventParams(
  215. "collision_start",
  216. new Object[] { new LSL_Types.LSLInteger(det.Count) },
  217. det.ToArray()));
  218. }
  219. public void collision(uint localID, ColliderArgs col)
  220. {
  221. // Add to queue for all scripts in ObjectID object
  222. List<DetectParams> det = new List<DetectParams>();
  223. foreach (DetectedObject detobj in col.Colliders)
  224. {
  225. DetectParams d = new DetectParams();
  226. d.Key =detobj.keyUUID;
  227. d.Populate(myScriptEngine.World);
  228. det.Add(d);
  229. }
  230. if (det.Count > 0)
  231. myScriptEngine.PostObjectEvent(localID, new EventParams(
  232. "collision", new Object[] { new LSL_Types.LSLInteger(det.Count) },
  233. det.ToArray()));
  234. }
  235. public void collision_end(uint localID, ColliderArgs col)
  236. {
  237. // Add to queue for all scripts in ObjectID object
  238. List<DetectParams> det = new List<DetectParams>();
  239. foreach (DetectedObject detobj in col.Colliders)
  240. {
  241. DetectParams d = new DetectParams();
  242. d.Key =detobj.keyUUID;
  243. d.Populate(myScriptEngine.World);
  244. det.Add(d);
  245. }
  246. if (det.Count > 0)
  247. myScriptEngine.PostObjectEvent(localID, new EventParams(
  248. "collision_end",
  249. new Object[] { new LSL_Types.LSLInteger(det.Count) },
  250. det.ToArray()));
  251. }
  252. public void land_collision_start(uint localID, UUID itemID)
  253. {
  254. myScriptEngine.PostObjectEvent(localID, new EventParams(
  255. "land_collision_start",
  256. new object[0],
  257. new DetectParams[0]));
  258. }
  259. public void land_collision(uint localID, UUID itemID)
  260. {
  261. myScriptEngine.PostObjectEvent(localID, new EventParams(
  262. "land_collision",
  263. new object[0],
  264. new DetectParams[0]));
  265. }
  266. public void land_collision_end(uint localID, UUID itemID)
  267. {
  268. myScriptEngine.PostObjectEvent(localID, new EventParams(
  269. "land_collision_end",
  270. new object[0],
  271. new DetectParams[0]));
  272. }
  273. // timer: not handled here
  274. // listen: not handled here
  275. public void control(uint localID, UUID itemID, UUID agentID, uint held, uint change)
  276. {
  277. myScriptEngine.PostObjectEvent(localID, new EventParams(
  278. "control",new object[] {
  279. new LSL_Types.LSLString(agentID.ToString()),
  280. new LSL_Types.LSLInteger(held),
  281. new LSL_Types.LSLInteger(change)},
  282. new DetectParams[0]));
  283. }
  284. public void email(uint localID, UUID itemID, string timeSent,
  285. string address, string subject, string message, int numLeft)
  286. {
  287. myScriptEngine.PostObjectEvent(localID, new EventParams(
  288. "email",new object[] {
  289. new LSL_Types.LSLString(timeSent),
  290. new LSL_Types.LSLString(address),
  291. new LSL_Types.LSLString(subject),
  292. new LSL_Types.LSLString(message),
  293. new LSL_Types.LSLInteger(numLeft)},
  294. new DetectParams[0]));
  295. }
  296. public void at_target(uint localID, uint handle, Vector3 targetpos,
  297. Vector3 atpos)
  298. {
  299. myScriptEngine.PostObjectEvent(localID, new EventParams(
  300. "at_target", new object[] {
  301. new LSL_Types.LSLInteger(handle),
  302. new LSL_Types.Vector3(targetpos.X,targetpos.Y,targetpos.Z),
  303. new LSL_Types.Vector3(atpos.X,atpos.Y,atpos.Z) },
  304. new DetectParams[0]));
  305. }
  306. public void not_at_target(uint localID)
  307. {
  308. myScriptEngine.PostObjectEvent(localID, new EventParams(
  309. "not_at_target",new object[0],
  310. new DetectParams[0]));
  311. }
  312. public void at_rot_target(uint localID, UUID itemID)
  313. {
  314. myScriptEngine.PostObjectEvent(localID, new EventParams(
  315. "at_rot_target",new object[0],
  316. new DetectParams[0]));
  317. }
  318. public void not_at_rot_target(uint localID, UUID itemID)
  319. {
  320. myScriptEngine.PostObjectEvent(localID, new EventParams(
  321. "not_at_rot_target",new object[0],
  322. new DetectParams[0]));
  323. }
  324. // run_time_permissions: not handled here
  325. public void attach(uint localID, UUID itemID, UUID avatar)
  326. {
  327. myScriptEngine.PostObjectEvent(localID, new EventParams(
  328. "attach",new object[] {
  329. new LSL_Types.LSLString(avatar.ToString()) },
  330. new DetectParams[0]));
  331. }
  332. // dataserver: not handled here
  333. // link_message: not handled here
  334. public void moving_start(uint localID, UUID itemID)
  335. {
  336. myScriptEngine.PostObjectEvent(localID, new EventParams(
  337. "moving_start",new object[0],
  338. new DetectParams[0]));
  339. }
  340. public void moving_end(uint localID, UUID itemID)
  341. {
  342. myScriptEngine.PostObjectEvent(localID, new EventParams(
  343. "moving_end",new object[0],
  344. new DetectParams[0]));
  345. }
  346. // object_rez: not handled here
  347. // remote_data: not handled here
  348. // http_response: not handled here
  349. }
  350. }