EventManager.cs 18 KB

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