1
0

SceneBase.cs 20 KB

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