SceneBase.cs 21 KB

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