XMREvents.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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)
  168. {
  169. int ch = (int)change;
  170. // Add to queue for all scripts in localID, Object pass change.
  171. this.PostObjectEvent(localID, new EventParams(
  172. "changed", new object[] { ch },
  173. zeroDetectParams));
  174. }
  175. // state_entry: not processed here
  176. // state_exit: not processed here
  177. public void money(uint localID, UUID agentID, int amount, DetectParams[] det)
  178. {
  179. this.PostObjectEvent(localID, new EventParams(
  180. "money", new object[] {
  181. agentID.ToString(),
  182. amount },
  183. det));
  184. }
  185. public void collision_start(uint localID, ColliderArgs col)
  186. {
  187. collisions(localID, col, "collision_start");
  188. }
  189. public void collision(uint localID, ColliderArgs col)
  190. {
  191. collisions(localID, col, "collision");
  192. }
  193. public void collision_end(uint localID, ColliderArgs col)
  194. {
  195. collisions(localID, col, "collision_end");
  196. }
  197. private void collisions(uint localID, ColliderArgs col, string eventname)
  198. {
  199. int dc = col.Colliders.Count;
  200. if(dc > 0)
  201. {
  202. DetectParams[] det = new DetectParams[dc];
  203. int i = 0;
  204. foreach(DetectedObject detobj in col.Colliders)
  205. {
  206. DetectParams d = new DetectParams();
  207. det[i++] = d;
  208. d.Key = detobj.keyUUID;
  209. d.Populate(this.World);
  210. /* not done by XEngine...
  211. d.Position = detobj.posVector;
  212. d.Rotation = detobj.rotQuat;
  213. d.Velocity = detobj.velVector;
  214. ... */
  215. }
  216. this.PostObjectEvent(localID, new EventParams(
  217. eventname,
  218. new Object[] { dc },
  219. det));
  220. }
  221. }
  222. public void land_collision_start(uint localID, ColliderArgs col)
  223. {
  224. land_collisions(localID, col, "land_collision_start");
  225. }
  226. public void land_collision(uint localID, ColliderArgs col)
  227. {
  228. land_collisions(localID, col, "land_collision");
  229. }
  230. public void land_collision_end(uint localID, ColliderArgs col)
  231. {
  232. land_collisions(localID, col, "land_collision_end");
  233. }
  234. private void land_collisions(uint localID, ColliderArgs col, string eventname)
  235. {
  236. foreach(DetectedObject detobj in col.Colliders)
  237. {
  238. LSL_Vector vec = new LSL_Vector(detobj.posVector.X,
  239. detobj.posVector.Y,
  240. detobj.posVector.Z);
  241. EventParams eps = new EventParams(eventname,
  242. new Object[] { vec },
  243. zeroDetectParams);
  244. this.PostObjectEvent(localID, eps);
  245. }
  246. }
  247. // timer: not handled here
  248. // listen: not handled here
  249. public void control(UUID itemID, UUID agentID, uint held, uint change)
  250. {
  251. this.PostScriptEvent(itemID, new EventParams(
  252. "control", new object[] {
  253. agentID.ToString(),
  254. (int)held,
  255. (int)change},
  256. zeroDetectParams));
  257. }
  258. public void email(uint localID, UUID itemID, string timeSent,
  259. string address, string subject, string message, int numLeft)
  260. {
  261. this.PostObjectEvent(localID, new EventParams(
  262. "email", new object[] {
  263. timeSent,
  264. address,
  265. subject,
  266. message,
  267. numLeft},
  268. zeroDetectParams));
  269. }
  270. public void at_target(uint localID, uint handle, Vector3 targetpos,
  271. Vector3 atpos)
  272. {
  273. this.PostObjectEvent(localID, new EventParams(
  274. "at_target", new object[] {
  275. (int)handle,
  276. new LSL_Vector(targetpos.X,targetpos.Y,targetpos.Z),
  277. new LSL_Vector(atpos.X,atpos.Y,atpos.Z) },
  278. zeroDetectParams));
  279. }
  280. public void not_at_target(uint localID)
  281. {
  282. this.PostObjectEvent(localID, new EventParams(
  283. "not_at_target", zeroObjectArray,
  284. zeroDetectParams));
  285. }
  286. public void at_rot_target(uint localID, uint handle, OpenMetaverse.Quaternion targetrot, OpenMetaverse.Quaternion atrot)
  287. {
  288. this.PostObjectEvent(
  289. localID,
  290. new EventParams(
  291. "at_rot_target",
  292. new object[] {
  293. new LSL_Integer(handle),
  294. new LSL_Rotation(targetrot.X, targetrot.Y, targetrot.Z, targetrot.W),
  295. new LSL_Rotation(atrot.X, atrot.Y, atrot.Z, atrot.W)
  296. },
  297. zeroDetectParams
  298. )
  299. );
  300. }
  301. public void not_at_rot_target(uint localID)
  302. {
  303. this.PostObjectEvent(localID, new EventParams(
  304. "not_at_rot_target", zeroObjectArray,
  305. zeroDetectParams));
  306. }
  307. // run_time_permissions: not handled here
  308. public void attach(uint localID, UUID itemID, UUID avatar)
  309. {
  310. this.PostObjectEvent(localID, new EventParams(
  311. "attach", new object[] {
  312. avatar.ToString() },
  313. zeroDetectParams));
  314. }
  315. // dataserver: not handled here
  316. // link_message: not handled here
  317. public void moving_start(uint localID)
  318. {
  319. this.PostObjectEvent(localID, new EventParams(
  320. "moving_start", zeroObjectArray,
  321. zeroDetectParams));
  322. }
  323. public void moving_end(uint localID)
  324. {
  325. this.PostObjectEvent(localID, new EventParams(
  326. "moving_end", zeroObjectArray,
  327. zeroDetectParams));
  328. }
  329. // object_rez: not handled here
  330. // remote_data: not handled here
  331. // http_response: not handled here
  332. }
  333. }