ArchiveWriteRequest.cs 27 KB

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