EventManager.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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.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. SurfaceTouchEventArgs surfaceArgs)
  189. {
  190. // Add to queue for all scripts in ObjectID object
  191. DetectParams[] det = new DetectParams[1];
  192. det[0] = new DetectParams();
  193. det[0].Key = remoteClient.AgentId;
  194. det[0].Populate(myScriptEngine.World);
  195. if (originalID == 0)
  196. {
  197. SceneObjectPart part =
  198. myScriptEngine.World.GetSceneObjectPart(localID);
  199. if (part == null)
  200. return;
  201. det[0].LinkNum = part.LinkNum;
  202. }
  203. else
  204. {
  205. SceneObjectPart originalPart =
  206. myScriptEngine.World.GetSceneObjectPart(originalID);
  207. det[0].LinkNum = originalPart.LinkNum;
  208. }
  209. if (surfaceArgs != null)
  210. {
  211. det[0].SurfaceTouchArgs = surfaceArgs;
  212. }
  213. myScriptEngine.PostObjectEvent(localID, new EventParams(
  214. "touch_end", new Object[] { new LSL_Types.LSLInteger(1) },
  215. det));
  216. }
  217. public void OnRezScript(uint localID, UUID itemID, string script,
  218. int startParam, bool postOnRez, string engine, int stateSource)
  219. {
  220. if (script.StartsWith("//MRM:"))
  221. return;
  222. List<IScriptModule> engines =
  223. new List<IScriptModule>(
  224. myScriptEngine.World.RequestModuleInterfaces<IScriptModule>());
  225. List<string> names = new List<string>();
  226. foreach (IScriptModule m in engines)
  227. names.Add(m.ScriptEngineName);
  228. int lineEnd = script.IndexOf('\n');
  229. if (lineEnd > 1)
  230. {
  231. string firstline = script.Substring(0, lineEnd).Trim();
  232. int colon = firstline.IndexOf(':');
  233. if (firstline.Length > 2 &&
  234. firstline.Substring(0, 2) == "//" && colon != -1)
  235. {
  236. string engineName = firstline.Substring(2, colon-2);
  237. if (names.Contains(engineName))
  238. {
  239. engine = engineName;
  240. script = "//" + script.Substring(script.IndexOf(':')+1);
  241. }
  242. else
  243. {
  244. if (engine == myScriptEngine.ScriptEngineName)
  245. {
  246. SceneObjectPart part =
  247. myScriptEngine.World.GetSceneObjectPart(
  248. localID);
  249. TaskInventoryItem item =
  250. part.Inventory.GetInventoryItem(itemID);
  251. ScenePresence presence =
  252. myScriptEngine.World.GetScenePresence(
  253. item.OwnerID);
  254. if (presence != null)
  255. {
  256. presence.ControllingClient.SendAgentAlertMessage(
  257. "Selected engine unavailable. "+
  258. "Running script on "+
  259. myScriptEngine.ScriptEngineName,
  260. false);
  261. }
  262. }
  263. }
  264. }
  265. }
  266. if (engine != myScriptEngine.ScriptEngineName)
  267. return;
  268. m_log.Debug("OnRezScript localID: " + localID +
  269. " LLUID: " + itemID.ToString() + " Size: " +
  270. script.Length);
  271. myScriptEngine.m_ScriptManager.StartScript(localID, itemID, script,
  272. startParam, postOnRez);
  273. }
  274. public void OnRemoveScript(uint localID, UUID itemID)
  275. {
  276. m_log.Debug("OnRemoveScript localID: " + localID + " LLUID: " + itemID.ToString());
  277. myScriptEngine.m_ScriptManager.StopScript(
  278. localID,
  279. itemID
  280. );
  281. }
  282. public void money(uint localID, UUID agentID, int amount)
  283. {
  284. myScriptEngine.PostObjectEvent(localID, new EventParams(
  285. "money", new object[] {
  286. new LSL_Types.LSLString(agentID.ToString()),
  287. new LSL_Types.LSLInteger(amount) },
  288. new DetectParams[0]));
  289. }
  290. // TODO: Replace placeholders below
  291. // NOTE! THE PARAMETERS FOR THESE FUNCTIONS ARE NOT CORRECT!
  292. // These needs to be hooked up to OpenSim during init of this class
  293. // then queued in EventQueueManager.
  294. // When queued in EventQueueManager they need to be LSL compatible (name and params)
  295. public void state_exit(uint localID)
  296. {
  297. myScriptEngine.PostObjectEvent(localID, new EventParams(
  298. "state_exit", new object[] { },
  299. new DetectParams[0]));
  300. }
  301. public void collision_start(uint localID, ColliderArgs col)
  302. {
  303. // Add to queue for all scripts in ObjectID object
  304. List<DetectParams> det = new List<DetectParams>();
  305. foreach (DetectedObject detobj in col.Colliders)
  306. {
  307. DetectParams d = new DetectParams();
  308. d.Key =detobj.keyUUID;
  309. d.Populate(myScriptEngine.World);
  310. det.Add(d);
  311. }
  312. if (det.Count > 0)
  313. myScriptEngine.PostObjectEvent(localID, new EventParams(
  314. "collision_start",
  315. new Object[] { new LSL_Types.LSLInteger(det.Count) },
  316. det.ToArray()));
  317. }
  318. public void collision(uint localID, ColliderArgs col)
  319. {
  320. // Add to queue for all scripts in ObjectID object
  321. List<DetectParams> det = new List<DetectParams>();
  322. foreach (DetectedObject detobj in col.Colliders)
  323. {
  324. DetectParams d = new DetectParams();
  325. d.Key =detobj.keyUUID;
  326. d.Populate(myScriptEngine.World);
  327. det.Add(d);
  328. }
  329. if (det.Count > 0)
  330. myScriptEngine.PostObjectEvent(localID, new EventParams(
  331. "collision", new Object[] { new LSL_Types.LSLInteger(det.Count) },
  332. det.ToArray()));
  333. }
  334. public void collision_end(uint localID, ColliderArgs col)
  335. {
  336. // Add to queue for all scripts in ObjectID object
  337. List<DetectParams> det = new List<DetectParams>();
  338. foreach (DetectedObject detobj in col.Colliders)
  339. {
  340. DetectParams d = new DetectParams();
  341. d.Key =detobj.keyUUID;
  342. d.Populate(myScriptEngine.World);
  343. det.Add(d);
  344. }
  345. if (det.Count > 0)
  346. myScriptEngine.PostObjectEvent(localID, new EventParams(
  347. "collision_end",
  348. new Object[] { new LSL_Types.LSLInteger(det.Count) },
  349. det.ToArray()));
  350. }
  351. public void land_collision_start(uint localID, UUID itemID)
  352. {
  353. myScriptEngine.PostObjectEvent(localID, new EventParams(
  354. "land_collision_start",
  355. new object[0],
  356. new DetectParams[0]));
  357. }
  358. public void land_collision(uint localID, UUID itemID)
  359. {
  360. myScriptEngine.PostObjectEvent(localID, new EventParams(
  361. "land_collision",
  362. new object[0],
  363. new DetectParams[0]));
  364. }
  365. public void land_collision_end(uint localID, UUID itemID)
  366. {
  367. myScriptEngine.PostObjectEvent(localID, new EventParams(
  368. "land_collision_end",
  369. new object[0],
  370. new DetectParams[0]));
  371. }
  372. // Handled by long commands
  373. public void timer(uint localID, UUID itemID)
  374. {
  375. }
  376. public void listen(uint localID, UUID itemID)
  377. {
  378. }
  379. public void control(uint localID, UUID itemID, UUID agentID, uint held, uint change)
  380. {
  381. if ((change == 0) && (myScriptEngine.m_EventQueueManager.CheckEeventQueueForEvent(localID,"control"))) return;
  382. myScriptEngine.PostObjectEvent(localID, new EventParams(
  383. "control",new object[] {
  384. new LSL_Types.LSLString(agentID.ToString()),
  385. new LSL_Types.LSLInteger(held),
  386. new LSL_Types.LSLInteger(change)},
  387. new DetectParams[0]));
  388. }
  389. public void email(uint localID, UUID itemID, string timeSent,
  390. string address, string subject, string message, int numLeft)
  391. {
  392. myScriptEngine.PostObjectEvent(localID, new EventParams(
  393. "email",new object[] {
  394. new LSL_Types.LSLString(timeSent),
  395. new LSL_Types.LSLString(address),
  396. new LSL_Types.LSLString(subject),
  397. new LSL_Types.LSLString(message),
  398. new LSL_Types.LSLInteger(numLeft)},
  399. new DetectParams[0]));
  400. }
  401. public void at_target(uint localID, uint handle, Vector3 targetpos,
  402. Vector3 atpos)
  403. {
  404. myScriptEngine.PostObjectEvent(localID, new EventParams(
  405. "at_target", new object[] {
  406. new LSL_Types.LSLInteger(handle),
  407. new LSL_Types.Vector3(targetpos.X,targetpos.Y,targetpos.Z),
  408. new LSL_Types.Vector3(atpos.X,atpos.Y,atpos.Z) },
  409. new DetectParams[0]));
  410. }
  411. public void not_at_target(uint localID)
  412. {
  413. myScriptEngine.PostObjectEvent(localID, new EventParams(
  414. "not_at_target",new object[0],
  415. new DetectParams[0]));
  416. }
  417. public void at_rot_target(uint localID, UUID itemID)
  418. {
  419. myScriptEngine.PostObjectEvent(localID, new EventParams(
  420. "at_rot_target",new object[0],
  421. new DetectParams[0]));
  422. }
  423. public void not_at_rot_target(uint localID, UUID itemID)
  424. {
  425. myScriptEngine.PostObjectEvent(localID, new EventParams(
  426. "not_at_rot_target",new object[0],
  427. new DetectParams[0]));
  428. }
  429. public void attach(uint localID, UUID itemID)
  430. {
  431. }
  432. public void dataserver(uint localID, UUID itemID)
  433. {
  434. }
  435. public void link_message(uint localID, UUID itemID)
  436. {
  437. }
  438. public void moving_start(uint localID, UUID itemID)
  439. {
  440. myScriptEngine.PostObjectEvent(localID, new EventParams(
  441. "moving_start",new object[0],
  442. new DetectParams[0]));
  443. }
  444. public void moving_end(uint localID, UUID itemID)
  445. {
  446. myScriptEngine.PostObjectEvent(localID, new EventParams(
  447. "moving_end",new object[0],
  448. new DetectParams[0]));
  449. }
  450. public void object_rez(uint localID, UUID itemID)
  451. {
  452. }
  453. public void remote_data(uint localID, UUID itemID)
  454. {
  455. }
  456. // Handled by long commands
  457. public void http_response(uint localID, UUID itemID)
  458. {
  459. }
  460. /// <summary>
  461. /// If set to true then threads and stuff should try to make a graceful exit
  462. /// </summary>
  463. public bool PleaseShutdown
  464. {
  465. get { return _PleaseShutdown; }
  466. set { _PleaseShutdown = value; }
  467. }
  468. private bool _PleaseShutdown = false;
  469. }
  470. }