SceneManager.cs 20 KB

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