AssetsDearchiver.cs 6.5 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 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.Xml;
  32. using log4net;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Serialization;
  36. using OpenSim.Services.Interfaces;
  37. namespace OpenSim.Region.CoreModules.World.Archiver
  38. {
  39. /// <summary>
  40. /// Dearchives assets
  41. /// </summary>
  42. public class AssetsDearchiver
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. /// <summary>
  46. /// Store for asset data we received before we get the metadata
  47. /// </summary>
  48. protected Dictionary<string, byte[]> m_assetDataAwaitingMetadata = new();
  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 IAssetService m_cache;
  57. public AssetsDearchiver(IAssetService 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(xml);
  85. XmlTextReader reader = new(sr)
  86. {
  87. DtdProcessing = DtdProcessing.Ignore
  88. };
  89. reader.ReadStartElement("assets");
  90. reader.Read();
  91. while (reader.Name.Equals("asset"))
  92. {
  93. reader.Read();
  94. AssetMetadata metadata = new();
  95. string filename = reader.ReadElementString("filename");
  96. m_log.Debug($"[DEARCHIVER]: Reading node {filename}");
  97. metadata.Name = reader.ReadElementString("name");
  98. metadata.Description = reader.ReadElementString("description");
  99. metadata.AssetType = Convert.ToSByte(reader.ReadElementString("asset-type"));
  100. m_metadata[filename] = metadata;
  101. // Read asset end tag
  102. reader.ReadEndElement();
  103. reader.Read();
  104. }
  105. m_log.Debug($"[DEARCHIVER]: Resolved {m_metadata.Count} items of asset metadata");
  106. ResolvePendingAssetData();
  107. }
  108. /// <summary>
  109. /// Resolve asset data that we collected before receiving the metadata
  110. /// </summary>
  111. protected void ResolvePendingAssetData()
  112. {
  113. foreach (string filename in m_assetDataAwaitingMetadata.Keys)
  114. {
  115. ResolveAssetData(filename, m_assetDataAwaitingMetadata[filename]);
  116. }
  117. }
  118. /// <summary>
  119. /// Resolve a new piece of asset data against stored metadata
  120. /// </summary>
  121. /// <param name="assetFilename"></param>
  122. /// <param name="data"></param>
  123. protected void ResolveAssetData(string assetPath, byte[] data)
  124. {
  125. // Right now we're nastily obtaining the UUID from the filename
  126. string filename = assetPath[ArchiveConstants.ASSETS_PATH.Length..];
  127. if (m_metadata.ContainsKey(filename))
  128. {
  129. AssetMetadata metadata = m_metadata[filename];
  130. if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.TryGetValue(metadata.AssetType, out string extension))
  131. {
  132. filename = filename.Remove(filename.Length - extension.Length);
  133. }
  134. m_log.Debug($"[ARCHIVER]: Importing asset {filename}");
  135. AssetBase asset = new(new UUID(filename), metadata.Name, metadata.AssetType, UUID.ZeroString)
  136. {
  137. Description = metadata.Description,
  138. Data = data
  139. };
  140. m_cache.Store(asset);
  141. }
  142. else
  143. {
  144. m_log.Error(
  145. $"[DEARCHIVER]: Tried to dearchive data with filename {assetPath} without any corresponding metadata");
  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. }