EventManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 libsecondlife;
  29. using OpenSim.Framework;
  30. using OpenSim.Region.Environment.Modules.Avatar.Currency.SampleMoney;
  31. using OpenSim.Region.Environment.Scenes;
  32. using OpenSim.Region.Environment.Interfaces;
  33. namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase
  34. {
  35. /// <summary>
  36. /// Prepares events so they can be directly executed upon a script by EventQueueManager, then queues it.
  37. /// </summary>
  38. [Serializable]
  39. public class EventManager : ScriptServerInterfaces.RemoteEvents, iScriptEngineFunctionModule
  40. {
  41. //
  42. // Class is instanced in "ScriptEngine" and Uses "EventQueueManager" that is also instanced in "ScriptEngine".
  43. // This class needs a bit of explaining:
  44. //
  45. // This class it the link between an event inside OpenSim and the corresponding event in a user script being executed.
  46. //
  47. // For example when an user touches an object then the "myScriptEngine.World.EventManager.OnObjectGrab" event is fired inside OpenSim.
  48. // We hook up to this event and queue a touch_start in EventQueueManager with the proper LSL parameters.
  49. // It will then be delivered to the script by EventQueueManager.
  50. //
  51. // You can check debug C# dump of an LSL script if you need to verify what exact parameters are needed.
  52. //
  53. private ScriptEngine myScriptEngine;
  54. //public IScriptHost TEMP_OBJECT_ID;
  55. public EventManager(ScriptEngine _ScriptEngine, bool performHookUp)
  56. {
  57. myScriptEngine = _ScriptEngine;
  58. ReadConfig();
  59. // Hook up to events from OpenSim
  60. // We may not want to do it because someone is controlling us and will deliver events to us
  61. if (performHookUp)
  62. {
  63. myScriptEngine.Log.Info("[" + myScriptEngine.ScriptEngineName + "]: Hooking up to server events");
  64. myScriptEngine.World.EventManager.OnObjectGrab += touch_start;
  65. myScriptEngine.World.EventManager.OnObjectDeGrab += touch_end;
  66. myScriptEngine.World.EventManager.OnRezScript += OnRezScript;
  67. myScriptEngine.World.EventManager.OnRemoveScript += OnRemoveScript;
  68. myScriptEngine.World.EventManager.OnScriptChangedEvent += changed;
  69. myScriptEngine.World.EventManager.OnScriptAtTargetEvent += at_target;
  70. myScriptEngine.World.EventManager.OnScriptNotAtTargetEvent += not_at_target;
  71. myScriptEngine.World.EventManager.OnScriptControlEvent += control;
  72. // TODO: HOOK ALL EVENTS UP TO SERVER!
  73. IMoneyModule money=myScriptEngine.World.RequestModuleInterface<IMoneyModule>();
  74. if (money != null)
  75. {
  76. money.OnObjectPaid+=HandleObjectPaid;
  77. }
  78. }
  79. }
  80. public void ReadConfig()
  81. {
  82. }
  83. private void HandleObjectPaid(LLUUID objectID, LLUUID agentID, int amount)
  84. {
  85. SceneObjectPart part=myScriptEngine.World.GetSceneObjectPart(objectID);
  86. if (part != null)
  87. {
  88. money(part.LocalId, agentID, amount);
  89. }
  90. }
  91. public void changed(uint localID, uint change)
  92. {
  93. // Add to queue for all scripts in localID, Object pass change.
  94. myScriptEngine.m_EventQueueManager.AddToObjectQueue(localID, "changed", EventQueueManager.llDetectNull, new object[] { new LSL_Types.LSLInteger(change) });
  95. }
  96. public void state_entry(uint localID)
  97. {
  98. // Add to queue for all scripts in ObjectID object
  99. myScriptEngine.m_EventQueueManager.AddToObjectQueue(localID, "state_entry", EventQueueManager.llDetectNull, new object[] { });
  100. }
  101. public void touch_start(uint localID, LLVector3 offsetPos, IClientAPI remoteClient)
  102. {
  103. // Add to queue for all scripts in ObjectID object
  104. EventQueueManager.Queue_llDetectParams_Struct detstruct = new EventQueueManager.Queue_llDetectParams_Struct();
  105. detstruct._key = new LSL_Types.key[1];
  106. detstruct._key[0] = new LSL_Types.key(remoteClient.AgentId.ToString());
  107. myScriptEngine.m_EventQueueManager.AddToObjectQueue(localID, "touch_start", detstruct, new object[] { new LSL_Types.LSLInteger(1) });
  108. }
  109. public void touch_end(uint localID, IClientAPI remoteClient)
  110. {
  111. // Add to queue for all scripts in ObjectID object
  112. EventQueueManager.Queue_llDetectParams_Struct detstruct = new EventQueueManager.Queue_llDetectParams_Struct();
  113. detstruct._key = new LSL_Types.key[1];
  114. detstruct._key[0] = new LSL_Types.key(remoteClient.AgentId.ToString());
  115. myScriptEngine.m_EventQueueManager.AddToObjectQueue(localID, "touch_end", detstruct, new object[] { new LSL_Types.LSLInteger(1) });
  116. }
  117. public void OnRezScript(uint localID, LLUUID itemID, string script)
  118. {
  119. Console.WriteLine("OnRezScript localID: " + localID + " LLUID: " + itemID.ToString() + " Size: " +
  120. script.Length);
  121. myScriptEngine.m_ScriptManager.StartScript(localID, itemID, script);
  122. }
  123. public void OnRemoveScript(uint localID, LLUUID itemID)
  124. {
  125. Console.WriteLine("OnRemoveScript localID: " + localID + " LLUID: " + itemID.ToString());
  126. myScriptEngine.m_ScriptManager.StopScript(
  127. localID,
  128. itemID
  129. );
  130. }
  131. public void money(uint localID, LLUUID agentID, int amount)
  132. {
  133. myScriptEngine.m_EventQueueManager.AddToObjectQueue(localID, "money", EventQueueManager.llDetectNull, new object[] { new LSL_Types.LSLString(agentID.ToString()), new LSL_Types.LSLInteger(amount) });
  134. }
  135. // TODO: Replace placeholders below
  136. // NOTE! THE PARAMETERS FOR THESE FUNCTIONS ARE NOT CORRECT!
  137. // These needs to be hooked up to OpenSim during init of this class
  138. // then queued in EventQueueManager.
  139. // When queued in EventQueueManager they need to be LSL compatible (name and params)
  140. public void state_exit(uint localID)
  141. {
  142. // Add to queue for all scripts in ObjectID object
  143. myScriptEngine.m_EventQueueManager.AddToObjectQueue(localID, "state_exit", EventQueueManager.llDetectNull, new object[] { });
  144. }
  145. public void touch(uint localID, LLUUID itemID)
  146. {
  147. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "touch", EventQueueManager.llDetectNull);
  148. }
  149. public void touch_end(uint localID, LLUUID itemID)
  150. {
  151. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "touch_end", EventQueueManager.llDetectNull, new object[] { new LSL_Types.LSLInteger(1) });
  152. }
  153. public void collision_start(uint localID, LLUUID itemID)
  154. {
  155. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "collision_start", EventQueueManager.llDetectNull, new object[] { new LSL_Types.LSLInteger(1) });
  156. }
  157. public void collision(uint localID, LLUUID itemID)
  158. {
  159. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "collision", EventQueueManager.llDetectNull, new object[] { new LSL_Types.LSLInteger(1) });
  160. }
  161. public void collision_end(uint localID, LLUUID itemID)
  162. {
  163. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "collision_end", EventQueueManager.llDetectNull, new object[] { new LSL_Types.LSLInteger(1) });
  164. }
  165. public void land_collision_start(uint localID, LLUUID itemID)
  166. {
  167. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "land_collision_start", EventQueueManager.llDetectNull);
  168. }
  169. public void land_collision(uint localID, LLUUID itemID)
  170. {
  171. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "land_collision", EventQueueManager.llDetectNull);
  172. }
  173. public void land_collision_end(uint localID, LLUUID itemID)
  174. {
  175. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "land_collision_end", EventQueueManager.llDetectNull);
  176. }
  177. // Handled by long commands
  178. public void timer(uint localID, LLUUID itemID)
  179. {
  180. //myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, String.Empty);
  181. }
  182. public void listen(uint localID, LLUUID itemID)
  183. {
  184. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "listen", EventQueueManager.llDetectNull);
  185. }
  186. public void on_rez(uint localID, LLUUID itemID)
  187. {
  188. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "on_rez", EventQueueManager.llDetectNull);
  189. }
  190. public void sensor(uint localID, LLUUID itemID)
  191. {
  192. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "sensor", EventQueueManager.llDetectNull);
  193. }
  194. public void no_sensor(uint localID, LLUUID itemID)
  195. {
  196. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "no_sensor", EventQueueManager.llDetectNull);
  197. }
  198. public void control(uint localID, LLUUID itemID, LLUUID agentID, uint held, uint change)
  199. {
  200. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "control", EventQueueManager.llDetectNull, new object[] { new LSL_Types.LSLString(agentID.ToString()), new LSL_Types.LSLInteger(held), new LSL_Types.LSLInteger(change)});
  201. }
  202. public void email(uint localID, LLUUID itemID)
  203. {
  204. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "email", EventQueueManager.llDetectNull);
  205. }
  206. public void at_target(uint localID, uint handle, LLVector3 targetpos, LLVector3 atpos)
  207. {
  208. myScriptEngine.m_EventQueueManager.AddToObjectQueue(localID, "at_target", EventQueueManager.llDetectNull, new object[] { new LSL_Types.LSLInteger(handle), new LSL_Types.Vector3(targetpos.X,targetpos.Y,targetpos.Z), new LSL_Types.Vector3(atpos.X,atpos.Y,atpos.Z) });
  209. }
  210. public void not_at_target(uint localID)
  211. {
  212. myScriptEngine.m_EventQueueManager.AddToObjectQueue(localID, "not_at_target", EventQueueManager.llDetectNull);
  213. }
  214. public void at_rot_target(uint localID, LLUUID itemID)
  215. {
  216. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "at_rot_target", EventQueueManager.llDetectNull);
  217. }
  218. public void not_at_rot_target(uint localID, LLUUID itemID)
  219. {
  220. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "not_at_rot_target", EventQueueManager.llDetectNull);
  221. }
  222. public void run_time_permissions(uint localID, LLUUID itemID)
  223. {
  224. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "run_time_permissions", EventQueueManager.llDetectNull);
  225. }
  226. public void changed(uint localID, LLUUID itemID)
  227. {
  228. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "changed", EventQueueManager.llDetectNull);
  229. }
  230. public void attach(uint localID, LLUUID itemID)
  231. {
  232. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "attach", EventQueueManager.llDetectNull);
  233. }
  234. public void dataserver(uint localID, LLUUID itemID)
  235. {
  236. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "dataserver", EventQueueManager.llDetectNull);
  237. }
  238. public void link_message(uint localID, LLUUID itemID)
  239. {
  240. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "link_message", EventQueueManager.llDetectNull);
  241. }
  242. public void moving_start(uint localID, LLUUID itemID)
  243. {
  244. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "moving_start", EventQueueManager.llDetectNull);
  245. }
  246. public void moving_end(uint localID, LLUUID itemID)
  247. {
  248. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "moving_end", EventQueueManager.llDetectNull);
  249. }
  250. public void object_rez(uint localID, LLUUID itemID)
  251. {
  252. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "object_rez", EventQueueManager.llDetectNull);
  253. }
  254. public void remote_data(uint localID, LLUUID itemID)
  255. {
  256. myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "remote_data", EventQueueManager.llDetectNull);
  257. }
  258. // Handled by long commands
  259. public void http_response(uint localID, LLUUID itemID)
  260. {
  261. // myScriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "http_response", EventQueueManager.llDetectNull);
  262. }
  263. /// <summary>
  264. /// If set to true then threads and stuff should try to make a graceful exit
  265. /// </summary>
  266. public bool PleaseShutdown
  267. {
  268. get { return _PleaseShutdown; }
  269. set { _PleaseShutdown = value; }
  270. }
  271. private bool _PleaseShutdown = false;
  272. }
  273. }