1
0

SceneBase.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. protected Dictionary<string, object> ModuleAPIMethods = new Dictionary<string, object>();
  72. /// <value>
  73. /// The module commanders available from this scene
  74. /// </value>
  75. protected Dictionary<string, ICommander> m_moduleCommanders = new Dictionary<string, ICommander>();
  76. /// <value>
  77. /// Registered classes that are capable of creating entities.
  78. /// </value>
  79. protected Dictionary<PCode, IEntityCreator> m_entityCreators = new Dictionary<PCode, IEntityCreator>();
  80. /// <summary>
  81. /// The last allocated local prim id. When a new local id is requested, the next number in the sequence is
  82. /// dispensed.
  83. /// </summary>
  84. protected uint m_lastAllocatedLocalId = 720000;
  85. private readonly Mutex _primAllocateMutex = new Mutex(false);
  86. protected readonly ClientManager m_clientManager = new ClientManager();
  87. public bool LoginsEnabled
  88. {
  89. get
  90. {
  91. return m_loginsEnabled;
  92. }
  93. set
  94. {
  95. if (m_loginsEnabled != value)
  96. {
  97. m_loginsEnabled = value;
  98. EventManager.TriggerRegionLoginsStatusChange(this);
  99. }
  100. }
  101. }
  102. private bool m_loginsEnabled;
  103. public bool Ready
  104. {
  105. get
  106. {
  107. return m_ready;
  108. }
  109. set
  110. {
  111. if (m_ready != value)
  112. {
  113. m_ready = value;
  114. EventManager.TriggerRegionReadyStatusChange(this);
  115. }
  116. }
  117. }
  118. private bool m_ready;
  119. public float TimeDilation
  120. {
  121. get { return 1.0f; }
  122. }
  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. /// <returns>true if update completed within minimum frame time, false otherwise.</returns>
  163. public abstract bool Update(int frames);
  164. #endregion
  165. #region Terrain Methods
  166. /// <summary>
  167. /// Loads the World heightmap
  168. /// </summary>
  169. public abstract void LoadWorldMap();
  170. /// <summary>
  171. /// Send the region heightmap to the client
  172. /// </summary>
  173. /// <param name="RemoteClient">Client to send to</param>
  174. public virtual void SendLayerData(IClientAPI RemoteClient)
  175. {
  176. // RemoteClient.SendLayerData(Heightmap.GetFloatsSerialised());
  177. ITerrainModule terrModule = RequestModuleInterface<ITerrainModule>();
  178. if (terrModule != null)
  179. {
  180. terrModule.PushTerrain(RemoteClient);
  181. }
  182. }
  183. #endregion
  184. #region Add/Remove Agent/Avatar
  185. public abstract ISceneAgent AddNewAgent(IClientAPI client, PresenceType type);
  186. public abstract bool CloseAgent(UUID agentID, bool force);
  187. public bool TryGetScenePresence(UUID agentID, out object scenePresence)
  188. {
  189. scenePresence = null;
  190. ScenePresence sp = null;
  191. if (TryGetScenePresence(agentID, out sp))
  192. {
  193. scenePresence = sp;
  194. return true;
  195. }
  196. return false;
  197. }
  198. /// <summary>
  199. /// Try to get a scene presence from the scene
  200. /// </summary>
  201. /// <param name="agentID"></param>
  202. /// <param name="scenePresence">null if there is no scene presence with the given agent id</param>
  203. /// <returns>true if there was a scene presence with the given id, false otherwise.</returns>
  204. public abstract bool TryGetScenePresence(UUID agentID, out ScenePresence scenePresence);
  205. #endregion
  206. /// <summary>
  207. ///
  208. /// </summary>
  209. /// <returns></returns>
  210. public virtual RegionInfo RegionInfo { get; private set; }
  211. #region admin stuff
  212. public abstract void OtherRegionUp(GridRegion otherRegion);
  213. public virtual string GetSimulatorVersion()
  214. {
  215. return "OpenSimulator Server";
  216. }
  217. #endregion
  218. #region Shutdown
  219. /// <summary>
  220. /// Tidy before shutdown
  221. /// </summary>
  222. public virtual void Close()
  223. {
  224. try
  225. {
  226. EventManager.TriggerShutdown();
  227. }
  228. catch (Exception e)
  229. {
  230. m_log.Error(string.Format("[SCENE]: SceneBase.cs: Close() - Failed with exception ", e));
  231. }
  232. }
  233. #endregion
  234. /// <summary>
  235. /// Returns a new unallocated local ID
  236. /// </summary>
  237. /// <returns>A brand new local ID</returns>
  238. public uint AllocateLocalId()
  239. {
  240. uint myID;
  241. _primAllocateMutex.WaitOne();
  242. myID = ++m_lastAllocatedLocalId;
  243. _primAllocateMutex.ReleaseMutex();
  244. return myID;
  245. }
  246. #region Module Methods
  247. /// <summary>
  248. /// Add a region-module to this scene. TODO: This will replace AddModule in the future.
  249. /// </summary>
  250. /// <param name="name"></param>
  251. /// <param name="module"></param>
  252. public void AddRegionModule(string name, IRegionModuleBase module)
  253. {
  254. if (!RegionModules.ContainsKey(name))
  255. {
  256. RegionModules.Add(name, module);
  257. }
  258. }
  259. public void RemoveRegionModule(string name)
  260. {
  261. RegionModules.Remove(name);
  262. }
  263. /// <summary>
  264. /// Register a module commander.
  265. /// </summary>
  266. /// <param name="commander"></param>
  267. public void RegisterModuleCommander(ICommander commander)
  268. {
  269. lock (m_moduleCommanders)
  270. {
  271. m_moduleCommanders.Add(commander.Name, commander);
  272. }
  273. }
  274. /// <summary>
  275. /// Unregister a module commander and all its commands
  276. /// </summary>
  277. /// <param name="name"></param>
  278. public void UnregisterModuleCommander(string name)
  279. {
  280. lock (m_moduleCommanders)
  281. {
  282. ICommander commander;
  283. if (m_moduleCommanders.TryGetValue(name, out commander))
  284. m_moduleCommanders.Remove(name);
  285. }
  286. }
  287. /// <summary>
  288. /// Get a module commander
  289. /// </summary>
  290. /// <param name="name"></param>
  291. /// <returns>The module commander, null if no module commander with that name was found</returns>
  292. public ICommander GetCommander(string name)
  293. {
  294. lock (m_moduleCommanders)
  295. {
  296. if (m_moduleCommanders.ContainsKey(name))
  297. return m_moduleCommanders[name];
  298. }
  299. return null;
  300. }
  301. public Dictionary<string, ICommander> GetCommanders()
  302. {
  303. return m_moduleCommanders;
  304. }
  305. /// <summary>
  306. /// Register an interface to a region module. This allows module methods to be called directly as
  307. /// well as via events. If there is already a module registered for this interface, it is not replaced
  308. /// (is this the best behaviour?)
  309. /// </summary>
  310. /// <param name="mod"></param>
  311. public void RegisterModuleInterface<M>(M mod)
  312. {
  313. // m_log.DebugFormat("[SCENE BASE]: Registering interface {0}", typeof(M));
  314. List<Object> l = null;
  315. if (!ModuleInterfaces.TryGetValue(typeof(M), out l))
  316. {
  317. l = new List<Object>();
  318. ModuleInterfaces.Add(typeof(M), l);
  319. }
  320. if (l.Count > 0)
  321. return;
  322. l.Add(mod);
  323. if (mod is IEntityCreator)
  324. {
  325. IEntityCreator entityCreator = (IEntityCreator)mod;
  326. foreach (PCode pcode in entityCreator.CreationCapabilities)
  327. {
  328. m_entityCreators[pcode] = entityCreator;
  329. }
  330. }
  331. }
  332. public void UnregisterModuleInterface<M>(M mod)
  333. {
  334. List<Object> l;
  335. if (ModuleInterfaces.TryGetValue(typeof(M), out l))
  336. {
  337. if (l.Remove(mod))
  338. {
  339. if (mod is IEntityCreator)
  340. {
  341. IEntityCreator entityCreator = (IEntityCreator)mod;
  342. foreach (PCode pcode in entityCreator.CreationCapabilities)
  343. {
  344. m_entityCreators[pcode] = null;
  345. }
  346. }
  347. }
  348. }
  349. }
  350. public void StackModuleInterface<M>(M mod)
  351. {
  352. List<Object> l;
  353. if (ModuleInterfaces.ContainsKey(typeof(M)))
  354. l = ModuleInterfaces[typeof(M)];
  355. else
  356. l = new List<Object>();
  357. if (l.Contains(mod))
  358. return;
  359. l.Add(mod);
  360. if (mod is IEntityCreator)
  361. {
  362. IEntityCreator entityCreator = (IEntityCreator)mod;
  363. foreach (PCode pcode in entityCreator.CreationCapabilities)
  364. {
  365. m_entityCreators[pcode] = entityCreator;
  366. }
  367. }
  368. ModuleInterfaces[typeof(M)] = l;
  369. }
  370. /// <summary>
  371. /// For the given interface, retrieve the region module which implements it.
  372. /// </summary>
  373. /// <returns>null if there is no registered module implementing that interface</returns>
  374. public T RequestModuleInterface<T>()
  375. {
  376. if (ModuleInterfaces.ContainsKey(typeof(T)) &&
  377. (ModuleInterfaces[typeof(T)].Count > 0))
  378. return (T)ModuleInterfaces[typeof(T)][0];
  379. else
  380. return default(T);
  381. }
  382. /// <summary>
  383. /// For the given interface, retrieve an array of region modules that implement it.
  384. /// </summary>
  385. /// <returns>an empty array if there are no registered modules implementing that interface</returns>
  386. public T[] RequestModuleInterfaces<T>()
  387. {
  388. if (ModuleInterfaces.ContainsKey(typeof(T)))
  389. {
  390. List<T> ret = new List<T>();
  391. foreach (Object o in ModuleInterfaces[typeof(T)])
  392. ret.Add((T)o);
  393. return ret.ToArray();
  394. }
  395. else
  396. {
  397. return new T[] {};
  398. }
  399. }
  400. #endregion
  401. /// <summary>
  402. /// Call this from a region module to add a command to the OpenSim console.
  403. /// </summary>
  404. /// <param name="mod"></param>
  405. /// <param name="command"></param>
  406. /// <param name="shorthelp"></param>
  407. /// <param name="longhelp"></param>
  408. /// <param name="callback"></param>
  409. public void AddCommand(IRegionModuleBase module, string command, string shorthelp, string longhelp, CommandDelegate callback)
  410. {
  411. AddCommand(module, command, shorthelp, longhelp, string.Empty, callback);
  412. }
  413. /// <summary>
  414. /// Call this from a region module to add a command to the OpenSim console.
  415. /// </summary>
  416. /// <param name="mod">
  417. /// The use of IRegionModuleBase is a cheap trick to get a different method signature,
  418. /// though all new modules should be using interfaces descended from IRegionModuleBase anyway.
  419. /// </param>
  420. /// <param name="category">
  421. /// Category of the command. This is the section under which it will appear when the user asks for help
  422. /// </param>
  423. /// <param name="command"></param>
  424. /// <param name="shorthelp"></param>
  425. /// <param name="longhelp"></param>
  426. /// <param name="callback"></param>
  427. public void AddCommand(
  428. string category, IRegionModuleBase module, string command, string shorthelp, string longhelp, CommandDelegate callback)
  429. {
  430. AddCommand(category, module, command, shorthelp, longhelp, string.Empty, callback);
  431. }
  432. /// <summary>
  433. /// Call this from a region module to add a command to the OpenSim console.
  434. /// </summary>
  435. /// <param name="mod"></param>
  436. /// <param name="command"></param>
  437. /// <param name="shorthelp"></param>
  438. /// <param name="longhelp"></param>
  439. /// <param name="descriptivehelp"></param>
  440. /// <param name="callback"></param>
  441. public void AddCommand(IRegionModuleBase module, string command, string shorthelp, string longhelp, string descriptivehelp, CommandDelegate callback)
  442. {
  443. string moduleName = "";
  444. if (module != null)
  445. moduleName = module.Name;
  446. AddCommand(moduleName, module, command, shorthelp, longhelp, descriptivehelp, callback);
  447. }
  448. /// <summary>
  449. /// Call this from a region module to add a command to the OpenSim console.
  450. /// </summary>
  451. /// <param name="category">
  452. /// Category of the command. This is the section under which it will appear when the user asks for help
  453. /// </param>
  454. /// <param name="mod"></param>
  455. /// <param name="command"></param>
  456. /// <param name="shorthelp"></param>
  457. /// <param name="longhelp"></param>
  458. /// <param name="descriptivehelp"></param>
  459. /// <param name="callback"></param>
  460. public void AddCommand(
  461. string category, IRegionModuleBase module, string command,
  462. string shorthelp, string longhelp, string descriptivehelp, CommandDelegate callback)
  463. {
  464. if (MainConsole.Instance == null)
  465. return;
  466. bool shared = false;
  467. if (module != null)
  468. shared = module is ISharedRegionModule;
  469. MainConsole.Instance.Commands.AddCommand(
  470. category, shared, command, shorthelp, longhelp, descriptivehelp, callback);
  471. }
  472. public virtual ISceneObject DeserializeObject(string representation)
  473. {
  474. return null;
  475. }
  476. public virtual bool AllowScriptCrossings
  477. {
  478. get { return false; }
  479. }
  480. public virtual void Start()
  481. {
  482. }
  483. public void Restart()
  484. {
  485. // This has to be here to fire the event
  486. restart handlerPhysicsCrash = OnRestart;
  487. if (handlerPhysicsCrash != null)
  488. handlerPhysicsCrash(RegionInfo);
  489. }
  490. public abstract bool CheckClient(UUID agentID, System.Net.IPEndPoint ep);
  491. }
  492. }