ScriptEngine.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.IO;
  30. using Nini.Config;
  31. using OpenSim.Framework.Console;
  32. using OpenSim.Region.Environment.Interfaces;
  33. using OpenSim.Region.Environment.Scenes;
  34. using OpenSim.Region.ScriptEngine.Common;
  35. using OpenSim.Region.ScriptEngine.Common.ScriptEngineBase;
  36. namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase
  37. {
  38. /// <summary>
  39. /// This is the root object for ScriptEngine. Objects access each other trough this class.
  40. /// </summary>
  41. ///
  42. [Serializable]
  43. public abstract class ScriptEngine : IRegionModule, OpenSim.Region.ScriptEngine.Common.ScriptServerInterfaces.ScriptEngine, iScriptEngineFunctionModule
  44. {
  45. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  46. public static List<ScriptEngine> ScriptEngines = new List<ScriptEngine>();
  47. public Scene World;
  48. public EventManager m_EventManager; // Handles and queues incoming events from OpenSim
  49. public EventQueueManager m_EventQueueManager; // Executes events, handles script threads
  50. public ScriptManager m_ScriptManager; // Load, unload and execute scripts
  51. public AppDomainManager m_AppDomainManager; // Handles loading/unloading of scripts into AppDomains
  52. public AsyncCommandManager m_ASYNCLSLCommandManager; // Asyncronous LSL commands (commands that returns with an event)
  53. public static MaintenanceThread m_MaintenanceThread; // Thread that does different kinds of maintenance, for example refreshing config and killing scripts that has been running too long
  54. public IConfigSource ConfigSource;
  55. public IConfig ScriptConfigSource;
  56. public abstract string ScriptEngineName { get; }
  57. /// <summary>
  58. /// How many seconds between re-reading config-file. 0 = never. ScriptEngine will try to adjust to new config changes.
  59. /// </summary>
  60. public int RefreshConfigFileSeconds {
  61. get { return (int)(RefreshConfigFilens / 10000000); }
  62. set { RefreshConfigFilens = value * 10000000; }
  63. }
  64. public long RefreshConfigFilens;
  65. public ScriptManager GetScriptManager()
  66. {
  67. return _GetScriptManager();
  68. }
  69. public abstract ScriptManager _GetScriptManager();
  70. public log4net.ILog Log
  71. {
  72. get { return m_log; }
  73. }
  74. public ScriptEngine()
  75. {
  76. Common.mySE = this; // For logging, just need any instance, doesn't matter
  77. lock (ScriptEngines)
  78. {
  79. ScriptEngines.Add(this); // Keep a list of ScriptEngines for shared threads to process all instances
  80. }
  81. }
  82. public void InitializeEngine(Scene Sceneworld, IConfigSource config, bool HookUpToServer, ScriptManager newScriptManager)
  83. {
  84. World = Sceneworld;
  85. ConfigSource = config;
  86. m_log.Info("[" + ScriptEngineName + "]: ScriptEngine initializing");
  87. // Make sure we have config
  88. if (ConfigSource.Configs[ScriptEngineName] == null)
  89. ConfigSource.AddConfig(ScriptEngineName);
  90. ScriptConfigSource = ConfigSource.Configs[ScriptEngineName];
  91. //m_log.Info("[" + ScriptEngineName + "]: InitializeEngine");
  92. // Create all objects we'll be using
  93. m_EventQueueManager = new EventQueueManager(this);
  94. m_EventManager = new EventManager(this, HookUpToServer);
  95. // We need to start it
  96. newScriptManager.Start();
  97. m_ScriptManager = newScriptManager;
  98. m_AppDomainManager = new AppDomainManager(this);
  99. m_ASYNCLSLCommandManager = new AsyncCommandManager(this);
  100. if (m_MaintenanceThread == null)
  101. m_MaintenanceThread = new MaintenanceThread();
  102. m_log.Info("[" + ScriptEngineName + "]: Reading configuration from config section \"" + ScriptEngineName + "\"");
  103. ReadConfig();
  104. // Should we iterate the region for scripts that needs starting?
  105. // Or can we assume we are loaded before anything else so we can use proper events?
  106. }
  107. public void Shutdown()
  108. {
  109. // We are shutting down
  110. lock (ScriptEngines)
  111. {
  112. ScriptEngines.Remove(this);
  113. }
  114. }
  115. ScriptServerInterfaces.RemoteEvents ScriptServerInterfaces.ScriptEngine.EventManager()
  116. {
  117. return this.m_EventManager;
  118. }
  119. public void ReadConfig()
  120. {
  121. #if DEBUG
  122. //m_log.Debug("[" + ScriptEngineName + "]: Refreshing configuration for all modules");
  123. #endif
  124. RefreshConfigFileSeconds = ScriptConfigSource.GetInt("RefreshConfig", 30);
  125. // Create a new object (probably not necessary?)
  126. // ScriptConfigSource = ConfigSource.Configs[ScriptEngineName];
  127. if (m_EventQueueManager != null) m_EventQueueManager.ReadConfig();
  128. if (m_EventManager != null) m_EventManager.ReadConfig();
  129. if (m_ScriptManager != null) m_ScriptManager.ReadConfig();
  130. if (m_AppDomainManager != null) m_AppDomainManager.ReadConfig();
  131. if (m_ASYNCLSLCommandManager != null) m_ASYNCLSLCommandManager.ReadConfig();
  132. if (m_MaintenanceThread != null) m_MaintenanceThread.ReadConfig();
  133. }
  134. #region IRegionModule
  135. public abstract void Initialise(Scene scene, IConfigSource config);
  136. public void PostInitialise()
  137. {
  138. }
  139. public void Close()
  140. {
  141. }
  142. public string Name
  143. {
  144. get { return "Common." + ScriptEngineName; }
  145. }
  146. public bool IsSharedModule
  147. {
  148. get { return false; }
  149. }
  150. #endregion
  151. }
  152. }