1
0

SceneManager.cs 18 KB

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