OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
ArchiveScenesGroup.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.Linq;
31 using System.Text;
32 using OpenSim.Region.Framework.Scenes;
33 using OpenMetaverse;
34 using System.Drawing;
35 
36 namespace OpenSim.Region.CoreModules.World.Archiver
37 {
46  public class ArchiveScenesGroup
47  {
52  public SortedDictionary<uint, SortedDictionary<uint, Scene>> Regions { get; set; }
53 
57  protected Dictionary<UUID, string> m_regionDirs;
58 
62  public Rectangle Rect { get; set; }
63 
64 
66  {
67  Regions = new SortedDictionary<uint, SortedDictionary<uint, Scene>>();
68  m_regionDirs = new Dictionary<UUID, string>();
69  Rect = new Rectangle(0, 0, 0, 0);
70  }
71 
72  public void AddScene(Scene scene)
73  {
74  uint x = scene.RegionInfo.RegionLocX;
75  uint y = scene.RegionInfo.RegionLocY;
76 
77  SortedDictionary<uint, Scene> row;
78  if (!Regions.TryGetValue(y, out row))
79  {
80  row = new SortedDictionary<uint, Scene>();
81  Regions[y] = row;
82  }
83 
84  row[x] = scene;
85  }
86 
91  public void CalcSceneLocations()
92  {
93  if (Regions.Count == 0)
94  return;
95 
96  // Find the bounding rectangle
97 
98  uint firstY = Regions.First().Key;
99  uint lastY = Regions.Last().Key;
100 
101  uint? firstX = null;
102  uint? lastX = null;
103 
104  foreach (SortedDictionary<uint, Scene> row in Regions.Values)
105  {
106  uint curFirstX = row.First().Key;
107  uint curLastX = row.Last().Key;
108 
109  firstX = (firstX == null) ? curFirstX : (firstX < curFirstX) ? firstX : curFirstX;
110  lastX = (lastX == null) ? curLastX : (lastX > curLastX) ? lastX : curLastX;
111  }
112 
113  Rect = new Rectangle((int)firstX, (int)firstY, (int)(lastX - firstX + 1), (int)(lastY - firstY + 1));
114 
115 
116  // Calculate the subdirectory in which each region will be stored in the archive
117 
118  m_regionDirs.Clear();
119  ForEachScene(delegate(Scene scene)
120  {
121  // We add the region's coordinates to ensure uniqueness even if multiple regions have the same name
122  string path = string.Format("{0}_{1}_{2}",
123  scene.RegionInfo.RegionLocX - Rect.X + 1,
124  scene.RegionInfo.RegionLocY - Rect.Y + 1,
125  scene.RegionInfo.RegionName.Replace(' ', '_'));
126  m_regionDirs[scene.RegionInfo.RegionID] = path;
127  });
128  }
129 
135  public string GetRegionDir(UUID regionID)
136  {
137  return m_regionDirs[regionID];
138  }
139 
145  public void ForEachScene(Action<Scene> action)
146  {
147  foreach (SortedDictionary<uint, Scene> row in Regions.Values)
148  {
149  foreach (Scene scene in row.Values)
150  {
151  action(scene);
152  }
153  }
154  }
155 
162  public bool TryGetScene(Point location, out Scene scene)
163  {
164  SortedDictionary<uint, Scene> row;
165  if (Regions.TryGetValue((uint)location.Y, out row))
166  {
167  if (row.TryGetValue((uint)location.X, out scene))
168  return true;
169  }
170 
171  scene = null;
172  return false;
173  }
174 
175  }
176 }
void CalcSceneLocations()
Called after all the scenes have been added. Performs calculations that require knowledge of all the ...
A group of regions arranged in a rectangle, possibly with holes.
string GetRegionDir(UUID regionID)
Returns the subdirectory where the region is stored.
bool TryGetScene(Point location, out Scene scene)
Returns the scene at position 'location'.
Dictionary< UUID, string > m_regionDirs
The subdirectory where each region is stored in the archive.
void ForEachScene(Action< Scene > action)
Performs an action on all the scenes in this order: rows from South to North, and within each row Wes...