SceneManager.cs 22 KB

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