DearchiveScenesGroup.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.Linq;
  30. using System.Text;
  31. using OpenSim.Region.Framework.Scenes;
  32. using OpenMetaverse;
  33. using System.Drawing;
  34. using log4net;
  35. using System.Reflection;
  36. using OpenSim.Framework.Serialization;
  37. namespace OpenSim.Region.CoreModules.World.Archiver
  38. {
  39. /// <summary>
  40. /// The regions included in an OAR file.
  41. /// </summary>
  42. public class DearchiveScenesInfo
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. /// <summary>
  46. /// One region in the archive.
  47. /// </summary>
  48. public class RegionInfo
  49. {
  50. /// <summary>
  51. /// The subdirectory in which the region is stored.
  52. /// </summary>
  53. public string Directory { get; set; }
  54. /// <summary>
  55. /// The region's coordinates (relative to the South-West corner of the block).
  56. /// </summary>
  57. public Point Location { get; set; }
  58. /// <summary>
  59. /// The UUID of the original scene from which this archived region was saved.
  60. /// </summary>
  61. public string OriginalID { get; set; }
  62. /// <summary>
  63. /// The scene in the current simulator into which this region is loaded.
  64. /// If null then the region doesn't have a corresponding scene, and it won't be loaded.
  65. /// </summary>
  66. public Scene Scene { get; set; }
  67. /// <summary>
  68. /// The size of the region being loaded.
  69. /// </summary>
  70. public Vector3 RegionSize { get; set; }
  71. public RegionInfo()
  72. {
  73. RegionSize = new Vector3(256f,256f,float.MaxValue);
  74. }
  75. }
  76. /// <summary>
  77. /// Whether this archive uses the multi-region format.
  78. /// </summary>
  79. public Boolean MultiRegionFormat { get; set; }
  80. /// <summary>
  81. /// Maps (Region directory -> region)
  82. /// </summary>
  83. protected Dictionary<string, RegionInfo> m_directory2region = new Dictionary<string, RegionInfo>();
  84. /// <summary>
  85. /// Maps (UUID of the scene in the simulator where the region will be loaded -> region)
  86. /// </summary>
  87. protected Dictionary<UUID, RegionInfo> m_newId2region = new Dictionary<UUID, RegionInfo>();
  88. public int LoadedCreationDateTime { get; set; }
  89. public string DefaultOriginalID { get; set; }
  90. // These variables are used while reading the archive control file
  91. protected int? m_curY = null;
  92. protected int? m_curX = null;
  93. protected RegionInfo m_curRegion;
  94. public DearchiveScenesInfo()
  95. {
  96. MultiRegionFormat = false;
  97. }
  98. // The following methods are used while reading the archive control file
  99. public void StartRow()
  100. {
  101. m_curY = (m_curY == null) ? 0 : m_curY + 1;
  102. m_curX = null;
  103. }
  104. public void StartRegion()
  105. {
  106. m_curX = (m_curX == null) ? 0 : m_curX + 1;
  107. // Note: this doesn't mean we have a real region in this location; this could just be a "hole"
  108. }
  109. public void SetRegionOriginalID(string id)
  110. {
  111. m_curRegion = new RegionInfo();
  112. int x = (int)((m_curX == null) ? 0 : m_curX);
  113. int y = (int)((m_curY == null) ? 0 : m_curY);
  114. m_curRegion.Location = new Point(x, y);
  115. m_curRegion.OriginalID = id;
  116. // 'curRegion' will be saved in 'm_directory2region' when SetRegionDir() is called
  117. }
  118. public void SetRegionDirectory(string directory)
  119. {
  120. if(m_curRegion != null)
  121. {
  122. m_curRegion.Directory = directory;
  123. m_directory2region[directory] = m_curRegion;
  124. }
  125. }
  126. public void SetRegionSize(Vector3 size)
  127. {
  128. if(m_curRegion != null)
  129. m_curRegion.RegionSize = size;
  130. }
  131. /// <summary>
  132. /// Sets all the scenes present in the simulator.
  133. /// </summary>
  134. /// <remarks>
  135. /// This method matches regions in the archive to scenes in the simulator according to
  136. /// their relative position. We only load regions if there's an existing Scene in the
  137. /// grid location where the region should be loaded.
  138. /// </remarks>
  139. /// <param name="rootScene">The scene where the Load OAR operation was run</param>
  140. /// <param name="simulatorScenes">All the scenes in the simulator</param>
  141. public void SetSimulatorScenes(Scene rootScene, ArchiveScenesGroup simulatorScenes)
  142. {
  143. foreach (RegionInfo archivedRegion in m_directory2region.Values)
  144. {
  145. Point location = new Point((int)rootScene.RegionInfo.RegionLocX,
  146. (int)rootScene.RegionInfo.RegionLocY);
  147. location.Offset(archivedRegion.Location);
  148. Scene scene;
  149. if (simulatorScenes.TryGetScene(location, out scene))
  150. {
  151. archivedRegion.Scene = scene;
  152. m_newId2region[scene.RegionInfo.RegionID] = archivedRegion;
  153. }
  154. else
  155. {
  156. m_log.WarnFormat("[ARCHIVER]: Not loading archived region {0} because there's no existing region at location {1},{2}",
  157. archivedRegion.Directory, location.X, location.Y);
  158. }
  159. }
  160. }
  161. /// <summary>
  162. /// Returns the archived region according to the path of a file in the archive.
  163. /// Also, converts the full path into a path that is relative to the region's directory.
  164. /// </summary>
  165. /// <param name="fullPath">The path of a file in the archive</param>
  166. /// <param name="scene">The corresponding Scene, or null if none</param>
  167. /// <param name="relativePath">The path relative to the region's directory. (Or the original
  168. /// path, if this file doesn't belong to a region.)</param>
  169. /// <returns>True: use this file; False: skip it</returns>
  170. public bool GetRegionFromPath(string fullPath, out Scene scene, out string relativePath)
  171. {
  172. scene = null;
  173. relativePath = fullPath;
  174. if (!MultiRegionFormat)
  175. {
  176. if (m_newId2region.Count > 0)
  177. scene = m_newId2region.First().Value.Scene;
  178. return true;
  179. }
  180. if (!fullPath.StartsWith(ArchiveConstants.REGIONS_PATH))
  181. return true; // this file doesn't belong to a region
  182. string[] parts = fullPath.Split(new Char[] { '/' }, 3);
  183. if (parts.Length != 3)
  184. return false;
  185. string regionDirectory = parts[1];
  186. relativePath = parts[2];
  187. RegionInfo region;
  188. if (m_directory2region.TryGetValue(regionDirectory, out region))
  189. {
  190. scene = region.Scene;
  191. return (scene != null);
  192. }
  193. else
  194. {
  195. return false;
  196. }
  197. }
  198. /// <summary>
  199. /// Returns the original UUID of a region (from the simulator where the OAR was saved),
  200. /// given the UUID of the scene it was loaded into in the current simulator.
  201. /// </summary>
  202. /// <param name="newID"></param>
  203. /// <returns></returns>
  204. public string GetOriginalRegionID(UUID newID)
  205. {
  206. RegionInfo region;
  207. if (m_newId2region.TryGetValue(newID, out region))
  208. return region.OriginalID;
  209. else
  210. return DefaultOriginalID;
  211. }
  212. /// <summary>
  213. /// Returns the scenes that have been (or will be) loaded.
  214. /// </summary>
  215. /// <returns></returns>
  216. public List<UUID> GetLoadedScenes()
  217. {
  218. return m_newId2region.Keys.ToList();
  219. }
  220. public int GetScenesCount()
  221. {
  222. return m_directory2region.Count;
  223. }
  224. }
  225. }