ScriptEngine.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 log4net;
  31. using Nini.Config;
  32. using OpenSim.Region.Interfaces;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Environment;
  35. using OpenSim.Region.Environment.Interfaces;
  36. using OpenSim.Region.Environment.Scenes;
  37. using OpenSim.Region.ScriptEngine.Interfaces;
  38. using OpenMetaverse;
  39. using OpenMetaverse.StructuredData;
  40. using OpenSim.Region.ScriptEngine.Shared;
  41. using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  42. namespace OpenSim.Region.ScriptEngine.DotNetEngine
  43. {
  44. [Serializable]
  45. public class ScriptEngine : IRegionModule, IScriptEngine, IScriptModule
  46. {
  47. private static readonly ILog m_log =
  48. LogManager.GetLogger(
  49. MethodBase.GetCurrentMethod().DeclaringType);
  50. public static List<ScriptEngine> ScriptEngines =
  51. new List<ScriptEngine>();
  52. private Scene m_Scene;
  53. public Scene World
  54. {
  55. get { return m_Scene; }
  56. }
  57. // Handles and queues incoming events from OpenSim
  58. public EventManager m_EventManager;
  59. // Executes events, handles script threads
  60. public EventQueueManager m_EventQueueManager;
  61. // Load, unload and execute scripts
  62. public ScriptManager m_ScriptManager;
  63. // Handles loading/unloading of scripts into AppDomains
  64. public AppDomainManager m_AppDomainManager;
  65. // Thread that does different kinds of maintenance,
  66. // for example refreshing config and killing scripts
  67. // that has been running too long
  68. public static MaintenanceThread m_MaintenanceThread;
  69. public IConfigSource ConfigSource;
  70. public IConfig ScriptConfigSource;
  71. private bool m_enabled = false;
  72. public IConfig Config
  73. {
  74. get { return ScriptConfigSource; }
  75. }
  76. // How many seconds between re-reading config-file.
  77. // 0 = never. ScriptEngine will try to adjust to new config changes.
  78. public int RefreshConfigFileSeconds {
  79. get { return (int)(RefreshConfigFilens / 10000000); }
  80. set { RefreshConfigFilens = value * 10000000; }
  81. }
  82. public long RefreshConfigFilens;
  83. public string ScriptEngineName
  84. {
  85. get { return "ScriptEngine.DotNetEngine"; }
  86. }
  87. public ILog Log
  88. {
  89. get { return m_log; }
  90. }
  91. public ScriptEngine()
  92. {
  93. // For logging, just need any instance, doesn't matter
  94. Common.mySE = this;
  95. lock (ScriptEngines)
  96. {
  97. // Keep a list of ScriptEngines for shared threads
  98. // to process all instances
  99. ScriptEngines.Add(this);
  100. }
  101. }
  102. public void Initialise(Scene Sceneworld, IConfigSource config)
  103. {
  104. m_log.Info("[" + ScriptEngineName + "]: ScriptEngine initializing");
  105. ConfigSource = config;
  106. m_Scene = Sceneworld;
  107. // Make sure we have config
  108. if (ConfigSource.Configs[ScriptEngineName] == null)
  109. ConfigSource.AddConfig(ScriptEngineName);
  110. ScriptConfigSource = ConfigSource.Configs[ScriptEngineName];
  111. m_enabled = ScriptConfigSource.GetBoolean("Enabled", true);
  112. if (!m_enabled)
  113. return;
  114. // Create all objects we'll be using
  115. m_EventQueueManager = new EventQueueManager(this);
  116. m_EventManager = new EventManager(this, true);
  117. // We need to start it
  118. m_ScriptManager = new ScriptManager(this);
  119. m_ScriptManager.Setup();
  120. m_AppDomainManager = new AppDomainManager(this);
  121. if (m_MaintenanceThread == null)
  122. m_MaintenanceThread = new MaintenanceThread();
  123. m_log.Info("[" + ScriptEngineName + "]: Reading configuration "+
  124. "from config section \"" + ScriptEngineName + "\"");
  125. ReadConfig();
  126. m_Scene.StackModuleInterface<IScriptModule>(this);
  127. }
  128. public void PostInitialise()
  129. {
  130. if (!m_enabled)
  131. return;
  132. m_EventManager.HookUpEvents();
  133. m_Scene.EventManager.OnScriptReset += OnScriptReset;
  134. m_Scene.EventManager.OnGetScriptRunning += OnGetScriptRunning;
  135. m_Scene.EventManager.OnStartScript += OnStartScript;
  136. m_Scene.EventManager.OnStopScript += OnStopScript;
  137. m_ScriptManager.Start();
  138. }
  139. public void Shutdown()
  140. {
  141. // We are shutting down
  142. lock (ScriptEngines)
  143. {
  144. ScriptEngines.Remove(this);
  145. }
  146. }
  147. public void ReadConfig()
  148. {
  149. RefreshConfigFileSeconds = ScriptConfigSource.GetInt("RefreshConfig", 30);
  150. if (m_EventQueueManager != null) m_EventQueueManager.ReadConfig();
  151. if (m_EventManager != null) m_EventManager.ReadConfig();
  152. if (m_ScriptManager != null) m_ScriptManager.ReadConfig();
  153. if (m_AppDomainManager != null) m_AppDomainManager.ReadConfig();
  154. if (m_MaintenanceThread != null) m_MaintenanceThread.ReadConfig();
  155. }
  156. #region IRegionModule
  157. public void Close()
  158. {
  159. }
  160. public string Name
  161. {
  162. get { return "Common." + ScriptEngineName; }
  163. }
  164. public bool IsSharedModule
  165. {
  166. get { return false; }
  167. }
  168. public bool PostObjectEvent(uint localID, EventParams p)
  169. {
  170. return m_EventQueueManager.AddToObjectQueue(localID, p.EventName,
  171. p.DetectParams, p.Params);
  172. }
  173. public bool PostScriptEvent(UUID itemID, EventParams p)
  174. {
  175. uint localID = m_ScriptManager.GetLocalID(itemID);
  176. return m_EventQueueManager.AddToScriptQueue(localID, itemID,
  177. p.EventName, p.DetectParams, p.Params);
  178. }
  179. public DetectParams GetDetectParams(UUID itemID, int number)
  180. {
  181. uint localID = m_ScriptManager.GetLocalID(itemID);
  182. if (localID == 0)
  183. return null;
  184. InstanceData id = m_ScriptManager.GetScript(localID, itemID);
  185. if (id == null)
  186. return null;
  187. DetectParams[] det = m_ScriptManager.GetDetectParams(id);
  188. if (number < 0 || number >= det.Length)
  189. return null;
  190. return det[number];
  191. }
  192. public int GetStartParameter(UUID itemID)
  193. {
  194. return m_ScriptManager.GetStartParameter(itemID);
  195. }
  196. public void SetMinEventDelay(UUID itemID, double delay)
  197. {
  198. // TODO in DotNet, done in XEngine
  199. throw new NotImplementedException();
  200. }
  201. #endregion
  202. public void SetState(UUID itemID, string state)
  203. {
  204. uint localID = m_ScriptManager.GetLocalID(itemID);
  205. if (localID == 0)
  206. return;
  207. InstanceData id = m_ScriptManager.GetScript(localID, itemID);
  208. if (id == null)
  209. return;
  210. string currentState = id.State;
  211. if (currentState != state)
  212. {
  213. try
  214. {
  215. m_EventManager.state_exit(localID);
  216. }
  217. catch (AppDomainUnloadedException)
  218. {
  219. Console.WriteLine("[SCRIPT]: state change called when "+
  220. "script was unloaded. Nothing to worry about, "+
  221. "but noting the occurance");
  222. }
  223. id.State = state;
  224. try
  225. {
  226. int eventFlags = m_ScriptManager.GetStateEventFlags(localID,
  227. itemID);
  228. SceneObjectPart part = m_Scene.GetSceneObjectPart(localID);
  229. if (part != null)
  230. part.SetScriptEvents(itemID, eventFlags);
  231. m_EventManager.state_entry(localID);
  232. }
  233. catch (AppDomainUnloadedException)
  234. {
  235. Console.WriteLine("[SCRIPT]: state change called when "+
  236. "script was unloaded. Nothing to worry about, but "+
  237. "noting the occurance");
  238. }
  239. }
  240. }
  241. public bool GetScriptState(UUID itemID)
  242. {
  243. uint localID = m_ScriptManager.GetLocalID(itemID);
  244. if (localID == 0)
  245. return false;
  246. InstanceData id = m_ScriptManager.GetScript(localID, itemID);
  247. if (id == null)
  248. return false;
  249. return id.Running;
  250. }
  251. public void SetScriptState(UUID itemID, bool state)
  252. {
  253. uint localID = m_ScriptManager.GetLocalID(itemID);
  254. if (localID == 0)
  255. return;
  256. InstanceData id = m_ScriptManager.GetScript(localID, itemID);
  257. if (id == null)
  258. return;
  259. if (!id.Disabled)
  260. id.Running = state;
  261. }
  262. public void ApiResetScript(UUID itemID)
  263. {
  264. uint localID = m_ScriptManager.GetLocalID(itemID);
  265. if (localID == 0)
  266. return;
  267. m_ScriptManager.ResetScript(localID, itemID);
  268. }
  269. public void ResetScript(UUID itemID)
  270. {
  271. uint localID = m_ScriptManager.GetLocalID(itemID);
  272. if (localID == 0)
  273. return;
  274. m_ScriptManager.ResetScript(localID, itemID);
  275. }
  276. public void OnScriptReset(uint localID, UUID itemID)
  277. {
  278. ResetScript(itemID);
  279. }
  280. public void OnStartScript(uint localID, UUID itemID)
  281. {
  282. InstanceData id = m_ScriptManager.GetScript(localID, itemID);
  283. if (id == null)
  284. return;
  285. if (!id.Disabled)
  286. id.Running = true;
  287. }
  288. public void OnStopScript(uint localID, UUID itemID)
  289. {
  290. InstanceData id = m_ScriptManager.GetScript(localID, itemID);
  291. if (id == null)
  292. return;
  293. id.Running = false;
  294. }
  295. public void OnGetScriptRunning(IClientAPI controllingClient,
  296. UUID objectID, UUID itemID)
  297. {
  298. uint localID = m_ScriptManager.GetLocalID(itemID);
  299. if (localID == 0)
  300. return;
  301. InstanceData id = m_ScriptManager.GetScript(localID, itemID);
  302. if (id == null)
  303. return;
  304. IEventQueue eq = World.RequestModuleInterface<IEventQueue>();
  305. if (eq == null)
  306. {
  307. controllingClient.SendScriptRunningReply(objectID, itemID,
  308. id.Running);
  309. }
  310. else
  311. {
  312. eq.Enqueue(EventQueueHelper.ScriptRunningReplyEvent(objectID, itemID, id.Running, true),
  313. controllingClient.AgentId);
  314. }
  315. }
  316. public IScriptApi GetApi(UUID itemID, string name)
  317. {
  318. return m_ScriptManager.GetApi(itemID, name);
  319. }
  320. public IScriptWorkItem QueueEventHandler(Object o)
  321. {
  322. return null;
  323. }
  324. public string GetAssemblyName(UUID itemID)
  325. {
  326. return "";
  327. }
  328. public string GetXMLState(UUID itemID)
  329. {
  330. return "";
  331. }
  332. }
  333. }