aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Framework
diff options
context:
space:
mode:
authorJustin Clarke Casey2009-02-18 20:00:21 +0000
committerJustin Clarke Casey2009-02-18 20:00:21 +0000
commita7dea4ee122e226008eb63a6a98a66f05c5089fd (patch)
tree637d3147e56140e049bbc01a11a2036d9dd79d92 /OpenSim/Region/Framework
parent* Make save iar behave properly if the nominated inventory path does not exist (diff)
downloadopensim-SC_OLD-a7dea4ee122e226008eb63a6a98a66f05c5089fd.zip
opensim-SC_OLD-a7dea4ee122e226008eb63a6a98a66f05c5089fd.tar.gz
opensim-SC_OLD-a7dea4ee122e226008eb63a6a98a66f05c5089fd.tar.bz2
opensim-SC_OLD-a7dea4ee122e226008eb63a6a98a66f05c5089fd.tar.xz
* Move asset gathering code from oar module to OpenSim.Region.Framework since this is useful in a variety of situations
* Comment out one oar test since I think somehow the two save tests are causing the occasional test failures
Diffstat (limited to 'OpenSim/Region/Framework')
-rw-r--r--OpenSim/Region/Framework/Scenes/AssetGatherer.cs259
1 files changed, 259 insertions, 0 deletions
diff --git a/OpenSim/Region/Framework/Scenes/AssetGatherer.cs b/OpenSim/Region/Framework/Scenes/AssetGatherer.cs
new file mode 100644
index 0000000..2726bd8
--- /dev/null
+++ b/OpenSim/Region/Framework/Scenes/AssetGatherer.cs
@@ -0,0 +1,259 @@
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 assets for a given object.
41 /// </summary>
42 ///
43 /// This does a deep inspection of the object 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 AssetGatherer
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 AssetGatherer(IAssetCache assetCache)
68 {
69 m_assetCache = assetCache;
70 }
71
72 /// <summary>
73 /// The callback made when we request the asset for an object from the asset service.
74 /// </summary>
75 public void AssetRequestCallback(UUID assetID, AssetBase asset)
76 {
77 lock (this)
78 {
79 m_requestedObjectAsset = asset;
80 m_waitingForObjectAsset = false;
81 Monitor.Pulse(this);
82 }
83 }
84
85 /// <summary>
86 /// Get an asset synchronously, potentially using an asynchronous callback. If the
87 /// asynchronous callback is used, we will wait for it to complete.
88 /// </summary>
89 /// <param name="uuid"></param>
90 /// <returns></returns>
91 protected AssetBase GetAsset(UUID uuid)
92 {
93 m_waitingForObjectAsset = true;
94 m_assetCache.GetAsset(uuid, AssetRequestCallback, true);
95
96 // The asset cache callback can either
97 //
98 // 1. Complete on the same thread (if the asset is already in the cache) or
99 // 2. Come in via a different thread (if we need to go fetch it).
100 //
101 // The code below handles both these alternatives.
102 lock (this)
103 {
104 if (m_waitingForObjectAsset)
105 {
106 Monitor.Wait(this);
107 m_waitingForObjectAsset = false;
108 }
109 }
110
111 return m_requestedObjectAsset;
112 }
113
114 /// <summary>
115 /// Record the asset uuids embedded within the given script.
116 /// </summary>
117 /// <param name="scriptUuid"></param>
118 /// <param name="assetUuids">Dictionary in which to record the references</param>
119 public void GetScriptAssetUuids(UUID scriptUuid, IDictionary<UUID, int> assetUuids)
120 {
121 AssetBase scriptAsset = GetAsset(scriptUuid);
122
123 if (null != scriptAsset)
124 {
125 string script = Utils.BytesToString(scriptAsset.Data);
126 //m_log.DebugFormat("[ARCHIVER]: Script {0}", script);
127 MatchCollection uuidMatches = Util.UUIDPattern.Matches(script);
128 //m_log.DebugFormat("[ARCHIVER]: Found {0} matches in script", uuidMatches.Count);
129
130 foreach (Match uuidMatch in uuidMatches)
131 {
132 UUID uuid = new UUID(uuidMatch.Value);
133 //m_log.DebugFormat("[ARCHIVER]: Recording {0} in script", uuid);
134 assetUuids[uuid] = 1;
135 }
136 }
137 }
138
139 /// <summary>
140 /// Record the uuids referenced by the given wearable asset
141 /// </summary>
142 /// <param name="wearableAssetUuid"></param>
143 /// <param name="assetUuids">Dictionary in which to record the references</param>
144 public void GetWearableAssetUuids(UUID wearableAssetUuid, IDictionary<UUID, int> assetUuids)
145 {
146 AssetBase assetBase = GetAsset(wearableAssetUuid);
147 //m_log.Debug(new System.Text.ASCIIEncoding().GetString(bodypartAsset.Data));
148 AssetWearable wearableAsset = new AssetBodypart(wearableAssetUuid, assetBase.Data);
149 wearableAsset.Decode();
150
151 //m_log.DebugFormat(
152 // "[ARCHIVER]: Wearable asset {0} references {1} assets", wearableAssetUuid, wearableAsset.Textures.Count);
153
154 foreach (UUID uuid in wearableAsset.Textures.Values)
155 {
156 //m_log.DebugFormat("[ARCHIVER]: Got bodypart uuid {0}", uuid);
157 assetUuids[uuid] = 1;
158 }
159 }
160
161 /// <summary>
162 /// Get all the asset uuids associated with a given object. This includes both those directly associated with
163 /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
164 /// within this object).
165 /// </summary>
166 /// <param name="sceneObject"></param>
167 /// <param name="assetUuids"></param>
168 public void GetSceneObjectAssetUuids(UUID sceneObjectUuid, IDictionary<UUID, int> assetUuids)
169 {
170 AssetBase objectAsset = GetAsset(sceneObjectUuid);
171
172 if (null != objectAsset)
173 {
174 string xml = Utils.BytesToString(objectAsset.Data);
175 SceneObjectGroup sog = new SceneObjectGroup(xml, true);
176 GetSceneObjectAssetUuids(sog, assetUuids);
177 }
178 }
179
180 /// <summary>
181 /// Get all the asset uuids associated with a given object.
182 /// </summary>
183 ///
184 /// This includes both those directly associated with
185 /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
186 /// within this object).
187 ///
188 /// <param name="sceneObject">The scene object for which to gather assets</param>
189 /// <param name="assetUuids">The assets gathered</param>
190 public void GetSceneObjectAssetUuids(SceneObjectGroup sceneObject, IDictionary<UUID, int> assetUuids)
191 {
192 m_log.DebugFormat(
193 "[ASSET GATHERER]: Getting assets for object {0}, {1}", sceneObject.Name, sceneObject.UUID);
194
195 foreach (SceneObjectPart part in sceneObject.GetParts())
196 {
197 //m_log.DebugFormat(
198 // "[ARCHIVER]: Getting part {0}, {1} for object {2}", part.Name, part.UUID, sceneObject.UUID);
199
200 try
201 {
202 Primitive.TextureEntry textureEntry = part.Shape.Textures;
203
204 // Get the prim's default texture. This will be used for faces which don't have their own texture
205 assetUuids[textureEntry.DefaultTexture.TextureID] = 1;
206
207 // XXX: Not a great way to iterate through face textures, but there's no
208 // other method available to tell how many faces there actually are
209 //int i = 0;
210 foreach (Primitive.TextureEntryFace texture in textureEntry.FaceTextures)
211 {
212 if (texture != null)
213 {
214 //m_log.DebugFormat("[ARCHIVER]: Got face {0}", i++);
215 assetUuids[texture.TextureID] = 1;
216 }
217 }
218
219 // If the prim is a sculpt then preserve this information too
220 if (part.Shape.SculptTexture != UUID.Zero)
221 assetUuids[part.Shape.SculptTexture] = 1;
222
223 // Now analyze this prim's inventory items to preserve all the uuids that they reference
224 foreach (TaskInventoryItem tii in part.TaskInventory.Values)
225 {
226 //m_log.DebugFormat("[ARCHIVER]: Analysing item asset type {0}", tii.Type);
227
228 if (!assetUuids.ContainsKey(tii.AssetID))
229 {
230 assetUuids[tii.AssetID] = 1;
231
232 if ((int)AssetType.Bodypart == tii.Type || ((int)AssetType.Clothing == tii.Type))
233 {
234 GetWearableAssetUuids(tii.AssetID, assetUuids);
235 }
236 else if ((int)AssetType.LSLText == tii.Type)
237 {
238 GetScriptAssetUuids(tii.AssetID, assetUuids);
239 }
240 else if ((int)AssetType.Object == tii.Type)
241 {
242 GetSceneObjectAssetUuids(tii.AssetID, assetUuids);
243 }
244 //else
245 //{
246 //m_log.DebugFormat("[ARCHIVER]: Recording asset {0} in object {1}", tii.AssetID, part.UUID);
247 //}
248 }
249 }
250 }
251 catch (Exception e)
252 {
253 m_log.ErrorFormat("[ASSET GATHERER]: Failed to get part - {0}", e);
254 m_log.DebugFormat("[ASSET GATHERER]: Texture entry length for prim was {0} (min is 46)", part.Shape.TextureEntry.Length);
255 }
256 }
257 }
258 }
259}