ArchiveReadRequest.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 OpenSim 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 OpenSim.Framework;
  28. using OpenSim.Region.Environment.Scenes;
  29. using OpenSim.Region.Environment.Modules.World.Serialiser;
  30. using OpenSim.Region.Environment.Modules.World.Terrain;
  31. using System;
  32. using System.Collections.Generic;
  33. using System.IO;
  34. using System.IO.Compression;
  35. using System.Reflection;
  36. using System.Xml;
  37. using libsecondlife;
  38. using log4net;
  39. namespace OpenSim.Region.Environment.Modules.World.Archiver
  40. {
  41. /// <summary>
  42. /// Handles an individual archive read request
  43. /// </summary>
  44. public class ArchiveReadRequest
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. protected static System.Text.ASCIIEncoding m_asciiEncoding = new System.Text.ASCIIEncoding();
  48. protected Scene m_scene;
  49. protected string m_loadPath;
  50. public ArchiveReadRequest(Scene scene, string loadPath)
  51. {
  52. m_scene = scene;
  53. m_loadPath = loadPath;
  54. DearchiveRegion();
  55. }
  56. protected void DearchiveRegion()
  57. {
  58. m_log.InfoFormat("[ARCHIVER]: Restoring archive {0}", m_loadPath);
  59. TarArchiveReader archive
  60. = new TarArchiveReader(
  61. new GZipStream(new FileStream(m_loadPath, FileMode.Open), CompressionMode.Decompress));
  62. //AssetsDearchiver dearchiver = new AssetsDearchiver(m_scene.AssetCache);
  63. List<string> serialisedSceneObjects = new List<string>();
  64. string filePath = "ERROR";
  65. int successfulAssetRestores = 0;
  66. int failedAssetRestores = 0;
  67. byte[] data;
  68. while ((data = archive.ReadEntry(out filePath)) != null)
  69. {
  70. //m_log.DebugFormat(
  71. // "[ARCHIVER]: Successfully read {0} ({1} bytes)}", filePath, data.Length);
  72. if (filePath.StartsWith(ArchiveConstants.OBJECTS_PATH))
  73. {
  74. serialisedSceneObjects.Add(m_asciiEncoding.GetString(data));
  75. }
  76. // else if (filePath.Equals(ArchiveConstants.ASSETS_METADATA_PATH))
  77. // {
  78. // string xml = m_asciiEncoding.GetString(data);
  79. // dearchiver.AddAssetMetadata(xml);
  80. // }
  81. else if (filePath.StartsWith(ArchiveConstants.ASSETS_PATH))
  82. {
  83. if (LoadAsset(filePath, data))
  84. successfulAssetRestores++;
  85. else
  86. failedAssetRestores++;
  87. }
  88. else if (filePath.StartsWith(ArchiveConstants.TERRAINS_PATH))
  89. {
  90. LoadTerrain(filePath, data);
  91. }
  92. }
  93. //m_log.Debug("[ARCHIVER]: Reached end of archive");
  94. archive.Close();
  95. m_log.InfoFormat("[ARCHIVER]: Restored {0} assets", successfulAssetRestores);
  96. if (failedAssetRestores > 0)
  97. m_log.ErrorFormat("[ARCHIVER]: Failed to load {0} assets", failedAssetRestores);
  98. m_log.Info("[ARCHIVER]: Clearing all existing scene objects");
  99. m_scene.DeleteAllSceneObjects();
  100. // Reload serialized prims
  101. m_log.InfoFormat("[ARCHIVER]: Loading {0} scene objects. Please wait.", serialisedSceneObjects.Count);
  102. IRegionSerialiser serialiser = m_scene.RequestModuleInterface<IRegionSerialiser>();
  103. ICollection<SceneObjectGroup> sceneObjects = new List<SceneObjectGroup>();
  104. foreach (string serialisedSceneObject in serialisedSceneObjects)
  105. {
  106. SceneObjectGroup sceneObject = serialiser.LoadGroupFromXml2(m_scene, serialisedSceneObject);
  107. // TODO: Change object creator/owner here
  108. if (null != sceneObject)
  109. {
  110. // Make the master the owner/creator of everything imported for now
  111. LLUUID masterAvatarId = m_scene.RegionInfo.MasterAvatarAssignedUUID;
  112. foreach (SceneObjectPart part in sceneObject.Children.Values)
  113. {
  114. part.CreatorID = masterAvatarId;
  115. part.OwnerID = masterAvatarId;
  116. part.LastOwnerID = masterAvatarId;
  117. }
  118. sceneObjects.Add(sceneObject);
  119. }
  120. }
  121. m_log.InfoFormat("[ARCHIVER]: Restored {0} scene objects to the scene", sceneObjects.Count);
  122. int ignoredObjects = serialisedSceneObjects.Count - sceneObjects.Count;
  123. if (ignoredObjects > 0)
  124. m_log.WarnFormat("[ARCHIVER]: Ignored {0} scene objects that already existed in the scene", ignoredObjects);
  125. m_log.InfoFormat("[ARCHIVER]: Successfully loaded archive");
  126. m_log.Debug("[ARCHIVER]: Starting scripts");
  127. foreach (SceneObjectGroup sceneObject in sceneObjects)
  128. {
  129. sceneObject.CreateScriptInstances(0, true);
  130. }
  131. }
  132. /// <summary>
  133. /// Load an asset
  134. /// </summary>
  135. /// <param name="assetFilename"></param>
  136. /// <param name="data"></param>
  137. /// <returns>true if asset was successfully loaded, false otherwise</returns>
  138. protected bool LoadAsset(string assetPath, byte[] data)
  139. {
  140. // Right now we're nastily obtaining the lluuid from the filename
  141. string filename = assetPath.Remove(0, ArchiveConstants.ASSETS_PATH.Length);
  142. string extension = filename.Substring(filename.LastIndexOf("_"));
  143. string uuid = filename.Remove(filename.Length - extension.Length);
  144. if (ArchiveConstants.EXTENSION_TO_ASSET_TYPE.ContainsKey(extension))
  145. {
  146. sbyte assetType = ArchiveConstants.EXTENSION_TO_ASSET_TYPE[extension];
  147. //m_log.DebugFormat("[ARCHIVER]: Importing asset {0}, type {1}", uuid, assetType);
  148. AssetBase asset = new AssetBase(new LLUUID(uuid), String.Empty);
  149. asset.Type = assetType;
  150. asset.Data = data;
  151. m_scene.AssetCache.AddAsset(asset);
  152. return true;
  153. }
  154. else
  155. {
  156. m_log.ErrorFormat(
  157. "[ARCHIVER]: Tried to dearchive data with path {0} with an unknown type extension {1}",
  158. assetPath, extension);
  159. return false;
  160. }
  161. }
  162. /// <summary>
  163. /// Load terrain data
  164. /// </summary>
  165. /// <param name="terrainPath"></param>
  166. /// <param name="data"></param>
  167. /// <returns>
  168. /// true if terrain was resolved successfully, false otherwise.
  169. /// </returns>
  170. protected bool LoadTerrain(string terrainPath, byte[] data)
  171. {
  172. ITerrainModule terrainModule = m_scene.RequestModuleInterface<ITerrainModule>();
  173. MemoryStream ms = new MemoryStream(data);
  174. terrainModule.LoadFromStream(terrainPath, ms);
  175. ms.Close();
  176. m_log.DebugFormat("[ARCHIVER]: Restored terrain {0}", terrainPath);
  177. return true;
  178. }
  179. }
  180. }