1
0

EventManager.cs 18 KB

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