OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
SceneManager.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using System.Net;
31 using System.Reflection;
32 using OpenMetaverse;
33 using log4net;
34 using OpenSim.Framework;
35 using OpenSim.Region.Framework.Interfaces;
36 
37 namespace OpenSim.Region.Framework.Scenes
38 {
39  public delegate void RestartSim(RegionInfo thisregion);
40 
44  public class SceneManager
45  {
46  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47 
48  public event RestartSim OnRestartSim;
49 
54  public event Action<SceneManager> OnRegionsReadyStatusChange;
55 
59  public bool AllRegionsReady
60  {
61  get
62  {
63  return m_allRegionsReady;
64  }
65 
66  private set
67  {
68  if (m_allRegionsReady != value)
69  {
70  m_allRegionsReady = value;
71  Action<SceneManager> handler = OnRegionsReadyStatusChange;
72  if (handler != null)
73  {
74  foreach (Action<SceneManager> d in handler.GetInvocationList())
75  {
76  try
77  {
78  d(this);
79  }
80  catch (Exception e)
81  {
82  m_log.ErrorFormat("[SCENE MANAGER]: Delegate for OnRegionsReadyStatusChange failed - continuing {0} - {1}",
83  e.Message, e.StackTrace);
84  }
85  }
86  }
87  }
88  }
89  }
90  private bool m_allRegionsReady;
91 
92  private static SceneManager m_instance = null;
93  public static SceneManager Instance
94  {
95  get {
96  if (m_instance == null)
97  m_instance = new SceneManager();
98  return m_instance;
99  }
100  }
101 
102  private readonly DoubleDictionary<UUID, string, Scene> m_localScenes = new DoubleDictionary<UUID, string, Scene>();
103 
104  public List<Scene> Scenes
105  {
106  get { return new List<Scene>(m_localScenes.FindAll(delegate(Scene s) { return true; })); }
107  }
108 
115  public Scene CurrentScene { get; private set; }
116 
117  public Scene CurrentOrFirstScene
118  {
119  get
120  {
121  if (CurrentScene == null)
122  {
123  List<Scene> sceneList = Scenes;
124  if (sceneList.Count == 0)
125  return null;
126  return sceneList[0];
127  }
128  else
129  {
130  return CurrentScene;
131  }
132  }
133  }
134 
135  public SceneManager()
136  {
137  m_instance = this;
138  m_localScenes = new DoubleDictionary<UUID, string, Scene>();
139  }
140 
141  public void Close()
142  {
143  List<Scene> localScenes = null;
144 
145  lock (m_localScenes)
146  {
147  localScenes = Scenes;
148  }
149 
150  for (int i = 0; i < localScenes.Count; i++)
151  {
152  localScenes[i].Close();
153  }
154  }
155 
156  public void Close(Scene cscene)
157  {
158  if (!m_localScenes.ContainsKey(cscene.RegionInfo.RegionID))
159  return;
160  cscene.Close();
161  }
162 
163  public void Add(Scene scene)
164  {
165  lock (m_localScenes)
166  m_localScenes.Add(scene.RegionInfo.RegionID, scene.RegionInfo.RegionName, scene);
167 
168  scene.OnRestart += HandleRestart;
169  scene.EventManager.OnRegionReadyStatusChange += HandleRegionReadyStatusChange;
170  }
171 
172  public void HandleRestart(RegionInfo rdata)
173  {
174  Scene restartedScene = null;
175 
176  lock (m_localScenes)
177  {
178  m_localScenes.TryGetValue(rdata.RegionID, out restartedScene);
179  m_localScenes.Remove(rdata.RegionID);
180  }
181 
182  // If the currently selected scene has been restarted, then we can't reselect here since we the scene
183  // hasn't yet been recreated. We will have to leave this to the caller.
184  if (CurrentScene == restartedScene)
185  CurrentScene = null;
186 
187  // Send signal to main that we're restarting this sim.
188  OnRestartSim(rdata);
189  }
190 
191  private void HandleRegionReadyStatusChange(IScene scene)
192  {
193  lock (m_localScenes)
194  AllRegionsReady = m_localScenes.FindAll(s => !s.Ready).Count == 0;
195  }
196 
197  public void SendSimOnlineNotification(ulong regionHandle)
198  {
199  RegionInfo Result = null;
200 
201  Scene s = m_localScenes.FindValue(delegate(Scene x)
202  {
203  if (x.RegionInfo.RegionHandle == regionHandle)
204  return true;
205  return false;
206  });
207 
208  if (s != null)
209  {
210  List<Scene> sceneList = Scenes;
211 
212  for (int i = 0; i < sceneList.Count; i++)
213  {
214  if (sceneList[i]!= s)
215  {
216  // Inform other regions to tell their avatar about me
217  //sceneList[i].OtherRegionUp(Result);
218  }
219  }
220  }
221  else
222  {
223  m_log.Error("[REGION]: Unable to notify Other regions of this Region coming up");
224  }
225  }
226 
231  public void SaveCurrentSceneToXml(string filename)
232  {
233  IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
234  if (serialiser != null)
235  serialiser.SavePrimsToXml(CurrentOrFirstScene, filename);
236  }
237 
244  public void LoadCurrentSceneFromXml(string filename, bool generateNewIDs, Vector3 loadOffset)
245  {
246  IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
247  if (serialiser != null)
248  serialiser.LoadPrimsFromXml(CurrentOrFirstScene, filename, generateNewIDs, loadOffset);
249  }
250 
255  public void SaveCurrentSceneToXml2(string filename)
256  {
257  IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
258  if (serialiser != null)
259  serialiser.SavePrimsToXml2(CurrentOrFirstScene, filename);
260  }
261 
262  public void SaveNamedPrimsToXml2(string primName, string filename)
263  {
264  IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
265  if (serialiser != null)
266  serialiser.SaveNamedPrimsToXml2(CurrentOrFirstScene, primName, filename);
267  }
268 
272  public void LoadCurrentSceneFromXml2(string filename)
273  {
274  IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
275  if (serialiser != null)
276  serialiser.LoadPrimsFromXml2(CurrentOrFirstScene, filename);
277  }
278 
284  public void SaveCurrentSceneToArchive(string[] cmdparams)
285  {
286  IRegionArchiverModule archiver = CurrentOrFirstScene.RequestModuleInterface<IRegionArchiverModule>();
287  if (archiver != null)
288  archiver.HandleSaveOarConsoleCommand(string.Empty, cmdparams);
289  }
290 
296  public void LoadArchiveToCurrentScene(string[] cmdparams)
297  {
298  IRegionArchiverModule archiver = CurrentOrFirstScene.RequestModuleInterface<IRegionArchiverModule>();
299  if (archiver != null)
300  archiver.HandleLoadOarConsoleCommand(string.Empty, cmdparams);
301  }
302 
304  {
305  return CurrentOrFirstScene.Heightmap.SaveToXmlString();
306  }
307 
308  public void LoadCurrenSceneMapFromXmlString(string mapData)
309  {
310  CurrentOrFirstScene.Heightmap.LoadFromXmlString(mapData);
311  }
312 
313  public void SendCommandToPluginModules(string[] cmdparams)
314  {
315  ForEachSelectedScene(delegate(Scene scene) { scene.SendCommandToPlugins(cmdparams); });
316  }
317 
318  public void SetBypassPermissionsOnCurrentScene(bool bypassPermissions)
319  {
320  ForEachSelectedScene(delegate(Scene scene) { scene.Permissions.SetBypassPermissions(bypassPermissions); });
321  }
322 
323  public void ForEachSelectedScene(Action<Scene> func)
324  {
325  if (CurrentScene == null)
326  ForEachScene(func);
327  else
328  func(CurrentScene);
329  }
330 
331  public void RestartCurrentScene()
332  {
333  ForEachSelectedScene(delegate(Scene scene) { scene.RestartNow(); });
334  }
335 
336  public void BackupCurrentScene()
337  {
338  ForEachSelectedScene(delegate(Scene scene) { scene.Backup(true); });
339  }
340 
341  public bool TrySetCurrentScene(string regionName)
342  {
343  if ((String.Compare(regionName, "root") == 0)
344  || (String.Compare(regionName, "..") == 0)
345  || (String.Compare(regionName, "/") == 0))
346  {
347  CurrentScene = null;
348  return true;
349  }
350  else
351  {
352  Scene s;
353 
354  if (m_localScenes.TryGetValue(regionName, out s))
355  {
356  CurrentScene = s;
357  return true;
358  }
359 
360  return false;
361  }
362  }
363 
364  public bool TrySetCurrentScene(UUID regionID)
365  {
366 // m_log.Debug("Searching for Region: '" + regionID + "'");
367 
368  Scene s;
369 
370  if (m_localScenes.TryGetValue(regionID, out s))
371  {
372  CurrentScene = s;
373  return true;
374  }
375 
376  return false;
377  }
378 
379  public bool TryGetScene(string regionName, out Scene scene)
380  {
381  return m_localScenes.TryGetValue(regionName, out scene);
382  }
383 
384  public bool TryGetScene(UUID regionID, out Scene scene)
385  {
386  return m_localScenes.TryGetValue(regionID, out scene);
387  }
388 
389  public bool TryGetScene(uint locX, uint locY, out Scene scene)
390  {
391  List<Scene> sceneList = Scenes;
392  foreach (Scene mscene in sceneList)
393  {
394  if (mscene.RegionInfo.RegionLocX == locX &&
395  mscene.RegionInfo.RegionLocY == locY)
396  {
397  scene = mscene;
398  return true;
399  }
400  }
401 
402  scene = null;
403  return false;
404  }
405 
406  public bool TryGetScene(IPEndPoint ipEndPoint, out Scene scene)
407  {
408  List<Scene> sceneList = Scenes;
409  foreach (Scene mscene in sceneList)
410  {
411  if ((mscene.RegionInfo.InternalEndPoint.Equals(ipEndPoint.Address)) &&
412  (mscene.RegionInfo.InternalEndPoint.Port == ipEndPoint.Port))
413  {
414  scene = mscene;
415  return true;
416  }
417  }
418 
419  scene = null;
420  return false;
421  }
422 
423  public List<ScenePresence> GetCurrentSceneAvatars()
424  {
425  List<ScenePresence> avatars = new List<ScenePresence>();
426 
427  ForEachSelectedScene(
428  delegate(Scene scene)
429  {
430  scene.ForEachRootScenePresence(delegate(ScenePresence scenePresence)
431  {
432  avatars.Add(scenePresence);
433  });
434  }
435  );
436 
437  return avatars;
438  }
439 
440  public List<ScenePresence> GetCurrentScenePresences()
441  {
442  List<ScenePresence> presences = new List<ScenePresence>();
443 
444  ForEachSelectedScene(delegate(Scene scene)
445  {
446  scene.ForEachScenePresence(delegate(ScenePresence sp)
447  {
448  presences.Add(sp);
449  });
450  });
451 
452  return presences;
453  }
454 
455  public RegionInfo GetRegionInfo(UUID regionID)
456  {
457  Scene s;
458  if (m_localScenes.TryGetValue(regionID, out s))
459  {
460  return s.RegionInfo;
461  }
462 
463  return null;
464  }
465 
467  {
468  ForEachSelectedScene(delegate(Scene scene) { scene.ForceClientUpdate(); });
469  }
470 
471  public void HandleEditCommandOnCurrentScene(string[] cmdparams)
472  {
473  ForEachSelectedScene(delegate(Scene scene) { scene.HandleEditCommand(cmdparams); });
474  }
475 
476  public bool TryGetScenePresence(UUID avatarId, out ScenePresence avatar)
477  {
478  List<Scene> sceneList = Scenes;
479  foreach (Scene scene in sceneList)
480  {
481  if (scene.TryGetScenePresence(avatarId, out avatar))
482  {
483  return true;
484  }
485  }
486 
487  avatar = null;
488  return false;
489  }
490 
491  public bool TryGetRootScenePresence(UUID avatarId, out ScenePresence avatar)
492  {
493  List<Scene> sceneList = Scenes;
494  foreach (Scene scene in sceneList)
495  {
496  avatar = scene.GetScenePresence(avatarId);
497 
498  if (avatar != null && !avatar.IsChildAgent)
499  return true;
500  }
501 
502  avatar = null;
503  return false;
504  }
505 
506  public void CloseScene(Scene scene)
507  {
508  lock (m_localScenes)
509  m_localScenes.Remove(scene.RegionInfo.RegionID);
510 
511  scene.Close();
512  }
513 
514  public bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
515  {
516  List<Scene> sceneList = Scenes;
517  foreach (Scene scene in sceneList)
518  {
519  if (scene.TryGetAvatarByName(avatarName, out avatar))
520  {
521  return true;
522  }
523  }
524 
525  avatar = null;
526  return false;
527  }
528 
529  public bool TryGetRootScenePresenceByName(string firstName, string lastName, out ScenePresence sp)
530  {
531  List<Scene> sceneList = Scenes;
532  foreach (Scene scene in sceneList)
533  {
534  sp = scene.GetScenePresence(firstName, lastName);
535  if (sp != null && !sp.IsChildAgent)
536  return true;
537  }
538 
539  sp = null;
540  return false;
541  }
542 
543  public void ForEachScene(Action<Scene> action)
544  {
545  List<Scene> sceneList = Scenes;
546  sceneList.ForEach(action);
547  }
548  }
549 }
bool TryGetScene(IPEndPoint ipEndPoint, out Scene scene)
bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
void LoadCurrentSceneFromXml2(string filename)
Load an xml file of prims in OpenSimulator's current 'xml2' file format to the current scene ...
List< ScenePresence > GetCurrentScenePresences()
List< ScenePresence > GetCurrentSceneAvatars()
OpenSim.Framework.RegionInfo RegionInfo
void SaveCurrentSceneToArchive(string[] cmdparams)
Save the current scene to an OpenSimulator archive. This archive will eventually include the prim's a...
void LoadArchiveToCurrentScene(string[] cmdparams)
Load an OpenSim archive into the current scene. This will load both the shapes of the prims and uploa...
bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
Definition: Scene.cs:5406
bool TryGetScene(UUID regionID, out Scene scene)
void SetBypassPermissionsOnCurrentScene(bool bypassPermissions)
void HandleEditCommandOnCurrentScene(string[] cmdparams)
bool TryGetScene(uint locX, uint locY, out Scene scene)
void LoadCurrentSceneFromXml(string filename, bool generateNewIDs, Vector3 loadOffset)
Load an xml file of prims in OpenSimulator's original 'xml' file format to the current scene ...
void SaveCurrentSceneToXml(string filename)
Save the prims in the current scene to an xml file in OpenSimulator's original 'xml' format ...
bool TryGetScenePresence(UUID avatarId, out ScenePresence avatar)
delegate void RestartSim(RegionInfo thisregion)
void ForEachSelectedScene(Action< Scene > func)
Manager for adding, closing and restarting scenes.
Definition: SceneManager.cs:44
bool TryGetRootScenePresenceByName(string firstName, string lastName, out ScenePresence sp)
override bool TryGetScenePresence(UUID agentID, out ScenePresence sp)
Try to get a scene presence from the scene
Definition: Scene.cs:5401
void LoadCurrenSceneMapFromXmlString(string mapData)
void SendCommandToPluginModules(string[] cmdparams)
void SaveNamedPrimsToXml2(string primName, string filename)
Action< SceneManager > OnRegionsReadyStatusChange
Fired when either all regions are ready for use or at least one region has become unready for use whe...
Definition: SceneManager.cs:54
void SaveCurrentSceneToXml2(string filename)
Save the prims in the current scene to an xml file in OpenSimulator's current 'xml2' format ...
bool TryGetRootScenePresence(UUID avatarId, out ScenePresence avatar)
uint RegionLocY
The y co-ordinate of this region in map tiles (e.g. 1000). Coordinate is scaled as world coordinates ...
Definition: RegionInfo.cs:496
void ForEachScene(Action< Scene > action)
uint RegionLocX
The x co-ordinate of this region in map tiles (e.g. 1000). Coordinate is scaled as world coordinates ...
Definition: RegionInfo.cs:485
bool TryGetScene(string regionName, out Scene scene)