ScriptEngine.cs 16 KB

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