DearchiveScenesGroup.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. }
  68. /// <summary>
  69. /// Whether this archive uses the multi-region format.
  70. /// </summary>
  71. public Boolean MultiRegionFormat { get; set; }
  72. /// <summary>
  73. /// Maps (Region directory -> region)
  74. /// </summary>
  75. protected Dictionary<string, RegionInfo> m_directory2region = new Dictionary<string, RegionInfo>();
  76. /// <summary>
  77. /// Maps (UUID of the scene in the simulator where the region will be loaded -> region)
  78. /// </summary>
  79. protected Dictionary<UUID, RegionInfo> m_newId2region = new Dictionary<UUID, RegionInfo>();
  80. public int LoadedCreationDateTime { get; set; }
  81. public string DefaultOriginalID { get; set; }
  82. // These variables are used while reading the archive control file
  83. protected int? m_curY = null;
  84. protected int? m_curX = null;
  85. protected RegionInfo m_curRegion;
  86. public DearchiveScenesInfo()
  87. {
  88. MultiRegionFormat = false;
  89. }
  90. // The following methods are used while reading the archive control file
  91. public void StartRow()
  92. {
  93. m_curY = (m_curY == null) ? 0 : m_curY + 1;
  94. m_curX = null;
  95. }
  96. public void StartRegion()
  97. {
  98. m_curX = (m_curX == null) ? 0 : m_curX + 1;
  99. // Note: this doesn't mean we have a real region in this location; this could just be a "hole"
  100. }
  101. public void SetRegionOriginalID(string id)
  102. {
  103. m_curRegion = new RegionInfo();
  104. m_curRegion.Location = new Point((int)m_curX, (int)m_curY);
  105. m_curRegion.OriginalID = id;
  106. // 'curRegion' will be saved in 'm_directory2region' when SetRegionDir() is called
  107. }
  108. public void SetRegionDirectory(string directory)
  109. {
  110. m_curRegion.Directory = directory;
  111. m_directory2region[directory] = m_curRegion;
  112. }
  113. /// <summary>
  114. /// Sets all the scenes present in the simulator.
  115. /// </summary>
  116. /// <remarks>
  117. /// This method matches regions in the archive to scenes in the simulator according to
  118. /// their relative position. We only load regions if there's an existing Scene in the
  119. /// grid location where the region should be loaded.
  120. /// </remarks>
  121. /// <param name="rootScene">The scene where the Load OAR operation was run</param>
  122. /// <param name="simulatorScenes">All the scenes in the simulator</param>
  123. public void SetSimulatorScenes(Scene rootScene, ArchiveScenesGroup simulatorScenes)
  124. {
  125. foreach (RegionInfo archivedRegion in m_directory2region.Values)
  126. {
  127. Point location = new Point((int)rootScene.RegionInfo.RegionLocX, (int)rootScene.RegionInfo.RegionLocY);
  128. location.Offset(archivedRegion.Location);
  129. Scene scene;
  130. if (simulatorScenes.TryGetScene(location, out scene))
  131. {
  132. archivedRegion.Scene = scene;
  133. m_newId2region[scene.RegionInfo.RegionID] = archivedRegion;
  134. }
  135. else
  136. {
  137. m_log.WarnFormat("[ARCHIVER]: Not loading archived region {0} because there's no existing region at location {1},{2}",
  138. archivedRegion.Directory, location.X, location.Y);
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// Returns the archived region according to the path of a file in the archive.
  144. /// Also, converts the full path into a path that is relative to the region's directory.
  145. /// </summary>
  146. /// <param name="fullPath">The path of a file in the archive</param>
  147. /// <param name="scene">The corresponding Scene, or null if none</param>
  148. /// <param name="relativePath">The path relative to the region's directory. (Or the original
  149. /// path, if this file doesn't belong to a region.)</param>
  150. /// <returns>True: use this file; False: skip it</returns>
  151. public bool GetRegionFromPath(string fullPath, out Scene scene, out string relativePath)
  152. {
  153. scene = null;
  154. relativePath = fullPath;
  155. if (!MultiRegionFormat)
  156. {
  157. if (m_newId2region.Count > 0)
  158. scene = m_newId2region.First().Value.Scene;
  159. return true;
  160. }
  161. if (!fullPath.StartsWith(ArchiveConstants.REGIONS_PATH))
  162. return true; // this file doesn't belong to a region
  163. string[] parts = fullPath.Split(new Char[] { '/' }, 3);
  164. if (parts.Length != 3)
  165. return false;
  166. string regionDirectory = parts[1];
  167. relativePath = parts[2];
  168. RegionInfo region;
  169. if (m_directory2region.TryGetValue(regionDirectory, out region))
  170. {
  171. scene = region.Scene;
  172. return (scene != null);
  173. }
  174. else
  175. {
  176. return false;
  177. }
  178. }
  179. /// <summary>
  180. /// Returns the original UUID of a region (from the simulator where the OAR was saved),
  181. /// given the UUID of the scene it was loaded into in the current simulator.
  182. /// </summary>
  183. /// <param name="newID"></param>
  184. /// <returns></returns>
  185. public string GetOriginalRegionID(UUID newID)
  186. {
  187. RegionInfo region;
  188. if (m_newId2region.TryGetValue(newID, out region))
  189. return region.OriginalID;
  190. else
  191. return DefaultOriginalID;
  192. }
  193. /// <summary>
  194. /// Returns the scenes that have been (or will be) loaded.
  195. /// </summary>
  196. /// <returns></returns>
  197. public List<UUID> GetLoadedScenes()
  198. {
  199. return m_newId2region.Keys.ToList();
  200. }
  201. }
  202. }