XMREvents.cs 15 KB

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