XMREvents.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 = World.GetSceneObjectPart(objectID);
  101. if(part == null)
  102. return;
  103. if((part.ScriptEvents & scriptEvents.money) == 0)
  104. part = part.ParentGroup.RootPart;
  105. Verbose("Paid: " + objectID + " from " + agentID + ", amount " + amount);
  106. if(part != null)
  107. {
  108. money(part.LocalId, agentID, amount, det);
  109. }
  110. }
  111. /// <summary>
  112. /// Handles piping the proper stuff to The script engine for touching
  113. /// Including DetectedParams
  114. /// </summary>
  115. /// <param name="localID"></param>
  116. /// <param name="originalID"></param>
  117. /// <param name="offsetPos"></param>
  118. /// <param name="remoteClient"></param>
  119. /// <param name="surfaceArgs"></param>
  120. public void touch_start(uint localID, uint originalID, Vector3 offsetPos,
  121. IClientAPI remoteClient, SurfaceTouchEventArgs surfaceArgs)
  122. {
  123. touches(localID, originalID, offsetPos, remoteClient, surfaceArgs, "touch_start");
  124. }
  125. public void touch(uint localID, uint originalID, Vector3 offsetPos,
  126. IClientAPI remoteClient, SurfaceTouchEventArgs surfaceArgs)
  127. {
  128. touches(localID, originalID, offsetPos, remoteClient, surfaceArgs, "touch");
  129. }
  130. private static Vector3 zeroVec3 = new Vector3(0, 0, 0);
  131. public void touch_end(uint localID, uint originalID, IClientAPI remoteClient,
  132. SurfaceTouchEventArgs surfaceArgs)
  133. {
  134. touches(localID, originalID, zeroVec3, remoteClient, surfaceArgs, "touch_end");
  135. }
  136. private void touches(uint localID, uint originalID, Vector3 offsetPos,
  137. IClientAPI remoteClient, SurfaceTouchEventArgs surfaceArgs, string eventname)
  138. {
  139. SceneObjectPart part;
  140. if(originalID == 0)
  141. {
  142. part = this.World.GetSceneObjectPart(localID);
  143. if(part == null)
  144. return;
  145. }
  146. else
  147. {
  148. part = this.World.GetSceneObjectPart(originalID);
  149. }
  150. DetectParams det = new DetectParams();
  151. det.Key = remoteClient.AgentId;
  152. det.Populate(this.World);
  153. det.OffsetPos = new LSL_Vector(offsetPos.X,
  154. offsetPos.Y,
  155. offsetPos.Z);
  156. det.LinkNum = part.LinkNum;
  157. if(surfaceArgs != null)
  158. {
  159. det.SurfaceTouchArgs = surfaceArgs;
  160. }
  161. // Add to queue for all scripts in ObjectID object
  162. this.PostObjectEvent(localID, new EventParams(
  163. eventname, oneObjectArrayOne,
  164. new DetectParams[] { det }));
  165. }
  166. public void changed(uint localID, uint change, object parameter)
  167. {
  168. int ch = (int)change;
  169. // Add to queue for all scripts in localID, Object pass change.
  170. if(parameter == null)
  171. {
  172. PostObjectEvent(localID, new EventParams(
  173. "changed", new object[] { ch },
  174. zeroDetectParams));
  175. return;
  176. }
  177. if ( parameter is UUID)
  178. {
  179. DetectParams det = new DetectParams();
  180. det.Key = (UUID)parameter;
  181. PostObjectEvent(localID, new EventParams(
  182. "changed", new object[] { ch },
  183. new DetectParams[] { det }));
  184. return;
  185. }
  186. }
  187. // state_entry: not processed here
  188. // state_exit: not processed here
  189. public void money(uint localID, UUID agentID, int amount, DetectParams[] det)
  190. {
  191. this.PostObjectEvent(localID, new EventParams(
  192. "money", new object[] {
  193. agentID.ToString(),
  194. amount },
  195. det));
  196. }
  197. public void collision_start(uint localID, ColliderArgs col)
  198. {
  199. collisions(localID, col, "collision_start");
  200. }
  201. public void collision(uint localID, ColliderArgs col)
  202. {
  203. collisions(localID, col, "collision");
  204. }
  205. public void collision_end(uint localID, ColliderArgs col)
  206. {
  207. collisions(localID, col, "collision_end");
  208. }
  209. private void collisions(uint localID, ColliderArgs col, string eventname)
  210. {
  211. int dc = col.Colliders.Count;
  212. if(dc > 0)
  213. {
  214. DetectParams[] det = new DetectParams[dc];
  215. int i = 0;
  216. foreach(DetectedObject detobj in col.Colliders)
  217. {
  218. DetectParams d = new DetectParams();
  219. det[i++] = d;
  220. d.Key = detobj.keyUUID;
  221. d.Populate(World, detobj);
  222. }
  223. this.PostObjectEvent(localID, new EventParams(
  224. eventname,
  225. new Object[] { dc },
  226. det));
  227. }
  228. }
  229. public void land_collision_start(uint localID, ColliderArgs col)
  230. {
  231. land_collisions(localID, col, "land_collision_start");
  232. }
  233. public void land_collision(uint localID, ColliderArgs col)
  234. {
  235. land_collisions(localID, col, "land_collision");
  236. }
  237. public void land_collision_end(uint localID, ColliderArgs col)
  238. {
  239. land_collisions(localID, col, "land_collision_end");
  240. }
  241. private void land_collisions(uint localID, ColliderArgs col, string eventname)
  242. {
  243. foreach(DetectedObject detobj in col.Colliders)
  244. {
  245. LSL_Vector vec = new LSL_Vector(detobj.posVector.X,
  246. detobj.posVector.Y,
  247. detobj.posVector.Z);
  248. EventParams eps = new EventParams(eventname,
  249. new Object[] { vec },
  250. zeroDetectParams);
  251. this.PostObjectEvent(localID, eps);
  252. }
  253. }
  254. // timer: not handled here
  255. // listen: not handled here
  256. public void control(UUID itemID, UUID agentID, uint held, uint change)
  257. {
  258. this.PostScriptEvent(itemID, new EventParams(
  259. "control", new object[] {
  260. agentID.ToString(),
  261. (int)held,
  262. (int)change},
  263. zeroDetectParams));
  264. }
  265. public void email(uint localID, UUID itemID, string timeSent,
  266. string address, string subject, string message, int numLeft)
  267. {
  268. this.PostObjectEvent(localID, new EventParams(
  269. "email", new object[] {
  270. timeSent,
  271. address,
  272. subject,
  273. message,
  274. numLeft},
  275. zeroDetectParams));
  276. }
  277. public void at_target(UUID scriptID, uint handle, Vector3 targetpos, Vector3 atpos)
  278. {
  279. PostScriptEvent(scriptID, new EventParams(
  280. "at_target", new object[] {
  281. (int)handle,
  282. new LSL_Vector(targetpos.X,targetpos.Y,targetpos.Z),
  283. new LSL_Vector(atpos.X,atpos.Y,atpos.Z) },
  284. zeroDetectParams));
  285. }
  286. public void not_at_target(UUID scriptID)
  287. {
  288. PostScriptEvent(scriptID, new EventParams(
  289. "not_at_target", zeroObjectArray,
  290. zeroDetectParams));
  291. }
  292. public void at_rot_target(UUID scriptID, uint handle, OpenMetaverse.Quaternion targetrot, OpenMetaverse.Quaternion atrot)
  293. {
  294. PostScriptEvent(scriptID, new EventParams(
  295. "at_rot_target",
  296. new object[] {
  297. new LSL_Integer(handle),
  298. new LSL_Rotation(targetrot.X, targetrot.Y, targetrot.Z, targetrot.W),
  299. new LSL_Rotation(atrot.X, atrot.Y, atrot.Z, atrot.W)
  300. },
  301. zeroDetectParams));
  302. }
  303. public void not_at_rot_target(UUID scriptID)
  304. {
  305. PostScriptEvent(scriptID, new EventParams(
  306. "not_at_rot_target", zeroObjectArray,
  307. zeroDetectParams));
  308. }
  309. // run_time_permissions: not handled here
  310. public void attach(uint localID, UUID itemID, UUID avatar)
  311. {
  312. this.PostObjectEvent(localID, new EventParams(
  313. "attach", new object[] {
  314. avatar.ToString() },
  315. zeroDetectParams));
  316. }
  317. // dataserver: not handled here
  318. // link_message: not handled here
  319. public void moving_start(uint localID)
  320. {
  321. this.PostObjectEvent(localID, new EventParams(
  322. "moving_start", zeroObjectArray,
  323. zeroDetectParams));
  324. }
  325. public void moving_end(uint localID)
  326. {
  327. this.PostObjectEvent(localID, new EventParams(
  328. "moving_end", zeroObjectArray,
  329. zeroDetectParams));
  330. }
  331. // object_rez: not handled here
  332. // remote_data: not handled here
  333. // http_response: not handled here
  334. }
  335. }