EventManager.cs 18 KB

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