EventManager.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 OpenSim 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.Generic;
  29. using System.Reflection;
  30. using OpenMetaverse;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.CoreModules;
  33. using OpenSim.Region;
  34. using OpenSim.Region.Framework.Scenes;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.ScriptEngine.Shared;
  37. using log4net;
  38. namespace OpenSim.Region.ScriptEngine.DotNetEngine
  39. {
  40. /// <summary>
  41. /// Prepares events so they can be directly executed upon a script by EventQueueManager, then queues it.
  42. /// </summary>
  43. [Serializable]
  44. public class EventManager
  45. {
  46. //
  47. // Class is instanced in "ScriptEngine" and Uses "EventQueueManager"
  48. // that is also instanced in "ScriptEngine".
  49. // This class needs a bit of explaining:
  50. //
  51. // This class it the link between an event inside OpenSim and
  52. // the corresponding event in a user script being executed.
  53. //
  54. // For example when an user touches an object then the
  55. // "myScriptEngine.World.EventManager.OnObjectGrab" event is fired
  56. // inside OpenSim.
  57. // We hook up to this event and queue a touch_start in
  58. // EventQueueManager with the proper LSL parameters.
  59. // It will then be delivered to the script by EventQueueManager.
  60. //
  61. // You can check debug C# dump of an LSL script if you need to
  62. // verify what exact parameters are needed.
  63. //
  64. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  65. private ScriptEngine myScriptEngine;
  66. public EventManager(ScriptEngine _ScriptEngine, bool performHookUp)
  67. {
  68. myScriptEngine = _ScriptEngine;
  69. ReadConfig();
  70. if (performHookUp)
  71. {
  72. myScriptEngine.World.EventManager.OnRezScript += OnRezScript;
  73. }
  74. }
  75. public void HookUpEvents()
  76. {
  77. m_log.Info("[" + myScriptEngine.ScriptEngineName +
  78. "]: Hooking up to server events");
  79. myScriptEngine.World.EventManager.OnObjectGrab +=
  80. touch_start;
  81. myScriptEngine.World.EventManager.OnObjectDeGrab +=
  82. touch_end;
  83. myScriptEngine.World.EventManager.OnRemoveScript +=
  84. OnRemoveScript;
  85. myScriptEngine.World.EventManager.OnScriptChangedEvent +=
  86. changed;
  87. myScriptEngine.World.EventManager.OnScriptAtTargetEvent +=
  88. at_target;
  89. myScriptEngine.World.EventManager.OnScriptNotAtTargetEvent +=
  90. not_at_target;
  91. myScriptEngine.World.EventManager.OnScriptControlEvent +=
  92. control;
  93. myScriptEngine.World.EventManager.OnScriptColliderStart +=
  94. collision_start;
  95. myScriptEngine.World.EventManager.OnScriptColliding +=
  96. collision;
  97. myScriptEngine.World.EventManager.OnScriptCollidingEnd +=
  98. collision_end;
  99. IMoneyModule money =
  100. myScriptEngine.World.RequestModuleInterface<IMoneyModule>();
  101. if (money != null)
  102. money.OnObjectPaid+=HandleObjectPaid;
  103. }
  104. public void ReadConfig()
  105. {
  106. }
  107. private void HandleObjectPaid(UUID objectID, UUID agentID, int amount)
  108. {
  109. SceneObjectPart part =
  110. myScriptEngine.World.GetSceneObjectPart(objectID);
  111. if (part != null)
  112. {
  113. money(part.LocalId, agentID, amount);
  114. }
  115. }
  116. public void changed(uint localID, uint change)
  117. {
  118. // Add to queue for all scripts in localID, Object pass change.
  119. myScriptEngine.PostObjectEvent(localID, new EventParams(
  120. "changed",new object[] { new LSL_Types.LSLInteger(change) },
  121. new DetectParams[0]));
  122. }
  123. public void state_entry(uint localID)
  124. {
  125. // Add to queue for all scripts in ObjectID object
  126. myScriptEngine.PostObjectEvent(localID, new EventParams(
  127. "state_entry",new object[] { },
  128. new DetectParams[0]));
  129. }
  130. public void touch_start(uint localID, uint originalID,
  131. Vector3 offsetPos, IClientAPI remoteClient, SurfaceTouchEventArgs surfaceArgs)
  132. {
  133. // Add to queue for all scripts in ObjectID object
  134. DetectParams[] det = new DetectParams[1];
  135. det[0] = new DetectParams();
  136. det[0].Key = remoteClient.AgentId;
  137. det[0].Populate(myScriptEngine.World);
  138. if (originalID == 0)
  139. {
  140. SceneObjectPart part =
  141. myScriptEngine.World.GetSceneObjectPart(localID);
  142. if (part == null)
  143. return;
  144. det[0].LinkNum = part.LinkNum;
  145. }
  146. else
  147. {
  148. SceneObjectPart originalPart =
  149. myScriptEngine.World.GetSceneObjectPart(originalID);
  150. det[0].LinkNum = originalPart.LinkNum;
  151. }
  152. if (surfaceArgs != null)
  153. {
  154. det[0].SurfaceTouchArgs = surfaceArgs;
  155. }
  156. myScriptEngine.PostObjectEvent(localID, new EventParams(
  157. "touch_start", new Object[] { new LSL_Types.LSLInteger(1) },
  158. det));
  159. }
  160. public void touch(uint localID, uint originalID, Vector3 offsetPos,
  161. IClientAPI remoteClient)
  162. {
  163. // Add to queue for all scripts in ObjectID object
  164. DetectParams[] det = new DetectParams[1];
  165. det[0] = new DetectParams();
  166. det[0].Key = remoteClient.AgentId;
  167. det[0].Populate(myScriptEngine.World);
  168. det[0].OffsetPos = new LSL_Types.Vector3(offsetPos.X,
  169. offsetPos.Y,
  170. offsetPos.Z);
  171. if (originalID == 0)
  172. {
  173. SceneObjectPart part = myScriptEngine.World.GetSceneObjectPart(localID);
  174. if (part == null)
  175. return;
  176. det[0].LinkNum = part.LinkNum;
  177. }
  178. else
  179. {
  180. SceneObjectPart originalPart = myScriptEngine.World.GetSceneObjectPart(originalID);
  181. det[0].LinkNum = originalPart.LinkNum;
  182. }
  183. myScriptEngine.PostObjectEvent(localID, new EventParams(
  184. "touch", new Object[] { new LSL_Types.LSLInteger(1) },
  185. det));
  186. }
  187. public void touch_end(uint localID, uint originalID, IClientAPI remoteClient)
  188. {
  189. // Add to queue for all scripts in ObjectID object
  190. DetectParams[] det = new DetectParams[1];
  191. det[0] = new DetectParams();
  192. det[0].Key = remoteClient.AgentId;
  193. det[0].Populate(myScriptEngine.World);
  194. if (originalID == 0)
  195. {
  196. SceneObjectPart part =
  197. myScriptEngine.World.GetSceneObjectPart(localID);
  198. if (part == null)
  199. return;
  200. det[0].LinkNum = part.LinkNum;
  201. }
  202. else
  203. {
  204. SceneObjectPart originalPart =
  205. myScriptEngine.World.GetSceneObjectPart(originalID);
  206. det[0].LinkNum = originalPart.LinkNum;
  207. }
  208. myScriptEngine.PostObjectEvent(localID, new EventParams(
  209. "touch_end", new Object[] { new LSL_Types.LSLInteger(1) },
  210. det));
  211. }
  212. public void OnRezScript(uint localID, UUID itemID, string script,
  213. int startParam, bool postOnRez, string engine, int stateSource)
  214. {
  215. if (script.StartsWith("//MRM:"))
  216. return;
  217. List<IScriptModule> engines =
  218. new List<IScriptModule>(
  219. myScriptEngine.World.RequestModuleInterfaces<IScriptModule>());
  220. List<string> names = new List<string>();
  221. foreach (IScriptModule m in engines)
  222. names.Add(m.ScriptEngineName);
  223. int lineEnd = script.IndexOf('\n');
  224. if (lineEnd > 1)
  225. {
  226. string firstline = script.Substring(0, lineEnd).Trim();
  227. int colon = firstline.IndexOf(':');
  228. if (firstline.Length > 2 &&
  229. firstline.Substring(0, 2) == "//" && colon != -1)
  230. {
  231. string engineName = firstline.Substring(2, colon-2);
  232. if (names.Contains(engineName))
  233. {
  234. engine = engineName;
  235. script = "//" + script.Substring(script.IndexOf(':')+1);
  236. }
  237. else
  238. {
  239. if (engine == myScriptEngine.ScriptEngineName)
  240. {
  241. SceneObjectPart part =
  242. myScriptEngine.World.GetSceneObjectPart(
  243. localID);
  244. TaskInventoryItem item =
  245. part.Inventory.GetInventoryItem(itemID);
  246. ScenePresence presence =
  247. myScriptEngine.World.GetScenePresence(
  248. item.OwnerID);
  249. if (presence != null)
  250. {
  251. presence.ControllingClient.SendAgentAlertMessage(
  252. "Selected engine unavailable. "+
  253. "Running script on "+
  254. myScriptEngine.ScriptEngineName,
  255. false);
  256. }
  257. }
  258. }
  259. }
  260. }
  261. if (engine != myScriptEngine.ScriptEngineName)
  262. return;
  263. m_log.Debug("OnRezScript localID: " + localID +
  264. " LLUID: " + itemID.ToString() + " Size: " +
  265. script.Length);
  266. myScriptEngine.m_ScriptManager.StartScript(localID, itemID, script,
  267. startParam, postOnRez);
  268. }
  269. public void OnRemoveScript(uint localID, UUID itemID)
  270. {
  271. m_log.Debug("OnRemoveScript localID: " + localID + " LLUID: " + itemID.ToString());
  272. myScriptEngine.m_ScriptManager.StopScript(
  273. localID,
  274. itemID
  275. );
  276. }
  277. public void money(uint localID, UUID agentID, int amount)
  278. {
  279. myScriptEngine.PostObjectEvent(localID, new EventParams(
  280. "money", new object[] {
  281. new LSL_Types.LSLString(agentID.ToString()),
  282. new LSL_Types.LSLInteger(amount) },
  283. new DetectParams[0]));
  284. }
  285. // TODO: Replace placeholders below
  286. // NOTE! THE PARAMETERS FOR THESE FUNCTIONS ARE NOT CORRECT!
  287. // These needs to be hooked up to OpenSim during init of this class
  288. // then queued in EventQueueManager.
  289. // When queued in EventQueueManager they need to be LSL compatible (name and params)
  290. public void state_exit(uint localID)
  291. {
  292. myScriptEngine.PostObjectEvent(localID, new EventParams(
  293. "state_exit", new object[] { },
  294. new DetectParams[0]));
  295. }
  296. public void collision_start(uint localID, ColliderArgs col)
  297. {
  298. // Add to queue for all scripts in ObjectID object
  299. List<DetectParams> det = new List<DetectParams>();
  300. foreach (DetectedObject detobj in col.Colliders)
  301. {
  302. DetectParams d = new DetectParams();
  303. d.Key =detobj.keyUUID;
  304. d.Populate(myScriptEngine.World);
  305. det.Add(d);
  306. }
  307. if (det.Count > 0)
  308. myScriptEngine.PostObjectEvent(localID, new EventParams(
  309. "collision_start",
  310. new Object[] { new LSL_Types.LSLInteger(det.Count) },
  311. det.ToArray()));
  312. }
  313. public void collision(uint localID, ColliderArgs col)
  314. {
  315. // Add to queue for all scripts in ObjectID object
  316. List<DetectParams> det = new List<DetectParams>();
  317. foreach (DetectedObject detobj in col.Colliders)
  318. {
  319. DetectParams d = new DetectParams();
  320. d.Key =detobj.keyUUID;
  321. d.Populate(myScriptEngine.World);
  322. det.Add(d);
  323. }
  324. if (det.Count > 0)
  325. myScriptEngine.PostObjectEvent(localID, new EventParams(
  326. "collision", new Object[] { new LSL_Types.LSLInteger(det.Count) },
  327. det.ToArray()));
  328. }
  329. public void collision_end(uint localID, ColliderArgs col)
  330. {
  331. // Add to queue for all scripts in ObjectID object
  332. List<DetectParams> det = new List<DetectParams>();
  333. foreach (DetectedObject detobj in col.Colliders)
  334. {
  335. DetectParams d = new DetectParams();
  336. d.Key =detobj.keyUUID;
  337. d.Populate(myScriptEngine.World);
  338. det.Add(d);
  339. }
  340. if (det.Count > 0)
  341. myScriptEngine.PostObjectEvent(localID, new EventParams(
  342. "collision_end",
  343. new Object[] { new LSL_Types.LSLInteger(det.Count) },
  344. det.ToArray()));
  345. }
  346. public void land_collision_start(uint localID, UUID itemID)
  347. {
  348. myScriptEngine.PostObjectEvent(localID, new EventParams(
  349. "land_collision_start",
  350. new object[0],
  351. new DetectParams[0]));
  352. }
  353. public void land_collision(uint localID, UUID itemID)
  354. {
  355. myScriptEngine.PostObjectEvent(localID, new EventParams(
  356. "land_collision",
  357. new object[0],
  358. new DetectParams[0]));
  359. }
  360. public void land_collision_end(uint localID, UUID itemID)
  361. {
  362. myScriptEngine.PostObjectEvent(localID, new EventParams(
  363. "land_collision_end",
  364. new object[0],
  365. new DetectParams[0]));
  366. }
  367. // Handled by long commands
  368. public void timer(uint localID, UUID itemID)
  369. {
  370. }
  371. public void listen(uint localID, UUID itemID)
  372. {
  373. }
  374. public void control(uint localID, UUID itemID, UUID agentID, uint held, uint change)
  375. {
  376. if ((change == 0) && (myScriptEngine.m_EventQueueManager.CheckEeventQueueForEvent(localID,"control"))) return;
  377. myScriptEngine.PostObjectEvent(localID, new EventParams(
  378. "control",new object[] {
  379. new LSL_Types.LSLString(agentID.ToString()),
  380. new LSL_Types.LSLInteger(held),
  381. new LSL_Types.LSLInteger(change)},
  382. new DetectParams[0]));
  383. }
  384. public void email(uint localID, UUID itemID, string timeSent,
  385. string address, string subject, string message, int numLeft)
  386. {
  387. myScriptEngine.PostObjectEvent(localID, new EventParams(
  388. "email",new object[] {
  389. new LSL_Types.LSLString(timeSent),
  390. new LSL_Types.LSLString(address),
  391. new LSL_Types.LSLString(subject),
  392. new LSL_Types.LSLString(message),
  393. new LSL_Types.LSLInteger(numLeft)},
  394. new DetectParams[0]));
  395. }
  396. public void at_target(uint localID, uint handle, Vector3 targetpos,
  397. Vector3 atpos)
  398. {
  399. myScriptEngine.PostObjectEvent(localID, new EventParams(
  400. "at_target", new object[] {
  401. new LSL_Types.LSLInteger(handle),
  402. new LSL_Types.Vector3(targetpos.X,targetpos.Y,targetpos.Z),
  403. new LSL_Types.Vector3(atpos.X,atpos.Y,atpos.Z) },
  404. new DetectParams[0]));
  405. }
  406. public void not_at_target(uint localID)
  407. {
  408. myScriptEngine.PostObjectEvent(localID, new EventParams(
  409. "not_at_target",new object[0],
  410. new DetectParams[0]));
  411. }
  412. public void at_rot_target(uint localID, UUID itemID)
  413. {
  414. myScriptEngine.PostObjectEvent(localID, new EventParams(
  415. "at_rot_target",new object[0],
  416. new DetectParams[0]));
  417. }
  418. public void not_at_rot_target(uint localID, UUID itemID)
  419. {
  420. myScriptEngine.PostObjectEvent(localID, new EventParams(
  421. "not_at_rot_target",new object[0],
  422. new DetectParams[0]));
  423. }
  424. public void attach(uint localID, UUID itemID)
  425. {
  426. }
  427. public void dataserver(uint localID, UUID itemID)
  428. {
  429. }
  430. public void link_message(uint localID, UUID itemID)
  431. {
  432. }
  433. public void moving_start(uint localID, UUID itemID)
  434. {
  435. myScriptEngine.PostObjectEvent(localID, new EventParams(
  436. "moving_start",new object[0],
  437. new DetectParams[0]));
  438. }
  439. public void moving_end(uint localID, UUID itemID)
  440. {
  441. myScriptEngine.PostObjectEvent(localID, new EventParams(
  442. "moving_end",new object[0],
  443. new DetectParams[0]));
  444. }
  445. public void object_rez(uint localID, UUID itemID)
  446. {
  447. }
  448. public void remote_data(uint localID, UUID itemID)
  449. {
  450. }
  451. // Handled by long commands
  452. public void http_response(uint localID, UUID itemID)
  453. {
  454. }
  455. /// <summary>
  456. /// If set to true then threads and stuff should try to make a graceful exit
  457. /// </summary>
  458. public bool PleaseShutdown
  459. {
  460. get { return _PleaseShutdown; }
  461. set { _PleaseShutdown = value; }
  462. }
  463. private bool _PleaseShutdown = false;
  464. }
  465. }