OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
WearableCacheItem.cs
Go to the documentation of this file.
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 OpenMetaverse;
31 using OpenMetaverse.StructuredData;
32 
33 namespace OpenSim.Framework
34 {
35  [Serializable]
36  public class WearableCacheItem
37  {
38  public uint TextureIndex { get; set; }
39  public UUID CacheId { get; set; }
40  public UUID TextureID { get; set; }
41  public AssetBase TextureAsset { get; set; }
42 
43 
45  {
46  int itemmax = AvatarAppearance.TEXTURE_COUNT;
47  WearableCacheItem[] retitems = new WearableCacheItem[itemmax];
48  for (uint i=0;i<itemmax;i++)
49  retitems[i] = new WearableCacheItem() {CacheId = UUID.Zero, TextureID = UUID.Zero, TextureIndex = i};
50  return retitems;
51  }
52 
53  public static WearableCacheItem[] FromOSD(OSD pInput, IImprovedAssetCache dataCache)
54  {
55  List<WearableCacheItem> ret = new List<WearableCacheItem>();
56  if (pInput.Type == OSDType.Array)
57  {
58  OSDArray itemarray = (OSDArray) pInput;
59  foreach (OSDMap item in itemarray)
60  {
61  ret.Add(new WearableCacheItem()
62  {
63  TextureIndex = item["textureindex"].AsUInteger(),
64  CacheId = item["cacheid"].AsUUID(),
65  TextureID = item["textureid"].AsUUID()
66  });
67 
68  if (dataCache != null && item.ContainsKey("assetdata"))
69  {
70  AssetBase asset = new AssetBase(item["textureid"].AsUUID(),"BakedTexture",(sbyte)AssetType.Texture,UUID.Zero.ToString());
71  asset.Temporary = true;
72  asset.Data = item["assetdata"].AsBinary();
73  dataCache.Cache(asset);
74  }
75  }
76  }
77  else if (pInput.Type == OSDType.Map)
78  {
79  OSDMap item = (OSDMap) pInput;
80  ret.Add(new WearableCacheItem(){
81  TextureIndex = item["textureindex"].AsUInteger(),
82  CacheId = item["cacheid"].AsUUID(),
83  TextureID = item["textureid"].AsUUID()
84  });
85  if (dataCache != null && item.ContainsKey("assetdata"))
86  {
87  string assetCreator = item["assetcreator"].AsString();
88  string assetName = item["assetname"].AsString();
89  AssetBase asset = new AssetBase(item["textureid"].AsUUID(), assetName, (sbyte)AssetType.Texture, assetCreator);
90  asset.Temporary = true;
91  asset.Data = item["assetdata"].AsBinary();
92  dataCache.Cache(asset);
93  }
94  }
95  else
96  {
97  return new WearableCacheItem[0];
98  }
99  return ret.ToArray();
100 
101  }
102 
103  public static OSD ToOSD(WearableCacheItem[] pcacheItems, IImprovedAssetCache dataCache)
104  {
105  OSDArray arr = new OSDArray();
106  foreach (WearableCacheItem item in pcacheItems)
107  {
108  OSDMap itemmap = new OSDMap();
109  itemmap.Add("textureindex", OSD.FromUInteger(item.TextureIndex));
110  itemmap.Add("cacheid", OSD.FromUUID(item.CacheId));
111  itemmap.Add("textureid", OSD.FromUUID(item.TextureID));
112  if (dataCache != null)
113  {
114  if (dataCache.Check(item.TextureID.ToString()))
115  {
116  AssetBase assetItem = dataCache.Get(item.TextureID.ToString());
117  if (assetItem != null)
118  {
119  itemmap.Add("assetdata", OSD.FromBinary(assetItem.Data));
120  itemmap.Add("assetcreator", OSD.FromString(assetItem.CreatorID));
121  itemmap.Add("assetname", OSD.FromString(assetItem.Name));
122  }
123  }
124  }
125  arr.Add(itemmap);
126  }
127  return arr;
128  }
129 
130  public static OSDArray BakedToOSD(WearableCacheItem[] pcacheItems)
131  {
132  if (pcacheItems.Length < AvatarAppearance.BAKE_INDICES[AvatarAppearance.BAKE_INDICES.Length - 1])
133  return null;
134 
135  OSDArray arr = new OSDArray();
136 
137  for (int i = 0; i < AvatarAppearance.BAKE_INDICES.Length; i++)
138  {
139  int idx = AvatarAppearance.BAKE_INDICES[i];
140 
141  WearableCacheItem item = pcacheItems[idx];
142 
143  OSDMap itemmap = new OSDMap();
144  itemmap.Add("textureindex", OSD.FromUInteger(item.TextureIndex));
145  itemmap.Add("cacheid", OSD.FromUUID(item.CacheId));
146  itemmap.Add("textureid", OSD.FromUUID(item.TextureID));
147 /*
148  if (item.TextureAsset != null)
149  {
150  itemmap.Add("assetdata", OSD.FromBinary(item.TextureAsset.Data));
151  itemmap.Add("assetcreator", OSD.FromString(item.TextureAsset.CreatorID));
152  itemmap.Add("assetname", OSD.FromString(item.TextureAsset.Name));
153  }
154  */
155  arr.Add(itemmap);
156  }
157  return arr;
158  }
159 
160  public static WearableCacheItem[] BakedFromOSD(OSD pInput)
161  {
162  WearableCacheItem[] pcache = WearableCacheItem.GetDefaultCacheItem();
163 
164  if (pInput.Type == OSDType.Array)
165  {
166  OSDArray itemarray = (OSDArray)pInput;
167  foreach (OSDMap item in itemarray)
168  {
169  int idx = (int)item["textureindex"].AsUInteger();
170  if (idx < 0 || idx > pcache.Length)
171  continue;
172  pcache[idx].CacheId = item["cacheid"].AsUUID();
173  pcache[idx].TextureID = item["textureid"].AsUUID();
174 /*
175  if (item.ContainsKey("assetdata"))
176  {
177  AssetBase asset = new AssetBase(item["textureid"].AsUUID(), "BakedTexture", (sbyte)AssetType.Texture, UUID.Zero.ToString());
178  asset.Temporary = true;
179  asset.Local = true;
180  asset.Data = item["assetdata"].AsBinary();
181  pcache[idx].TextureAsset = asset;
182  }
183  else
184  */
185  pcache[idx].TextureAsset = null;
186  }
187  }
188  return pcache;
189  }
190 
191  public static WearableCacheItem SearchTextureIndex(uint pTextureIndex,WearableCacheItem[] pcacheItems)
192  {
193  for (int i = 0; i < pcacheItems.Length; i++)
194  {
195  if (pcacheItems[i].TextureIndex == pTextureIndex)
196  return pcacheItems[i];
197  }
198  return null;
199  }
200  public static WearableCacheItem SearchTextureCacheId(UUID pCacheId, WearableCacheItem[] pcacheItems)
201  {
202  for (int i = 0; i < pcacheItems.Length; i++)
203  {
204  if (pcacheItems[i].CacheId == pCacheId)
205  return pcacheItems[i];
206  }
207  return null;
208  }
209  public static WearableCacheItem SearchTextureTextureId(UUID pTextureId, WearableCacheItem[] pcacheItems)
210  {
211  for (int i = 0; i < pcacheItems.Length; i++)
212  {
213  if (pcacheItems[i].TextureID == pTextureId)
214  return pcacheItems[i];
215  }
216  return null;
217  }
218  }
219 
220 
221 }
OpenMetaverse.StructuredData.OSDArray OSDArray
static OSD ToOSD(WearableCacheItem[] pcacheItems, IImprovedAssetCache dataCache)
static readonly byte[] BAKE_INDICES
Contains the Avatar's Appearance and methods to manipulate the appearance.
OpenMetaverse.StructuredData.OSDMap OSDMap
bool Check(string id)
Check whether an asset with the specified id exists in the cache.
static WearableCacheItem[] BakedFromOSD(OSD pInput)
static WearableCacheItem SearchTextureCacheId(UUID pCacheId, WearableCacheItem[] pcacheItems)
static OSDArray BakedToOSD(WearableCacheItem[] pcacheItems)
static WearableCacheItem[] FromOSD(OSD pInput, IImprovedAssetCache dataCache)
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
static WearableCacheItem SearchTextureTextureId(UUID pTextureId, WearableCacheItem[] pcacheItems)
OpenMetaverse.StructuredData.OSD OSD
static WearableCacheItem[] GetDefaultCacheItem()
static WearableCacheItem SearchTextureIndex(uint pTextureIndex, WearableCacheItem[] pcacheItems)