EventManager.cs 14 KB

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