AssetsDearchiver.cs 6.6 KB

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