SceneManager.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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.Net;
  30. using System.Reflection;
  31. using OpenMetaverse;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Framework.Interfaces;
  35. namespace OpenSim.Region.Framework.Scenes
  36. {
  37. public delegate void RestartSim(RegionInfo thisregion);
  38. /// <summary>
  39. /// Manager for adding, closing and restarting scenes.
  40. /// </summary>
  41. public class SceneManager
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. public event RestartSim OnRestartSim;
  45. /// <summary>
  46. /// Fired when either all regions are ready for use or at least one region has become unready for use where
  47. /// previously all regions were ready.
  48. /// </summary>
  49. public event Action<SceneManager> OnRegionsReadyStatusChange;
  50. /// <summary>
  51. /// Are all regions ready for use?
  52. /// </summary>
  53. public bool AllRegionsReady
  54. {
  55. get
  56. {
  57. return m_allRegionsReady;
  58. }
  59. private set
  60. {
  61. if (m_allRegionsReady != value)
  62. {
  63. m_allRegionsReady = value;
  64. Action<SceneManager> handler = OnRegionsReadyStatusChange;
  65. if (handler != null)
  66. {
  67. foreach (Action<SceneManager> d in handler.GetInvocationList())
  68. {
  69. try
  70. {
  71. d(this);
  72. }
  73. catch (Exception e)
  74. {
  75. m_log.ErrorFormat("[SCENE MANAGER]: Delegate for OnRegionsReadyStatusChange failed - continuing {0} - {1}",
  76. e.Message, e.StackTrace);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. private bool m_allRegionsReady;
  84. private static SceneManager m_instance = null;
  85. public static SceneManager Instance
  86. {
  87. get {
  88. if (m_instance == null)
  89. m_instance = new SceneManager();
  90. return m_instance;
  91. }
  92. }
  93. private readonly List<Scene> m_localScenes = new List<Scene>();
  94. public List<Scene> Scenes
  95. {
  96. get { return new List<Scene>(m_localScenes); }
  97. }
  98. /// <summary>
  99. /// Scene selected from the console.
  100. /// </summary>
  101. /// <value>
  102. /// If null, then all scenes are considered selected (signalled as "Root" on the console).
  103. /// </value>
  104. public Scene CurrentScene { get; private set; }
  105. public Scene CurrentOrFirstScene
  106. {
  107. get
  108. {
  109. if (CurrentScene == null)
  110. {
  111. lock (m_localScenes)
  112. {
  113. if (m_localScenes.Count > 0)
  114. return m_localScenes[0];
  115. else
  116. return null;
  117. }
  118. }
  119. else
  120. {
  121. return CurrentScene;
  122. }
  123. }
  124. }
  125. public SceneManager()
  126. {
  127. m_instance = this;
  128. m_localScenes = new List<Scene>();
  129. }
  130. public void Close()
  131. {
  132. lock (m_localScenes)
  133. {
  134. for (int i = 0; i < m_localScenes.Count; i++)
  135. {
  136. m_localScenes[i].Close();
  137. }
  138. }
  139. }
  140. public void Close(Scene cscene)
  141. {
  142. lock (m_localScenes)
  143. {
  144. if (m_localScenes.Contains(cscene))
  145. {
  146. for (int i = 0; i < m_localScenes.Count; i++)
  147. {
  148. if (m_localScenes[i].Equals(cscene))
  149. {
  150. m_localScenes[i].Close();
  151. }
  152. }
  153. }
  154. }
  155. }
  156. public void Add(Scene scene)
  157. {
  158. lock (m_localScenes)
  159. m_localScenes.Add(scene);
  160. scene.OnRestart += HandleRestart;
  161. scene.EventManager.OnRegionReadyStatusChange += HandleRegionReadyStatusChange;
  162. }
  163. public void HandleRestart(RegionInfo rdata)
  164. {
  165. Scene restartedScene = null;
  166. lock (m_localScenes)
  167. {
  168. for (int i = 0; i < m_localScenes.Count; i++)
  169. {
  170. if (rdata.RegionName == m_localScenes[i].RegionInfo.RegionName)
  171. {
  172. restartedScene = m_localScenes[i];
  173. m_localScenes.RemoveAt(i);
  174. break;
  175. }
  176. }
  177. }
  178. // If the currently selected scene has been restarted, then we can't reselect here since we the scene
  179. // hasn't yet been recreated. We will have to leave this to the caller.
  180. if (CurrentScene == restartedScene)
  181. CurrentScene = null;
  182. // Send signal to main that we're restarting this sim.
  183. OnRestartSim(rdata);
  184. }
  185. private void HandleRegionReadyStatusChange(IScene scene)
  186. {
  187. lock (m_localScenes)
  188. AllRegionsReady = m_localScenes.TrueForAll(s => s.Ready);
  189. }
  190. public void SendSimOnlineNotification(ulong regionHandle)
  191. {
  192. RegionInfo Result = null;
  193. lock (m_localScenes)
  194. {
  195. for (int i = 0; i < m_localScenes.Count; i++)
  196. {
  197. if (m_localScenes[i].RegionInfo.RegionHandle == regionHandle)
  198. {
  199. // Inform other regions to tell their avatar about me
  200. Result = m_localScenes[i].RegionInfo;
  201. }
  202. }
  203. if (Result != null)
  204. {
  205. for (int i = 0; i < m_localScenes.Count; i++)
  206. {
  207. if (m_localScenes[i].RegionInfo.RegionHandle != regionHandle)
  208. {
  209. // Inform other regions to tell their avatar about me
  210. //m_localScenes[i].OtherRegionUp(Result);
  211. }
  212. }
  213. }
  214. else
  215. {
  216. m_log.Error("[REGION]: Unable to notify Other regions of this Region coming up");
  217. }
  218. }
  219. }
  220. /// <summary>
  221. /// Save the prims in the current scene to an xml file in OpenSimulator's original 'xml' format
  222. /// </summary>
  223. /// <param name="filename"></param>
  224. public void SaveCurrentSceneToXml(string filename)
  225. {
  226. IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
  227. if (serialiser != null)
  228. serialiser.SavePrimsToXml(CurrentOrFirstScene, filename);
  229. }
  230. /// <summary>
  231. /// Load an xml file of prims in OpenSimulator's original 'xml' file format to the current scene
  232. /// </summary>
  233. /// <param name="filename"></param>
  234. /// <param name="generateNewIDs"></param>
  235. /// <param name="loadOffset"></param>
  236. public void LoadCurrentSceneFromXml(string filename, bool generateNewIDs, Vector3 loadOffset)
  237. {
  238. IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
  239. if (serialiser != null)
  240. serialiser.LoadPrimsFromXml(CurrentOrFirstScene, filename, generateNewIDs, loadOffset);
  241. }
  242. /// <summary>
  243. /// Save the prims in the current scene to an xml file in OpenSimulator's current 'xml2' format
  244. /// </summary>
  245. /// <param name="filename"></param>
  246. public void SaveCurrentSceneToXml2(string filename)
  247. {
  248. IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
  249. if (serialiser != null)
  250. serialiser.SavePrimsToXml2(CurrentOrFirstScene, filename);
  251. }
  252. public void SaveNamedPrimsToXml2(string primName, string filename)
  253. {
  254. IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
  255. if (serialiser != null)
  256. serialiser.SaveNamedPrimsToXml2(CurrentOrFirstScene, primName, filename);
  257. }
  258. /// <summary>
  259. /// Load an xml file of prims in OpenSimulator's current 'xml2' file format to the current scene
  260. /// </summary>
  261. public void LoadCurrentSceneFromXml2(string filename)
  262. {
  263. IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
  264. if (serialiser != null)
  265. serialiser.LoadPrimsFromXml2(CurrentOrFirstScene, filename);
  266. }
  267. /// <summary>
  268. /// Save the current scene to an OpenSimulator archive. This archive will eventually include the prim's assets
  269. /// as well as the details of the prims themselves.
  270. /// </summary>
  271. /// <param name="cmdparams"></param>
  272. public void SaveCurrentSceneToArchive(string[] cmdparams)
  273. {
  274. IRegionArchiverModule archiver = CurrentOrFirstScene.RequestModuleInterface<IRegionArchiverModule>();
  275. if (archiver != null)
  276. archiver.HandleSaveOarConsoleCommand(string.Empty, cmdparams);
  277. }
  278. /// <summary>
  279. /// Load an OpenSim archive into the current scene. This will load both the shapes of the prims and upload
  280. /// their assets to the asset service.
  281. /// </summary>
  282. /// <param name="cmdparams"></param>
  283. public void LoadArchiveToCurrentScene(string[] cmdparams)
  284. {
  285. IRegionArchiverModule archiver = CurrentOrFirstScene.RequestModuleInterface<IRegionArchiverModule>();
  286. if (archiver != null)
  287. archiver.HandleLoadOarConsoleCommand(string.Empty, cmdparams);
  288. }
  289. public string SaveCurrentSceneMapToXmlString()
  290. {
  291. return CurrentOrFirstScene.Heightmap.SaveToXmlString();
  292. }
  293. public void LoadCurrenSceneMapFromXmlString(string mapData)
  294. {
  295. CurrentOrFirstScene.Heightmap.LoadFromXmlString(mapData);
  296. }
  297. public void SendCommandToPluginModules(string[] cmdparams)
  298. {
  299. ForEachCurrentScene(delegate(Scene scene) { scene.SendCommandToPlugins(cmdparams); });
  300. }
  301. public void SetBypassPermissionsOnCurrentScene(bool bypassPermissions)
  302. {
  303. ForEachCurrentScene(delegate(Scene scene) { scene.Permissions.SetBypassPermissions(bypassPermissions); });
  304. }
  305. private void ForEachCurrentScene(Action<Scene> func)
  306. {
  307. if (CurrentScene == null)
  308. {
  309. lock (m_localScenes)
  310. m_localScenes.ForEach(func);
  311. }
  312. else
  313. {
  314. func(CurrentScene);
  315. }
  316. }
  317. public void RestartCurrentScene()
  318. {
  319. ForEachCurrentScene(delegate(Scene scene) { scene.RestartNow(); });
  320. }
  321. public void BackupCurrentScene()
  322. {
  323. ForEachCurrentScene(delegate(Scene scene) { scene.Backup(true); });
  324. }
  325. public bool TrySetCurrentScene(string regionName)
  326. {
  327. if ((String.Compare(regionName, "root") == 0)
  328. || (String.Compare(regionName, "..") == 0)
  329. || (String.Compare(regionName, "/") == 0))
  330. {
  331. CurrentScene = null;
  332. return true;
  333. }
  334. else
  335. {
  336. lock (m_localScenes)
  337. {
  338. foreach (Scene scene in m_localScenes)
  339. {
  340. if (String.Compare(scene.RegionInfo.RegionName, regionName, true) == 0)
  341. {
  342. CurrentScene = scene;
  343. return true;
  344. }
  345. }
  346. }
  347. return false;
  348. }
  349. }
  350. public bool TrySetCurrentScene(UUID regionID)
  351. {
  352. m_log.Debug("Searching for Region: '" + regionID + "'");
  353. lock (m_localScenes)
  354. {
  355. foreach (Scene scene in m_localScenes)
  356. {
  357. if (scene.RegionInfo.RegionID == regionID)
  358. {
  359. CurrentScene = scene;
  360. return true;
  361. }
  362. }
  363. }
  364. return false;
  365. }
  366. public bool TryGetScene(string regionName, out Scene scene)
  367. {
  368. lock (m_localScenes)
  369. {
  370. foreach (Scene mscene in m_localScenes)
  371. {
  372. if (String.Compare(mscene.RegionInfo.RegionName, regionName, true) == 0)
  373. {
  374. scene = mscene;
  375. return true;
  376. }
  377. }
  378. }
  379. scene = null;
  380. return false;
  381. }
  382. public bool TryGetScene(UUID regionID, out Scene scene)
  383. {
  384. lock (m_localScenes)
  385. {
  386. foreach (Scene mscene in m_localScenes)
  387. {
  388. if (mscene.RegionInfo.RegionID == regionID)
  389. {
  390. scene = mscene;
  391. return true;
  392. }
  393. }
  394. }
  395. scene = null;
  396. return false;
  397. }
  398. public bool TryGetScene(uint locX, uint locY, out Scene scene)
  399. {
  400. lock (m_localScenes)
  401. {
  402. foreach (Scene mscene in m_localScenes)
  403. {
  404. if (mscene.RegionInfo.RegionLocX == locX &&
  405. mscene.RegionInfo.RegionLocY == locY)
  406. {
  407. scene = mscene;
  408. return true;
  409. }
  410. }
  411. }
  412. scene = null;
  413. return false;
  414. }
  415. public bool TryGetScene(IPEndPoint ipEndPoint, out Scene scene)
  416. {
  417. lock (m_localScenes)
  418. {
  419. foreach (Scene mscene in m_localScenes)
  420. {
  421. if ((mscene.RegionInfo.InternalEndPoint.Equals(ipEndPoint.Address)) &&
  422. (mscene.RegionInfo.InternalEndPoint.Port == ipEndPoint.Port))
  423. {
  424. scene = mscene;
  425. return true;
  426. }
  427. }
  428. }
  429. scene = null;
  430. return false;
  431. }
  432. /// <summary>
  433. /// Set the debug packet level on each current scene. This level governs which packets are printed out to the
  434. /// console.
  435. /// </summary>
  436. /// <param name="newDebug"></param>
  437. /// <param name="name">Name of avatar to debug</param>
  438. public void SetDebugPacketLevelOnCurrentScene(int newDebug, string name)
  439. {
  440. ForEachCurrentScene(scene =>
  441. scene.ForEachScenePresence(sp =>
  442. {
  443. if (name == null || sp.Name == name)
  444. {
  445. m_log.DebugFormat(
  446. "Packet debug for {0} ({1}) set to {2}",
  447. sp.Name, sp.IsChildAgent ? "child" : "root", newDebug);
  448. sp.ControllingClient.DebugPacketLevel = newDebug;
  449. }
  450. })
  451. );
  452. }
  453. public List<ScenePresence> GetCurrentSceneAvatars()
  454. {
  455. List<ScenePresence> avatars = new List<ScenePresence>();
  456. ForEachCurrentScene(
  457. delegate(Scene scene)
  458. {
  459. scene.ForEachRootScenePresence(delegate(ScenePresence scenePresence)
  460. {
  461. avatars.Add(scenePresence);
  462. });
  463. }
  464. );
  465. return avatars;
  466. }
  467. public List<ScenePresence> GetCurrentScenePresences()
  468. {
  469. List<ScenePresence> presences = new List<ScenePresence>();
  470. ForEachCurrentScene(delegate(Scene scene)
  471. {
  472. scene.ForEachScenePresence(delegate(ScenePresence sp)
  473. {
  474. presences.Add(sp);
  475. });
  476. });
  477. return presences;
  478. }
  479. public RegionInfo GetRegionInfo(UUID regionID)
  480. {
  481. lock (m_localScenes)
  482. {
  483. foreach (Scene scene in m_localScenes)
  484. {
  485. if (scene.RegionInfo.RegionID == regionID)
  486. {
  487. return scene.RegionInfo;
  488. }
  489. }
  490. }
  491. return null;
  492. }
  493. public void ForceCurrentSceneClientUpdate()
  494. {
  495. ForEachCurrentScene(delegate(Scene scene) { scene.ForceClientUpdate(); });
  496. }
  497. public void HandleEditCommandOnCurrentScene(string[] cmdparams)
  498. {
  499. ForEachCurrentScene(delegate(Scene scene) { scene.HandleEditCommand(cmdparams); });
  500. }
  501. public bool TryGetScenePresence(UUID avatarId, out ScenePresence avatar)
  502. {
  503. lock (m_localScenes)
  504. {
  505. foreach (Scene scene in m_localScenes)
  506. {
  507. if (scene.TryGetScenePresence(avatarId, out avatar))
  508. {
  509. return true;
  510. }
  511. }
  512. }
  513. avatar = null;
  514. return false;
  515. }
  516. public bool TryGetRootScenePresence(UUID avatarId, out ScenePresence avatar)
  517. {
  518. lock (m_localScenes)
  519. {
  520. foreach (Scene scene in m_localScenes)
  521. {
  522. avatar = scene.GetScenePresence(avatarId);
  523. if (avatar != null && !avatar.IsChildAgent)
  524. return true;
  525. }
  526. }
  527. avatar = null;
  528. return false;
  529. }
  530. public void CloseScene(Scene scene)
  531. {
  532. lock (m_localScenes)
  533. m_localScenes.Remove(scene);
  534. scene.Close();
  535. }
  536. public bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
  537. {
  538. lock (m_localScenes)
  539. {
  540. foreach (Scene scene in m_localScenes)
  541. {
  542. if (scene.TryGetAvatarByName(avatarName, out avatar))
  543. {
  544. return true;
  545. }
  546. }
  547. }
  548. avatar = null;
  549. return false;
  550. }
  551. public bool TryGetRootScenePresenceByName(string firstName, string lastName, out ScenePresence sp)
  552. {
  553. lock (m_localScenes)
  554. {
  555. foreach (Scene scene in m_localScenes)
  556. {
  557. sp = scene.GetScenePresence(firstName, lastName);
  558. if (sp != null && !sp.IsChildAgent)
  559. return true;
  560. }
  561. }
  562. sp = null;
  563. return false;
  564. }
  565. public void ForEachScene(Action<Scene> action)
  566. {
  567. lock (m_localScenes)
  568. m_localScenes.ForEach(action);
  569. }
  570. }
  571. }