SceneManager.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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="filename"></param>
  220. public void SaveCurrentSceneToArchive(string filename)
  221. {
  222. IRegionArchiverModule archiver = CurrentOrFirstScene.RequestModuleInterface<IRegionArchiverModule>();
  223. if (archiver != null)
  224. archiver.ArchiveRegion(filename);
  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="filename"></param>
  231. public void LoadArchiveToCurrentScene(string filename)
  232. {
  233. IRegionArchiverModule archiver = CurrentOrFirstScene.RequestModuleInterface<IRegionArchiverModule>();
  234. if (archiver != null)
  235. archiver.DearchiveRegion(filename);
  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(delegate(Scene scene)
  369. {
  370. List<ScenePresence> scenePresences = scene.GetScenePresences();
  371. foreach (ScenePresence scenePresence in scenePresences)
  372. {
  373. if (!scenePresence.IsChildAgent)
  374. {
  375. m_log.ErrorFormat("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. public List<ScenePresence> GetCurrentSceneAvatars()
  385. {
  386. List<ScenePresence> avatars = new List<ScenePresence>();
  387. ForEachCurrentScene(delegate(Scene scene)
  388. {
  389. List<ScenePresence> scenePresences = scene.GetScenePresences();
  390. foreach (ScenePresence scenePresence in scenePresences)
  391. {
  392. if (!scenePresence.IsChildAgent)
  393. {
  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. List<ScenePresence> scenePresences = scene.GetScenePresences();
  406. presences.AddRange(scenePresences);
  407. });
  408. return presences;
  409. }
  410. public RegionInfo GetRegionInfo(ulong regionHandle)
  411. {
  412. foreach (Scene scene in m_localScenes)
  413. {
  414. if (scene.RegionInfo.RegionHandle == regionHandle)
  415. {
  416. return scene.RegionInfo;
  417. }
  418. }
  419. return null;
  420. }
  421. public void ForceCurrentSceneClientUpdate()
  422. {
  423. ForEachCurrentScene(delegate(Scene scene) { scene.ForceClientUpdate(); });
  424. }
  425. public void HandleEditCommandOnCurrentScene(string[] cmdparams)
  426. {
  427. ForEachCurrentScene(delegate(Scene scene) { scene.HandleEditCommand(cmdparams); });
  428. }
  429. public bool TryGetAvatar(UUID avatarId, out ScenePresence avatar)
  430. {
  431. foreach (Scene scene in m_localScenes)
  432. {
  433. if (scene.TryGetAvatar(avatarId, out avatar))
  434. {
  435. return true;
  436. }
  437. }
  438. avatar = null;
  439. return false;
  440. }
  441. public bool TryGetAvatarsScene(UUID avatarId, out Scene scene)
  442. {
  443. ScenePresence avatar = null;
  444. foreach (Scene mScene in m_localScenes)
  445. {
  446. if (mScene.TryGetAvatar(avatarId, out avatar))
  447. {
  448. scene = mScene;
  449. return true;
  450. }
  451. }
  452. scene = null;
  453. return false;
  454. }
  455. public void CloseScene(Scene scene)
  456. {
  457. m_localScenes.Remove(scene);
  458. scene.Close();
  459. }
  460. public bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
  461. {
  462. foreach (Scene scene in m_localScenes)
  463. {
  464. if (scene.TryGetAvatarByName(avatarName, out avatar))
  465. {
  466. return true;
  467. }
  468. }
  469. avatar = null;
  470. return false;
  471. }
  472. public void ForEachScene(Action<Scene> action)
  473. {
  474. m_localScenes.ForEach(action);
  475. }
  476. public void CacheJ2kDecode(int threads)
  477. {
  478. if (threads < 1) threads = 1;
  479. IJ2KDecoder m_decoder = m_localScenes[0].RequestModuleInterface<IJ2KDecoder>();
  480. List<UUID> assetRequestList = new List<UUID>();
  481. #region AssetGathering!
  482. foreach (Scene scene in m_localScenes)
  483. {
  484. List<EntityBase> entitles = scene.GetEntities();
  485. foreach (EntityBase entity in entitles)
  486. {
  487. if (entity is SceneObjectGroup)
  488. {
  489. SceneObjectGroup sog = (SceneObjectGroup) entity;
  490. foreach (SceneObjectPart part in sog.Children.Values)
  491. {
  492. if (part.Shape != null)
  493. {
  494. if (part.Shape.TextureEntry.Length > 0)
  495. {
  496. OpenMetaverse.Primitive.TextureEntry te =
  497. new Primitive.TextureEntry(part.Shape.TextureEntry, 0,
  498. part.Shape.TextureEntry.Length);
  499. if (te.DefaultTexture != null) // this has been null for some reason...
  500. {
  501. if (te.DefaultTexture.TextureID != UUID.Zero)
  502. assetRequestList.Add(te.DefaultTexture.TextureID);
  503. }
  504. for (int i=0; i<te.FaceTextures.Length; i++)
  505. {
  506. if (te.FaceTextures[i] != null)
  507. {
  508. if (te.FaceTextures[i].TextureID != UUID.Zero)
  509. {
  510. assetRequestList.Add(te.FaceTextures[i].TextureID);
  511. }
  512. }
  513. }
  514. }
  515. if (part.Shape.SculptTexture != UUID.Zero)
  516. {
  517. assetRequestList.Add(part.Shape.SculptTexture);
  518. }
  519. }
  520. }
  521. }
  522. }
  523. }
  524. #endregion
  525. int entries_per_thread = (assetRequestList.Count / threads) + 1;
  526. UUID[] arrAssetRequestList = assetRequestList.ToArray();
  527. List<UUID[]> arrvalus = new List<UUID[]>();
  528. //split into separate arrays
  529. for (int j = 0; j < threads; j++)
  530. {
  531. List<UUID> val = new List<UUID>();
  532. for (int k = j * entries_per_thread; k < ((j + 1) * entries_per_thread); k++)
  533. {
  534. if (k < arrAssetRequestList.Length)
  535. {
  536. val.Add(arrAssetRequestList[k]);
  537. }
  538. }
  539. arrvalus.Add(val.ToArray());
  540. }
  541. for (int l = 0; l < arrvalus.Count; l++)
  542. {
  543. DecodeThreadContents threadworkItem = new DecodeThreadContents();
  544. threadworkItem.sn = m_localScenes[0];
  545. threadworkItem.j2kdecode = m_decoder;
  546. threadworkItem.arrassets = arrvalus[l];
  547. System.Threading.Thread decodethread =
  548. new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(threadworkItem.run));
  549. threadworkItem.SetThread(decodethread);
  550. decodethread.Priority = System.Threading.ThreadPriority.Lowest;
  551. decodethread.Name = "J2kCacheDecodeThread_" + l + 1;
  552. ThreadTracker.Add(decodethread);
  553. decodethread.Start();
  554. }
  555. }
  556. }
  557. public class DecodeThreadContents
  558. {
  559. public Scene sn;
  560. public UUID[] arrassets;
  561. public IJ2KDecoder j2kdecode;
  562. private System.Threading.Thread thisthread;
  563. public void run(object o)
  564. {
  565. for (int i=0;i<arrassets.Length;i++)
  566. {
  567. AssetBase ab = sn.AssetService.Get(arrassets[i].ToString());
  568. if (ab != null && ab.Data != null)
  569. {
  570. j2kdecode.syncdecode(arrassets[i], ab.Data);
  571. }
  572. }
  573. ThreadTracker.Remove(thisthread);
  574. }
  575. public void SetThread(System.Threading.Thread thr)
  576. {
  577. thisthread = thr;
  578. }
  579. }
  580. }