AssetsDearchiver.cs 6.7 KB

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