diff options
author | Melanie Thielker | 2014-06-21 00:39:55 +0200 |
---|---|---|
committer | Melanie Thielker | 2014-06-21 00:39:55 +0200 |
commit | 159fcbf150b7da0e229b29aa7b94793484543d12 (patch) | |
tree | b8c0ff3b4c758a3fba8315b556c923ef4c02a185 /OpenSim/Region/OptionalModules/Materials/MaterialsModule.cs | |
parent | Merge commit '68c8633ba18f0a11cfc0ed04d1d0c7c59e6cec76' (diff) | |
parent | Merge branch 'master' into careminster (diff) | |
download | opensim-SC_OLD-159fcbf150b7da0e229b29aa7b94793484543d12.zip opensim-SC_OLD-159fcbf150b7da0e229b29aa7b94793484543d12.tar.gz opensim-SC_OLD-159fcbf150b7da0e229b29aa7b94793484543d12.tar.bz2 opensim-SC_OLD-159fcbf150b7da0e229b29aa7b94793484543d12.tar.xz |
Merge branch 'master' of ssh://3dhosting.de/var/git/careminster
Conflicts:
OpenSim/Region/Framework/Scenes/SceneObjectGroup.cs
Diffstat (limited to 'OpenSim/Region/OptionalModules/Materials/MaterialsModule.cs')
-rw-r--r-- | OpenSim/Region/OptionalModules/Materials/MaterialsModule.cs | 590 |
1 files changed, 590 insertions, 0 deletions
diff --git a/OpenSim/Region/OptionalModules/Materials/MaterialsModule.cs b/OpenSim/Region/OptionalModules/Materials/MaterialsModule.cs new file mode 100644 index 0000000..afb788b --- /dev/null +++ b/OpenSim/Region/OptionalModules/Materials/MaterialsModule.cs | |||
@@ -0,0 +1,590 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.IO; | ||
31 | using System.Reflection; | ||
32 | using System.Security.Cryptography; // for computing md5 hash | ||
33 | using log4net; | ||
34 | using Mono.Addins; | ||
35 | using Nini.Config; | ||
36 | |||
37 | using OpenMetaverse; | ||
38 | using OpenMetaverse.StructuredData; | ||
39 | |||
40 | using OpenSim.Framework; | ||
41 | using OpenSim.Framework.Servers; | ||
42 | using OpenSim.Framework.Servers.HttpServer; | ||
43 | using OpenSim.Region.Framework.Interfaces; | ||
44 | using OpenSim.Region.Framework.Scenes; | ||
45 | using OpenSimAssetType = OpenSim.Framework.SLUtil.OpenSimAssetType; | ||
46 | |||
47 | using Ionic.Zlib; | ||
48 | |||
49 | // You will need to uncomment these lines if you are adding a region module to some other assembly which does not already | ||
50 | // specify its assembly. Otherwise, the region modules in the assembly will not be picked up when OpenSimulator scans | ||
51 | // the available DLLs | ||
52 | //[assembly: Addin("MaterialsModule", "1.0")] | ||
53 | //[assembly: AddinDependency("OpenSim", "0.5")] | ||
54 | |||
55 | namespace OpenSim.Region.OptionalModules.Materials | ||
56 | { | ||
57 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "MaterialsModule")] | ||
58 | public class MaterialsModule : INonSharedRegionModule | ||
59 | { | ||
60 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
61 | |||
62 | public string Name { get { return "MaterialsModule"; } } | ||
63 | |||
64 | public Type ReplaceableInterface { get { return null; } } | ||
65 | |||
66 | private Scene m_scene = null; | ||
67 | private bool m_enabled = false; | ||
68 | |||
69 | public Dictionary<UUID, OSDMap> m_regionMaterials = new Dictionary<UUID, OSDMap>(); | ||
70 | |||
71 | public void Initialise(IConfigSource source) | ||
72 | { | ||
73 | IConfig config = source.Configs["Materials"]; | ||
74 | if (config == null) | ||
75 | return; | ||
76 | |||
77 | m_enabled = config.GetBoolean("enable_materials", true); | ||
78 | if (!m_enabled) | ||
79 | return; | ||
80 | |||
81 | m_log.DebugFormat("[Materials]: Initialized"); | ||
82 | } | ||
83 | |||
84 | public void Close() | ||
85 | { | ||
86 | if (!m_enabled) | ||
87 | return; | ||
88 | } | ||
89 | |||
90 | public void AddRegion(Scene scene) | ||
91 | { | ||
92 | if (!m_enabled) | ||
93 | return; | ||
94 | |||
95 | m_scene = scene; | ||
96 | m_scene.EventManager.OnRegisterCaps += OnRegisterCaps; | ||
97 | m_scene.EventManager.OnObjectAddedToScene += EventManager_OnObjectAddedToScene; | ||
98 | } | ||
99 | |||
100 | private void EventManager_OnObjectAddedToScene(SceneObjectGroup obj) | ||
101 | { | ||
102 | foreach (var part in obj.Parts) | ||
103 | if (part != null) | ||
104 | GetStoredMaterialsInPart(part); | ||
105 | } | ||
106 | |||
107 | private void OnRegisterCaps(OpenMetaverse.UUID agentID, OpenSim.Framework.Capabilities.Caps caps) | ||
108 | { | ||
109 | string capsBase = "/CAPS/" + caps.CapsObjectPath; | ||
110 | |||
111 | IRequestHandler renderMaterialsPostHandler | ||
112 | = new RestStreamHandler("POST", capsBase + "/", | ||
113 | (request, path, param, httpRequest, httpResponse) | ||
114 | => RenderMaterialsPostCap(request, agentID), | ||
115 | "RenderMaterials", null); | ||
116 | caps.RegisterHandler("RenderMaterials", renderMaterialsPostHandler); | ||
117 | |||
118 | // OpenSimulator CAPs infrastructure seems to be somewhat hostile towards any CAP that requires both GET | ||
119 | // and POST handlers, (at least at the time this was originally written), so we first set up a POST | ||
120 | // handler normally and then add a GET handler via MainServer | ||
121 | |||
122 | IRequestHandler renderMaterialsGetHandler | ||
123 | = new RestStreamHandler("GET", capsBase + "/", | ||
124 | (request, path, param, httpRequest, httpResponse) | ||
125 | => RenderMaterialsGetCap(request), | ||
126 | "RenderMaterials", null); | ||
127 | MainServer.Instance.AddStreamHandler(renderMaterialsGetHandler); | ||
128 | |||
129 | // materials viewer seems to use either POST or PUT, so assign POST handler for PUT as well | ||
130 | IRequestHandler renderMaterialsPutHandler | ||
131 | = new RestStreamHandler("PUT", capsBase + "/", | ||
132 | (request, path, param, httpRequest, httpResponse) | ||
133 | => RenderMaterialsPostCap(request, agentID), | ||
134 | "RenderMaterials", null); | ||
135 | MainServer.Instance.AddStreamHandler(renderMaterialsPutHandler); | ||
136 | } | ||
137 | |||
138 | public void RemoveRegion(Scene scene) | ||
139 | { | ||
140 | if (!m_enabled) | ||
141 | return; | ||
142 | |||
143 | m_scene.EventManager.OnRegisterCaps -= OnRegisterCaps; | ||
144 | m_scene.EventManager.OnObjectAddedToScene -= EventManager_OnObjectAddedToScene; | ||
145 | } | ||
146 | |||
147 | public void RegionLoaded(Scene scene) | ||
148 | { | ||
149 | } | ||
150 | |||
151 | /// <summary> | ||
152 | /// Finds any legacy materials stored in DynAttrs that may exist for this part and add them to 'm_regionMaterials'. | ||
153 | /// </summary> | ||
154 | /// <param name="part"></param> | ||
155 | private void GetLegacyStoredMaterialsInPart(SceneObjectPart part) | ||
156 | { | ||
157 | if (part.DynAttrs == null) | ||
158 | return; | ||
159 | |||
160 | OSD OSMaterials = null; | ||
161 | OSDArray matsArr = null; | ||
162 | |||
163 | lock (part.DynAttrs) | ||
164 | { | ||
165 | if (part.DynAttrs.ContainsStore("OpenSim", "Materials")) | ||
166 | { | ||
167 | OSDMap materialsStore = part.DynAttrs.GetStore("OpenSim", "Materials"); | ||
168 | |||
169 | if (materialsStore == null) | ||
170 | return; | ||
171 | |||
172 | materialsStore.TryGetValue("Materials", out OSMaterials); | ||
173 | } | ||
174 | |||
175 | if (OSMaterials != null && OSMaterials is OSDArray) | ||
176 | matsArr = OSMaterials as OSDArray; | ||
177 | else | ||
178 | return; | ||
179 | } | ||
180 | |||
181 | if (matsArr == null) | ||
182 | return; | ||
183 | |||
184 | foreach (OSD elemOsd in matsArr) | ||
185 | { | ||
186 | if (elemOsd != null && elemOsd is OSDMap) | ||
187 | { | ||
188 | OSDMap matMap = elemOsd as OSDMap; | ||
189 | if (matMap.ContainsKey("ID") && matMap.ContainsKey("Material")) | ||
190 | { | ||
191 | try | ||
192 | { | ||
193 | lock (m_regionMaterials) | ||
194 | m_regionMaterials[matMap["ID"].AsUUID()] = (OSDMap)matMap["Material"]; | ||
195 | } | ||
196 | catch (Exception e) | ||
197 | { | ||
198 | m_log.Warn("[Materials]: exception decoding persisted legacy material: " + e.ToString()); | ||
199 | } | ||
200 | } | ||
201 | } | ||
202 | } | ||
203 | } | ||
204 | |||
205 | /// <summary> | ||
206 | /// Find the materials used in the SOP, and add them to 'm_regionMaterials'. | ||
207 | /// </summary> | ||
208 | private void GetStoredMaterialsInPart(SceneObjectPart part) | ||
209 | { | ||
210 | if (part.Shape == null) | ||
211 | return; | ||
212 | |||
213 | var te = new Primitive.TextureEntry(part.Shape.TextureEntry, 0, part.Shape.TextureEntry.Length); | ||
214 | if (te == null) | ||
215 | return; | ||
216 | |||
217 | GetLegacyStoredMaterialsInPart(part); | ||
218 | |||
219 | GetStoredMaterialInFace(part, te.DefaultTexture); | ||
220 | |||
221 | foreach (Primitive.TextureEntryFace face in te.FaceTextures) | ||
222 | { | ||
223 | if (face != null) | ||
224 | GetStoredMaterialInFace(part, face); | ||
225 | } | ||
226 | } | ||
227 | |||
228 | /// <summary> | ||
229 | /// Find the materials used in one Face, and add them to 'm_regionMaterials'. | ||
230 | /// </summary> | ||
231 | private void GetStoredMaterialInFace(SceneObjectPart part, Primitive.TextureEntryFace face) | ||
232 | { | ||
233 | UUID id = face.MaterialID; | ||
234 | if (id == UUID.Zero) | ||
235 | return; | ||
236 | |||
237 | lock (m_regionMaterials) | ||
238 | { | ||
239 | if (m_regionMaterials.ContainsKey(id)) | ||
240 | return; | ||
241 | |||
242 | byte[] data = m_scene.AssetService.GetData(id.ToString()); | ||
243 | if (data == null) | ||
244 | { | ||
245 | m_log.WarnFormat("[Materials]: Prim \"{0}\" ({1}) contains unknown material ID {2}", part.Name, part.UUID, id); | ||
246 | return; | ||
247 | } | ||
248 | |||
249 | OSDMap mat; | ||
250 | try | ||
251 | { | ||
252 | mat = (OSDMap)OSDParser.DeserializeLLSDXml(data); | ||
253 | } | ||
254 | catch (Exception e) | ||
255 | { | ||
256 | m_log.WarnFormat("[Materials]: cannot decode material asset {0}: {1}", id, e.Message); | ||
257 | return; | ||
258 | } | ||
259 | |||
260 | m_regionMaterials[id] = mat; | ||
261 | } | ||
262 | } | ||
263 | |||
264 | public string RenderMaterialsPostCap(string request, UUID agentID) | ||
265 | { | ||
266 | OSDMap req = (OSDMap)OSDParser.DeserializeLLSDXml(request); | ||
267 | OSDMap resp = new OSDMap(); | ||
268 | |||
269 | OSDMap materialsFromViewer = null; | ||
270 | |||
271 | OSDArray respArr = new OSDArray(); | ||
272 | |||
273 | if (req.ContainsKey("Zipped")) | ||
274 | { | ||
275 | OSD osd = null; | ||
276 | |||
277 | byte[] inBytes = req["Zipped"].AsBinary(); | ||
278 | |||
279 | try | ||
280 | { | ||
281 | osd = ZDecompressBytesToOsd(inBytes); | ||
282 | |||
283 | if (osd != null) | ||
284 | { | ||
285 | if (osd is OSDArray) // assume array of MaterialIDs designating requested material entries | ||
286 | { | ||
287 | foreach (OSD elem in (OSDArray)osd) | ||
288 | { | ||
289 | try | ||
290 | { | ||
291 | UUID id = new UUID(elem.AsBinary(), 0); | ||
292 | |||
293 | lock (m_regionMaterials) | ||
294 | { | ||
295 | if (m_regionMaterials.ContainsKey(id)) | ||
296 | { | ||
297 | OSDMap matMap = new OSDMap(); | ||
298 | matMap["ID"] = OSD.FromBinary(id.GetBytes()); | ||
299 | matMap["Material"] = m_regionMaterials[id]; | ||
300 | respArr.Add(matMap); | ||
301 | } | ||
302 | else | ||
303 | { | ||
304 | m_log.Warn("[Materials]: request for unknown material ID: " + id.ToString()); | ||
305 | |||
306 | // Theoretically we could try to load the material from the assets service, | ||
307 | // but that shouldn't be necessary because the viewer should only request | ||
308 | // materials that exist in a prim on the region, and all of these materials | ||
309 | // are already stored in m_regionMaterials. | ||
310 | } | ||
311 | } | ||
312 | } | ||
313 | catch (Exception e) | ||
314 | { | ||
315 | m_log.Error("Error getting materials in response to viewer request", e); | ||
316 | continue; | ||
317 | } | ||
318 | } | ||
319 | } | ||
320 | else if (osd is OSDMap) // request to assign a material | ||
321 | { | ||
322 | materialsFromViewer = osd as OSDMap; | ||
323 | |||
324 | if (materialsFromViewer.ContainsKey("FullMaterialsPerFace")) | ||
325 | { | ||
326 | OSD matsOsd = materialsFromViewer["FullMaterialsPerFace"]; | ||
327 | if (matsOsd is OSDArray) | ||
328 | { | ||
329 | OSDArray matsArr = matsOsd as OSDArray; | ||
330 | |||
331 | try | ||
332 | { | ||
333 | foreach (OSDMap matsMap in matsArr) | ||
334 | { | ||
335 | uint primLocalID = 0; | ||
336 | try { | ||
337 | primLocalID = matsMap["ID"].AsUInteger(); | ||
338 | } | ||
339 | catch (Exception e) { | ||
340 | m_log.Warn("[Materials]: cannot decode \"ID\" from matsMap: " + e.Message); | ||
341 | continue; | ||
342 | } | ||
343 | |||
344 | OSDMap mat = null; | ||
345 | try | ||
346 | { | ||
347 | mat = matsMap["Material"] as OSDMap; | ||
348 | } | ||
349 | catch (Exception e) | ||
350 | { | ||
351 | m_log.Warn("[Materials]: cannot decode \"Material\" from matsMap: " + e.Message); | ||
352 | continue; | ||
353 | } | ||
354 | |||
355 | SceneObjectPart sop = m_scene.GetSceneObjectPart(primLocalID); | ||
356 | if (sop == null) | ||
357 | { | ||
358 | m_log.WarnFormat("[Materials]: SOP not found for localId: {0}", primLocalID.ToString()); | ||
359 | continue; | ||
360 | } | ||
361 | |||
362 | if (!m_scene.Permissions.CanEditObject(sop.UUID, agentID)) | ||
363 | { | ||
364 | m_log.WarnFormat("User {0} can't edit object {1} {2}", agentID, sop.Name, sop.UUID); | ||
365 | continue; | ||
366 | } | ||
367 | |||
368 | Primitive.TextureEntry te = new Primitive.TextureEntry(sop.Shape.TextureEntry, 0, sop.Shape.TextureEntry.Length); | ||
369 | if (te == null) | ||
370 | { | ||
371 | m_log.WarnFormat("[Materials]: Error in TextureEntry for SOP {0} {1}", sop.Name, sop.UUID); | ||
372 | continue; | ||
373 | } | ||
374 | |||
375 | |||
376 | UUID id; | ||
377 | if (mat == null) | ||
378 | { | ||
379 | // This happens then the user removes a material from a prim | ||
380 | id = UUID.Zero; | ||
381 | } | ||
382 | else | ||
383 | { | ||
384 | id = StoreMaterialAsAsset(agentID, mat, sop); | ||
385 | } | ||
386 | |||
387 | |||
388 | int face = -1; | ||
389 | |||
390 | if (matsMap.ContainsKey("Face")) | ||
391 | { | ||
392 | face = matsMap["Face"].AsInteger(); | ||
393 | Primitive.TextureEntryFace faceEntry = te.CreateFace((uint)face); | ||
394 | faceEntry.MaterialID = id; | ||
395 | } | ||
396 | else | ||
397 | { | ||
398 | if (te.DefaultTexture == null) | ||
399 | m_log.WarnFormat("[Materials]: TextureEntry.DefaultTexture is null in {0} {1}", sop.Name, sop.UUID); | ||
400 | else | ||
401 | te.DefaultTexture.MaterialID = id; | ||
402 | } | ||
403 | |||
404 | //m_log.DebugFormat("[Materials]: in \"{0}\" {1}, setting material ID for face {2} to {3}", sop.Name, sop.UUID, face, id); | ||
405 | |||
406 | // We can't use sop.UpdateTextureEntry(te) because it filters, so do it manually | ||
407 | sop.Shape.TextureEntry = te.GetBytes(); | ||
408 | |||
409 | if (sop.ParentGroup != null) | ||
410 | { | ||
411 | sop.TriggerScriptChangedEvent(Changed.TEXTURE); | ||
412 | sop.UpdateFlag = UpdateRequired.FULL; | ||
413 | sop.ParentGroup.HasGroupChanged = true; | ||
414 | sop.ScheduleFullUpdate(); | ||
415 | } | ||
416 | } | ||
417 | } | ||
418 | catch (Exception e) | ||
419 | { | ||
420 | m_log.Warn("[Materials]: exception processing received material ", e); | ||
421 | } | ||
422 | } | ||
423 | } | ||
424 | } | ||
425 | } | ||
426 | |||
427 | } | ||
428 | catch (Exception e) | ||
429 | { | ||
430 | m_log.Warn("[Materials]: exception decoding zipped CAP payload ", e); | ||
431 | //return ""; | ||
432 | } | ||
433 | } | ||
434 | |||
435 | |||
436 | resp["Zipped"] = ZCompressOSD(respArr, false); | ||
437 | string response = OSDParser.SerializeLLSDXmlString(resp); | ||
438 | |||
439 | //m_log.Debug("[Materials]: cap request: " + request); | ||
440 | //m_log.Debug("[Materials]: cap request (zipped portion): " + ZippedOsdBytesToString(req["Zipped"].AsBinary())); | ||
441 | //m_log.Debug("[Materials]: cap response: " + response); | ||
442 | return response; | ||
443 | } | ||
444 | |||
445 | private UUID StoreMaterialAsAsset(UUID agentID, OSDMap mat, SceneObjectPart sop) | ||
446 | { | ||
447 | UUID id; | ||
448 | // Material UUID = hash of the material's data. | ||
449 | // This makes materials deduplicate across the entire grid (but isn't otherwise required). | ||
450 | byte[] data = System.Text.Encoding.ASCII.GetBytes(OSDParser.SerializeLLSDXmlString(mat)); | ||
451 | using (var md5 = MD5.Create()) | ||
452 | id = new UUID(md5.ComputeHash(data), 0); | ||
453 | |||
454 | lock (m_regionMaterials) | ||
455 | { | ||
456 | if (!m_regionMaterials.ContainsKey(id)) | ||
457 | { | ||
458 | m_regionMaterials[id] = mat; | ||
459 | |||
460 | // This asset might exist already, but it's ok to try to store it again | ||
461 | string name = "Material " + ChooseMaterialName(mat, sop); | ||
462 | name = name.Substring(0, Math.Min(64, name.Length)).Trim(); | ||
463 | AssetBase asset = new AssetBase(id, name, (sbyte)OpenSimAssetType.Material, agentID.ToString()); | ||
464 | asset.Data = data; | ||
465 | m_scene.AssetService.Store(asset); | ||
466 | } | ||
467 | } | ||
468 | return id; | ||
469 | } | ||
470 | |||
471 | /// <summary> | ||
472 | /// Use heuristics to choose a good name for the material. | ||
473 | /// </summary> | ||
474 | private string ChooseMaterialName(OSDMap mat, SceneObjectPart sop) | ||
475 | { | ||
476 | UUID normMap = mat["NormMap"].AsUUID(); | ||
477 | if (normMap != UUID.Zero) | ||
478 | { | ||
479 | AssetBase asset = m_scene.AssetService.GetCached(normMap.ToString()); | ||
480 | if ((asset != null) && (asset.Name.Length > 0) && !asset.Name.Equals("From IAR")) | ||
481 | return asset.Name; | ||
482 | } | ||
483 | |||
484 | UUID specMap = mat["SpecMap"].AsUUID(); | ||
485 | if (specMap != UUID.Zero) | ||
486 | { | ||
487 | AssetBase asset = m_scene.AssetService.GetCached(specMap.ToString()); | ||
488 | if ((asset != null) && (asset.Name.Length > 0) && !asset.Name.Equals("From IAR")) | ||
489 | return asset.Name; | ||
490 | } | ||
491 | |||
492 | if (sop.Name != "Primitive") | ||
493 | return sop.Name; | ||
494 | |||
495 | if ((sop.ParentGroup != null) && (sop.ParentGroup.Name != "Primitive")) | ||
496 | return sop.ParentGroup.Name; | ||
497 | |||
498 | return ""; | ||
499 | } | ||
500 | |||
501 | |||
502 | public string RenderMaterialsGetCap(string request) | ||
503 | { | ||
504 | OSDMap resp = new OSDMap(); | ||
505 | int matsCount = 0; | ||
506 | OSDArray allOsd = new OSDArray(); | ||
507 | |||
508 | lock (m_regionMaterials) | ||
509 | { | ||
510 | foreach (KeyValuePair<UUID, OSDMap> kvp in m_regionMaterials) | ||
511 | { | ||
512 | OSDMap matMap = new OSDMap(); | ||
513 | |||
514 | matMap["ID"] = OSD.FromBinary(kvp.Key.GetBytes()); | ||
515 | matMap["Material"] = kvp.Value; | ||
516 | allOsd.Add(matMap); | ||
517 | matsCount++; | ||
518 | } | ||
519 | } | ||
520 | |||
521 | resp["Zipped"] = ZCompressOSD(allOsd, false); | ||
522 | |||
523 | return OSDParser.SerializeLLSDXmlString(resp); | ||
524 | } | ||
525 | |||
526 | private static string ZippedOsdBytesToString(byte[] bytes) | ||
527 | { | ||
528 | try | ||
529 | { | ||
530 | return OSDParser.SerializeJsonString(ZDecompressBytesToOsd(bytes)); | ||
531 | } | ||
532 | catch (Exception e) | ||
533 | { | ||
534 | return "ZippedOsdBytesToString caught an exception: " + e.ToString(); | ||
535 | } | ||
536 | } | ||
537 | |||
538 | /// <summary> | ||
539 | /// computes a UUID by hashing a OSD object | ||
540 | /// </summary> | ||
541 | /// <param name="osd"></param> | ||
542 | /// <returns></returns> | ||
543 | private static UUID HashOsd(OSD osd) | ||
544 | { | ||
545 | byte[] data = OSDParser.SerializeLLSDBinary(osd, false); | ||
546 | using (var md5 = MD5.Create()) | ||
547 | return new UUID(md5.ComputeHash(data), 0); | ||
548 | } | ||
549 | |||
550 | public static OSD ZCompressOSD(OSD inOsd, bool useHeader) | ||
551 | { | ||
552 | OSD osd = null; | ||
553 | |||
554 | byte[] data = OSDParser.SerializeLLSDBinary(inOsd, useHeader); | ||
555 | |||
556 | using (MemoryStream msSinkCompressed = new MemoryStream()) | ||
557 | { | ||
558 | using (Ionic.Zlib.ZlibStream zOut = new Ionic.Zlib.ZlibStream(msSinkCompressed, | ||
559 | Ionic.Zlib.CompressionMode.Compress, CompressionLevel.BestCompression, true)) | ||
560 | { | ||
561 | zOut.Write(data, 0, data.Length); | ||
562 | } | ||
563 | |||
564 | msSinkCompressed.Seek(0L, SeekOrigin.Begin); | ||
565 | osd = OSD.FromBinary(msSinkCompressed.ToArray()); | ||
566 | } | ||
567 | |||
568 | return osd; | ||
569 | } | ||
570 | |||
571 | |||
572 | public static OSD ZDecompressBytesToOsd(byte[] input) | ||
573 | { | ||
574 | OSD osd = null; | ||
575 | |||
576 | using (MemoryStream msSinkUnCompressed = new MemoryStream()) | ||
577 | { | ||
578 | using (Ionic.Zlib.ZlibStream zOut = new Ionic.Zlib.ZlibStream(msSinkUnCompressed, CompressionMode.Decompress, true)) | ||
579 | { | ||
580 | zOut.Write(input, 0, input.Length); | ||
581 | } | ||
582 | |||
583 | msSinkUnCompressed.Seek(0L, SeekOrigin.Begin); | ||
584 | osd = OSDParser.DeserializeLLSDBinary(msSinkUnCompressed.ToArray()); | ||
585 | } | ||
586 | |||
587 | return osd; | ||
588 | } | ||
589 | } | ||
590 | } | ||