opensim-development – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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  
41 /// <summary>
42 /// Manager for adding, closing and restarting scenes.
43 /// </summary>
44 public class SceneManager
45 {
46 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47  
48 public event RestartSim OnRestartSim;
49  
50 /// <summary>
51 /// Fired when either all regions are ready for use or at least one region has become unready for use where
52 /// previously all regions were ready.
53 /// </summary>
54 public event Action<SceneManager> OnRegionsReadyStatusChange;
55  
56 /// <summary>
57 /// Are all regions ready for use?
58 /// </summary>
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 List<Scene> m_localScenes = new List<Scene>();
103  
104 public List<Scene> Scenes
105 {
106 get { return new List<Scene>(m_localScenes); }
107 }
108  
109 /// <summary>
110 /// Scene selected from the console.
111 /// </summary>
112 /// <value>
113 /// If null, then all scenes are considered selected (signalled as "Root" on the console).
114 /// </value>
115 public Scene CurrentScene { get; private set; }
116  
117 public Scene CurrentOrFirstScene
118 {
119 get
120 {
121 if (CurrentScene == null)
122 {
123 lock (m_localScenes)
124 {
125 if (m_localScenes.Count > 0)
126 return m_localScenes[0];
127 else
128 return null;
129 }
130 }
131 else
132 {
133 return CurrentScene;
134 }
135 }
136 }
137  
138 public SceneManager()
139 {
140 m_instance = this;
141 m_localScenes = new List<Scene>();
142 }
143  
144 public void Close()
145 {
146 lock (m_localScenes)
147 {
148 for (int i = 0; i < m_localScenes.Count; i++)
149 {
150 m_localScenes[i].Close();
151 }
152 }
153 }
154  
155 public void Close(Scene cscene)
156 {
157 lock (m_localScenes)
158 {
159 if (m_localScenes.Contains(cscene))
160 {
161 for (int i = 0; i < m_localScenes.Count; i++)
162 {
163 if (m_localScenes[i].Equals(cscene))
164 {
165 m_localScenes[i].Close();
166 }
167 }
168 }
169 }
170 }
171  
172 public void Add(Scene scene)
173 {
174 lock (m_localScenes)
175 m_localScenes.Add(scene);
176  
177 scene.OnRestart += HandleRestart;
178 scene.EventManager.OnRegionReadyStatusChange += HandleRegionReadyStatusChange;
179 }
180  
181 public void HandleRestart(RegionInfo rdata)
182 {
183 Scene restartedScene = null;
184  
185 lock (m_localScenes)
186 {
187 for (int i = 0; i < m_localScenes.Count; i++)
188 {
189 if (rdata.RegionName == m_localScenes[i].RegionInfo.RegionName)
190 {
191 restartedScene = m_localScenes[i];
192 m_localScenes.RemoveAt(i);
193 break;
194 }
195 }
196 }
197  
198 // If the currently selected scene has been restarted, then we can't reselect here since we the scene
199 // hasn't yet been recreated. We will have to leave this to the caller.
200 if (CurrentScene == restartedScene)
201 CurrentScene = null;
202  
203 // Send signal to main that we're restarting this sim.
204 OnRestartSim(rdata);
205 }
206  
207 private void HandleRegionReadyStatusChange(IScene scene)
208 {
209 lock (m_localScenes)
210 AllRegionsReady = m_localScenes.TrueForAll(s => s.Ready);
211 }
212  
213 public void SendSimOnlineNotification(ulong regionHandle)
214 {
215 RegionInfo Result = null;
216  
217 lock (m_localScenes)
218 {
219 for (int i = 0; i < m_localScenes.Count; i++)
220 {
221 if (m_localScenes[i].RegionInfo.RegionHandle == regionHandle)
222 {
223 // Inform other regions to tell their avatar about me
224 Result = m_localScenes[i].RegionInfo;
225 }
226 }
227  
228 if (Result != null)
229 {
230 for (int i = 0; i < m_localScenes.Count; i++)
231 {
232 if (m_localScenes[i].RegionInfo.RegionHandle != regionHandle)
233 {
234 // Inform other regions to tell their avatar about me
235 //m_localScenes[i].OtherRegionUp(Result);
236 }
237 }
238 }
239 else
240 {
241 m_log.Error("[REGION]: Unable to notify Other regions of this Region coming up");
242 }
243 }
244 }
245  
246 /// <summary>
247 /// Save the prims in the current scene to an xml file in OpenSimulator's original 'xml' format
248 /// </summary>
249 /// <param name="filename"></param>
250 public void SaveCurrentSceneToXml(string filename)
251 {
252 IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
253 if (serialiser != null)
254 serialiser.SavePrimsToXml(CurrentOrFirstScene, filename);
255 }
256  
257 /// <summary>
258 /// Load an xml file of prims in OpenSimulator's original 'xml' file format to the current scene
259 /// </summary>
260 /// <param name="filename"></param>
261 /// <param name="generateNewIDs"></param>
262 /// <param name="loadOffset"></param>
263 public void LoadCurrentSceneFromXml(string filename, bool generateNewIDs, Vector3 loadOffset)
264 {
265 IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
266 if (serialiser != null)
267 serialiser.LoadPrimsFromXml(CurrentOrFirstScene, filename, generateNewIDs, loadOffset);
268 }
269  
270 /// <summary>
271 /// Save the prims in the current scene to an xml file in OpenSimulator's current 'xml2' format
272 /// </summary>
273 /// <param name="filename"></param>
274 public void SaveCurrentSceneToXml2(string filename)
275 {
276 IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
277 if (serialiser != null)
278 serialiser.SavePrimsToXml2(CurrentOrFirstScene, filename);
279 }
280  
281 public void SaveNamedPrimsToXml2(string primName, string filename)
282 {
283 IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
284 if (serialiser != null)
285 serialiser.SaveNamedPrimsToXml2(CurrentOrFirstScene, primName, filename);
286 }
287  
288 /// <summary>
289 /// Load an xml file of prims in OpenSimulator's current 'xml2' file format to the current scene
290 /// </summary>
291 public void LoadCurrentSceneFromXml2(string filename)
292 {
293 IRegionSerialiserModule serialiser = CurrentOrFirstScene.RequestModuleInterface<IRegionSerialiserModule>();
294 if (serialiser != null)
295 serialiser.LoadPrimsFromXml2(CurrentOrFirstScene, filename);
296 }
297  
298 /// <summary>
299 /// Save the current scene to an OpenSimulator archive. This archive will eventually include the prim's assets
300 /// as well as the details of the prims themselves.
301 /// </summary>
302 /// <param name="cmdparams"></param>
303 public void SaveCurrentSceneToArchive(string[] cmdparams)
304 {
305 IRegionArchiverModule archiver = CurrentOrFirstScene.RequestModuleInterface<IRegionArchiverModule>();
306 if (archiver != null)
307 archiver.HandleSaveOarConsoleCommand(string.Empty, cmdparams);
308 }
309  
310 /// <summary>
311 /// Load an OpenSim archive into the current scene. This will load both the shapes of the prims and upload
312 /// their assets to the asset service.
313 /// </summary>
314 /// <param name="cmdparams"></param>
315 public void LoadArchiveToCurrentScene(string[] cmdparams)
316 {
317 IRegionArchiverModule archiver = CurrentOrFirstScene.RequestModuleInterface<IRegionArchiverModule>();
318 if (archiver != null)
319 archiver.HandleLoadOarConsoleCommand(string.Empty, cmdparams);
320 }
321  
322 public string SaveCurrentSceneMapToXmlString()
323 {
324 return CurrentOrFirstScene.Heightmap.SaveToXmlString();
325 }
326  
327 public void LoadCurrenSceneMapFromXmlString(string mapData)
328 {
329 CurrentOrFirstScene.Heightmap.LoadFromXmlString(mapData);
330 }
331  
332 public void SendCommandToPluginModules(string[] cmdparams)
333 {
334 ForEachSelectedScene(delegate(Scene scene) { scene.SendCommandToPlugins(cmdparams); });
335 }
336  
337 public void SetBypassPermissionsOnCurrentScene(bool bypassPermissions)
338 {
339 ForEachSelectedScene(delegate(Scene scene) { scene.Permissions.SetBypassPermissions(bypassPermissions); });
340 }
341  
342 public void ForEachSelectedScene(Action<Scene> func)
343 {
344 if (CurrentScene == null)
345 ForEachScene(func);
346 else
347 func(CurrentScene);
348 }
349  
350 public void RestartCurrentScene()
351 {
352 ForEachSelectedScene(delegate(Scene scene) { scene.RestartNow(); });
353 }
354  
355 public void BackupCurrentScene()
356 {
357 ForEachSelectedScene(delegate(Scene scene) { scene.Backup(true); });
358 }
359  
360 public bool TrySetCurrentScene(string regionName)
361 {
362 if ((String.Compare(regionName, "root") == 0)
363 || (String.Compare(regionName, "..") == 0)
364 || (String.Compare(regionName, "/") == 0))
365 {
366 CurrentScene = null;
367 return true;
368 }
369 else
370 {
371 lock (m_localScenes)
372 {
373 foreach (Scene scene in m_localScenes)
374 {
375 if (String.Compare(scene.RegionInfo.RegionName, regionName, true) == 0)
376 {
377 CurrentScene = scene;
378 return true;
379 }
380 }
381 }
382  
383 return false;
384 }
385 }
386  
387 public bool TrySetCurrentScene(UUID regionID)
388 {
389 m_log.Debug("Searching for Region: '" + regionID + "'");
390  
391 lock (m_localScenes)
392 {
393 foreach (Scene scene in m_localScenes)
394 {
395 if (scene.RegionInfo.RegionID == regionID)
396 {
397 CurrentScene = scene;
398 return true;
399 }
400 }
401 }
402  
403 return false;
404 }
405  
406 public bool TryGetScene(string regionName, out Scene scene)
407 {
408 lock (m_localScenes)
409 {
410 foreach (Scene mscene in m_localScenes)
411 {
412 if (String.Compare(mscene.RegionInfo.RegionName, regionName, true) == 0)
413 {
414 scene = mscene;
415 return true;
416 }
417 }
418 }
419  
420 scene = null;
421 return false;
422 }
423  
424 public bool TryGetScene(UUID regionID, out Scene scene)
425 {
426 lock (m_localScenes)
427 {
428 foreach (Scene mscene in m_localScenes)
429 {
430 if (mscene.RegionInfo.RegionID == regionID)
431 {
432 scene = mscene;
433 return true;
434 }
435 }
436 }
437  
438 scene = null;
439 return false;
440 }
441  
442 public bool TryGetScene(uint locX, uint locY, out Scene scene)
443 {
444 lock (m_localScenes)
445 {
446 foreach (Scene mscene in m_localScenes)
447 {
448 if (mscene.RegionInfo.RegionLocX == locX &&
449 mscene.RegionInfo.RegionLocY == locY)
450 {
451 scene = mscene;
452 return true;
453 }
454 }
455 }
456  
457 scene = null;
458 return false;
459 }
460  
461 public bool TryGetScene(IPEndPoint ipEndPoint, out Scene scene)
462 {
463 lock (m_localScenes)
464 {
465 foreach (Scene mscene in m_localScenes)
466 {
467 if ((mscene.RegionInfo.InternalEndPoint.Equals(ipEndPoint.Address)) &&
468 (mscene.RegionInfo.InternalEndPoint.Port == ipEndPoint.Port))
469 {
470 scene = mscene;
471 return true;
472 }
473 }
474 }
475  
476 scene = null;
477 return false;
478 }
479  
480 public List<ScenePresence> GetCurrentSceneAvatars()
481 {
482 List<ScenePresence> avatars = new List<ScenePresence>();
483  
484 ForEachSelectedScene(
485 delegate(Scene scene)
486 {
487 scene.ForEachRootScenePresence(delegate(ScenePresence scenePresence)
488 {
489 avatars.Add(scenePresence);
490 });
491 }
492 );
493  
494 return avatars;
495 }
496  
497 public List<ScenePresence> GetCurrentScenePresences()
498 {
499 List<ScenePresence> presences = new List<ScenePresence>();
500  
501 ForEachSelectedScene(delegate(Scene scene)
502 {
503 scene.ForEachScenePresence(delegate(ScenePresence sp)
504 {
505 presences.Add(sp);
506 });
507 });
508  
509 return presences;
510 }
511  
512 public RegionInfo GetRegionInfo(UUID regionID)
513 {
514 lock (m_localScenes)
515 {
516 foreach (Scene scene in m_localScenes)
517 {
518 if (scene.RegionInfo.RegionID == regionID)
519 {
520 return scene.RegionInfo;
521 }
522 }
523 }
524  
525 return null;
526 }
527  
528 public void ForceCurrentSceneClientUpdate()
529 {
530 ForEachSelectedScene(delegate(Scene scene) { scene.ForceClientUpdate(); });
531 }
532  
533 public void HandleEditCommandOnCurrentScene(string[] cmdparams)
534 {
535 ForEachSelectedScene(delegate(Scene scene) { scene.HandleEditCommand(cmdparams); });
536 }
537  
538 public bool TryGetScenePresence(UUID avatarId, out ScenePresence avatar)
539 {
540 lock (m_localScenes)
541 {
542 foreach (Scene scene in m_localScenes)
543 {
544 if (scene.TryGetScenePresence(avatarId, out avatar))
545 {
546 return true;
547 }
548 }
549 }
550  
551 avatar = null;
552 return false;
553 }
554  
555 public bool TryGetRootScenePresence(UUID avatarId, out ScenePresence avatar)
556 {
557 lock (m_localScenes)
558 {
559 foreach (Scene scene in m_localScenes)
560 {
561 avatar = scene.GetScenePresence(avatarId);
562  
563 if (avatar != null && !avatar.IsChildAgent)
564 return true;
565 }
566 }
567  
568 avatar = null;
569 return false;
570 }
571  
572 public void CloseScene(Scene scene)
573 {
574 lock (m_localScenes)
575 m_localScenes.Remove(scene);
576  
577 scene.Close();
578 }
579  
580 public bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
581 {
582 lock (m_localScenes)
583 {
584 foreach (Scene scene in m_localScenes)
585 {
586 if (scene.TryGetAvatarByName(avatarName, out avatar))
587 {
588 return true;
589 }
590 }
591 }
592  
593 avatar = null;
594 return false;
595 }
596  
597 public bool TryGetRootScenePresenceByName(string firstName, string lastName, out ScenePresence sp)
598 {
599 lock (m_localScenes)
600 {
601 foreach (Scene scene in m_localScenes)
602 {
603 sp = scene.GetScenePresence(firstName, lastName);
604 if (sp != null && !sp.IsChildAgent)
605 return true;
606 }
607 }
608  
609 sp = null;
610 return false;
611 }
612  
613 public void ForEachScene(Action<Scene> action)
614 {
615 lock (m_localScenes)
616 m_localScenes.ForEach(action);
617 }
618 }
619 }