SceneManager.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 DoubleDictionary<UUID, string, Scene> m_localScenes = new DoubleDictionary<UUID, string, Scene>();
  94. public List<Scene> Scenes
  95. {
  96. get { return new List<Scene>(m_localScenes.FindAll(delegate(Scene s) { return true; })); }
  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. List<Scene> sceneList = Scenes;
  112. if (sceneList.Count == 0)
  113. return null;
  114. return sceneList[0];
  115. }
  116. else
  117. {
  118. return CurrentScene;
  119. }
  120. }
  121. }
  122. public SceneManager()
  123. {
  124. m_instance = this;
  125. m_localScenes = new DoubleDictionary<UUID, string, Scene>();
  126. }
  127. public void Close()
  128. {
  129. List<Scene> localScenes = null;
  130. lock (m_localScenes)
  131. {
  132. localScenes = Scenes;
  133. }
  134. for (int i = 0; i < localScenes.Count; i++)
  135. {
  136. localScenes[i].Close();
  137. }
  138. }
  139. public void Close(Scene cscene)
  140. {
  141. if (!m_localScenes.ContainsKey(cscene.RegionInfo.RegionID))
  142. return;
  143. cscene.Close();
  144. }
  145. public void Add(Scene scene)
  146. {
  147. lock (m_localScenes)
  148. m_localScenes.Add(scene.RegionInfo.RegionID, scene.RegionInfo.RegionName, scene);
  149. scene.OnRestart += HandleRestart;
  150. scene.EventManager.OnRegionReadyStatusChange += HandleRegionReadyStatusChange;
  151. }
  152. public void HandleRestart(RegionInfo rdata)
  153. {
  154. Scene restartedScene = null;
  155. lock (m_localScenes)
  156. {
  157. m_localScenes.TryGetValue(rdata.RegionID, out restartedScene);
  158. m_localScenes.Remove(rdata.RegionID);
  159. }
  160. // If the currently selected scene has been restarted, then we can't reselect here since we the scene
  161. // hasn't yet been recreated. We will have to leave this to the caller.
  162. if (CurrentScene == restartedScene)
  163. CurrentScene = null;
  164. // Send signal to main that we're restarting this sim.
  165. OnRestartSim(rdata);
  166. }
  167. private void HandleRegionReadyStatusChange(IScene scene)
  168. {
  169. lock (m_localScenes)
  170. AllRegionsReady = m_localScenes.FindAll(s => !s.Ready).Count == 0;
  171. }
  172. public void SendSimOnlineNotification(ulong regionHandle)
  173. {
  174. Scene s = m_localScenes.FindValue(delegate(Scene x)
  175. {
  176. if (x.RegionInfo.RegionHandle == regionHandle)
  177. return true;
  178. return false;
  179. });
  180. if (s != null)
  181. {
  182. List<Scene> sceneList = Scenes;
  183. for (int i = 0; i < sceneList.Count; i++)
  184. {
  185. if (sceneList[i]!= s)
  186. {
  187. // Inform other regions to tell their avatar about me
  188. //sceneList[i].OtherRegionUp(Result);
  189. }
  190. }
  191. }
  192. else
  193. {
  194. m_log.Error("[REGION]: Unable to notify Other regions of this Region coming up");
  195. }
  196. }
  197. /// <summary>
  198. /// Save the prims in the current scene to an xml file in OpenSimulator's original 'xml' format
  199. /// </summary>
  200. /// <param name="filename"></param>
  201. public void SaveCurrentSceneToXml(string filename)
  202. {
  203. IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
  204. if (serialiser != null)
  205. serialiser.SavePrimsToXml(CurrentOrFirstScene, filename);
  206. }
  207. /// <summary>
  208. /// Load an xml file of prims in OpenSimulator's original 'xml' file format to the current scene
  209. /// </summary>
  210. /// <param name="filename"></param>
  211. /// <param name="generateNewIDs"></param>
  212. /// <param name="loadOffset"></param>
  213. public void LoadCurrentSceneFromXml(string filename, bool generateNewIDs, Vector3 loadOffset)
  214. {
  215. IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
  216. if (serialiser != null)
  217. serialiser.LoadPrimsFromXml(CurrentOrFirstScene, filename, generateNewIDs, loadOffset);
  218. }
  219. /// <summary>
  220. /// Save the prims in the current scene to an xml file in OpenSimulator's current 'xml2' format
  221. /// </summary>
  222. /// <param name="filename"></param>
  223. public void SaveCurrentSceneToXml2(string filename)
  224. {
  225. IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
  226. if (serialiser != null)
  227. serialiser.SavePrimsToXml2(CurrentOrFirstScene, filename);
  228. }
  229. public void SaveNamedPrimsToXml2(string primName, string filename)
  230. {
  231. IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
  232. if (serialiser != null)
  233. serialiser.SaveNamedPrimsToXml2(CurrentOrFirstScene, primName, filename);
  234. }
  235. /// <summary>
  236. /// Load an xml file of prims in OpenSimulator's current 'xml2' file format to the current scene
  237. /// </summary>
  238. public void LoadCurrentSceneFromXml2(string filename)
  239. {
  240. IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
  241. if (serialiser != null)
  242. serialiser.LoadPrimsFromXml2(CurrentOrFirstScene, filename);
  243. }
  244. /// <summary>
  245. /// Save the current scene to an OpenSimulator archive. This archive will eventually include the prim's assets
  246. /// as well as the details of the prims themselves.
  247. /// </summary>
  248. /// <param name="cmdparams"></param>
  249. public void SaveCurrentSceneToArchive(string[] cmdparams)
  250. {
  251. IRegionArchiverModule archiver = CurrentOrFirstScene.RequestModuleInterface<IRegionArchiverModule>();
  252. if (archiver != null)
  253. archiver.HandleSaveOarConsoleCommand(string.Empty, cmdparams);
  254. }
  255. /// <summary>
  256. /// Load an OpenSim archive into the current scene. This will load both the shapes of the prims and upload
  257. /// their assets to the asset service.
  258. /// </summary>
  259. /// <param name="cmdparams"></param>
  260. public void LoadArchiveToCurrentScene(string[] cmdparams)
  261. {
  262. IRegionArchiverModule archiver = CurrentOrFirstScene.RequestModuleInterface<IRegionArchiverModule>();
  263. if (archiver != null)
  264. archiver.HandleLoadOarConsoleCommand(string.Empty, cmdparams);
  265. }
  266. public string SaveCurrentSceneMapToXmlString()
  267. {
  268. return CurrentOrFirstScene.Heightmap.SaveToXmlString();
  269. }
  270. public void LoadCurrenSceneMapFromXmlString(string mapData)
  271. {
  272. CurrentOrFirstScene.Heightmap.LoadFromXmlString(mapData);
  273. }
  274. public void SendCommandToPluginModules(string[] cmdparams)
  275. {
  276. ForEachSelectedScene(delegate(Scene scene) { scene.SendCommandToPlugins(cmdparams); });
  277. }
  278. public void SetBypassPermissionsOnCurrentScene(bool bypassPermissions)
  279. {
  280. ForEachSelectedScene(delegate(Scene scene) { scene.Permissions.SetBypassPermissions(bypassPermissions); });
  281. }
  282. public void ForEachSelectedScene(Action<Scene> func)
  283. {
  284. if (CurrentScene == null)
  285. ForEachScene(func);
  286. else
  287. func(CurrentScene);
  288. }
  289. public void RestartCurrentScene()
  290. {
  291. ForEachSelectedScene(delegate(Scene scene) { scene.RestartNow(); });
  292. }
  293. public void BackupCurrentScene()
  294. {
  295. ForEachSelectedScene(delegate(Scene scene) { scene.Backup(true); });
  296. }
  297. public bool TrySetCurrentScene(string regionName)
  298. {
  299. if ((String.Compare(regionName, "root") == 0)
  300. || (String.Compare(regionName, "..") == 0)
  301. || (String.Compare(regionName, "/") == 0))
  302. {
  303. CurrentScene = null;
  304. return true;
  305. }
  306. else
  307. {
  308. Scene s;
  309. if (m_localScenes.TryGetValue(regionName, out s))
  310. {
  311. CurrentScene = s;
  312. return true;
  313. }
  314. return false;
  315. }
  316. }
  317. public bool TrySetCurrentScene(UUID regionID)
  318. {
  319. // m_log.Debug("Searching for Region: '" + regionID + "'");
  320. Scene s;
  321. if (m_localScenes.TryGetValue(regionID, out s))
  322. {
  323. CurrentScene = s;
  324. return true;
  325. }
  326. return false;
  327. }
  328. public bool TryGetScene(string regionName, out Scene scene)
  329. {
  330. return m_localScenes.TryGetValue(regionName, out scene);
  331. }
  332. public bool TryGetScene(UUID regionID, out Scene scene)
  333. {
  334. return m_localScenes.TryGetValue(regionID, out scene);
  335. }
  336. public bool TryGetScene(uint locX, uint locY, out Scene scene)
  337. {
  338. List<Scene> sceneList = Scenes;
  339. foreach (Scene mscene in sceneList)
  340. {
  341. if (mscene.RegionInfo.RegionLocX == locX &&
  342. mscene.RegionInfo.RegionLocY == locY)
  343. {
  344. scene = mscene;
  345. return true;
  346. }
  347. }
  348. scene = null;
  349. return false;
  350. }
  351. public bool TryGetScene(IPEndPoint ipEndPoint, out Scene scene)
  352. {
  353. List<Scene> sceneList = Scenes;
  354. foreach (Scene mscene in sceneList)
  355. {
  356. if ((mscene.RegionInfo.InternalEndPoint.Equals(ipEndPoint.Address)) &&
  357. (mscene.RegionInfo.InternalEndPoint.Port == ipEndPoint.Port))
  358. {
  359. scene = mscene;
  360. return true;
  361. }
  362. }
  363. scene = null;
  364. return false;
  365. }
  366. public List<ScenePresence> GetCurrentSceneAvatars()
  367. {
  368. List<ScenePresence> avatars = new List<ScenePresence>();
  369. ForEachSelectedScene(
  370. delegate(Scene scene)
  371. {
  372. scene.ForEachRootScenePresence(delegate(ScenePresence scenePresence)
  373. {
  374. avatars.Add(scenePresence);
  375. });
  376. }
  377. );
  378. return avatars;
  379. }
  380. public List<ScenePresence> GetCurrentScenePresences()
  381. {
  382. List<ScenePresence> presences = new List<ScenePresence>();
  383. ForEachSelectedScene(delegate(Scene scene)
  384. {
  385. scene.ForEachScenePresence(delegate(ScenePresence sp)
  386. {
  387. presences.Add(sp);
  388. });
  389. });
  390. return presences;
  391. }
  392. public RegionInfo GetRegionInfo(UUID regionID)
  393. {
  394. Scene s;
  395. if (m_localScenes.TryGetValue(regionID, out s))
  396. {
  397. return s.RegionInfo;
  398. }
  399. return null;
  400. }
  401. public void ForceCurrentSceneClientUpdate()
  402. {
  403. ForEachSelectedScene(delegate(Scene scene) { scene.ForceClientUpdate(); });
  404. }
  405. public void HandleEditCommandOnCurrentScene(string[] cmdparams)
  406. {
  407. ForEachSelectedScene(delegate(Scene scene) { scene.HandleEditCommand(cmdparams); });
  408. }
  409. public bool TryGetScenePresence(UUID avatarId, out ScenePresence avatar)
  410. {
  411. List<Scene> sceneList = Scenes;
  412. foreach (Scene scene in sceneList)
  413. {
  414. if (scene.TryGetScenePresence(avatarId, out avatar))
  415. {
  416. return true;
  417. }
  418. }
  419. avatar = null;
  420. return false;
  421. }
  422. public bool TryGetRootScenePresence(UUID avatarId, out ScenePresence avatar)
  423. {
  424. List<Scene> sceneList = Scenes;
  425. foreach (Scene scene in sceneList)
  426. {
  427. avatar = scene.GetScenePresence(avatarId);
  428. if (avatar != null && !avatar.IsChildAgent)
  429. return true;
  430. }
  431. avatar = null;
  432. return false;
  433. }
  434. public void CloseScene(Scene scene)
  435. {
  436. lock (m_localScenes)
  437. m_localScenes.Remove(scene.RegionInfo.RegionID);
  438. scene.Close();
  439. }
  440. public bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
  441. {
  442. List<Scene> sceneList = Scenes;
  443. foreach (Scene scene in sceneList)
  444. {
  445. if (scene.TryGetAvatarByName(avatarName, out avatar))
  446. {
  447. return true;
  448. }
  449. }
  450. avatar = null;
  451. return false;
  452. }
  453. public bool TryGetRootScenePresenceByName(string firstName, string lastName, out ScenePresence sp)
  454. {
  455. List<Scene> sceneList = Scenes;
  456. foreach (Scene scene in sceneList)
  457. {
  458. sp = scene.GetScenePresence(firstName, lastName);
  459. if (sp != null && !sp.IsChildAgent)
  460. return true;
  461. }
  462. sp = null;
  463. return false;
  464. }
  465. public void ForEachScene(Action<Scene> action)
  466. {
  467. List<Scene> sceneList = Scenes;
  468. sceneList.ForEach(action);
  469. }
  470. }
  471. }