SceneManager.cs 18 KB

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