1
0

XMREvents.cs 15 KB

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