SceneManager.cs 16 KB

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