SceneBase.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 System.Threading;
  31. using OpenMetaverse;
  32. using log4net;
  33. using Nini.Config;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  38. namespace OpenSim.Region.Framework.Scenes
  39. {
  40. public abstract class SceneBase : IScene
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. #region Events
  44. public event restart OnRestart;
  45. #endregion
  46. #region Fields
  47. public IConfigSource Config
  48. {
  49. get { return GetConfig(); }
  50. }
  51. protected virtual IConfigSource GetConfig()
  52. {
  53. return null;
  54. }
  55. /// <value>
  56. /// All the region modules attached to this scene.
  57. /// </value>
  58. public Dictionary<string, IRegionModule> Modules
  59. {
  60. get { return m_modules; }
  61. }
  62. protected Dictionary<string, IRegionModule> m_modules = new Dictionary<string, IRegionModule>();
  63. public Dictionary<string, IRegionModuleBase> RegionModules
  64. {
  65. get { return m_regionModules; }
  66. }
  67. private Dictionary<string, IRegionModuleBase> m_regionModules = new Dictionary<string, IRegionModuleBase>();
  68. /// <value>
  69. /// The module interfaces available from this scene.
  70. /// </value>
  71. protected Dictionary<Type, List<object>> ModuleInterfaces = new Dictionary<Type, List<object>>();
  72. protected Dictionary<string, object> ModuleAPIMethods = new Dictionary<string, object>();
  73. /// <value>
  74. /// The module commanders available from this scene
  75. /// </value>
  76. protected Dictionary<string, ICommander> m_moduleCommanders = new Dictionary<string, ICommander>();
  77. /// <value>
  78. /// Registered classes that are capable of creating entities.
  79. /// </value>
  80. protected Dictionary<PCode, IEntityCreator> m_entityCreators = new Dictionary<PCode, IEntityCreator>();
  81. /// <summary>
  82. /// The last allocated local prim id. When a new local id is requested, the next number in the sequence is
  83. /// dispensed.
  84. /// </summary>
  85. protected uint m_lastAllocatedLocalId = 720000;
  86. private readonly Mutex _primAllocateMutex = new Mutex(false);
  87. protected readonly ClientManager m_clientManager = new ClientManager();
  88. public float TimeDilation
  89. {
  90. get { return 1.0f; }
  91. }
  92. protected ulong m_regionHandle;
  93. protected string m_regionName;
  94. protected RegionInfo m_regInfo;
  95. public ITerrainChannel Heightmap;
  96. /// <value>
  97. /// Allows retrieval of land information for this scene.
  98. /// </value>
  99. public ILandChannel LandChannel;
  100. /// <value>
  101. /// Manage events that occur in this scene (avatar movement, script rez, etc.). Commonly used by region modules
  102. /// to subscribe to scene events.
  103. /// </value>
  104. public EventManager EventManager
  105. {
  106. get { return m_eventManager; }
  107. }
  108. protected EventManager m_eventManager;
  109. protected ScenePermissions m_permissions;
  110. public ScenePermissions Permissions
  111. {
  112. get { return m_permissions; }
  113. }
  114. protected string m_datastore;
  115. /* Used by the loadbalancer plugin on GForge */
  116. protected RegionStatus m_regStatus;
  117. public RegionStatus RegionStatus
  118. {
  119. get { return m_regStatus; }
  120. set { m_regStatus = value; }
  121. }
  122. #endregion
  123. #region Update Methods
  124. /// <summary>
  125. /// Normally called once every frame/tick to let the world preform anything required (like running the physics simulation)
  126. /// </summary>
  127. public abstract void Update();
  128. #endregion
  129. #region Terrain Methods
  130. /// <summary>
  131. /// Loads the World heightmap
  132. /// </summary>
  133. public abstract void LoadWorldMap();
  134. /// <summary>
  135. /// Send the region heightmap to the client
  136. /// </summary>
  137. /// <param name="RemoteClient">Client to send to</param>
  138. public virtual void SendLayerData(IClientAPI RemoteClient)
  139. {
  140. RemoteClient.SendLayerData(Heightmap.GetFloatsSerialised());
  141. }
  142. #endregion
  143. #region Add/Remove Agent/Avatar
  144. /// <summary>
  145. /// Register the new client with the scene. The client starts off as a child agent - the later agent crossing
  146. /// will promote it to a root agent during login.
  147. /// </summary>
  148. /// <param name="client"></param
  149. public abstract void AddNewClient(IClientAPI client);
  150. /// <summary>
  151. /// Remove a client from the scene
  152. /// </summary>
  153. /// <param name="agentID"></param>
  154. public abstract void RemoveClient(UUID agentID);
  155. public bool TryGetScenePresence(UUID agentID, out object scenePresence)
  156. {
  157. scenePresence = null;
  158. ScenePresence sp = null;
  159. if (TryGetScenePresence(agentID, out sp))
  160. {
  161. scenePresence = sp;
  162. return true;
  163. }
  164. return false;
  165. }
  166. public abstract bool TryGetScenePresence(UUID agentID, out ScenePresence scenePresence);
  167. #endregion
  168. /// <summary>
  169. ///
  170. /// </summary>
  171. /// <returns></returns>
  172. public virtual RegionInfo RegionInfo
  173. {
  174. get { return m_regInfo; }
  175. }
  176. #region admin stuff
  177. /// <summary>
  178. /// Region Restart - Seconds till restart.
  179. /// </summary>
  180. /// <param name="seconds"></param>
  181. public virtual void Restart(int seconds)
  182. {
  183. m_log.Error("[REGION]: passing Restart Message up the namespace");
  184. restart handlerPhysicsCrash = OnRestart;
  185. if (handlerPhysicsCrash != null)
  186. handlerPhysicsCrash(RegionInfo);
  187. }
  188. public virtual bool PresenceChildStatus(UUID avatarID)
  189. {
  190. return false;
  191. }
  192. public abstract void OtherRegionUp(GridRegion otherRegion);
  193. public virtual string GetSimulatorVersion()
  194. {
  195. return "OpenSimulator Server";
  196. }
  197. #endregion
  198. #region Shutdown
  199. /// <summary>
  200. /// Tidy before shutdown
  201. /// </summary>
  202. public virtual void Close()
  203. {
  204. // Shut down all non shared modules.
  205. foreach (IRegionModule module in Modules.Values)
  206. {
  207. if (!module.IsSharedModule)
  208. {
  209. module.Close();
  210. }
  211. }
  212. Modules.Clear();
  213. try
  214. {
  215. EventManager.TriggerShutdown();
  216. }
  217. catch (Exception e)
  218. {
  219. m_log.Error(string.Format("[SCENE]: SceneBase.cs: Close() - Failed with exception ", e));
  220. }
  221. }
  222. #endregion
  223. /// <summary>
  224. /// Returns a new unallocated local ID
  225. /// </summary>
  226. /// <returns>A brand new local ID</returns>
  227. public uint AllocateLocalId()
  228. {
  229. uint myID;
  230. _primAllocateMutex.WaitOne();
  231. myID = ++m_lastAllocatedLocalId;
  232. _primAllocateMutex.ReleaseMutex();
  233. return myID;
  234. }
  235. #region Module Methods
  236. /// <summary>
  237. /// Add a module to this scene.
  238. /// </summary>
  239. /// <param name="name"></param>
  240. /// <param name="module"></param>
  241. public void AddModule(string name, IRegionModule module)
  242. {
  243. if (!Modules.ContainsKey(name))
  244. {
  245. Modules.Add(name, module);
  246. }
  247. }
  248. /// <summary>
  249. /// Add a region-module to this scene. TODO: This will replace AddModule in the future.
  250. /// </summary>
  251. /// <param name="name"></param>
  252. /// <param name="module"></param>
  253. public void AddRegionModule(string name, IRegionModuleBase module)
  254. {
  255. if (!RegionModules.ContainsKey(name))
  256. {
  257. RegionModules.Add(name, module);
  258. }
  259. }
  260. public void RemoveRegionModule(string name)
  261. {
  262. RegionModules.Remove(name);
  263. }
  264. /// <summary>
  265. /// Register a module commander.
  266. /// </summary>
  267. /// <param name="commander"></param>
  268. public void RegisterModuleCommander(ICommander commander)
  269. {
  270. lock (m_moduleCommanders)
  271. {
  272. m_moduleCommanders.Add(commander.Name, commander);
  273. }
  274. }
  275. /// <summary>
  276. /// Unregister a module commander and all its commands
  277. /// </summary>
  278. /// <param name="name"></param>
  279. public void UnregisterModuleCommander(string name)
  280. {
  281. lock (m_moduleCommanders)
  282. {
  283. ICommander commander;
  284. if (m_moduleCommanders.TryGetValue(name, out commander))
  285. m_moduleCommanders.Remove(name);
  286. }
  287. }
  288. /// <summary>
  289. /// Get a module commander
  290. /// </summary>
  291. /// <param name="name"></param>
  292. /// <returns>The module commander, null if no module commander with that name was found</returns>
  293. public ICommander GetCommander(string name)
  294. {
  295. lock (m_moduleCommanders)
  296. {
  297. if (m_moduleCommanders.ContainsKey(name))
  298. return m_moduleCommanders[name];
  299. }
  300. return null;
  301. }
  302. public Dictionary<string, ICommander> GetCommanders()
  303. {
  304. return m_moduleCommanders;
  305. }
  306. /// <summary>
  307. /// Register an interface to a region module. This allows module methods to be called directly as
  308. /// well as via events. If there is already a module registered for this interface, it is not replaced
  309. /// (is this the best behaviour?)
  310. /// </summary>
  311. /// <param name="mod"></param>
  312. public void RegisterModuleInterface<M>(M mod)
  313. {
  314. // m_log.DebugFormat("[SCENE BASE]: Registering interface {0}", typeof(M));
  315. List<Object> l = null;
  316. if (!ModuleInterfaces.TryGetValue(typeof(M), out l))
  317. {
  318. l = new List<Object>();
  319. ModuleInterfaces.Add(typeof(M), l);
  320. }
  321. if (l.Count > 0)
  322. return;
  323. l.Add(mod);
  324. if (mod is IEntityCreator)
  325. {
  326. IEntityCreator entityCreator = (IEntityCreator)mod;
  327. foreach (PCode pcode in entityCreator.CreationCapabilities)
  328. {
  329. m_entityCreators[pcode] = entityCreator;
  330. }
  331. }
  332. }
  333. public void UnregisterModuleInterface<M>(M mod)
  334. {
  335. List<Object> l;
  336. if (ModuleInterfaces.TryGetValue(typeof(M), out l))
  337. {
  338. if (l.Remove(mod))
  339. {
  340. if (mod is IEntityCreator)
  341. {
  342. IEntityCreator entityCreator = (IEntityCreator)mod;
  343. foreach (PCode pcode in entityCreator.CreationCapabilities)
  344. {
  345. m_entityCreators[pcode] = null;
  346. }
  347. }
  348. }
  349. }
  350. }
  351. public void StackModuleInterface<M>(M mod)
  352. {
  353. List<Object> l;
  354. if (ModuleInterfaces.ContainsKey(typeof(M)))
  355. l = ModuleInterfaces[typeof(M)];
  356. else
  357. l = new List<Object>();
  358. if (l.Contains(mod))
  359. return;
  360. l.Add(mod);
  361. if (mod is IEntityCreator)
  362. {
  363. IEntityCreator entityCreator = (IEntityCreator)mod;
  364. foreach (PCode pcode in entityCreator.CreationCapabilities)
  365. {
  366. m_entityCreators[pcode] = entityCreator;
  367. }
  368. }
  369. ModuleInterfaces[typeof(M)] = l;
  370. }
  371. /// <summary>
  372. /// For the given interface, retrieve the region module which implements it.
  373. /// </summary>
  374. /// <returns>null if there is no registered module implementing that interface</returns>
  375. public T RequestModuleInterface<T>()
  376. {
  377. if (ModuleInterfaces.ContainsKey(typeof(T)) &&
  378. (ModuleInterfaces[typeof(T)].Count > 0))
  379. return (T)ModuleInterfaces[typeof(T)][0];
  380. else
  381. return default(T);
  382. }
  383. /// <summary>
  384. /// For the given interface, retrieve an array of region modules that implement it.
  385. /// </summary>
  386. /// <returns>an empty array if there are no registered modules implementing that interface</returns>
  387. public T[] RequestModuleInterfaces<T>()
  388. {
  389. if (ModuleInterfaces.ContainsKey(typeof(T)))
  390. {
  391. List<T> ret = new List<T>();
  392. foreach (Object o in ModuleInterfaces[typeof(T)])
  393. ret.Add((T)o);
  394. return ret.ToArray();
  395. }
  396. else
  397. {
  398. return new T[] { default(T) };
  399. }
  400. }
  401. #endregion
  402. /// <summary>
  403. /// Shows various details about the sim based on the parameters supplied by the console command in openSimMain.
  404. /// </summary>
  405. /// <param name="showParams">What to show</param>
  406. public virtual void Show(string[] showParams)
  407. {
  408. switch (showParams[0])
  409. {
  410. case "modules":
  411. m_log.Error("The currently loaded modules in " + RegionInfo.RegionName + " are:");
  412. foreach (IRegionModule module in Modules.Values)
  413. {
  414. if (!module.IsSharedModule)
  415. {
  416. m_log.Error("Region Module: " + module.Name);
  417. }
  418. }
  419. break;
  420. }
  421. }
  422. /// <summary>
  423. /// Call this from a region module to add a command to the OpenSim console.
  424. /// </summary>
  425. /// <param name="mod"></param>
  426. /// <param name="command"></param>
  427. /// <param name="shorthelp"></param>
  428. /// <param name="longhelp"></param>
  429. /// <param name="callback"></param>
  430. public void AddCommand(object mod, string command, string shorthelp, string longhelp, CommandDelegate callback)
  431. {
  432. AddCommand(mod, command, shorthelp, longhelp, string.Empty, callback);
  433. }
  434. /// <summary>
  435. /// Call this from a region module to add a command to the OpenSim console.
  436. /// </summary>
  437. /// <param name="mod"></param>
  438. /// <param name="command"></param>
  439. /// <param name="shorthelp"></param>
  440. /// <param name="longhelp"></param>
  441. /// <param name="descriptivehelp"></param>
  442. /// <param name="callback"></param>
  443. public void AddCommand(
  444. object mod, string command, string shorthelp, string longhelp, string descriptivehelp, CommandDelegate callback)
  445. {
  446. if (MainConsole.Instance == null)
  447. return;
  448. string modulename = String.Empty;
  449. bool shared = false;
  450. if (mod != null)
  451. {
  452. if (mod is IRegionModule)
  453. {
  454. IRegionModule module = (IRegionModule)mod;
  455. modulename = module.Name;
  456. shared = module.IsSharedModule;
  457. }
  458. else if (mod is IRegionModuleBase)
  459. {
  460. IRegionModuleBase module = (IRegionModuleBase)mod;
  461. modulename = module.Name;
  462. shared = mod is ISharedRegionModule;
  463. }
  464. else throw new Exception("AddCommand module parameter must be IRegionModule or IRegionModuleBase");
  465. }
  466. MainConsole.Instance.Commands.AddCommand(
  467. modulename, shared, command, shorthelp, longhelp, descriptivehelp, callback);
  468. }
  469. public virtual ISceneObject DeserializeObject(string representation)
  470. {
  471. return null;
  472. }
  473. public virtual bool AllowScriptCrossings
  474. {
  475. get { return false; }
  476. }
  477. public abstract bool CheckClient(UUID agentID, System.Net.IPEndPoint ep);
  478. }
  479. }