ArchiveWriteRequest.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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.IO;
  30. using System.IO.Compression;
  31. using System.Reflection;
  32. using System.Text.RegularExpressions;
  33. using System.Threading;
  34. using System.Xml;
  35. using log4net;
  36. using OpenMetaverse;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Monitoring;
  39. using OpenSim.Framework.Serialization;
  40. using OpenSim.Region.CoreModules.World.Terrain;
  41. using OpenSim.Region.Framework.Interfaces;
  42. using OpenSim.Region.Framework.Scenes;
  43. using Ionic.Zlib;
  44. using GZipStream = Ionic.Zlib.GZipStream;
  45. using CompressionMode = Ionic.Zlib.CompressionMode;
  46. using OpenSim.Framework.Serialization.External;
  47. using PermissionMask = OpenSim.Framework.PermissionMask;
  48. namespace OpenSim.Region.CoreModules.World.Archiver
  49. {
  50. /// <summary>
  51. /// Prepare to write out an archive.
  52. /// </summary>
  53. public class ArchiveWriteRequest
  54. {
  55. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  56. /// <summary>
  57. /// The minimum major version of OAR that we can write.
  58. /// </summary>
  59. public static int MIN_MAJOR_VERSION = 0;
  60. /// <summary>
  61. /// The maximum major version of OAR that we can write.
  62. /// </summary>
  63. public static int MAX_MAJOR_VERSION = 1;
  64. /// <summary>
  65. /// Whether we're saving a multi-region archive.
  66. /// </summary>
  67. public bool MultiRegionFormat { get; set; }
  68. /// <summary>
  69. /// Determine whether this archive will save assets. Default is true.
  70. /// </summary>
  71. public bool SaveAssets { get; set; }
  72. /// <summary>
  73. /// Determines which objects will be included in the archive, according to their permissions.
  74. /// Default is null, meaning no permission checks.
  75. /// </summary>
  76. public string FilterContent { get; set; }
  77. protected Scene m_rootScene;
  78. protected Stream m_saveStream;
  79. protected TarArchiveWriter m_archiveWriter;
  80. protected Guid m_requestId;
  81. protected Dictionary<string, object> m_options;
  82. /// <summary>
  83. /// Constructor
  84. /// </summary>
  85. /// <param name="module">Calling module</param>
  86. /// <param name="savePath">The path to which to save data.</param>
  87. /// <param name="requestId">The id associated with this request</param>
  88. /// <exception cref="System.IO.IOException">
  89. /// If there was a problem opening a stream for the file specified by the savePath
  90. /// </exception>
  91. public ArchiveWriteRequest(Scene scene, string savePath, Guid requestId) : this(scene, requestId)
  92. {
  93. try
  94. {
  95. m_saveStream = new GZipStream(new FileStream(savePath, FileMode.Create), CompressionMode.Compress, CompressionLevel.BestCompression);
  96. }
  97. catch (EntryPointNotFoundException e)
  98. {
  99. m_log.ErrorFormat(
  100. "[ARCHIVER]: Mismatch between Mono and zlib1g library version when trying to create compression stream."
  101. + "If you've manually installed Mono, have you appropriately updated zlib1g as well?");
  102. m_log.ErrorFormat("{0} {1}", e.Message, e.StackTrace);
  103. }
  104. }
  105. /// <summary>
  106. /// Constructor.
  107. /// </summary>
  108. /// <param name="scene">The root scene to archive</param>
  109. /// <param name="saveStream">The stream to which to save data.</param>
  110. /// <param name="requestId">The id associated with this request</param>
  111. public ArchiveWriteRequest(Scene scene, Stream saveStream, Guid requestId) : this(scene, requestId)
  112. {
  113. m_saveStream = saveStream;
  114. }
  115. protected ArchiveWriteRequest(Scene scene, Guid requestId)
  116. {
  117. m_rootScene = scene;
  118. m_requestId = requestId;
  119. m_archiveWriter = null;
  120. MultiRegionFormat = false;
  121. SaveAssets = true;
  122. FilterContent = null;
  123. }
  124. /// <summary>
  125. /// Archive the region requested.
  126. /// </summary>
  127. /// <exception cref="System.IO.IOException">if there was an io problem with creating the file</exception>
  128. public void ArchiveRegion(Dictionary<string, object> options)
  129. {
  130. m_options = options;
  131. if (options.ContainsKey("all") && (bool)options["all"])
  132. MultiRegionFormat = true;
  133. if (options.ContainsKey("noassets") && (bool)options["noassets"])
  134. SaveAssets = false;
  135. Object temp;
  136. if (options.TryGetValue("checkPermissions", out temp))
  137. FilterContent = (string)temp;
  138. // Find the regions to archive
  139. ArchiveScenesGroup scenesGroup = new ArchiveScenesGroup();
  140. if (MultiRegionFormat)
  141. {
  142. m_log.InfoFormat("[ARCHIVER]: Saving {0} regions", SceneManager.Instance.Scenes.Count);
  143. SceneManager.Instance.ForEachScene(delegate(Scene scene)
  144. {
  145. scenesGroup.AddScene(scene);
  146. });
  147. }
  148. else
  149. {
  150. scenesGroup.AddScene(m_rootScene);
  151. }
  152. scenesGroup.CalcSceneLocations();
  153. m_archiveWriter = new TarArchiveWriter(m_saveStream);
  154. try
  155. {
  156. // Write out control file. It should be first so that it will be found ASAP when loading the file.
  157. m_archiveWriter.WriteFile(ArchiveConstants.CONTROL_FILE_PATH, CreateControlFile(scenesGroup));
  158. m_log.InfoFormat("[ARCHIVER]: Added control file to archive.");
  159. // Archive the regions
  160. Dictionary<UUID, sbyte> assetUuids = new Dictionary<UUID, sbyte>();
  161. scenesGroup.ForEachScene(delegate(Scene scene)
  162. {
  163. string regionDir = MultiRegionFormat ? scenesGroup.GetRegionDir(scene.RegionInfo.RegionID) : "";
  164. ArchiveOneRegion(scene, regionDir, assetUuids);
  165. });
  166. // Archive the assets
  167. if (SaveAssets)
  168. {
  169. m_log.DebugFormat("[ARCHIVER]: Saving {0} assets", assetUuids.Count);
  170. // Asynchronously request all the assets required to perform this archive operation
  171. AssetsRequest ar
  172. = new AssetsRequest(
  173. new AssetsArchiver(m_archiveWriter), assetUuids,
  174. m_rootScene.AssetService, m_rootScene.UserAccountService,
  175. m_rootScene.RegionInfo.ScopeID, options, ReceivedAllAssets);
  176. WorkManager.RunInThread(o => ar.Execute(), null, "Archive Assets Request");
  177. // CloseArchive() will be called from ReceivedAllAssets()
  178. }
  179. else
  180. {
  181. m_log.DebugFormat("[ARCHIVER]: Not saving assets since --noassets was specified");
  182. CloseArchive(string.Empty);
  183. }
  184. }
  185. catch (Exception e)
  186. {
  187. CloseArchive(e.Message);
  188. throw;
  189. }
  190. }
  191. private void ArchiveOneRegion(Scene scene, string regionDir, Dictionary<UUID, sbyte> assetUuids)
  192. {
  193. m_log.InfoFormat("[ARCHIVER]: Writing region {0}", scene.Name);
  194. EntityBase[] entities = scene.GetEntities();
  195. List<SceneObjectGroup> sceneObjects = new List<SceneObjectGroup>();
  196. int numObjectsSkippedPermissions = 0;
  197. // Filter entities so that we only have scene objects.
  198. // FIXME: Would be nicer to have this as a proper list in SceneGraph, since lots of methods
  199. // end up having to do this
  200. IPermissionsModule permissionsModule = scene.RequestModuleInterface<IPermissionsModule>();
  201. foreach (EntityBase entity in entities)
  202. {
  203. if (entity is SceneObjectGroup)
  204. {
  205. SceneObjectGroup sceneObject = (SceneObjectGroup)entity;
  206. if (!sceneObject.IsDeleted && !sceneObject.IsAttachment)
  207. {
  208. if (!CanUserArchiveObject(scene.RegionInfo.EstateSettings.EstateOwner, sceneObject, FilterContent, permissionsModule))
  209. {
  210. // The user isn't allowed to copy/transfer this object, so it will not be included in the OAR.
  211. ++numObjectsSkippedPermissions;
  212. }
  213. else
  214. {
  215. sceneObjects.Add(sceneObject);
  216. }
  217. }
  218. }
  219. }
  220. if (SaveAssets)
  221. {
  222. UuidGatherer assetGatherer = new UuidGatherer(scene.AssetService, assetUuids);
  223. int prevAssets = assetUuids.Count;
  224. foreach (SceneObjectGroup sceneObject in sceneObjects)
  225. assetGatherer.AddForInspection(sceneObject);
  226. assetGatherer.GatherAll();
  227. m_log.DebugFormat(
  228. "[ARCHIVER]: {0} scene objects to serialize requiring save of {1} assets",
  229. sceneObjects.Count, assetUuids.Count - prevAssets);
  230. }
  231. if (numObjectsSkippedPermissions > 0)
  232. {
  233. m_log.DebugFormat(
  234. "[ARCHIVER]: {0} scene objects skipped due to lack of permissions",
  235. numObjectsSkippedPermissions);
  236. }
  237. // Make sure that we also request terrain texture assets
  238. RegionSettings regionSettings = scene.RegionInfo.RegionSettings;
  239. if (regionSettings.TerrainTexture1 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_1)
  240. assetUuids[regionSettings.TerrainTexture1] = (sbyte)AssetType.Texture;
  241. if (regionSettings.TerrainTexture2 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_2)
  242. assetUuids[regionSettings.TerrainTexture2] = (sbyte)AssetType.Texture;
  243. if (regionSettings.TerrainTexture3 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_3)
  244. assetUuids[regionSettings.TerrainTexture3] = (sbyte)AssetType.Texture;
  245. if (regionSettings.TerrainTexture4 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_4)
  246. assetUuids[regionSettings.TerrainTexture4] = (sbyte)AssetType.Texture;
  247. Save(scene, sceneObjects, regionDir);
  248. }
  249. /// <summary>
  250. /// Checks whether the user has permission to export an object group to an OAR.
  251. /// </summary>
  252. /// <param name="user">The user</param>
  253. /// <param name="objGroup">The object group</param>
  254. /// <param name="filterContent">Which permissions to check: "C" = Copy, "T" = Transfer</param>
  255. /// <param name="permissionsModule">The scene's permissions module</param>
  256. /// <returns>Whether the user is allowed to export the object to an OAR</returns>
  257. private bool CanUserArchiveObject(UUID user, SceneObjectGroup objGroup, string filterContent, IPermissionsModule permissionsModule)
  258. {
  259. if (filterContent == null)
  260. return true;
  261. if (permissionsModule == null)
  262. return true; // this shouldn't happen
  263. // Check whether the user is permitted to export all of the parts in the SOG. If any
  264. // part can't be exported then the entire SOG can't be exported.
  265. bool permitted = true;
  266. //int primNumber = 1;
  267. foreach (SceneObjectPart obj in objGroup.Parts)
  268. {
  269. uint perm;
  270. PermissionClass permissionClass = permissionsModule.GetPermissionClass(user, obj);
  271. switch (permissionClass)
  272. {
  273. case PermissionClass.Owner:
  274. perm = obj.BaseMask;
  275. break;
  276. case PermissionClass.Group:
  277. perm = obj.GroupMask | obj.EveryoneMask;
  278. break;
  279. case PermissionClass.Everyone:
  280. default:
  281. perm = obj.EveryoneMask;
  282. break;
  283. }
  284. bool canCopy = (perm & (uint)PermissionMask.Copy) != 0;
  285. bool canTransfer = (perm & (uint)PermissionMask.Transfer) != 0;
  286. // Special case: if Everyone can copy the object then this implies it can also be
  287. // Transferred.
  288. // However, if the user is the Owner then we don't check EveryoneMask, because it seems that the mask
  289. // always (incorrectly) includes the Copy bit set in this case. But that's a mistake: the viewer
  290. // does NOT show that the object has Everyone-Copy permissions, and doesn't allow it to be copied.
  291. if (permissionClass != PermissionClass.Owner)
  292. canTransfer |= (obj.EveryoneMask & (uint)PermissionMask.Copy) != 0;
  293. bool partPermitted = true;
  294. if (filterContent.Contains("C") && !canCopy)
  295. partPermitted = false;
  296. if (filterContent.Contains("T") && !canTransfer)
  297. partPermitted = false;
  298. // If the user is the Creator of the object then it can always be included in the OAR
  299. bool creator = (obj.CreatorID.Guid == user.Guid);
  300. if (creator)
  301. partPermitted = true;
  302. //string name = (objGroup.PrimCount == 1) ? objGroup.Name : string.Format("{0} ({1}/{2})", obj.Name, primNumber, objGroup.PrimCount);
  303. //m_log.DebugFormat("[ARCHIVER]: Object permissions: {0}: Base={1:X4}, Owner={2:X4}, Everyone={3:X4}, permissionClass={4}, checkPermissions={5}, canCopy={6}, canTransfer={7}, creator={8}, permitted={9}",
  304. // name, obj.BaseMask, obj.OwnerMask, obj.EveryoneMask,
  305. // permissionClass, checkPermissions, canCopy, canTransfer, creator, partPermitted);
  306. if (!partPermitted)
  307. {
  308. permitted = false;
  309. break;
  310. }
  311. //++primNumber;
  312. }
  313. return permitted;
  314. }
  315. /// <summary>
  316. /// Create the control file.
  317. /// </summary>
  318. /// <returns></returns>
  319. public string CreateControlFile(ArchiveScenesGroup scenesGroup)
  320. {
  321. int majorVersion;
  322. int minorVersion;
  323. if (MultiRegionFormat)
  324. {
  325. majorVersion = MAX_MAJOR_VERSION;
  326. minorVersion = 0;
  327. }
  328. else
  329. {
  330. // To support older versions of OpenSim, we continue to create single-region OARs
  331. // using the old file format. In the future this format will be discontinued.
  332. majorVersion = 0;
  333. minorVersion = 8;
  334. }
  335. //
  336. // if (m_options.ContainsKey("version"))
  337. // {
  338. // string[] parts = m_options["version"].ToString().Split('.');
  339. // if (parts.Length >= 1)
  340. // {
  341. // majorVersion = Int32.Parse(parts[0]);
  342. //
  343. // if (parts.Length >= 2)
  344. // minorVersion = Int32.Parse(parts[1]);
  345. // }
  346. // }
  347. //
  348. // if (majorVersion < MIN_MAJOR_VERSION || majorVersion > MAX_MAJOR_VERSION)
  349. // {
  350. // throw new Exception(
  351. // string.Format(
  352. // "OAR version number for save must be between {0} and {1}",
  353. // MIN_MAJOR_VERSION, MAX_MAJOR_VERSION));
  354. // }
  355. // else if (majorVersion == MAX_MAJOR_VERSION)
  356. // {
  357. // // Force 1.0
  358. // minorVersion = 0;
  359. // }
  360. // else if (majorVersion == MIN_MAJOR_VERSION)
  361. // {
  362. // // Force 0.4
  363. // minorVersion = 4;
  364. // }
  365. m_log.InfoFormat("[ARCHIVER]: Creating version {0}.{1} OAR", majorVersion, minorVersion);
  366. if (majorVersion == 1)
  367. {
  368. m_log.WarnFormat("[ARCHIVER]: Please be aware that version 1.0 OARs are not compatible with OpenSim versions prior to 0.7.4. Do not use the --all option if you want to produce a compatible OAR");
  369. }
  370. String s;
  371. using (StringWriter sw = new StringWriter())
  372. {
  373. using (XmlTextWriter xtw = new XmlTextWriter(sw))
  374. {
  375. xtw.Formatting = Formatting.Indented;
  376. xtw.WriteStartDocument();
  377. xtw.WriteStartElement("archive");
  378. xtw.WriteAttributeString("major_version", majorVersion.ToString());
  379. xtw.WriteAttributeString("minor_version", minorVersion.ToString());
  380. xtw.WriteStartElement("creation_info");
  381. DateTime now = DateTime.UtcNow;
  382. TimeSpan t = now - new DateTime(1970, 1, 1);
  383. xtw.WriteElementString("datetime", ((int)t.TotalSeconds).ToString());
  384. if (!MultiRegionFormat)
  385. xtw.WriteElementString("id", m_rootScene.RegionInfo.RegionID.ToString());
  386. xtw.WriteEndElement();
  387. xtw.WriteElementString("assets_included", SaveAssets.ToString());
  388. if (MultiRegionFormat)
  389. {
  390. WriteRegionsManifest(scenesGroup, xtw);
  391. }
  392. else
  393. {
  394. xtw.WriteStartElement("region_info");
  395. WriteRegionInfo(m_rootScene, xtw);
  396. xtw.WriteEndElement();
  397. }
  398. xtw.WriteEndElement();
  399. xtw.Flush();
  400. }
  401. s = sw.ToString();
  402. }
  403. return s;
  404. }
  405. /// <summary>
  406. /// Writes the list of regions included in a multi-region OAR.
  407. /// </summary>
  408. private static void WriteRegionsManifest(ArchiveScenesGroup scenesGroup, XmlTextWriter xtw)
  409. {
  410. xtw.WriteStartElement("regions");
  411. // Write the regions in order: rows from South to North, then regions from West to East.
  412. // The list of regions can have "holes"; we write empty elements in their position.
  413. for (uint y = (uint)scenesGroup.Rect.Top; y < scenesGroup.Rect.Bottom; ++y)
  414. {
  415. SortedDictionary<uint, Scene> row;
  416. if (scenesGroup.Regions.TryGetValue(y, out row))
  417. {
  418. xtw.WriteStartElement("row");
  419. for (uint x = (uint)scenesGroup.Rect.Left; x < scenesGroup.Rect.Right; ++x)
  420. {
  421. Scene scene;
  422. if (row.TryGetValue(x, out scene))
  423. {
  424. xtw.WriteStartElement("region");
  425. xtw.WriteElementString("id", scene.RegionInfo.RegionID.ToString());
  426. xtw.WriteElementString("dir", scenesGroup.GetRegionDir(scene.RegionInfo.RegionID));
  427. WriteRegionInfo(scene, xtw);
  428. xtw.WriteEndElement();
  429. }
  430. else
  431. {
  432. // Write a placeholder for a missing region
  433. xtw.WriteElementString("region", "");
  434. }
  435. }
  436. xtw.WriteEndElement();
  437. }
  438. else
  439. {
  440. // Write a placeholder for a missing row
  441. xtw.WriteElementString("row", "");
  442. }
  443. }
  444. xtw.WriteEndElement(); // "regions"
  445. }
  446. protected static void WriteRegionInfo(Scene scene, XmlTextWriter xtw)
  447. {
  448. bool isMegaregion;
  449. Vector2 size;
  450. IRegionCombinerModule rcMod = scene.RequestModuleInterface<IRegionCombinerModule>();
  451. if (rcMod != null)
  452. isMegaregion = rcMod.IsRootForMegaregion(scene.RegionInfo.RegionID);
  453. else
  454. isMegaregion = false;
  455. if (isMegaregion)
  456. size = rcMod.GetSizeOfMegaregion(scene.RegionInfo.RegionID);
  457. else
  458. size = new Vector2((float)scene.RegionInfo.RegionSizeX, (float)scene.RegionInfo.RegionSizeY);
  459. xtw.WriteElementString("is_megaregion", isMegaregion.ToString());
  460. xtw.WriteElementString("size_in_meters", string.Format("{0},{1}", size.X, size.Y));
  461. }
  462. protected void Save(Scene scene, List<SceneObjectGroup> sceneObjects, string regionDir)
  463. {
  464. if (regionDir != string.Empty)
  465. regionDir = ArchiveConstants.REGIONS_PATH + regionDir + "/";
  466. m_log.InfoFormat("[ARCHIVER]: Adding region settings to archive.");
  467. // Write out region settings
  468. string settingsPath = String.Format("{0}{1}{2}.xml",
  469. regionDir, ArchiveConstants.SETTINGS_PATH, scene.RegionInfo.RegionName);
  470. m_archiveWriter.WriteFile(settingsPath, RegionSettingsSerializer.Serialize(scene.RegionInfo.RegionSettings));
  471. m_log.InfoFormat("[ARCHIVER]: Adding parcel settings to archive.");
  472. // Write out land data (aka parcel) settings
  473. List<ILandObject> landObjects = scene.LandChannel.AllParcels();
  474. foreach (ILandObject lo in landObjects)
  475. {
  476. LandData landData = lo.LandData;
  477. string landDataPath
  478. = String.Format("{0}{1}", regionDir, ArchiveConstants.CreateOarLandDataPath(landData));
  479. m_archiveWriter.WriteFile(landDataPath, LandDataSerializer.Serialize(landData, m_options));
  480. }
  481. m_log.InfoFormat("[ARCHIVER]: Adding terrain information to archive.");
  482. // Write out terrain
  483. string terrainPath = String.Format("{0}{1}{2}.r32",
  484. regionDir, ArchiveConstants.TERRAINS_PATH, scene.RegionInfo.RegionName);
  485. using (MemoryStream ms = new MemoryStream())
  486. {
  487. scene.RequestModuleInterface<ITerrainModule>().SaveToStream(terrainPath, ms);
  488. m_archiveWriter.WriteFile(terrainPath, ms.ToArray());
  489. }
  490. m_log.InfoFormat("[ARCHIVER]: Adding scene objects to archive.");
  491. // Write out scene object metadata
  492. IRegionSerialiserModule serializer = scene.RequestModuleInterface<IRegionSerialiserModule>();
  493. foreach (SceneObjectGroup sceneObject in sceneObjects)
  494. {
  495. //m_log.DebugFormat("[ARCHIVER]: Saving {0} {1}, {2}", entity.Name, entity.UUID, entity.GetType());
  496. string serializedObject = serializer.SerializeGroupToXml2(sceneObject, m_options);
  497. string objectPath = string.Format("{0}{1}", regionDir, ArchiveHelpers.CreateObjectPath(sceneObject));
  498. m_archiveWriter.WriteFile(objectPath, serializedObject);
  499. }
  500. }
  501. protected void ReceivedAllAssets(ICollection<UUID> assetsFoundUuids, ICollection<UUID> assetsNotFoundUuids, bool timedOut)
  502. {
  503. string errorMessage;
  504. if (timedOut)
  505. {
  506. errorMessage = "Loading assets timed out";
  507. }
  508. else
  509. {
  510. foreach (UUID uuid in assetsNotFoundUuids)
  511. {
  512. m_log.DebugFormat("[ARCHIVER]: Could not find asset {0}", uuid);
  513. }
  514. // m_log.InfoFormat(
  515. // "[ARCHIVER]: Received {0} of {1} assets requested",
  516. // assetsFoundUuids.Count, assetsFoundUuids.Count + assetsNotFoundUuids.Count);
  517. errorMessage = String.Empty;
  518. }
  519. CloseArchive(errorMessage);
  520. }
  521. /// <summary>
  522. /// Closes the archive and notifies that we're done.
  523. /// </summary>
  524. /// <param name="errorMessage">The error that occurred, or empty for success</param>
  525. protected void CloseArchive(string errorMessage)
  526. {
  527. try
  528. {
  529. if (m_archiveWriter != null)
  530. m_archiveWriter.Close();
  531. m_saveStream.Close();
  532. }
  533. catch (Exception e)
  534. {
  535. m_log.Error(string.Format("[ARCHIVER]: Error closing archive: {0} ", e.Message), e);
  536. if (errorMessage == string.Empty)
  537. errorMessage = e.Message;
  538. }
  539. m_log.InfoFormat("[ARCHIVER]: Finished writing out OAR for {0}", m_rootScene.RegionInfo.RegionName);
  540. m_rootScene.EventManager.TriggerOarFileSaved(m_requestId, errorMessage);
  541. }
  542. }
  543. }