ArchiveWriteRequest.cs 26 KB

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