SceneManager.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 each current scene. This level governs which packets are printed out to the
  400. /// console.
  401. /// </summary>
  402. /// <param name="newDebug"></param>
  403. /// <param name="name">Name of avatar to debug</param>
  404. public void SetDebugPacketLevelOnCurrentScene(int newDebug, string name)
  405. {
  406. ForEachCurrentScene(scene =>
  407. scene.ForEachScenePresence(sp =>
  408. {
  409. if (name == null || sp.Name == name)
  410. {
  411. m_log.DebugFormat(
  412. "Packet debug for {0} ({1}) set to {2}",
  413. sp.Name, sp.IsChildAgent ? "child" : "root", newDebug);
  414. sp.ControllingClient.DebugPacketLevel = newDebug;
  415. }
  416. })
  417. );
  418. }
  419. public List<ScenePresence> GetCurrentSceneAvatars()
  420. {
  421. List<ScenePresence> avatars = new List<ScenePresence>();
  422. ForEachCurrentScene(
  423. delegate(Scene scene)
  424. {
  425. scene.ForEachRootScenePresence(delegate(ScenePresence scenePresence)
  426. {
  427. avatars.Add(scenePresence);
  428. });
  429. }
  430. );
  431. return avatars;
  432. }
  433. public List<ScenePresence> GetCurrentScenePresences()
  434. {
  435. List<ScenePresence> presences = new List<ScenePresence>();
  436. ForEachCurrentScene(delegate(Scene scene)
  437. {
  438. scene.ForEachScenePresence(delegate(ScenePresence sp)
  439. {
  440. presences.Add(sp);
  441. });
  442. });
  443. return presences;
  444. }
  445. public RegionInfo GetRegionInfo(UUID regionID)
  446. {
  447. lock (m_localScenes)
  448. {
  449. foreach (Scene scene in m_localScenes)
  450. {
  451. if (scene.RegionInfo.RegionID == regionID)
  452. {
  453. return scene.RegionInfo;
  454. }
  455. }
  456. }
  457. return null;
  458. }
  459. public void ForceCurrentSceneClientUpdate()
  460. {
  461. ForEachCurrentScene(delegate(Scene scene) { scene.ForceClientUpdate(); });
  462. }
  463. public void HandleEditCommandOnCurrentScene(string[] cmdparams)
  464. {
  465. ForEachCurrentScene(delegate(Scene scene) { scene.HandleEditCommand(cmdparams); });
  466. }
  467. public bool TryGetScenePresence(UUID avatarId, out ScenePresence avatar)
  468. {
  469. lock (m_localScenes)
  470. {
  471. foreach (Scene scene in m_localScenes)
  472. {
  473. if (scene.TryGetScenePresence(avatarId, out avatar))
  474. {
  475. return true;
  476. }
  477. }
  478. }
  479. avatar = null;
  480. return false;
  481. }
  482. public bool TryGetRootScenePresence(UUID avatarId, out ScenePresence avatar)
  483. {
  484. lock (m_localScenes)
  485. {
  486. foreach (Scene scene in m_localScenes)
  487. {
  488. avatar = scene.GetScenePresence(avatarId);
  489. if (avatar != null && !avatar.IsChildAgent)
  490. return true;
  491. }
  492. }
  493. avatar = null;
  494. return false;
  495. }
  496. public void CloseScene(Scene scene)
  497. {
  498. lock (m_localScenes)
  499. m_localScenes.Remove(scene);
  500. scene.Close();
  501. }
  502. public bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
  503. {
  504. lock (m_localScenes)
  505. {
  506. foreach (Scene scene in m_localScenes)
  507. {
  508. if (scene.TryGetAvatarByName(avatarName, out avatar))
  509. {
  510. return true;
  511. }
  512. }
  513. }
  514. avatar = null;
  515. return false;
  516. }
  517. public bool TryGetRootScenePresenceByName(string firstName, string lastName, out ScenePresence sp)
  518. {
  519. lock (m_localScenes)
  520. {
  521. foreach (Scene scene in m_localScenes)
  522. {
  523. sp = scene.GetScenePresence(firstName, lastName);
  524. if (sp != null && !sp.IsChildAgent)
  525. return true;
  526. }
  527. }
  528. sp = null;
  529. return false;
  530. }
  531. public void ForEachScene(Action<Scene> action)
  532. {
  533. lock (m_localScenes)
  534. m_localScenes.ForEach(action);
  535. }
  536. }
  537. }