SceneManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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.Equals(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. public void SaveCurrentSceneToXml(string filename)
  142. {
  143. CurrentOrFirstScene.SavePrimsToXml(filename);
  144. }
  145. public void LoadCurrentSceneFromXml(string filename, bool generateNewIDs, LLVector3 loadOffset)
  146. {
  147. CurrentOrFirstScene.LoadPrimsFromXml(filename, generateNewIDs, loadOffset);
  148. }
  149. public void SaveCurrentSceneToXml2(string filename)
  150. {
  151. CurrentOrFirstScene.SavePrimsToXml2(filename);
  152. }
  153. public void LoadCurrentSceneFromXml2(string filename)
  154. {
  155. CurrentOrFirstScene.LoadPrimsFromXml2(filename);
  156. }
  157. [Obsolete("TODO: Remove this warning by 0.7")]
  158. public bool RunTerrainCmdOnCurrentScene(string[] cmdparams, ref string result)
  159. {
  160. m_log.Warn("DEPRECIATED: The terrain engine has been replaced with a new terrain plugin module. Please type 'plugin terrain help' for new commands.");
  161. return false;
  162. }
  163. public void SendCommandToPluginModules(string[] cmdparams)
  164. {
  165. ForEachCurrentScene(delegate(Scene scene) { scene.SendCommandToPlugins(cmdparams); });
  166. }
  167. public void SetBypassPermissionsOnCurrentScene(bool bypassPermissions)
  168. {
  169. ForEachCurrentScene(delegate(Scene scene) { scene.PermissionsMngr.BypassPermissions = bypassPermissions; });
  170. }
  171. private void ForEachCurrentScene(Action<Scene> func)
  172. {
  173. if (m_currentScene == null)
  174. {
  175. m_localScenes.ForEach(func);
  176. }
  177. else
  178. {
  179. func(m_currentScene);
  180. }
  181. }
  182. public void RestartCurrentScene()
  183. {
  184. ForEachCurrentScene(delegate(Scene scene) { scene.RestartNow(); });
  185. }
  186. public void BackupCurrentScene()
  187. {
  188. ForEachCurrentScene(delegate(Scene scene) { scene.Backup(); });
  189. }
  190. public void HandleAlertCommandOnCurrentScene(string[] cmdparams)
  191. {
  192. ForEachCurrentScene(delegate(Scene scene) { scene.HandleAlertCommand(cmdparams); });
  193. }
  194. public void SendGeneralMessage(string msg)
  195. {
  196. ForEachCurrentScene(delegate(Scene scene) { scene.SendGeneralAlert(msg); });
  197. }
  198. public bool TrySetCurrentScene(string regionName)
  199. {
  200. if ((String.Compare(regionName, "root") == 0) || (String.Compare(regionName, "..") == 0))
  201. {
  202. m_currentScene = null;
  203. return true;
  204. }
  205. else
  206. {
  207. Console.WriteLine("Searching for Region: '" + regionName + "'");
  208. foreach (Scene scene in m_localScenes)
  209. {
  210. if (String.Compare(scene.RegionInfo.RegionName, regionName, true) == 0)
  211. {
  212. m_currentScene = scene;
  213. return true;
  214. }
  215. }
  216. return false;
  217. }
  218. }
  219. public bool TrySetCurrentScene(LLUUID regionID)
  220. {
  221. Console.WriteLine("Searching for Region: '{0}'", regionID.ToString());
  222. foreach (Scene scene in m_localScenes)
  223. {
  224. if (scene.RegionInfo.RegionID == regionID)
  225. {
  226. m_currentScene = scene;
  227. return true;
  228. }
  229. }
  230. return false;
  231. }
  232. public bool TryGetScene(string regionName, out Scene scene)
  233. {
  234. foreach (Scene mscene in m_localScenes)
  235. {
  236. if (String.Compare(mscene.RegionInfo.RegionName, regionName, true) == 0)
  237. {
  238. scene = mscene;
  239. return true;
  240. }
  241. }
  242. scene = null;
  243. return false;
  244. }
  245. public bool TryGetScene(LLUUID regionID, out Scene scene)
  246. {
  247. foreach (Scene mscene in m_localScenes)
  248. {
  249. if (mscene.RegionInfo.RegionID == regionID)
  250. {
  251. scene = mscene;
  252. return true;
  253. }
  254. }
  255. scene = null;
  256. return false;
  257. }
  258. public bool TryGetScene(uint locX, uint locY, out Scene scene)
  259. {
  260. foreach (Scene mscene in m_localScenes)
  261. {
  262. if (mscene.RegionInfo.RegionLocX == locX &&
  263. mscene.RegionInfo.RegionLocY == locY)
  264. {
  265. scene = mscene;
  266. return true;
  267. }
  268. }
  269. scene = null;
  270. return false;
  271. }
  272. public bool TryGetScene(IPEndPoint ipEndPoint, out Scene scene)
  273. {
  274. foreach (Scene mscene in m_localScenes)
  275. {
  276. if((mscene.RegionInfo.InternalEndPoint.Equals(ipEndPoint.Address)) &&
  277. (mscene.RegionInfo.InternalEndPoint.Port == ipEndPoint.Port))
  278. {
  279. scene = mscene;
  280. return true;
  281. }
  282. }
  283. scene = null;
  284. return false;
  285. }
  286. public void SetDebugPacketOnCurrentScene(int newDebug)
  287. {
  288. ForEachCurrentScene(delegate(Scene scene)
  289. {
  290. List<ScenePresence> scenePresences = scene.GetScenePresences();
  291. foreach (ScenePresence scenePresence in scenePresences)
  292. {
  293. if (!scenePresence.IsChildAgent)
  294. {
  295. m_log.ErrorFormat("Packet debug for {0} {1} set to {2}",
  296. scenePresence.Firstname,
  297. scenePresence.Lastname,
  298. newDebug);
  299. scenePresence.ControllingClient.SetDebug(newDebug);
  300. }
  301. }
  302. });
  303. }
  304. public List<ScenePresence> GetCurrentSceneAvatars()
  305. {
  306. List<ScenePresence> avatars = new List<ScenePresence>();
  307. ForEachCurrentScene(delegate(Scene scene)
  308. {
  309. List<ScenePresence> scenePresences = scene.GetScenePresences();
  310. foreach (ScenePresence scenePresence in scenePresences)
  311. {
  312. if (!scenePresence.IsChildAgent)
  313. {
  314. avatars.Add(scenePresence);
  315. }
  316. }
  317. });
  318. return avatars;
  319. }
  320. public RegionInfo GetRegionInfo(ulong regionHandle)
  321. {
  322. foreach (Scene scene in m_localScenes)
  323. {
  324. if (scene.RegionInfo.RegionHandle == regionHandle)
  325. {
  326. return scene.RegionInfo;
  327. }
  328. }
  329. return null;
  330. }
  331. public void SetCurrentSceneTimePhase(int timePhase)
  332. {
  333. ForEachCurrentScene(delegate(Scene scene)
  334. {
  335. scene.SetTimePhase(
  336. timePhase)
  337. ;
  338. });
  339. }
  340. public void ForceCurrentSceneClientUpdate()
  341. {
  342. ForEachCurrentScene(delegate(Scene scene) { scene.ForceClientUpdate(); });
  343. }
  344. public void HandleEditCommandOnCurrentScene(string[] cmdparams)
  345. {
  346. ForEachCurrentScene(delegate(Scene scene) { scene.HandleEditCommand(cmdparams); });
  347. }
  348. public bool TryGetAvatar(LLUUID avatarId, out ScenePresence avatar)
  349. {
  350. foreach (Scene scene in m_localScenes)
  351. {
  352. if (scene.TryGetAvatar(avatarId, out avatar))
  353. {
  354. return true;
  355. }
  356. }
  357. avatar = null;
  358. return false;
  359. }
  360. public bool TryGetAvatarsScene(LLUUID avatarId, out Scene scene)
  361. {
  362. ScenePresence avatar = null;
  363. foreach (Scene mScene in m_localScenes)
  364. {
  365. if (mScene.TryGetAvatar(avatarId, out avatar))
  366. {
  367. scene = mScene;
  368. return true;
  369. }
  370. }
  371. scene = null;
  372. return false;
  373. }
  374. public void CloseScene(Scene scene)
  375. {
  376. m_localScenes.Remove(scene);
  377. scene.Close();
  378. }
  379. public bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
  380. {
  381. foreach (Scene scene in m_localScenes)
  382. {
  383. if (scene.TryGetAvatarByName(avatarName, out avatar))
  384. {
  385. return true;
  386. }
  387. }
  388. avatar = null;
  389. return false;
  390. }
  391. public void ForEachScene(Action<Scene> action)
  392. {
  393. m_localScenes.ForEach(action);
  394. }
  395. }
  396. }