SceneBase.cs 19 KB

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