aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Modules/World/Archiver/AssetsDearchiver.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Environment/Modules/World/Archiver/AssetsDearchiver.cs')
-rw-r--r--OpenSim/Region/Environment/Modules/World/Archiver/AssetsDearchiver.cs184
1 files changed, 184 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/World/Archiver/AssetsDearchiver.cs b/OpenSim/Region/Environment/Modules/World/Archiver/AssetsDearchiver.cs
new file mode 100644
index 0000000..1c5b535
--- /dev/null
+++ b/OpenSim/Region/Environment/Modules/World/Archiver/AssetsDearchiver.cs
@@ -0,0 +1,184 @@
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
28using System;
29using System.Collections.Generic;
30using System.IO;
31using System.Reflection;
32using System.Xml;
33using libsecondlife;
34using log4net;
35using OpenSim.Framework;
36using OpenSim.Framework.Communications.Cache;
37
38namespace OpenSim.Region.Environment.Modules.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
47 protected static System.Text.ASCIIEncoding m_asciiEncoding = new System.Text.ASCIIEncoding();
48
49 /// <summary>
50 /// Store for asset data we received before we get the metadata
51 /// </summary>
52 protected Dictionary<string, byte[]> m_assetDataAwaitingMetadata = new Dictionary<string, byte[]>();
53
54 /// <summary>
55 /// Asset metadata. Is null if asset metadata isn't yet available.
56 /// </summary>
57 protected Dictionary<string, AssetMetadata> m_metadata;
58
59 /// <summary>
60 /// Cache to which dearchived assets will be added
61 /// </summary>
62 protected AssetCache m_cache;
63
64 public AssetsDearchiver(AssetCache cache)
65 {
66 m_cache = cache;
67 }
68
69 /// <summary>
70 /// Add asset data to the dearchiver
71 /// </summary>
72 /// <param name="assetFilename"></param>
73 /// <param name="data"></param>
74 public void AddAssetData(string assetFilename, byte[] data)
75 {
76 if (null == m_metadata)
77 {
78 m_assetDataAwaitingMetadata[assetFilename] = data;
79 }
80 else
81 {
82 ResolveAssetData(assetFilename, data);
83 }
84 }
85
86 /// <summary>
87 /// Add asset metadata xml
88 /// </summary>
89 /// <param name="xml"></param>
90 public void AddAssetMetadata(string xml)
91 {
92 m_metadata = new Dictionary<string, AssetMetadata>();
93
94 StringReader sr = new StringReader(xml);
95 XmlTextReader reader = new XmlTextReader(sr);
96
97 reader.ReadStartElement("assets");
98 reader.Read();
99
100 m_log.DebugFormat("next node {0}", reader.Name);
101 while (reader.Name.Equals("asset"))
102 {
103 reader.Read();
104
105 AssetMetadata metadata = new AssetMetadata();
106
107 string filename = reader.ReadElementString("filename");
108 m_log.DebugFormat("[DEARCHIVER]: Reading node {0}", filename);
109
110 metadata.Name = reader.ReadElementString("name");
111 metadata.Description = reader.ReadElementString("description");
112 metadata.AssetType = Convert.ToSByte(reader.ReadElementString("asset-type"));
113 metadata.AssetType = Convert.ToSByte(reader.ReadElementString("inventory-type"));
114
115 m_metadata[filename] = metadata;
116
117 // Read asset end tag
118 reader.ReadEndElement();
119
120 reader.Read();
121 }
122
123 m_log.DebugFormat("[DEARCHIVER]: Resolved {0} items of asset metadata", m_metadata.Count);
124
125 ResolvePendingAssetData();
126 }
127
128 /// <summary>
129 /// Resolve asset data that we collected before receiving the metadata
130 /// </summary>
131 protected void ResolvePendingAssetData()
132 {
133 foreach (string filename in m_assetDataAwaitingMetadata.Keys)
134 {
135 ResolveAssetData(filename, m_assetDataAwaitingMetadata[filename]);
136 }
137 }
138
139 /// <summary>
140 /// Resolve a new piece of asset data against stored metadata
141 /// </summary>
142 /// <param name="assetFilename"></param>
143 /// <param name="data"></param>
144 protected void ResolveAssetData(string assetPath, byte[] data)
145 {
146 // Right now we're nastily obtaining the lluuid from the filename
147 string filename = assetPath.Remove(0, ArchiveConstants.TEXTURES_PATH.Length);
148
149 if (m_metadata.ContainsKey(filename))
150 {
151 AssetMetadata metadata = m_metadata[filename];
152
153 string rawId = filename.Remove(filename.Length - ArchiveConstants.TEXTURE_EXTENSION.Length);
154
155 m_log.DebugFormat("[ARCHIVER]: Importing asset {0}", rawId);
156
157 AssetBase asset = new AssetBase(new LLUUID(rawId), metadata.Name);
158 asset.Description = metadata.Description;
159 asset.Type = metadata.AssetType;
160 asset.InvType = metadata.InventoryType;
161 asset.Data = data;
162
163 m_cache.AddAsset(asset);
164 }
165 else
166 {
167 m_log.ErrorFormat(
168 "[DEARCHIVER]: Tried to dearchive data with filename {0} without any corresponding metadata",
169 assetPath);
170 }
171 }
172
173 /// <summary>
174 /// Metadata for an asset
175 /// </summary>
176 protected struct AssetMetadata
177 {
178 public string Name;
179 public string Description;
180 public sbyte AssetType;
181 public sbyte InventoryType;
182 }
183 }
184}