ArchiveWriteRequest.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 CompressionLevel = Ionic.Zlib.CompressionLevel;
  47. using OpenSim.Framework.Serialization.External;
  48. using PermissionMask = OpenSim.Framework.PermissionMask;
  49. namespace OpenSim.Region.CoreModules.World.Archiver
  50. {
  51. /// <summary>
  52. /// Prepare to write out an archive.
  53. /// </summary>
  54. public class ArchiveWriteRequest
  55. {
  56. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  57. /// <summary>
  58. /// The minimum major version of OAR that we can write.
  59. /// </summary>
  60. public static int MIN_MAJOR_VERSION = 0;
  61. /// <summary>
  62. /// The maximum major version of OAR that we can write.
  63. /// </summary>
  64. public static int MAX_MAJOR_VERSION = 1;
  65. /// <summary>
  66. /// Whether we're saving a multi-region archive.
  67. /// </summary>
  68. public bool MultiRegionFormat { get; set; }
  69. /// <summary>
  70. /// Determine whether this archive will save assets. Default is true.
  71. /// </summary>
  72. public bool SaveAssets { get; set; }
  73. /// <summary>
  74. /// Determines which objects will be included in the archive, according to their permissions.
  75. /// Default is null, meaning no permission checks.
  76. /// </summary>
  77. public string FilterContent { get; set; }
  78. protected Scene m_rootScene;
  79. protected Stream m_saveStream;
  80. protected TarArchiveWriter m_archiveWriter;
  81. protected Guid m_requestId;
  82. protected Dictionary<string, object> m_options;
  83. /// <summary>
  84. /// Constructor
  85. /// </summary>
  86. /// <param name="module">Calling module</param>
  87. /// <param name="savePath">The path to which to save data.</param>
  88. /// <param name="requestId">The id associated with this request</param>
  89. /// <exception cref="System.IO.IOException">
  90. /// If there was a problem opening a stream for the file specified by the savePath
  91. /// </exception>
  92. public ArchiveWriteRequest(Scene scene, string savePath, Guid requestId) : this(scene, requestId)
  93. {
  94. try
  95. {
  96. m_saveStream = new GZipStream(new FileStream(savePath, FileMode.Create), CompressionMode.Compress, CompressionLevel.BestCompression);
  97. }
  98. catch (EntryPointNotFoundException e)
  99. {
  100. m_log.ErrorFormat(
  101. "[ARCHIVER]: Mismatch between Mono and zlib1g library version when trying to create compression stream."
  102. + "If you've manually installed Mono, have you appropriately updated zlib1g as well?");
  103. m_log.ErrorFormat("{0} {1}", e.Message, e.StackTrace);
  104. }
  105. }
  106. /// <summary>
  107. /// Constructor.
  108. /// </summary>
  109. /// <param name="scene">The root scene to archive</param>
  110. /// <param name="saveStream">The stream to which to save data.</param>
  111. /// <param name="requestId">The id associated with this request</param>
  112. public ArchiveWriteRequest(Scene scene, Stream saveStream, Guid requestId) : this(scene, requestId)
  113. {
  114. m_saveStream = saveStream;
  115. }
  116. protected ArchiveWriteRequest(Scene scene, Guid requestId)
  117. {
  118. m_rootScene = scene;
  119. m_requestId = requestId;
  120. m_archiveWriter = null;
  121. MultiRegionFormat = false;
  122. SaveAssets = true;
  123. FilterContent = null;
  124. }
  125. /// <summary>
  126. /// Archive the region requested.
  127. /// </summary>
  128. /// <exception cref="System.IO.IOException">if there was an io problem with creating the file</exception>
  129. public void ArchiveRegion(Dictionary<string, object> options)
  130. {
  131. m_options = options;
  132. if (options.ContainsKey("all") && (bool)options["all"])
  133. MultiRegionFormat = true;
  134. if (options.ContainsKey("noassets") && (bool)options["noassets"])
  135. SaveAssets = false;
  136. Object temp;
  137. if (options.TryGetValue("checkPermissions", out temp))
  138. FilterContent = (string)temp;
  139. // Find the regions to archive
  140. ArchiveScenesGroup scenesGroup = new ArchiveScenesGroup();
  141. if (MultiRegionFormat)
  142. {
  143. m_log.InfoFormat("[ARCHIVER]: Saving {0} regions", SceneManager.Instance.Scenes.Count);
  144. SceneManager.Instance.ForEachScene(delegate(Scene scene)
  145. {
  146. scenesGroup.AddScene(scene);
  147. });
  148. }
  149. else
  150. {
  151. scenesGroup.AddScene(m_rootScene);
  152. }
  153. scenesGroup.CalcSceneLocations();
  154. m_archiveWriter = new TarArchiveWriter(m_saveStream);
  155. try
  156. {
  157. // Write out control file. It should be first so that it will be found ASAP when loading the file.
  158. m_archiveWriter.WriteFile(ArchiveConstants.CONTROL_FILE_PATH, CreateControlFile(scenesGroup));
  159. m_log.InfoFormat("[ARCHIVER]: Added control file to archive.");
  160. // Archive the regions
  161. Dictionary<UUID, sbyte> assetUuids = new Dictionary<UUID, sbyte>();
  162. scenesGroup.ForEachScene(delegate(Scene scene)
  163. {
  164. string regionDir = MultiRegionFormat ? scenesGroup.GetRegionDir(scene.RegionInfo.RegionID) : "";
  165. ArchiveOneRegion(scene, regionDir, assetUuids);
  166. });
  167. // Archive the assets
  168. if (SaveAssets)
  169. {
  170. m_log.DebugFormat("[ARCHIVER]: Saving {0} assets", assetUuids.Count);
  171. // Asynchronously request all the assets required to perform this archive operation
  172. AssetsRequest ar
  173. = new AssetsRequest(
  174. new AssetsArchiver(m_archiveWriter), assetUuids,
  175. m_rootScene.AssetService, m_rootScene.UserAccountService,
  176. m_rootScene.RegionInfo.ScopeID, options, ReceivedAllAssets);
  177. WorkManager.RunInThread(o => ar.Execute(), null, "Archive Assets Request");
  178. // CloseArchive() will be called from ReceivedAllAssets()
  179. }
  180. else
  181. {
  182. m_log.DebugFormat("[ARCHIVER]: Not saving assets since --noassets was specified");
  183. CloseArchive(string.Empty);
  184. }
  185. }
  186. catch (Exception e)
  187. {
  188. CloseArchive(e.Message);
  189. throw;
  190. }
  191. }
  192. private void ArchiveOneRegion(Scene scene, string regionDir, Dictionary<UUID, sbyte> assetUuids)
  193. {
  194. m_log.InfoFormat("[ARCHIVER]: Writing region {0}", scene.Name);
  195. EntityBase[] entities = scene.GetEntities();
  196. List<SceneObjectGroup> sceneObjects = new List<SceneObjectGroup>();
  197. int numObjectsSkippedPermissions = 0;
  198. // Filter entities so that we only have scene objects.
  199. // FIXME: Would be nicer to have this as a proper list in SceneGraph, since lots of methods
  200. // end up having to do this
  201. IPermissionsModule permissionsModule = scene.RequestModuleInterface<IPermissionsModule>();
  202. foreach (EntityBase entity in entities)
  203. {
  204. if (entity is SceneObjectGroup)
  205. {
  206. SceneObjectGroup sceneObject = (SceneObjectGroup)entity;
  207. if (!sceneObject.IsDeleted && !sceneObject.IsAttachment)
  208. {
  209. if (!CanUserArchiveObject(scene.RegionInfo.EstateSettings.EstateOwner, sceneObject, FilterContent, permissionsModule))
  210. {
  211. // The user isn't allowed to copy/transfer this object, so it will not be included in the OAR.
  212. ++numObjectsSkippedPermissions;
  213. }
  214. else
  215. {
  216. sceneObjects.Add(sceneObject);
  217. }
  218. }
  219. }
  220. }
  221. if (SaveAssets)
  222. {
  223. UuidGatherer assetGatherer = new UuidGatherer(scene.AssetService, assetUuids);
  224. int prevAssets = assetUuids.Count;
  225. foreach (SceneObjectGroup sceneObject in sceneObjects)
  226. assetGatherer.AddForInspection(sceneObject);
  227. assetGatherer.GatherAll();
  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="filterContent">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 filterContent, IPermissionsModule permissionsModule)
  259. {
  260. if (filterContent == 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 (filterContent.Contains("C") && !canCopy)
  296. partPermitted = false;
  297. if (filterContent.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. Vector2 size;
  450. size = new Vector2((float)scene.RegionInfo.RegionSizeX, (float)scene.RegionInfo.RegionSizeY);
  451. xtw.WriteElementString("size_in_meters", string.Format("{0},{1}", size.X, size.Y));
  452. }
  453. protected void Save(Scene scene, List<SceneObjectGroup> sceneObjects, string regionDir)
  454. {
  455. if (regionDir != string.Empty)
  456. regionDir = ArchiveConstants.REGIONS_PATH + regionDir + "/";
  457. m_log.InfoFormat("[ARCHIVER]: Adding region settings to archive.");
  458. // Write out region settings
  459. string settingsPath = String.Format("{0}{1}{2}.xml",
  460. regionDir, ArchiveConstants.SETTINGS_PATH, scene.RegionInfo.RegionName);
  461. m_archiveWriter.WriteFile(settingsPath, RegionSettingsSerializer.Serialize(scene.RegionInfo.RegionSettings));
  462. m_log.InfoFormat("[ARCHIVER]: Adding parcel settings to archive.");
  463. // Write out land data (aka parcel) settings
  464. List<ILandObject> landObjects = scene.LandChannel.AllParcels();
  465. foreach (ILandObject lo in landObjects)
  466. {
  467. LandData landData = lo.LandData;
  468. string landDataPath
  469. = String.Format("{0}{1}", regionDir, ArchiveConstants.CreateOarLandDataPath(landData));
  470. m_archiveWriter.WriteFile(landDataPath, LandDataSerializer.Serialize(landData, m_options));
  471. }
  472. m_log.InfoFormat("[ARCHIVER]: Adding terrain information to archive.");
  473. // Write out terrain
  474. string terrainPath = String.Format("{0}{1}{2}.r32",
  475. regionDir, ArchiveConstants.TERRAINS_PATH, scene.RegionInfo.RegionName);
  476. using (MemoryStream ms = new MemoryStream())
  477. {
  478. scene.RequestModuleInterface<ITerrainModule>().SaveToStream(terrainPath, ms);
  479. m_archiveWriter.WriteFile(terrainPath, ms.ToArray());
  480. }
  481. m_log.InfoFormat("[ARCHIVER]: Adding scene objects to archive.");
  482. // Write out scene object metadata
  483. IRegionSerialiserModule serializer = scene.RequestModuleInterface<IRegionSerialiserModule>();
  484. foreach (SceneObjectGroup sceneObject in sceneObjects)
  485. {
  486. //m_log.DebugFormat("[ARCHIVER]: Saving {0} {1}, {2}", entity.Name, entity.UUID, entity.GetType());
  487. string serializedObject = serializer.SerializeGroupToXml2(sceneObject, m_options);
  488. string objectPath = string.Format("{0}{1}", regionDir, ArchiveHelpers.CreateObjectPath(sceneObject));
  489. m_archiveWriter.WriteFile(objectPath, serializedObject);
  490. }
  491. }
  492. protected void ReceivedAllAssets(ICollection<UUID> assetsFoundUuids, ICollection<UUID> assetsNotFoundUuids, bool timedOut)
  493. {
  494. string errorMessage;
  495. if (timedOut)
  496. {
  497. errorMessage = "Loading assets timed out";
  498. }
  499. else
  500. {
  501. foreach (UUID uuid in assetsNotFoundUuids)
  502. {
  503. m_log.DebugFormat("[ARCHIVER]: Could not find asset {0}", uuid);
  504. }
  505. // m_log.InfoFormat(
  506. // "[ARCHIVER]: Received {0} of {1} assets requested",
  507. // assetsFoundUuids.Count, assetsFoundUuids.Count + assetsNotFoundUuids.Count);
  508. errorMessage = String.Empty;
  509. }
  510. CloseArchive(errorMessage);
  511. }
  512. /// <summary>
  513. /// Closes the archive and notifies that we're done.
  514. /// </summary>
  515. /// <param name="errorMessage">The error that occurred, or empty for success</param>
  516. protected void CloseArchive(string errorMessage)
  517. {
  518. try
  519. {
  520. if (m_archiveWriter != null)
  521. m_archiveWriter.Close();
  522. m_saveStream.Close();
  523. }
  524. catch (Exception e)
  525. {
  526. m_log.Error(string.Format("[ARCHIVER]: Error closing archive: {0} ", e.Message), e);
  527. if (errorMessage == string.Empty)
  528. errorMessage = e.Message;
  529. }
  530. m_log.InfoFormat("[ARCHIVER]: Finished writing out OAR for {0}", m_rootScene.RegionInfo.RegionName);
  531. m_rootScene.EventManager.TriggerOarFileSaved(m_requestId, errorMessage);
  532. }
  533. }
  534. }