aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/OptionalModules/Materials/MaterialsDemoModule.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/OptionalModules/Materials/MaterialsDemoModule.cs162
1 files changed, 154 insertions, 8 deletions
diff --git a/OpenSim/Region/OptionalModules/Materials/MaterialsDemoModule.cs b/OpenSim/Region/OptionalModules/Materials/MaterialsDemoModule.cs
index 4501dad..4ab6609 100644
--- a/OpenSim/Region/OptionalModules/Materials/MaterialsDemoModule.cs
+++ b/OpenSim/Region/OptionalModules/Materials/MaterialsDemoModule.cs
@@ -62,15 +62,21 @@ namespace OpenSim.Region.OptionalModules.MaterialsDemoModule
62 // ## ## # # # # # ## # # ## # # 62 // ## ## # # # # # ## # # ## # #
63 // # # # # # # # # # # # #### 63 // # # # # # # # # # # # ####
64 // 64 //
65 // 65 // THIS MODULE IS FOR EXPERIMENTAL USE ONLY AND MAY CAUSE REGION OR ASSET CORRUPTION!
66 // 66 //
67 ////////////// WARNING ////////////////////////////////////////////////////////////////// 67 ////////////// WARNING //////////////////////////////////////////////////////////////////
68 /// This is an *Experimental* module for developing support for materials-capable viewers 68 /// This is an *Experimental* module for developing support for materials-capable viewers
69 /// This module should NOT be used in a production environment! It may cause data corruption and 69 /// This module should NOT be used in a production environment! It may cause data corruption and
70 /// viewer crashes. It should be only used to evaluate implementations of materials. 70 /// viewer crashes. It should be only used to evaluate implementations of materials.
71 /// 71 ///
72 /// CURRENTLY NO MATERIALS ARE PERSISTED ACROSS SIMULATOR RESTARTS OR ARE STORED IN ANY INVENTORY OR ASSETS 72 /// Materials are persisted via SceneObjectPart.dynattrs. This is a relatively new feature
73 /// This may change in future implementations. 73 /// of OpenSimulator and is not field proven at the time this module was written. Persistence
74 /// may fail or become corrupt and this could cause viewer crashes due to erroneous materials
75 /// data being sent to viewers. Materials descriptions might survive IAR, OAR, or other means
76 /// of archiving however the texture resources used by these materials probably will not as they
77 /// may not be adequately referenced to ensure proper archiving.
78 ///
79 ///
74 /// 80 ///
75 /// To enable this module, add this string at the bottom of OpenSim.ini: 81 /// To enable this module, add this string at the bottom of OpenSim.ini:
76 /// [MaterialsDemoModule] 82 /// [MaterialsDemoModule]
@@ -89,6 +95,8 @@ namespace OpenSim.Region.OptionalModules.MaterialsDemoModule
89 95
90 private Scene m_scene = null; 96 private Scene m_scene = null;
91 private bool m_enabled = false; 97 private bool m_enabled = false;
98
99 public Dictionary<UUID, OSDMap> m_knownMaterials = new Dictionary<UUID, OSDMap>();
92 100
93 public void Initialise(IConfigSource source) 101 public void Initialise(IConfigSource source)
94 { 102 {
@@ -97,7 +105,6 @@ namespace OpenSim.Region.OptionalModules.MaterialsDemoModule
97 return; 105 return;
98 106
99 m_log.DebugFormat("[MaterialsDemoModule]: INITIALIZED MODULE"); 107 m_log.DebugFormat("[MaterialsDemoModule]: INITIALIZED MODULE");
100
101 } 108 }
102 109
103 public void Close() 110 public void Close()
@@ -116,6 +123,14 @@ namespace OpenSim.Region.OptionalModules.MaterialsDemoModule
116 m_log.DebugFormat("[MaterialsDemoModule]: REGION {0} ADDED", scene.RegionInfo.RegionName); 123 m_log.DebugFormat("[MaterialsDemoModule]: REGION {0} ADDED", scene.RegionInfo.RegionName);
117 m_scene = scene; 124 m_scene = scene;
118 m_scene.EventManager.OnRegisterCaps += new EventManager.RegisterCapsEvent(OnRegisterCaps); 125 m_scene.EventManager.OnRegisterCaps += new EventManager.RegisterCapsEvent(OnRegisterCaps);
126 m_scene.EventManager.OnObjectAddedToScene += new Action<SceneObjectGroup>(EventManager_OnObjectAddedToScene);
127 }
128
129 void EventManager_OnObjectAddedToScene(SceneObjectGroup obj)
130 {
131 foreach (var part in obj.Parts)
132 if (part != null)
133 GetStoredMaterialsForPart(part);
119 } 134 }
120 135
121 void OnRegisterCaps(OpenMetaverse.UUID agentID, OpenSim.Framework.Capabilities.Caps caps) 136 void OnRegisterCaps(OpenMetaverse.UUID agentID, OpenSim.Framework.Capabilities.Caps caps)
@@ -147,11 +162,130 @@ namespace OpenSim.Region.OptionalModules.MaterialsDemoModule
147 162
148 public void RegionLoaded(Scene scene) 163 public void RegionLoaded(Scene scene)
149 { 164 {
150 if (!m_enabled) 165 }
166
167 OSDMap GetMaterial(UUID id)
168 {
169 OSDMap map = null;
170 if (m_knownMaterials.ContainsKey(id))
171 {
172 map = new OSDMap();
173 map["ID"] = OSD.FromBinary(id.GetBytes());
174 map["Material"] = m_knownMaterials[id];
175 }
176 return map;
177 }
178
179 void GetStoredMaterialsForPart(SceneObjectPart part)
180 {
181 OSDMap OSMaterials = null;
182 OSDArray matsArr = null;
183
184 if (part.DynAttrs == null)
185 {
186 m_log.Warn("[MaterialsDemoModule]: NULL DYNATTRS :( ");
187 }
188
189 lock (part.DynAttrs)
190 {
191 if (part.DynAttrs.ContainsKey("OS:Materials"))
192 OSMaterials = part.DynAttrs["OS:Materials"];
193 if (OSMaterials != null && OSMaterials.ContainsKey("Materials"))
194 {
195
196 OSD osd = OSMaterials["Materials"];
197 if (osd is OSDArray)
198 matsArr = osd as OSDArray;
199 }
200 }
201
202 if (OSMaterials == null)
151 return; 203 return;
152 m_log.DebugFormat("[MaterialsDemoModule]: REGION {0} LOADED", scene.RegionInfo.RegionName); 204
205 m_log.Info("[MaterialsDemoModule]: OSMaterials: " + OSDParser.SerializeJsonString(OSMaterials));
206
207
208 if (matsArr == null)
209 {
210 m_log.Info("[MaterialsDemoModule]: matsArr is null :( ");
211 return;
212 }
213
214 foreach (OSD elemOsd in matsArr)
215 {
216 if (elemOsd != null && elemOsd is OSDMap)
217 {
218
219 OSDMap matMap = elemOsd as OSDMap;
220 if (matMap.ContainsKey("ID") && matMap.ContainsKey("Material"))
221 {
222 try
223 {
224 m_knownMaterials[matMap["ID"].AsUUID()] = (OSDMap)matMap["Material"];
225 }
226 catch (Exception e)
227 {
228 m_log.Warn("[MaterialsDemoModule]: exception decoding persisted material: " + e.ToString());
229 }
230 }
231 }
232 }
153 } 233 }
154 234
235
236 void StoreMaterialsForPart(SceneObjectPart part)
237 {
238 try
239 {
240 if (part == null || part.Shape == null)
241 return;
242
243 Dictionary<UUID, OSDMap> mats = new Dictionary<UUID, OSDMap>();
244
245 Primitive.TextureEntry te = part.Shape.Textures;
246
247 if (te.DefaultTexture != null)
248 {
249 if (m_knownMaterials.ContainsKey(te.DefaultTexture.MaterialID))
250 mats[te.DefaultTexture.MaterialID] = m_knownMaterials[te.DefaultTexture.MaterialID];
251 }
252
253 if (te.FaceTextures != null)
254 {
255 foreach (var face in te.FaceTextures)
256 {
257 if (face != null)
258 {
259 if (m_knownMaterials.ContainsKey(face.MaterialID))
260 mats[face.MaterialID] = m_knownMaterials[face.MaterialID];
261 }
262 }
263 }
264 if (mats.Count == 0)
265 return;
266
267 OSDArray matsArr = new OSDArray();
268 foreach (KeyValuePair<UUID, OSDMap> kvp in mats)
269 {
270 OSDMap matOsd = new OSDMap();
271 matOsd["ID"] = OSD.FromUUID(kvp.Key);
272 matOsd["Material"] = kvp.Value;
273 matsArr.Add(matOsd);
274 }
275
276 OSDMap OSMaterials = new OSDMap();
277 OSMaterials["Materials"] = matsArr;
278
279 lock (part.DynAttrs)
280 part.DynAttrs["OS:Materials"] = OSMaterials;
281 }
282 catch (Exception e)
283 {
284 m_log.Warn("[MaterialsDemoModule]: exception in StoreMaterialsForPart(): " + e.ToString());
285 }
286 }
287
288
155 public string RenderMaterialsPostCap(string request, string path, 289 public string RenderMaterialsPostCap(string request, string path,
156 string param, IOSHttpRequest httpRequest, 290 string param, IOSHttpRequest httpRequest,
157 IOSHttpResponse httpResponse) 291 IOSHttpResponse httpResponse)
@@ -300,6 +434,7 @@ namespace OpenSim.Region.OptionalModules.MaterialsDemoModule
300 434
301 sop.ScheduleFullUpdate(); 435 sop.ScheduleFullUpdate();
302 436
437 StoreMaterialsForPart(sop);
303 } 438 }
304 } 439 }
305 } 440 }
@@ -327,7 +462,8 @@ namespace OpenSim.Region.OptionalModules.MaterialsDemoModule
327 resp["Zipped"] = ZCompressOSD(respArr, false); 462 resp["Zipped"] = ZCompressOSD(respArr, false);
328 string response = OSDParser.SerializeLLSDXmlString(resp); 463 string response = OSDParser.SerializeLLSDXmlString(resp);
329 464
330 m_log.Debug("[MaterialsDemoModule]: cap request: " + request); 465 //m_log.Debug("[MaterialsDemoModule]: cap request: " + request);
466 m_log.Debug("[MaterialsDemoModule]: cap request (zipped portion): " + ZippedOsdBytesToString(req["Zipped"].AsBinary()));
331 m_log.Debug("[MaterialsDemoModule]: cap response: " + response); 467 m_log.Debug("[MaterialsDemoModule]: cap response: " + response);
332 return response; 468 return response;
333 } 469 }
@@ -364,7 +500,17 @@ namespace OpenSim.Region.OptionalModules.MaterialsDemoModule
364 return OSDParser.SerializeLLSDXmlString(resp); 500 return OSDParser.SerializeLLSDXmlString(resp);
365 } 501 }
366 502
367 public Dictionary<UUID, OSDMap> m_knownMaterials = new Dictionary<UUID, OSDMap>(); 503 static string ZippedOsdBytesToString(byte[] bytes)
504 {
505 try
506 {
507 return OSDParser.SerializeJsonString(ZDecompressBytesToOsd(bytes));
508 }
509 catch (Exception e)
510 {
511 return "ZippedOsdBytesToString caught an exception: " + e.ToString();
512 }
513 }
368 514
369 /// <summary> 515 /// <summary>
370 /// computes a UUID by hashing a OSD object 516 /// computes a UUID by hashing a OSD object