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 CheckPermissions { 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. CheckPermissions = 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. CheckPermissions = (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. Watchdog.RunInThread(o => ar.Execute(), "Archive Assets Request", null);
  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.RegionInfo.RegionName);
  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, CheckPermissions, 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);
  223. int prevAssets = assetUuids.Count;
  224. foreach (SceneObjectGroup sceneObject in sceneObjects)
  225. {
  226. assetGatherer.GatherAssetUuids(sceneObject, assetUuids);
  227. }
  228. m_log.DebugFormat(
  229. "[ARCHIVER]: {0} scene objects to serialize requiring save of {1} assets",
  230. sceneObjects.Count, assetUuids.Count - prevAssets);
  231. }
  232. if (numObjectsSkippedPermissions > 0)
  233. {
  234. m_log.DebugFormat(
  235. "[ARCHIVER]: {0} scene objects skipped due to lack of permissions",
  236. numObjectsSkippedPermissions);
  237. }
  238. // Make sure that we also request terrain texture assets
  239. RegionSettings regionSettings = scene.RegionInfo.RegionSettings;
  240. if (regionSettings.TerrainTexture1 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_1)
  241. assetUuids[regionSettings.TerrainTexture1] = (sbyte)AssetType.Texture;
  242. if (regionSettings.TerrainTexture2 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_2)
  243. assetUuids[regionSettings.TerrainTexture2] = (sbyte)AssetType.Texture;
  244. if (regionSettings.TerrainTexture3 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_3)
  245. assetUuids[regionSettings.TerrainTexture3] = (sbyte)AssetType.Texture;
  246. if (regionSettings.TerrainTexture4 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_4)
  247. assetUuids[regionSettings.TerrainTexture4] = (sbyte)AssetType.Texture;
  248. Save(scene, sceneObjects, regionDir);
  249. }
  250. /// <summary>
  251. /// Checks whether the user has permission to export an object group to an OAR.
  252. /// </summary>
  253. /// <param name="user">The user</param>
  254. /// <param name="objGroup">The object group</param>
  255. /// <param name="checkPermissions">Which permissions to check: "C" = Copy, "T" = Transfer</param>
  256. /// <param name="permissionsModule">The scene's permissions module</param>
  257. /// <returns>Whether the user is allowed to export the object to an OAR</returns>
  258. private bool CanUserArchiveObject(UUID user, SceneObjectGroup objGroup, string checkPermissions, IPermissionsModule permissionsModule)
  259. {
  260. if (checkPermissions == null)
  261. return true;
  262. if (permissionsModule == null)
  263. return true; // this shouldn't happen
  264. // Check whether the user is permitted to export all of the parts in the SOG. If any
  265. // part can't be exported then the entire SOG can't be exported.
  266. bool permitted = true;
  267. //int primNumber = 1;
  268. foreach (SceneObjectPart obj in objGroup.Parts)
  269. {
  270. uint perm;
  271. PermissionClass permissionClass = permissionsModule.GetPermissionClass(user, obj);
  272. switch (permissionClass)
  273. {
  274. case PermissionClass.Owner:
  275. perm = obj.BaseMask;
  276. break;
  277. case PermissionClass.Group:
  278. perm = obj.GroupMask | obj.EveryoneMask;
  279. break;
  280. case PermissionClass.Everyone:
  281. default:
  282. perm = obj.EveryoneMask;
  283. break;
  284. }
  285. bool canCopy = (perm & (uint)PermissionMask.Copy) != 0;
  286. bool canTransfer = (perm & (uint)PermissionMask.Transfer) != 0;
  287. // Special case: if Everyone can copy the object then this implies it can also be
  288. // Transferred.
  289. // However, if the user is the Owner then we don't check EveryoneMask, because it seems that the mask
  290. // always (incorrectly) includes the Copy bit set in this case. But that's a mistake: the viewer
  291. // does NOT show that the object has Everyone-Copy permissions, and doesn't allow it to be copied.
  292. if (permissionClass != PermissionClass.Owner)
  293. canTransfer |= (obj.EveryoneMask & (uint)PermissionMask.Copy) != 0;
  294. bool partPermitted = true;
  295. if (checkPermissions.Contains("C") && !canCopy)
  296. partPermitted = false;
  297. if (checkPermissions.Contains("T") && !canTransfer)
  298. partPermitted = false;
  299. // If the user is the Creator of the object then it can always be included in the OAR
  300. bool creator = (obj.CreatorID.Guid == user.Guid);
  301. if (creator)
  302. partPermitted = true;
  303. //string name = (objGroup.PrimCount == 1) ? objGroup.Name : string.Format("{0} ({1}/{2})", obj.Name, primNumber, objGroup.PrimCount);
  304. //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}",
  305. // name, obj.BaseMask, obj.OwnerMask, obj.EveryoneMask,
  306. // permissionClass, checkPermissions, canCopy, canTransfer, creator, partPermitted);
  307. if (!partPermitted)
  308. {
  309. permitted = false;
  310. break;
  311. }
  312. //++primNumber;
  313. }
  314. return permitted;
  315. }
  316. /// <summary>
  317. /// Create the control file.
  318. /// </summary>
  319. /// <returns></returns>
  320. public string CreateControlFile(ArchiveScenesGroup scenesGroup)
  321. {
  322. int majorVersion;
  323. int minorVersion;
  324. if (MultiRegionFormat)
  325. {
  326. majorVersion = MAX_MAJOR_VERSION;
  327. minorVersion = 0;
  328. }
  329. else
  330. {
  331. // To support older versions of OpenSim, we continue to create single-region OARs
  332. // using the old file format. In the future this format will be discontinued.
  333. majorVersion = 0;
  334. minorVersion = 8;
  335. }
  336. //
  337. // if (m_options.ContainsKey("version"))
  338. // {
  339. // string[] parts = m_options["version"].ToString().Split('.');
  340. // if (parts.Length >= 1)
  341. // {
  342. // majorVersion = Int32.Parse(parts[0]);
  343. //
  344. // if (parts.Length >= 2)
  345. // minorVersion = Int32.Parse(parts[1]);
  346. // }
  347. // }
  348. //
  349. // if (majorVersion < MIN_MAJOR_VERSION || majorVersion > MAX_MAJOR_VERSION)
  350. // {
  351. // throw new Exception(
  352. // string.Format(
  353. // "OAR version number for save must be between {0} and {1}",
  354. // MIN_MAJOR_VERSION, MAX_MAJOR_VERSION));
  355. // }
  356. // else if (majorVersion == MAX_MAJOR_VERSION)
  357. // {
  358. // // Force 1.0
  359. // minorVersion = 0;
  360. // }
  361. // else if (majorVersion == MIN_MAJOR_VERSION)
  362. // {
  363. // // Force 0.4
  364. // minorVersion = 4;
  365. // }
  366. m_log.InfoFormat("[ARCHIVER]: Creating version {0}.{1} OAR", majorVersion, minorVersion);
  367. if (majorVersion == 1)
  368. {
  369. 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");
  370. }
  371. String s;
  372. using (StringWriter sw = new StringWriter())
  373. {
  374. using (XmlTextWriter xtw = new XmlTextWriter(sw))
  375. {
  376. xtw.Formatting = Formatting.Indented;
  377. xtw.WriteStartDocument();
  378. xtw.WriteStartElement("archive");
  379. xtw.WriteAttributeString("major_version", majorVersion.ToString());
  380. xtw.WriteAttributeString("minor_version", minorVersion.ToString());
  381. xtw.WriteStartElement("creation_info");
  382. DateTime now = DateTime.UtcNow;
  383. TimeSpan t = now - new DateTime(1970, 1, 1);
  384. xtw.WriteElementString("datetime", ((int)t.TotalSeconds).ToString());
  385. if (!MultiRegionFormat)
  386. xtw.WriteElementString("id", m_rootScene.RegionInfo.RegionID.ToString());
  387. xtw.WriteEndElement();
  388. xtw.WriteElementString("assets_included", SaveAssets.ToString());
  389. if (MultiRegionFormat)
  390. {
  391. WriteRegionsManifest(scenesGroup, xtw);
  392. }
  393. else
  394. {
  395. xtw.WriteStartElement("region_info");
  396. WriteRegionInfo(m_rootScene, xtw);
  397. xtw.WriteEndElement();
  398. }
  399. xtw.WriteEndElement();
  400. xtw.Flush();
  401. }
  402. s = sw.ToString();
  403. }
  404. return s;
  405. }
  406. /// <summary>
  407. /// Writes the list of regions included in a multi-region OAR.
  408. /// </summary>
  409. private static void WriteRegionsManifest(ArchiveScenesGroup scenesGroup, XmlTextWriter xtw)
  410. {
  411. xtw.WriteStartElement("regions");
  412. // Write the regions in order: rows from South to North, then regions from West to East.
  413. // The list of regions can have "holes"; we write empty elements in their position.
  414. for (uint y = (uint)scenesGroup.Rect.Top; y < scenesGroup.Rect.Bottom; ++y)
  415. {
  416. SortedDictionary<uint, Scene> row;
  417. if (scenesGroup.Regions.TryGetValue(y, out row))
  418. {
  419. xtw.WriteStartElement("row");
  420. for (uint x = (uint)scenesGroup.Rect.Left; x < scenesGroup.Rect.Right; ++x)
  421. {
  422. Scene scene;
  423. if (row.TryGetValue(x, out scene))
  424. {
  425. xtw.WriteStartElement("region");
  426. xtw.WriteElementString("id", scene.RegionInfo.RegionID.ToString());
  427. xtw.WriteElementString("dir", scenesGroup.GetRegionDir(scene.RegionInfo.RegionID));
  428. WriteRegionInfo(scene, xtw);
  429. xtw.WriteEndElement();
  430. }
  431. else
  432. {
  433. // Write a placeholder for a missing region
  434. xtw.WriteElementString("region", "");
  435. }
  436. }
  437. xtw.WriteEndElement();
  438. }
  439. else
  440. {
  441. // Write a placeholder for a missing row
  442. xtw.WriteElementString("row", "");
  443. }
  444. }
  445. xtw.WriteEndElement(); // "regions"
  446. }
  447. protected static void WriteRegionInfo(Scene scene, XmlTextWriter xtw)
  448. {
  449. bool isMegaregion;
  450. Vector2 size;
  451. IRegionCombinerModule rcMod = scene.RequestModuleInterface<IRegionCombinerModule>();
  452. if (rcMod != null)
  453. isMegaregion = rcMod.IsRootForMegaregion(scene.RegionInfo.RegionID);
  454. else
  455. isMegaregion = false;
  456. if (isMegaregion)
  457. size = rcMod.GetSizeOfMegaregion(scene.RegionInfo.RegionID);
  458. else
  459. size = new Vector2((float)scene.RegionInfo.RegionSizeX, (float)scene.RegionInfo.RegionSizeY);
  460. xtw.WriteElementString("is_megaregion", isMegaregion.ToString());
  461. xtw.WriteElementString("size_in_meters", string.Format("{0},{1}", size.X, size.Y));
  462. }
  463. protected void Save(Scene scene, List<SceneObjectGroup> sceneObjects, string regionDir)
  464. {
  465. if (regionDir != string.Empty)
  466. regionDir = ArchiveConstants.REGIONS_PATH + regionDir + "/";
  467. m_log.InfoFormat("[ARCHIVER]: Adding region settings to archive.");
  468. // Write out region settings
  469. string settingsPath = String.Format("{0}{1}{2}.xml",
  470. regionDir, ArchiveConstants.SETTINGS_PATH, scene.RegionInfo.RegionName);
  471. m_archiveWriter.WriteFile(settingsPath, RegionSettingsSerializer.Serialize(scene.RegionInfo.RegionSettings));
  472. m_log.InfoFormat("[ARCHIVER]: Adding parcel settings to archive.");
  473. // Write out land data (aka parcel) settings
  474. List<ILandObject> landObjects = scene.LandChannel.AllParcels();
  475. foreach (ILandObject lo in landObjects)
  476. {
  477. LandData landData = lo.LandData;
  478. string landDataPath
  479. = String.Format("{0}{1}", regionDir, ArchiveConstants.CreateOarLandDataPath(landData));
  480. m_archiveWriter.WriteFile(landDataPath, LandDataSerializer.Serialize(landData, m_options));
  481. }
  482. m_log.InfoFormat("[ARCHIVER]: Adding terrain information to archive.");
  483. // Write out terrain
  484. string terrainPath = String.Format("{0}{1}{2}.r32",
  485. regionDir, ArchiveConstants.TERRAINS_PATH, scene.RegionInfo.RegionName);
  486. using (MemoryStream ms = new MemoryStream())
  487. {
  488. scene.RequestModuleInterface<ITerrainModule>().SaveToStream(terrainPath, ms);
  489. m_archiveWriter.WriteFile(terrainPath, ms.ToArray());
  490. }
  491. m_log.InfoFormat("[ARCHIVER]: Adding scene objects to archive.");
  492. // Write out scene object metadata
  493. IRegionSerialiserModule serializer = scene.RequestModuleInterface<IRegionSerialiserModule>();
  494. foreach (SceneObjectGroup sceneObject in sceneObjects)
  495. {
  496. //m_log.DebugFormat("[ARCHIVER]: Saving {0} {1}, {2}", entity.Name, entity.UUID, entity.GetType());
  497. string serializedObject = serializer.SerializeGroupToXml2(sceneObject, m_options);
  498. string objectPath = string.Format("{0}{1}", regionDir, ArchiveHelpers.CreateObjectPath(sceneObject));
  499. m_archiveWriter.WriteFile(objectPath, serializedObject);
  500. }
  501. }
  502. protected void ReceivedAllAssets(ICollection<UUID> assetsFoundUuids, ICollection<UUID> assetsNotFoundUuids, bool timedOut)
  503. {
  504. string errorMessage;
  505. if (timedOut)
  506. {
  507. errorMessage = "Loading assets timed out";
  508. }
  509. else
  510. {
  511. foreach (UUID uuid in assetsNotFoundUuids)
  512. {
  513. m_log.DebugFormat("[ARCHIVER]: Could not find asset {0}", uuid);
  514. }
  515. // m_log.InfoFormat(
  516. // "[ARCHIVER]: Received {0} of {1} assets requested",
  517. // assetsFoundUuids.Count, assetsFoundUuids.Count + assetsNotFoundUuids.Count);
  518. errorMessage = String.Empty;
  519. }
  520. CloseArchive(errorMessage);
  521. }
  522. /// <summary>
  523. /// Closes the archive and notifies that we're done.
  524. /// </summary>
  525. /// <param name="errorMessage">The error that occurred, or empty for success</param>
  526. protected void CloseArchive(string errorMessage)
  527. {
  528. try
  529. {
  530. if (m_archiveWriter != null)
  531. m_archiveWriter.Close();
  532. m_saveStream.Close();
  533. }
  534. catch (Exception e)
  535. {
  536. m_log.Error(string.Format("[ARCHIVER]: Error closing archive: {0} ", e.Message), e);
  537. if (errorMessage == string.Empty)
  538. errorMessage = e.Message;
  539. }
  540. m_log.InfoFormat("[ARCHIVER]: Finished writing out OAR for {0}", m_rootScene.RegionInfo.RegionName);
  541. m_rootScene.EventManager.TriggerOarFileSaved(m_requestId, errorMessage);
  542. }
  543. }
  544. }