AssetsDearchiver.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.Reflection;
  31. using System.Text;
  32. using System.Xml;
  33. using log4net;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Serialization;
  37. using OpenSim.Services.Interfaces;
  38. namespace OpenSim.Region.CoreModules.World.Archiver
  39. {
  40. /// <summary>
  41. /// Dearchives assets
  42. /// </summary>
  43. public class AssetsDearchiver
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. /// <summary>
  47. /// Store for asset data we received before we get the metadata
  48. /// </summary>
  49. protected Dictionary<string, byte[]> m_assetDataAwaitingMetadata = new Dictionary<string, byte[]>();
  50. /// <summary>
  51. /// Asset metadata. Is null if asset metadata isn't yet available.
  52. /// </summary>
  53. protected Dictionary<string, AssetMetadata> m_metadata;
  54. /// <summary>
  55. /// Cache to which dearchived assets will be added
  56. /// </summary>
  57. protected IAssetService m_cache;
  58. public AssetsDearchiver(IAssetService cache)
  59. {
  60. m_cache = cache;
  61. }
  62. /// <summary>
  63. /// Add asset data to the dearchiver
  64. /// </summary>
  65. /// <param name="assetFilename"></param>
  66. /// <param name="data"></param>
  67. public void AddAssetData(string assetFilename, byte[] data)
  68. {
  69. if (null == m_metadata)
  70. {
  71. m_assetDataAwaitingMetadata[assetFilename] = data;
  72. }
  73. else
  74. {
  75. ResolveAssetData(assetFilename, data);
  76. }
  77. }
  78. /// <summary>
  79. /// Add asset metadata xml
  80. /// </summary>
  81. /// <param name="xml"></param>
  82. public void AddAssetMetadata(string xml)
  83. {
  84. m_metadata = new Dictionary<string, AssetMetadata>();
  85. StringReader sr = new StringReader(xml);
  86. XmlTextReader reader = new XmlTextReader(sr);
  87. reader.ReadStartElement("assets");
  88. reader.Read();
  89. while (reader.Name.Equals("asset"))
  90. {
  91. reader.Read();
  92. AssetMetadata metadata = new AssetMetadata();
  93. string filename = reader.ReadElementString("filename");
  94. m_log.DebugFormat("[DEARCHIVER]: Reading node {0}", filename);
  95. metadata.Name = reader.ReadElementString("name");
  96. metadata.Description = reader.ReadElementString("description");
  97. metadata.AssetType = Convert.ToSByte(reader.ReadElementString("asset-type"));
  98. m_metadata[filename] = metadata;
  99. // Read asset end tag
  100. reader.ReadEndElement();
  101. reader.Read();
  102. }
  103. m_log.DebugFormat("[DEARCHIVER]: Resolved {0} items of asset metadata", m_metadata.Count);
  104. ResolvePendingAssetData();
  105. }
  106. /// <summary>
  107. /// Resolve asset data that we collected before receiving the metadata
  108. /// </summary>
  109. protected void ResolvePendingAssetData()
  110. {
  111. foreach (string filename in m_assetDataAwaitingMetadata.Keys)
  112. {
  113. ResolveAssetData(filename, m_assetDataAwaitingMetadata[filename]);
  114. }
  115. }
  116. /// <summary>
  117. /// Resolve a new piece of asset data against stored metadata
  118. /// </summary>
  119. /// <param name="assetFilename"></param>
  120. /// <param name="data"></param>
  121. protected void ResolveAssetData(string assetPath, byte[] data)
  122. {
  123. // Right now we're nastily obtaining the UUID from the filename
  124. string filename = assetPath.Remove(0, ArchiveConstants.ASSETS_PATH.Length);
  125. if (m_metadata.ContainsKey(filename))
  126. {
  127. AssetMetadata metadata = m_metadata[filename];
  128. if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.ContainsKey(metadata.AssetType))
  129. {
  130. string extension = ArchiveConstants.ASSET_TYPE_TO_EXTENSION[metadata.AssetType];
  131. filename = filename.Remove(filename.Length - extension.Length);
  132. }
  133. m_log.DebugFormat("[ARCHIVER]: Importing asset {0}", filename);
  134. AssetBase asset = new AssetBase(new UUID(filename), metadata.Name, metadata.AssetType, UUID.Zero.ToString());
  135. asset.Description = metadata.Description;
  136. asset.Data = data;
  137. m_cache.Store(asset);
  138. }
  139. else
  140. {
  141. m_log.ErrorFormat(
  142. "[DEARCHIVER]: Tried to dearchive data with filename {0} without any corresponding metadata",
  143. assetPath);
  144. }
  145. }
  146. /// <summary>
  147. /// Metadata for an asset
  148. /// </summary>
  149. protected struct AssetMetadata
  150. {
  151. public string Name;
  152. public string Description;
  153. public sbyte AssetType;
  154. }
  155. }
  156. }