SceneBase.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 string Name { get { return RegionInfo.RegionName; } }
  48. public IConfigSource Config
  49. {
  50. get { return GetConfig(); }
  51. }
  52. protected virtual IConfigSource GetConfig()
  53. {
  54. return null;
  55. }
  56. /// <value>
  57. /// All the region modules attached to this scene.
  58. /// </value>
  59. public Dictionary<string, IRegionModule> Modules
  60. {
  61. get { return m_modules; }
  62. }
  63. protected Dictionary<string, IRegionModule> m_modules = new Dictionary<string, IRegionModule>();
  64. public Dictionary<string, IRegionModuleBase> RegionModules
  65. {
  66. get { return m_regionModules; }
  67. }
  68. private Dictionary<string, IRegionModuleBase> m_regionModules = new Dictionary<string, IRegionModuleBase>();
  69. /// <value>
  70. /// The module interfaces available from this scene.
  71. /// </value>
  72. protected Dictionary<Type, List<object>> ModuleInterfaces = new Dictionary<Type, List<object>>();
  73. protected Dictionary<string, object> ModuleAPIMethods = new Dictionary<string, object>();
  74. /// <value>
  75. /// The module commanders available from this scene
  76. /// </value>
  77. protected Dictionary<string, ICommander> m_moduleCommanders = new Dictionary<string, ICommander>();
  78. /// <value>
  79. /// Registered classes that are capable of creating entities.
  80. /// </value>
  81. protected Dictionary<PCode, IEntityCreator> m_entityCreators = new Dictionary<PCode, IEntityCreator>();
  82. /// <summary>
  83. /// The last allocated local prim id. When a new local id is requested, the next number in the sequence is
  84. /// dispensed.
  85. /// </summary>
  86. protected uint m_lastAllocatedLocalId = 720000;
  87. private readonly Mutex _primAllocateMutex = new Mutex(false);
  88. protected readonly ClientManager m_clientManager = new ClientManager();
  89. public bool LoginsEnabled
  90. {
  91. get
  92. {
  93. return m_loginsEnabled;
  94. }
  95. set
  96. {
  97. if (m_loginsEnabled != value)
  98. {
  99. m_loginsEnabled = value;
  100. EventManager.TriggerRegionLoginsStatusChange(this);
  101. }
  102. }
  103. }
  104. private bool m_loginsEnabled;
  105. public bool Ready
  106. {
  107. get
  108. {
  109. return m_ready;
  110. }
  111. set
  112. {
  113. if (m_ready != value)
  114. {
  115. m_ready = value;
  116. EventManager.TriggerRegionReadyStatusChange(this);
  117. }
  118. }
  119. }
  120. private bool m_ready;
  121. public float TimeDilation
  122. {
  123. get { return 1.0f; }
  124. }
  125. protected ulong m_regionHandle;
  126. protected string m_regionName;
  127. protected RegionInfo m_regInfo;
  128. public ITerrainChannel Heightmap;
  129. /// <value>
  130. /// Allows retrieval of land information for this scene.
  131. /// </value>
  132. public ILandChannel LandChannel;
  133. /// <value>
  134. /// Manage events that occur in this scene (avatar movement, script rez, etc.). Commonly used by region modules
  135. /// to subscribe to scene events.
  136. /// </value>
  137. public EventManager EventManager
  138. {
  139. get { return m_eventManager; }
  140. }
  141. protected EventManager m_eventManager;
  142. protected ScenePermissions m_permissions;
  143. public ScenePermissions Permissions
  144. {
  145. get { return m_permissions; }
  146. }
  147. /* Used by the loadbalancer plugin on GForge */
  148. protected RegionStatus m_regStatus;
  149. public RegionStatus RegionStatus
  150. {
  151. get { return m_regStatus; }
  152. set { m_regStatus = value; }
  153. }
  154. #endregion
  155. public SceneBase(RegionInfo regInfo)
  156. {
  157. RegionInfo = regInfo;
  158. }
  159. #region Update Methods
  160. /// <summary>
  161. /// Called to update the scene loop by a number of frames and until shutdown.
  162. /// </summary>
  163. /// <param name="frames">
  164. /// Number of frames to update. Exits on shutdown even if there are frames remaining.
  165. /// If -1 then updates until shutdown.
  166. /// </param>
  167. public abstract void Update(int frames);
  168. #endregion
  169. #region Terrain Methods
  170. /// <summary>
  171. /// Loads the World heightmap
  172. /// </summary>
  173. public abstract void LoadWorldMap();
  174. /// <summary>
  175. /// Send the region heightmap to the client
  176. /// </summary>
  177. /// <param name="RemoteClient">Client to send to</param>
  178. public virtual void SendLayerData(IClientAPI RemoteClient)
  179. {
  180. RemoteClient.SendLayerData(Heightmap.GetFloatsSerialised());
  181. }
  182. #endregion
  183. #region Add/Remove Agent/Avatar
  184. public abstract ISceneAgent AddNewClient(IClientAPI client, PresenceType type);
  185. public abstract void RemoveClient(UUID agentID, bool closeChildAgents);
  186. public bool TryGetScenePresence(UUID agentID, out object scenePresence)
  187. {
  188. scenePresence = null;
  189. ScenePresence sp = null;
  190. if (TryGetScenePresence(agentID, out sp))
  191. {
  192. scenePresence = sp;
  193. return true;
  194. }
  195. return false;
  196. }
  197. /// <summary>
  198. /// Try to get a scene presence from the scene
  199. /// </summary>
  200. /// <param name="agentID"></param>
  201. /// <param name="scenePresence">null if there is no scene presence with the given agent id</param>
  202. /// <returns>true if there was a scene presence with the given id, false otherwise.</returns>
  203. public abstract bool TryGetScenePresence(UUID agentID, out ScenePresence scenePresence);
  204. #endregion
  205. /// <summary>
  206. ///
  207. /// </summary>
  208. /// <returns></returns>
  209. public virtual RegionInfo RegionInfo { get; private set; }
  210. #region admin stuff
  211. public abstract void OtherRegionUp(GridRegion otherRegion);
  212. public virtual string GetSimulatorVersion()
  213. {
  214. return "OpenSimulator Server";
  215. }
  216. #endregion
  217. #region Shutdown
  218. /// <summary>
  219. /// Tidy before shutdown
  220. /// </summary>
  221. public virtual void Close()
  222. {
  223. // Shut down all non shared modules.
  224. foreach (IRegionModule module in Modules.Values)
  225. {
  226. if (!module.IsSharedModule)
  227. {
  228. module.Close();
  229. }
  230. }
  231. Modules.Clear();
  232. try
  233. {
  234. EventManager.TriggerShutdown();
  235. }
  236. catch (Exception e)
  237. {
  238. m_log.Error(string.Format("[SCENE]: SceneBase.cs: Close() - Failed with exception ", e));
  239. }
  240. }
  241. #endregion
  242. /// <summary>
  243. /// Returns a new unallocated local ID
  244. /// </summary>
  245. /// <returns>A brand new local ID</returns>
  246. public uint AllocateLocalId()
  247. {
  248. uint myID;
  249. _primAllocateMutex.WaitOne();
  250. myID = ++m_lastAllocatedLocalId;
  251. _primAllocateMutex.ReleaseMutex();
  252. return myID;
  253. }
  254. #region Module Methods
  255. /// <summary>
  256. /// Add a module to this scene.
  257. /// </summary>
  258. /// <param name="name"></param>
  259. /// <param name="module"></param>
  260. public void AddModule(string name, IRegionModule module)
  261. {
  262. if (!Modules.ContainsKey(name))
  263. {
  264. Modules.Add(name, module);
  265. }
  266. }
  267. /// <summary>
  268. /// Add a region-module to this scene. TODO: This will replace AddModule in the future.
  269. /// </summary>
  270. /// <param name="name"></param>
  271. /// <param name="module"></param>
  272. public void AddRegionModule(string name, IRegionModuleBase module)
  273. {
  274. if (!RegionModules.ContainsKey(name))
  275. {
  276. RegionModules.Add(name, module);
  277. }
  278. }
  279. public void RemoveRegionModule(string name)
  280. {
  281. RegionModules.Remove(name);
  282. }
  283. /// <summary>
  284. /// Register a module commander.
  285. /// </summary>
  286. /// <param name="commander"></param>
  287. public void RegisterModuleCommander(ICommander commander)
  288. {
  289. lock (m_moduleCommanders)
  290. {
  291. m_moduleCommanders.Add(commander.Name, commander);
  292. }
  293. }
  294. /// <summary>
  295. /// Unregister a module commander and all its commands
  296. /// </summary>
  297. /// <param name="name"></param>
  298. public void UnregisterModuleCommander(string name)
  299. {
  300. lock (m_moduleCommanders)
  301. {
  302. ICommander commander;
  303. if (m_moduleCommanders.TryGetValue(name, out commander))
  304. m_moduleCommanders.Remove(name);
  305. }
  306. }
  307. /// <summary>
  308. /// Get a module commander
  309. /// </summary>
  310. /// <param name="name"></param>
  311. /// <returns>The module commander, null if no module commander with that name was found</returns>
  312. public ICommander GetCommander(string name)
  313. {
  314. lock (m_moduleCommanders)
  315. {
  316. if (m_moduleCommanders.ContainsKey(name))
  317. return m_moduleCommanders[name];
  318. }
  319. return null;
  320. }
  321. public Dictionary<string, ICommander> GetCommanders()
  322. {
  323. return m_moduleCommanders;
  324. }
  325. /// <summary>
  326. /// Register an interface to a region module. This allows module methods to be called directly as
  327. /// well as via events. If there is already a module registered for this interface, it is not replaced
  328. /// (is this the best behaviour?)
  329. /// </summary>
  330. /// <param name="mod"></param>
  331. public void RegisterModuleInterface<M>(M mod)
  332. {
  333. // m_log.DebugFormat("[SCENE BASE]: Registering interface {0}", typeof(M));
  334. List<Object> l = null;
  335. if (!ModuleInterfaces.TryGetValue(typeof(M), out l))
  336. {
  337. l = new List<Object>();
  338. ModuleInterfaces.Add(typeof(M), l);
  339. }
  340. if (l.Count > 0)
  341. return;
  342. l.Add(mod);
  343. if (mod is IEntityCreator)
  344. {
  345. IEntityCreator entityCreator = (IEntityCreator)mod;
  346. foreach (PCode pcode in entityCreator.CreationCapabilities)
  347. {
  348. m_entityCreators[pcode] = entityCreator;
  349. }
  350. }
  351. }
  352. public void UnregisterModuleInterface<M>(M mod)
  353. {
  354. List<Object> l;
  355. if (ModuleInterfaces.TryGetValue(typeof(M), out l))
  356. {
  357. if (l.Remove(mod))
  358. {
  359. if (mod is IEntityCreator)
  360. {
  361. IEntityCreator entityCreator = (IEntityCreator)mod;
  362. foreach (PCode pcode in entityCreator.CreationCapabilities)
  363. {
  364. m_entityCreators[pcode] = null;
  365. }
  366. }
  367. }
  368. }
  369. }
  370. public void StackModuleInterface<M>(M mod)
  371. {
  372. List<Object> l;
  373. if (ModuleInterfaces.ContainsKey(typeof(M)))
  374. l = ModuleInterfaces[typeof(M)];
  375. else
  376. l = new List<Object>();
  377. if (l.Contains(mod))
  378. return;
  379. l.Add(mod);
  380. if (mod is IEntityCreator)
  381. {
  382. IEntityCreator entityCreator = (IEntityCreator)mod;
  383. foreach (PCode pcode in entityCreator.CreationCapabilities)
  384. {
  385. m_entityCreators[pcode] = entityCreator;
  386. }
  387. }
  388. ModuleInterfaces[typeof(M)] = l;
  389. }
  390. /// <summary>
  391. /// For the given interface, retrieve the region module which implements it.
  392. /// </summary>
  393. /// <returns>null if there is no registered module implementing that interface</returns>
  394. public T RequestModuleInterface<T>()
  395. {
  396. if (ModuleInterfaces.ContainsKey(typeof(T)) &&
  397. (ModuleInterfaces[typeof(T)].Count > 0))
  398. return (T)ModuleInterfaces[typeof(T)][0];
  399. else
  400. return default(T);
  401. }
  402. /// <summary>
  403. /// For the given interface, retrieve an array of region modules that implement it.
  404. /// </summary>
  405. /// <returns>an empty array if there are no registered modules implementing that interface</returns>
  406. public T[] RequestModuleInterfaces<T>()
  407. {
  408. if (ModuleInterfaces.ContainsKey(typeof(T)))
  409. {
  410. List<T> ret = new List<T>();
  411. foreach (Object o in ModuleInterfaces[typeof(T)])
  412. ret.Add((T)o);
  413. return ret.ToArray();
  414. }
  415. else
  416. {
  417. return new T[] {};
  418. }
  419. }
  420. #endregion
  421. /// <summary>
  422. /// Call this from a region module to add a command to the OpenSim console.
  423. /// </summary>
  424. /// <param name="mod"></param>
  425. /// <param name="command"></param>
  426. /// <param name="shorthelp"></param>
  427. /// <param name="longhelp"></param>
  428. /// <param name="callback"></param>
  429. public void AddCommand(object mod, string command, string shorthelp, string longhelp, CommandDelegate callback)
  430. {
  431. AddCommand(mod, command, shorthelp, longhelp, string.Empty, callback);
  432. }
  433. /// <summary>
  434. /// Call this from a region module to add a command to the OpenSim console.
  435. /// </summary>
  436. /// <param name="mod">
  437. /// The use of IRegionModuleBase is a cheap trick to get a different method signature,
  438. /// though all new modules should be using interfaces descended from IRegionModuleBase anyway.
  439. /// </param>
  440. /// <param name="category">
  441. /// Category of the command. This is the section under which it will appear when the user asks for help
  442. /// </param>
  443. /// <param name="command"></param>
  444. /// <param name="shorthelp"></param>
  445. /// <param name="longhelp"></param>
  446. /// <param name="callback"></param>
  447. public void AddCommand(
  448. string category, object mod, string command, string shorthelp, string longhelp, CommandDelegate callback)
  449. {
  450. AddCommand(category, mod, command, shorthelp, longhelp, string.Empty, callback);
  451. }
  452. /// <summary>
  453. /// Call this from a region module to add a command to the OpenSim console.
  454. /// </summary>
  455. /// <param name="mod"></param>
  456. /// <param name="command"></param>
  457. /// <param name="shorthelp"></param>
  458. /// <param name="longhelp"></param>
  459. /// <param name="descriptivehelp"></param>
  460. /// <param name="callback"></param>
  461. public void AddCommand(object mod, string command, string shorthelp, string longhelp, string descriptivehelp, CommandDelegate callback)
  462. {
  463. string moduleName = "";
  464. if (mod != null)
  465. {
  466. if (mod is IRegionModule)
  467. {
  468. IRegionModule module = (IRegionModule)mod;
  469. moduleName = module.Name;
  470. }
  471. else if (mod is IRegionModuleBase)
  472. {
  473. IRegionModuleBase module = (IRegionModuleBase)mod;
  474. moduleName = module.Name;
  475. }
  476. else
  477. {
  478. throw new Exception("AddCommand module parameter must be IRegionModule or IRegionModuleBase");
  479. }
  480. }
  481. AddCommand(moduleName, mod, command, shorthelp, longhelp, descriptivehelp, callback);
  482. }
  483. /// <summary>
  484. /// Call this from a region module to add a command to the OpenSim console.
  485. /// </summary>
  486. /// <param name="category">
  487. /// Category of the command. This is the section under which it will appear when the user asks for help
  488. /// </param>
  489. /// <param name="mod"></param>
  490. /// <param name="command"></param>
  491. /// <param name="shorthelp"></param>
  492. /// <param name="longhelp"></param>
  493. /// <param name="descriptivehelp"></param>
  494. /// <param name="callback"></param>
  495. public void AddCommand(
  496. string category, object mod, string command,
  497. string shorthelp, string longhelp, string descriptivehelp, CommandDelegate callback)
  498. {
  499. if (MainConsole.Instance == null)
  500. return;
  501. bool shared = false;
  502. if (mod != null)
  503. {
  504. if (mod is IRegionModule)
  505. {
  506. IRegionModule module = (IRegionModule)mod;
  507. shared = module.IsSharedModule;
  508. }
  509. else if (mod is IRegionModuleBase)
  510. {
  511. shared = mod is ISharedRegionModule;
  512. }
  513. else
  514. {
  515. throw new Exception("AddCommand module parameter must be IRegionModule or IRegionModuleBase");
  516. }
  517. }
  518. MainConsole.Instance.Commands.AddCommand(
  519. category, shared, command, shorthelp, longhelp, descriptivehelp, callback);
  520. }
  521. public virtual ISceneObject DeserializeObject(string representation)
  522. {
  523. return null;
  524. }
  525. public virtual bool AllowScriptCrossings
  526. {
  527. get { return false; }
  528. }
  529. public void Restart()
  530. {
  531. // This has to be here to fire the event
  532. restart handlerPhysicsCrash = OnRestart;
  533. if (handlerPhysicsCrash != null)
  534. handlerPhysicsCrash(RegionInfo);
  535. }
  536. public abstract bool CheckClient(UUID agentID, System.Net.IPEndPoint ep);
  537. }
  538. }