SceneManager.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. ForEachSelectedScene(delegate(Scene scene) { scene.SendCommandToPlugins(cmdparams); });
  300. }
  301. public void SetBypassPermissionsOnCurrentScene(bool bypassPermissions)
  302. {
  303. ForEachSelectedScene(delegate(Scene scene) { scene.Permissions.SetBypassPermissions(bypassPermissions); });
  304. }
  305. public void ForEachSelectedScene(Action<Scene> func)
  306. {
  307. if (CurrentScene == null)
  308. ForEachScene(func);
  309. else
  310. func(CurrentScene);
  311. }
  312. public void RestartCurrentScene()
  313. {
  314. ForEachSelectedScene(delegate(Scene scene) { scene.RestartNow(); });
  315. }
  316. public void BackupCurrentScene()
  317. {
  318. ForEachSelectedScene(delegate(Scene scene) { scene.Backup(true); });
  319. }
  320. public bool TrySetCurrentScene(string regionName)
  321. {
  322. if ((String.Compare(regionName, "root") == 0)
  323. || (String.Compare(regionName, "..") == 0)
  324. || (String.Compare(regionName, "/") == 0))
  325. {
  326. CurrentScene = null;
  327. return true;
  328. }
  329. else
  330. {
  331. lock (m_localScenes)
  332. {
  333. foreach (Scene scene in m_localScenes)
  334. {
  335. if (String.Compare(scene.RegionInfo.RegionName, regionName, true) == 0)
  336. {
  337. CurrentScene = scene;
  338. return true;
  339. }
  340. }
  341. }
  342. return false;
  343. }
  344. }
  345. public bool TrySetCurrentScene(UUID regionID)
  346. {
  347. m_log.Debug("Searching for Region: '" + regionID + "'");
  348. lock (m_localScenes)
  349. {
  350. foreach (Scene scene in m_localScenes)
  351. {
  352. if (scene.RegionInfo.RegionID == regionID)
  353. {
  354. CurrentScene = scene;
  355. return true;
  356. }
  357. }
  358. }
  359. return false;
  360. }
  361. public bool TryGetScene(string regionName, out Scene scene)
  362. {
  363. lock (m_localScenes)
  364. {
  365. foreach (Scene mscene in m_localScenes)
  366. {
  367. if (String.Compare(mscene.RegionInfo.RegionName, regionName, true) == 0)
  368. {
  369. scene = mscene;
  370. return true;
  371. }
  372. }
  373. }
  374. scene = null;
  375. return false;
  376. }
  377. public bool TryGetScene(UUID regionID, out Scene scene)
  378. {
  379. lock (m_localScenes)
  380. {
  381. foreach (Scene mscene in m_localScenes)
  382. {
  383. if (mscene.RegionInfo.RegionID == regionID)
  384. {
  385. scene = mscene;
  386. return true;
  387. }
  388. }
  389. }
  390. scene = null;
  391. return false;
  392. }
  393. public bool TryGetScene(uint locX, uint locY, out Scene scene)
  394. {
  395. lock (m_localScenes)
  396. {
  397. foreach (Scene mscene in m_localScenes)
  398. {
  399. if (mscene.RegionInfo.RegionLocX == locX &&
  400. mscene.RegionInfo.RegionLocY == locY)
  401. {
  402. scene = mscene;
  403. return true;
  404. }
  405. }
  406. }
  407. scene = null;
  408. return false;
  409. }
  410. public bool TryGetScene(IPEndPoint ipEndPoint, out Scene scene)
  411. {
  412. lock (m_localScenes)
  413. {
  414. foreach (Scene mscene in m_localScenes)
  415. {
  416. if ((mscene.RegionInfo.InternalEndPoint.Equals(ipEndPoint.Address)) &&
  417. (mscene.RegionInfo.InternalEndPoint.Port == ipEndPoint.Port))
  418. {
  419. scene = mscene;
  420. return true;
  421. }
  422. }
  423. }
  424. scene = null;
  425. return false;
  426. }
  427. public List<ScenePresence> GetCurrentSceneAvatars()
  428. {
  429. List<ScenePresence> avatars = new List<ScenePresence>();
  430. ForEachSelectedScene(
  431. delegate(Scene scene)
  432. {
  433. scene.ForEachRootScenePresence(delegate(ScenePresence scenePresence)
  434. {
  435. avatars.Add(scenePresence);
  436. });
  437. }
  438. );
  439. return avatars;
  440. }
  441. public List<ScenePresence> GetCurrentScenePresences()
  442. {
  443. List<ScenePresence> presences = new List<ScenePresence>();
  444. ForEachSelectedScene(delegate(Scene scene)
  445. {
  446. scene.ForEachScenePresence(delegate(ScenePresence sp)
  447. {
  448. presences.Add(sp);
  449. });
  450. });
  451. return presences;
  452. }
  453. public RegionInfo GetRegionInfo(UUID regionID)
  454. {
  455. lock (m_localScenes)
  456. {
  457. foreach (Scene scene in m_localScenes)
  458. {
  459. if (scene.RegionInfo.RegionID == regionID)
  460. {
  461. return scene.RegionInfo;
  462. }
  463. }
  464. }
  465. return null;
  466. }
  467. public void ForceCurrentSceneClientUpdate()
  468. {
  469. ForEachSelectedScene(delegate(Scene scene) { scene.ForceClientUpdate(); });
  470. }
  471. public void HandleEditCommandOnCurrentScene(string[] cmdparams)
  472. {
  473. ForEachSelectedScene(delegate(Scene scene) { scene.HandleEditCommand(cmdparams); });
  474. }
  475. public bool TryGetScenePresence(UUID avatarId, out ScenePresence avatar)
  476. {
  477. lock (m_localScenes)
  478. {
  479. foreach (Scene scene in m_localScenes)
  480. {
  481. if (scene.TryGetScenePresence(avatarId, out avatar))
  482. {
  483. return true;
  484. }
  485. }
  486. }
  487. avatar = null;
  488. return false;
  489. }
  490. public bool TryGetRootScenePresence(UUID avatarId, out ScenePresence avatar)
  491. {
  492. lock (m_localScenes)
  493. {
  494. foreach (Scene scene in m_localScenes)
  495. {
  496. avatar = scene.GetScenePresence(avatarId);
  497. if (avatar != null && !avatar.IsChildAgent)
  498. return true;
  499. }
  500. }
  501. avatar = null;
  502. return false;
  503. }
  504. public void CloseScene(Scene scene)
  505. {
  506. lock (m_localScenes)
  507. m_localScenes.Remove(scene);
  508. scene.Close();
  509. }
  510. public bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
  511. {
  512. lock (m_localScenes)
  513. {
  514. foreach (Scene scene in m_localScenes)
  515. {
  516. if (scene.TryGetAvatarByName(avatarName, out avatar))
  517. {
  518. return true;
  519. }
  520. }
  521. }
  522. avatar = null;
  523. return false;
  524. }
  525. public bool TryGetRootScenePresenceByName(string firstName, string lastName, out ScenePresence sp)
  526. {
  527. lock (m_localScenes)
  528. {
  529. foreach (Scene scene in m_localScenes)
  530. {
  531. sp = scene.GetScenePresence(firstName, lastName);
  532. if (sp != null && !sp.IsChildAgent)
  533. return true;
  534. }
  535. }
  536. sp = null;
  537. return false;
  538. }
  539. public void ForEachScene(Action<Scene> action)
  540. {
  541. lock (m_localScenes)
  542. m_localScenes.ForEach(action);
  543. }
  544. }
  545. }