1
0

SceneManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using libsecondlife;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Console;
  33. namespace OpenSim.Region.Environment.Scenes
  34. {
  35. public delegate void RestartSim(RegionInfo thisregion);
  36. public class SceneManager
  37. {
  38. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  39. public event RestartSim OnRestartSim;
  40. private readonly List<Scene> m_localScenes;
  41. private Scene m_currentScene = null;
  42. public List<Scene> Scenes
  43. {
  44. get { return m_localScenes; }
  45. }
  46. public Scene CurrentScene
  47. {
  48. get { return m_currentScene; }
  49. }
  50. public Scene CurrentOrFirstScene
  51. {
  52. get
  53. {
  54. if (m_currentScene == null)
  55. {
  56. return m_localScenes[0];
  57. }
  58. else
  59. {
  60. return m_currentScene;
  61. }
  62. }
  63. }
  64. public SceneManager()
  65. {
  66. m_localScenes = new List<Scene>();
  67. }
  68. public void Close()
  69. {
  70. for (int i = 0; i < m_localScenes.Count; i++)
  71. {
  72. m_localScenes[i].Close();
  73. }
  74. }
  75. public void Close(Scene cscene)
  76. {
  77. if (m_localScenes.Contains(cscene))
  78. {
  79. for (int i = 0; i < m_localScenes.Count; i++)
  80. {
  81. if (m_localScenes[i].Equals(cscene))
  82. {
  83. m_localScenes[i].Close();
  84. }
  85. }
  86. }
  87. }
  88. public void Add(Scene scene)
  89. {
  90. scene.OnRestart += HandleRestart;
  91. m_localScenes.Add(scene);
  92. }
  93. public void HandleRestart(RegionInfo rdata)
  94. {
  95. m_log.Error("[SCENEMANAGER]: Got Restart message for region:" + rdata.RegionName + " Sending up to main");
  96. int RegionSceneElement = -1;
  97. for (int i = 0; i < m_localScenes.Count; i++)
  98. {
  99. if (rdata.RegionName == m_localScenes[i].RegionInfo.RegionName)
  100. {
  101. RegionSceneElement = i;
  102. }
  103. }
  104. // Now we make sure the region is no longer known about by the SceneManager
  105. // Prevents duplicates.
  106. if (RegionSceneElement >= 0)
  107. {
  108. m_localScenes.RemoveAt(RegionSceneElement);
  109. }
  110. // Send signal to main that we're restarting this sim.
  111. OnRestartSim(rdata);
  112. }
  113. public void SendSimOnlineNotification(ulong regionHandle)
  114. {
  115. RegionInfo Result = null;
  116. for (int i = 0; i < m_localScenes.Count; i++)
  117. {
  118. if (m_localScenes[i].RegionInfo.RegionHandle == regionHandle)
  119. {
  120. // Inform other regions to tell their avatar about me
  121. Result = m_localScenes[i].RegionInfo;
  122. }
  123. }
  124. if (!(Result.Equals(null)))
  125. {
  126. for (int i = 0; i < m_localScenes.Count; i++)
  127. {
  128. if (m_localScenes[i].RegionInfo.RegionHandle != regionHandle)
  129. {
  130. // Inform other regions to tell their avatar about me
  131. //m_localScenes[i].OtherRegionUp(Result);
  132. }
  133. }
  134. }
  135. else
  136. {
  137. m_log.Error("[REGION]: Unable to notify Other regions of this Region coming up");
  138. }
  139. }
  140. public void SaveCurrentSceneToXml(string filename)
  141. {
  142. CurrentOrFirstScene.SavePrimsToXml(filename);
  143. }
  144. public void LoadCurrentSceneFromXml(string filename, bool generateNewIDs, LLVector3 loadOffset)
  145. {
  146. CurrentOrFirstScene.LoadPrimsFromXml(filename, generateNewIDs, loadOffset);
  147. }
  148. public void SaveCurrentSceneToXml2(string filename)
  149. {
  150. CurrentOrFirstScene.SavePrimsToXml2(filename);
  151. }
  152. public void LoadCurrentSceneFromXml2(string filename)
  153. {
  154. CurrentOrFirstScene.LoadPrimsFromXml2(filename);
  155. }
  156. public bool RunTerrainCmdOnCurrentScene(string[] cmdparams, ref string result)
  157. {
  158. if (m_currentScene == null)
  159. {
  160. bool success = true;
  161. foreach (Scene scene in m_localScenes)
  162. {
  163. if (!scene.Terrain.RunTerrainCmd(cmdparams, ref result, scene.RegionInfo.RegionName))
  164. {
  165. success = false;
  166. }
  167. // Messy way of preventing us printing out the same help text for each scene
  168. if (cmdparams.Length <= 0 || cmdparams[0] == "help")
  169. {
  170. break;
  171. }
  172. }
  173. return success;
  174. }
  175. else
  176. {
  177. return m_currentScene.Terrain.RunTerrainCmd(cmdparams, ref result, m_currentScene.RegionInfo.RegionName);
  178. }
  179. }
  180. public void SendCommandToCurrentSceneScripts(string[] cmdparams)
  181. {
  182. ForEachCurrentScene(delegate(Scene scene) { scene.SendCommandToPlugins(cmdparams); });
  183. }
  184. public void SetBypassPermissionsOnCurrentScene(bool bypassPermissions)
  185. {
  186. ForEachCurrentScene(delegate(Scene scene) { scene.PermissionsMngr.BypassPermissions = bypassPermissions; });
  187. }
  188. private void ForEachCurrentScene(Action<Scene> func)
  189. {
  190. if (m_currentScene == null)
  191. {
  192. m_localScenes.ForEach(func);
  193. }
  194. else
  195. {
  196. func(m_currentScene);
  197. }
  198. }
  199. public void RestartCurrentScene()
  200. {
  201. ForEachCurrentScene(delegate(Scene scene) { scene.RestartNow(); });
  202. }
  203. public void BackupCurrentScene()
  204. {
  205. ForEachCurrentScene(delegate(Scene scene) { scene.Backup(); });
  206. }
  207. public void HandleAlertCommandOnCurrentScene(string[] cmdparams)
  208. {
  209. ForEachCurrentScene(delegate(Scene scene) { scene.HandleAlertCommand(cmdparams); });
  210. }
  211. public void SendGeneralMessage(string msg)
  212. {
  213. ForEachCurrentScene(delegate(Scene scene) { scene.SendGeneralAlert(msg); });
  214. }
  215. public bool TrySetCurrentScene(string regionName)
  216. {
  217. if ((String.Compare(regionName, "root") == 0) || (String.Compare(regionName, "..") == 0))
  218. {
  219. m_currentScene = null;
  220. return true;
  221. }
  222. else
  223. {
  224. Console.WriteLine("Searching for Region: '" + regionName + "'");
  225. foreach (Scene scene in m_localScenes)
  226. {
  227. if (String.Compare(scene.RegionInfo.RegionName, regionName, true) == 0)
  228. {
  229. m_currentScene = scene;
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. }
  236. public bool TryGetScene(string regionName, out Scene scene)
  237. {
  238. foreach (Scene mscene in m_localScenes)
  239. {
  240. if (String.Compare(mscene.RegionInfo.RegionName, regionName, true) == 0)
  241. {
  242. scene = mscene;
  243. return true;
  244. }
  245. }
  246. scene = null;
  247. return false;
  248. }
  249. public bool TryGetScene(LLUUID regionID, out Scene scene)
  250. {
  251. foreach (Scene mscene in m_localScenes)
  252. {
  253. if (mscene.RegionInfo.RegionID == regionID)
  254. {
  255. scene = mscene;
  256. return true;
  257. }
  258. }
  259. scene = null;
  260. return false;
  261. }
  262. public void SetDebugPacketOnCurrentScene(int newDebug)
  263. {
  264. ForEachCurrentScene(delegate(Scene scene)
  265. {
  266. List<ScenePresence> scenePresences = scene.GetScenePresences();
  267. foreach (ScenePresence scenePresence in scenePresences)
  268. {
  269. if (!scenePresence.IsChildAgent)
  270. {
  271. m_log.ErrorFormat("Packet debug for {0} {1} set to {2}",
  272. scenePresence.Firstname,
  273. scenePresence.Lastname,
  274. newDebug);
  275. scenePresence.ControllingClient.SetDebug(newDebug);
  276. }
  277. }
  278. });
  279. }
  280. public List<ScenePresence> GetCurrentSceneAvatars()
  281. {
  282. List<ScenePresence> avatars = new List<ScenePresence>();
  283. ForEachCurrentScene(delegate(Scene scene)
  284. {
  285. List<ScenePresence> scenePrescences = scene.GetScenePresences();
  286. foreach (ScenePresence scenePrescence in scenePrescences)
  287. {
  288. if (!scenePrescence.IsChildAgent)
  289. {
  290. avatars.Add(scenePrescence);
  291. }
  292. }
  293. });
  294. return avatars;
  295. }
  296. public RegionInfo GetRegionInfo(ulong regionHandle)
  297. {
  298. foreach (Scene scene in m_localScenes)
  299. {
  300. if (scene.RegionInfo.RegionHandle == regionHandle)
  301. {
  302. return scene.RegionInfo;
  303. }
  304. }
  305. return null;
  306. }
  307. public void SetCurrentSceneTimePhase(int timePhase)
  308. {
  309. ForEachCurrentScene(delegate(Scene scene)
  310. {
  311. scene.SetTimePhase(
  312. timePhase)
  313. ;
  314. });
  315. }
  316. public void ForceCurrentSceneClientUpdate()
  317. {
  318. ForEachCurrentScene(delegate(Scene scene) { scene.ForceClientUpdate(); });
  319. }
  320. public void HandleEditCommandOnCurrentScene(string[] cmdparams)
  321. {
  322. ForEachCurrentScene(delegate(Scene scene) { scene.HandleEditCommand(cmdparams); });
  323. }
  324. public bool TryGetAvatar(LLUUID avatarId, out ScenePresence avatar)
  325. {
  326. foreach (Scene scene in m_localScenes)
  327. {
  328. if (scene.TryGetAvatar(avatarId, out avatar))
  329. {
  330. return true;
  331. }
  332. }
  333. avatar = null;
  334. return false;
  335. }
  336. public bool TryGetAvatarsScene(LLUUID avatarId, out Scene scene)
  337. {
  338. ScenePresence avatar = null;
  339. foreach (Scene mScene in m_localScenes)
  340. {
  341. if (mScene.TryGetAvatar(avatarId, out avatar))
  342. {
  343. scene = mScene;
  344. return true;
  345. }
  346. }
  347. scene = null;
  348. return false;
  349. }
  350. public void CloseScene(Scene scene)
  351. {
  352. m_localScenes.Remove(scene);
  353. scene.Close();
  354. }
  355. public bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
  356. {
  357. foreach (Scene scene in m_localScenes)
  358. {
  359. if (scene.TryGetAvatarByName(avatarName, out avatar))
  360. {
  361. return true;
  362. }
  363. }
  364. avatar = null;
  365. return false;
  366. }
  367. public void ForEachScene(Action<Scene> action)
  368. {
  369. m_localScenes.ForEach(action);
  370. }
  371. }
  372. }