SceneManager.cs 17 KB

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