aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Framework/Scenes/UuidGatherer.cs
diff options
context:
space:
mode:
authorJustin Clarke Casey2009-02-19 17:19:08 +0000
committerJustin Clarke Casey2009-02-19 17:19:08 +0000
commit4203cf15034cc0389a583e6276f6134b6a5416fd (patch)
tree68b643513ca72ed37b0e820e93871d5ae677860c /OpenSim/Region/Framework/Scenes/UuidGatherer.cs
parent* Do deep inspection when saving inventory items in order to capture all the ... (diff)
downloadopensim-SC_OLD-4203cf15034cc0389a583e6276f6134b6a5416fd.zip
opensim-SC_OLD-4203cf15034cc0389a583e6276f6134b6a5416fd.tar.gz
opensim-SC_OLD-4203cf15034cc0389a583e6276f6134b6a5416fd.tar.bz2
opensim-SC_OLD-4203cf15034cc0389a583e6276f6134b6a5416fd.tar.xz
* refactor: Rename new class AssetGatherer to UuidGatherer to reflect what it actually does
Diffstat (limited to 'OpenSim/Region/Framework/Scenes/UuidGatherer.cs')
-rw-r--r--OpenSim/Region/Framework/Scenes/UuidGatherer.cs269
1 files changed, 269 insertions, 0 deletions
diff --git a/OpenSim/Region/Framework/Scenes/UuidGatherer.cs b/OpenSim/Region/Framework/Scenes/UuidGatherer.cs
new file mode 100644
index 0000000..bc8896e
--- /dev/null
+++ b/OpenSim/Region/Framework/Scenes/UuidGatherer.cs
@@ -0,0 +1,269 @@
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.Reflection;
31using System.Text.RegularExpressions;
32using System.Threading;
33using log4net;
34using OpenMetaverse;
35using OpenSim.Framework;
36
37namespace OpenSim.Region.Framework.Scenes
38{
39 /// <summary>
40 /// Gather uuids for a given entity.
41 /// </summary>
42 ///
43 /// This does a deep inspection of the entity to retrieve all the assets it uses (whether as textures, as scripts
44 /// contained in inventory, as scripts contained in objects contained in another object's inventory, etc. Assets
45 /// are only retrieved when they are necessary to carry out the inspection (i.e. a serialized object needs to be
46 /// retrieved to work out which assets it references).
47 public class UuidGatherer
48 {
49 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
50
51 /// <summary>
52 /// Asset cache used for gathering assets
53 /// </summary>
54 protected IAssetCache m_assetCache;
55
56 /// <summary>
57 /// Used as a temporary store of an asset which represents an object. This can be a null if no appropriate
58 /// asset was found by the asset service.
59 /// </summary>
60 protected AssetBase m_requestedObjectAsset;
61
62 /// <summary>
63 /// Signal whether we are currently waiting for the asset service to deliver an asset.
64 /// </summary>
65 protected bool m_waitingForObjectAsset;
66
67 public UuidGatherer(IAssetCache assetCache)
68 {
69 m_assetCache = assetCache;
70 }
71
72 /// <summary>
73 /// Gather all the asset uuids associated with the asset referenced by a given uuid
74 /// </summary>
75 ///
76 /// This includes both those directly associated with
77 /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
78 /// within this object).
79 ///
80 /// <param name="assetUuid">The uuid of the asset for which to gather referenced assets</param>
81 /// <param name="assetType">The type of the asset for the uuid given</param>
82 /// <param name="assetUuids">The assets gathered</param>
83 public void GatherAssetUuids(UUID assetUuid, AssetType assetType, IDictionary<UUID, int> assetUuids)
84 {
85 assetUuids[assetUuid] = 1;
86
87 if (AssetType.Bodypart == assetType || AssetType.Clothing == assetType)
88 {
89 GetWearableAssetUuids(assetUuid, assetUuids);
90 }
91 else if (AssetType.LSLText == assetType)
92 {
93 GetScriptAssetUuids(assetUuid, assetUuids);
94 }
95 else if (AssetType.Object == assetType)
96 {
97 GetSceneObjectAssetUuids(assetUuid, assetUuids);
98 }
99 }
100
101 /// <summary>
102 /// Gather all the asset uuids associated with a given object.
103 /// </summary>
104 ///
105 /// This includes both those directly associated with
106 /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
107 /// within this object).
108 ///
109 /// <param name="sceneObject">The scene object for which to gather assets</param>
110 /// <param name="assetUuids">The assets gathered</param>
111 public void GatherAssetUuids(SceneObjectGroup sceneObject, IDictionary<UUID, int> assetUuids)
112 {
113 m_log.DebugFormat(
114 "[ASSET GATHERER]: Getting assets for object {0}, {1}", sceneObject.Name, sceneObject.UUID);
115
116 foreach (SceneObjectPart part in sceneObject.GetParts())
117 {
118 //m_log.DebugFormat(
119 // "[ARCHIVER]: Getting part {0}, {1} for object {2}", part.Name, part.UUID, sceneObject.UUID);
120
121 try
122 {
123 Primitive.TextureEntry textureEntry = part.Shape.Textures;
124
125 // Get the prim's default texture. This will be used for faces which don't have their own texture
126 assetUuids[textureEntry.DefaultTexture.TextureID] = 1;
127
128 // XXX: Not a great way to iterate through face textures, but there's no
129 // other method available to tell how many faces there actually are
130 //int i = 0;
131 foreach (Primitive.TextureEntryFace texture in textureEntry.FaceTextures)
132 {
133 if (texture != null)
134 {
135 //m_log.DebugFormat("[ARCHIVER]: Got face {0}", i++);
136 assetUuids[texture.TextureID] = 1;
137 }
138 }
139
140 // If the prim is a sculpt then preserve this information too
141 if (part.Shape.SculptTexture != UUID.Zero)
142 assetUuids[part.Shape.SculptTexture] = 1;
143
144 // Now analyze this prim's inventory items to preserve all the uuids that they reference
145 foreach (TaskInventoryItem tii in part.TaskInventory.Values)
146 {
147 //m_log.DebugFormat("[ARCHIVER]: Analysing item asset type {0}", tii.Type);
148
149 if (!assetUuids.ContainsKey(tii.AssetID))
150 GatherAssetUuids(tii.AssetID, (AssetType)tii.Type, assetUuids);
151 }
152 }
153 catch (Exception e)
154 {
155 m_log.ErrorFormat("[ASSET GATHERER]: Failed to get part - {0}", e);
156 m_log.DebugFormat("[ASSET GATHERER]: Texture entry length for prim was {0} (min is 46)", part.Shape.TextureEntry.Length);
157 }
158 }
159 }
160
161 /// <summary>
162 /// The callback made when we request the asset for an object from the asset service.
163 /// </summary>
164 protected void AssetRequestCallback(UUID assetID, AssetBase asset)
165 {
166 lock (this)
167 {
168 m_requestedObjectAsset = asset;
169 m_waitingForObjectAsset = false;
170 Monitor.Pulse(this);
171 }
172 }
173
174 /// <summary>
175 /// Get an asset synchronously, potentially using an asynchronous callback. If the
176 /// asynchronous callback is used, we will wait for it to complete.
177 /// </summary>
178 /// <param name="uuid"></param>
179 /// <returns></returns>
180 protected AssetBase GetAsset(UUID uuid)
181 {
182 m_waitingForObjectAsset = true;
183 m_assetCache.GetAsset(uuid, AssetRequestCallback, true);
184
185 // The asset cache callback can either
186 //
187 // 1. Complete on the same thread (if the asset is already in the cache) or
188 // 2. Come in via a different thread (if we need to go fetch it).
189 //
190 // The code below handles both these alternatives.
191 lock (this)
192 {
193 if (m_waitingForObjectAsset)
194 {
195 Monitor.Wait(this);
196 m_waitingForObjectAsset = false;
197 }
198 }
199
200 return m_requestedObjectAsset;
201 }
202
203 /// <summary>
204 /// Record the asset uuids embedded within the given script.
205 /// </summary>
206 /// <param name="scriptUuid"></param>
207 /// <param name="assetUuids">Dictionary in which to record the references</param>
208 protected void GetScriptAssetUuids(UUID scriptUuid, IDictionary<UUID, int> assetUuids)
209 {
210 AssetBase scriptAsset = GetAsset(scriptUuid);
211
212 if (null != scriptAsset)
213 {
214 string script = Utils.BytesToString(scriptAsset.Data);
215 //m_log.DebugFormat("[ARCHIVER]: Script {0}", script);
216 MatchCollection uuidMatches = Util.UUIDPattern.Matches(script);
217 //m_log.DebugFormat("[ARCHIVER]: Found {0} matches in script", uuidMatches.Count);
218
219 foreach (Match uuidMatch in uuidMatches)
220 {
221 UUID uuid = new UUID(uuidMatch.Value);
222 //m_log.DebugFormat("[ARCHIVER]: Recording {0} in script", uuid);
223 assetUuids[uuid] = 1;
224 }
225 }
226 }
227
228 /// <summary>
229 /// Record the uuids referenced by the given wearable asset
230 /// </summary>
231 /// <param name="wearableAssetUuid"></param>
232 /// <param name="assetUuids">Dictionary in which to record the references</param>
233 protected void GetWearableAssetUuids(UUID wearableAssetUuid, IDictionary<UUID, int> assetUuids)
234 {
235 AssetBase assetBase = GetAsset(wearableAssetUuid);
236 //m_log.Debug(new System.Text.ASCIIEncoding().GetString(bodypartAsset.Data));
237 AssetWearable wearableAsset = new AssetBodypart(wearableAssetUuid, assetBase.Data);
238 wearableAsset.Decode();
239
240 //m_log.DebugFormat(
241 // "[ARCHIVER]: Wearable asset {0} references {1} assets", wearableAssetUuid, wearableAsset.Textures.Count);
242
243 foreach (UUID uuid in wearableAsset.Textures.Values)
244 {
245 //m_log.DebugFormat("[ARCHIVER]: Got bodypart uuid {0}", uuid);
246 assetUuids[uuid] = 1;
247 }
248 }
249
250 /// <summary>
251 /// Get all the asset uuids associated with a given object. This includes both those directly associated with
252 /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
253 /// within this object).
254 /// </summary>
255 /// <param name="sceneObject"></param>
256 /// <param name="assetUuids"></param>
257 protected void GetSceneObjectAssetUuids(UUID sceneObjectUuid, IDictionary<UUID, int> assetUuids)
258 {
259 AssetBase objectAsset = GetAsset(sceneObjectUuid);
260
261 if (null != objectAsset)
262 {
263 string xml = Utils.BytesToString(objectAsset.Data);
264 SceneObjectGroup sog = new SceneObjectGroup(xml, true);
265 GatherAssetUuids(sog, assetUuids);
266 }
267 }
268 }
269}