SceneBase.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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, IRegionModuleBase> RegionModules
  60. {
  61. get { return m_regionModules; }
  62. }
  63. private Dictionary<string, IRegionModuleBase> m_regionModules = new Dictionary<string, IRegionModuleBase>();
  64. /// <value>
  65. /// The module interfaces available from this scene.
  66. /// </value>
  67. protected Dictionary<Type, List<object>> ModuleInterfaces = new Dictionary<Type, List<object>>();
  68. protected Dictionary<string, object> ModuleAPIMethods = new Dictionary<string, object>();
  69. /// <value>
  70. /// The module commanders available from this scene
  71. /// </value>
  72. protected Dictionary<string, ICommander> m_moduleCommanders = new Dictionary<string, ICommander>();
  73. /// <value>
  74. /// Registered classes that are capable of creating entities.
  75. /// </value>
  76. protected Dictionary<PCode, IEntityCreator> m_entityCreators = new Dictionary<PCode, IEntityCreator>();
  77. /// <summary>
  78. /// The last allocated local prim id. When a new local id is requested, the next number in the sequence is
  79. /// dispensed.
  80. /// </summary>
  81. protected uint m_lastAllocatedLocalId = 720000;
  82. private readonly Mutex _primAllocateMutex = new Mutex(false);
  83. protected readonly ClientManager m_clientManager = new ClientManager();
  84. public bool LoginsEnabled
  85. {
  86. get
  87. {
  88. return m_loginsEnabled;
  89. }
  90. set
  91. {
  92. if (m_loginsEnabled != value)
  93. {
  94. m_loginsEnabled = value;
  95. EventManager.TriggerRegionLoginsStatusChange(this);
  96. }
  97. }
  98. }
  99. private bool m_loginsEnabled;
  100. public bool Ready
  101. {
  102. get
  103. {
  104. return m_ready;
  105. }
  106. set
  107. {
  108. if (m_ready != value)
  109. {
  110. m_ready = value;
  111. EventManager.TriggerRegionReadyStatusChange(this);
  112. }
  113. }
  114. }
  115. private bool m_ready;
  116. public float TimeDilation
  117. {
  118. get { return 1.0f; }
  119. }
  120. protected ulong m_regionHandle;
  121. protected string m_regionName;
  122. protected RegionInfo m_regInfo;
  123. public ITerrainChannel Heightmap;
  124. /// <value>
  125. /// Allows retrieval of land information for this scene.
  126. /// </value>
  127. public ILandChannel LandChannel;
  128. /// <value>
  129. /// Manage events that occur in this scene (avatar movement, script rez, etc.). Commonly used by region modules
  130. /// to subscribe to scene events.
  131. /// </value>
  132. public EventManager EventManager
  133. {
  134. get { return m_eventManager; }
  135. }
  136. protected EventManager m_eventManager;
  137. protected ScenePermissions m_permissions;
  138. public ScenePermissions Permissions
  139. {
  140. get { return m_permissions; }
  141. }
  142. /* Used by the loadbalancer plugin on GForge */
  143. protected RegionStatus m_regStatus;
  144. public RegionStatus RegionStatus
  145. {
  146. get { return m_regStatus; }
  147. set { m_regStatus = value; }
  148. }
  149. #endregion
  150. public SceneBase(RegionInfo regInfo)
  151. {
  152. RegionInfo = regInfo;
  153. }
  154. #region Update Methods
  155. /// <summary>
  156. /// Called to update the scene loop by a number of frames and until shutdown.
  157. /// </summary>
  158. /// <param name="frames">
  159. /// Number of frames to update. Exits on shutdown even if there are frames remaining.
  160. /// If -1 then updates until shutdown.
  161. /// </param>
  162. public abstract void Update(int frames);
  163. #endregion
  164. #region Terrain Methods
  165. /// <summary>
  166. /// Loads the World heightmap
  167. /// </summary>
  168. public abstract void LoadWorldMap();
  169. /// <summary>
  170. /// Send the region heightmap to the client
  171. /// </summary>
  172. /// <param name="RemoteClient">Client to send to</param>
  173. public virtual void SendLayerData(IClientAPI RemoteClient)
  174. {
  175. RemoteClient.SendLayerData(Heightmap.GetFloatsSerialised());
  176. }
  177. #endregion
  178. #region Add/Remove Agent/Avatar
  179. public abstract ISceneAgent AddNewClient(IClientAPI client, PresenceType type);
  180. /// <summary>
  181. /// Remove the given client from the scene.
  182. /// </summary>
  183. /// <remarks>
  184. /// Only clientstack code should call this directly. All other code should call IncomingCloseAgent() instead
  185. /// to properly operate the state machine and avoid race conditions with other close requests (such as directly
  186. /// from viewers).
  187. /// </remarks>
  188. /// <param name='agentID'>ID of agent to close</param>
  189. /// <param name='closeChildAgents'>
  190. /// Close the neighbour child agents associated with this client.
  191. /// </param>
  192. public abstract void RemoveClient(UUID agentID, bool closeChildAgents);
  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. /// <summary>
  312. /// Register an interface to a region module. This allows module methods to be called directly as
  313. /// well as via events. If there is already a module registered for this interface, it is not replaced
  314. /// (is this the best behaviour?)
  315. /// </summary>
  316. /// <param name="mod"></param>
  317. public void RegisterModuleInterface<M>(M mod)
  318. {
  319. // m_log.DebugFormat("[SCENE BASE]: Registering interface {0}", typeof(M));
  320. List<Object> l = null;
  321. if (!ModuleInterfaces.TryGetValue(typeof(M), out l))
  322. {
  323. l = new List<Object>();
  324. ModuleInterfaces.Add(typeof(M), l);
  325. }
  326. if (l.Count > 0)
  327. return;
  328. l.Add(mod);
  329. if (mod is IEntityCreator)
  330. {
  331. IEntityCreator entityCreator = (IEntityCreator)mod;
  332. foreach (PCode pcode in entityCreator.CreationCapabilities)
  333. {
  334. m_entityCreators[pcode] = entityCreator;
  335. }
  336. }
  337. }
  338. public void UnregisterModuleInterface<M>(M mod)
  339. {
  340. List<Object> l;
  341. if (ModuleInterfaces.TryGetValue(typeof(M), out l))
  342. {
  343. if (l.Remove(mod))
  344. {
  345. if (mod is IEntityCreator)
  346. {
  347. IEntityCreator entityCreator = (IEntityCreator)mod;
  348. foreach (PCode pcode in entityCreator.CreationCapabilities)
  349. {
  350. m_entityCreators[pcode] = null;
  351. }
  352. }
  353. }
  354. }
  355. }
  356. public void StackModuleInterface<M>(M mod)
  357. {
  358. List<Object> l;
  359. if (ModuleInterfaces.ContainsKey(typeof(M)))
  360. l = ModuleInterfaces[typeof(M)];
  361. else
  362. l = new List<Object>();
  363. if (l.Contains(mod))
  364. return;
  365. l.Add(mod);
  366. if (mod is IEntityCreator)
  367. {
  368. IEntityCreator entityCreator = (IEntityCreator)mod;
  369. foreach (PCode pcode in entityCreator.CreationCapabilities)
  370. {
  371. m_entityCreators[pcode] = entityCreator;
  372. }
  373. }
  374. ModuleInterfaces[typeof(M)] = l;
  375. }
  376. /// <summary>
  377. /// For the given interface, retrieve the region module which implements it.
  378. /// </summary>
  379. /// <returns>null if there is no registered module implementing that interface</returns>
  380. public T RequestModuleInterface<T>()
  381. {
  382. if (ModuleInterfaces.ContainsKey(typeof(T)) &&
  383. (ModuleInterfaces[typeof(T)].Count > 0))
  384. return (T)ModuleInterfaces[typeof(T)][0];
  385. else
  386. return default(T);
  387. }
  388. /// <summary>
  389. /// For the given interface, retrieve an array of region modules that implement it.
  390. /// </summary>
  391. /// <returns>an empty array if there are no registered modules implementing that interface</returns>
  392. public T[] RequestModuleInterfaces<T>()
  393. {
  394. if (ModuleInterfaces.ContainsKey(typeof(T)))
  395. {
  396. List<T> ret = new List<T>();
  397. foreach (Object o in ModuleInterfaces[typeof(T)])
  398. ret.Add((T)o);
  399. return ret.ToArray();
  400. }
  401. else
  402. {
  403. return new T[] {};
  404. }
  405. }
  406. #endregion
  407. /// <summary>
  408. /// Call this from a region module to add a command to the OpenSim console.
  409. /// </summary>
  410. /// <param name="mod"></param>
  411. /// <param name="command"></param>
  412. /// <param name="shorthelp"></param>
  413. /// <param name="longhelp"></param>
  414. /// <param name="callback"></param>
  415. public void AddCommand(IRegionModuleBase module, string command, string shorthelp, string longhelp, CommandDelegate callback)
  416. {
  417. AddCommand(module, command, shorthelp, longhelp, string.Empty, callback);
  418. }
  419. /// <summary>
  420. /// Call this from a region module to add a command to the OpenSim console.
  421. /// </summary>
  422. /// <param name="mod">
  423. /// The use of IRegionModuleBase is a cheap trick to get a different method signature,
  424. /// though all new modules should be using interfaces descended from IRegionModuleBase anyway.
  425. /// </param>
  426. /// <param name="category">
  427. /// Category of the command. This is the section under which it will appear when the user asks for help
  428. /// </param>
  429. /// <param name="command"></param>
  430. /// <param name="shorthelp"></param>
  431. /// <param name="longhelp"></param>
  432. /// <param name="callback"></param>
  433. public void AddCommand(
  434. string category, IRegionModuleBase module, string command, string shorthelp, string longhelp, CommandDelegate callback)
  435. {
  436. AddCommand(category, module, command, shorthelp, longhelp, string.Empty, callback);
  437. }
  438. /// <summary>
  439. /// Call this from a region module to add a command to the OpenSim console.
  440. /// </summary>
  441. /// <param name="mod"></param>
  442. /// <param name="command"></param>
  443. /// <param name="shorthelp"></param>
  444. /// <param name="longhelp"></param>
  445. /// <param name="descriptivehelp"></param>
  446. /// <param name="callback"></param>
  447. public void AddCommand(IRegionModuleBase module, string command, string shorthelp, string longhelp, string descriptivehelp, CommandDelegate callback)
  448. {
  449. string moduleName = "";
  450. if (module != null)
  451. moduleName = module.Name;
  452. AddCommand(moduleName, module, command, shorthelp, longhelp, descriptivehelp, callback);
  453. }
  454. /// <summary>
  455. /// Call this from a region module to add a command to the OpenSim console.
  456. /// </summary>
  457. /// <param name="category">
  458. /// Category of the command. This is the section under which it will appear when the user asks for help
  459. /// </param>
  460. /// <param name="mod"></param>
  461. /// <param name="command"></param>
  462. /// <param name="shorthelp"></param>
  463. /// <param name="longhelp"></param>
  464. /// <param name="descriptivehelp"></param>
  465. /// <param name="callback"></param>
  466. public void AddCommand(
  467. string category, IRegionModuleBase module, string command,
  468. string shorthelp, string longhelp, string descriptivehelp, CommandDelegate callback)
  469. {
  470. if (MainConsole.Instance == null)
  471. return;
  472. bool shared = false;
  473. if (module != null)
  474. shared = module is ISharedRegionModule;
  475. MainConsole.Instance.Commands.AddCommand(
  476. category, shared, command, shorthelp, longhelp, descriptivehelp, callback);
  477. }
  478. public virtual ISceneObject DeserializeObject(string representation)
  479. {
  480. return null;
  481. }
  482. public virtual bool AllowScriptCrossings
  483. {
  484. get { return false; }
  485. }
  486. public virtual void Start()
  487. {
  488. }
  489. public void Restart()
  490. {
  491. // This has to be here to fire the event
  492. restart handlerPhysicsCrash = OnRestart;
  493. if (handlerPhysicsCrash != null)
  494. handlerPhysicsCrash(RegionInfo);
  495. }
  496. public abstract bool CheckClient(UUID agentID, System.Net.IPEndPoint ep);
  497. }
  498. }