WearableCacheItem.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. using System;
  28. using System.Collections.Generic;
  29. using OpenMetaverse;
  30. using OpenMetaverse.StructuredData;
  31. namespace OpenSim.Framework
  32. {
  33. [Serializable]
  34. public class WearableCacheItem
  35. {
  36. public uint TextureIndex { get; set; }
  37. public UUID CacheId { get; set; }
  38. public UUID TextureID { get; set; }
  39. public AssetBase TextureAsset { get; set; }
  40. public static WearableCacheItem[] GetDefaultCacheItem()
  41. {
  42. int itemmax = AvatarAppearance.TEXTURE_COUNT;
  43. WearableCacheItem[] retitems = new WearableCacheItem[itemmax];
  44. for (uint i=0;i<itemmax;i++)
  45. retitems[i] = new WearableCacheItem() {CacheId = UUID.Zero, TextureID = UUID.Zero, TextureIndex = i};
  46. return retitems;
  47. }
  48. public static WearableCacheItem[] FromOSD(OSD pInput, IAssetCache dataCache)
  49. {
  50. List<WearableCacheItem> ret = new List<WearableCacheItem>();
  51. if (pInput.Type == OSDType.Array)
  52. {
  53. OSDArray itemarray = (OSDArray) pInput;
  54. foreach (OSDMap item in itemarray)
  55. {
  56. ret.Add(new WearableCacheItem()
  57. {
  58. TextureIndex = item["textureindex"].AsUInteger(),
  59. CacheId = item["cacheid"].AsUUID(),
  60. TextureID = item["textureid"].AsUUID()
  61. });
  62. if (dataCache != null && item.ContainsKey("assetdata"))
  63. {
  64. AssetBase asset = new AssetBase(item["textureid"].AsUUID(),"BakedTexture",(sbyte)AssetType.Texture,UUID.Zero.ToString());
  65. asset.Temporary = true;
  66. asset.Data = item["assetdata"].AsBinary();
  67. dataCache.Cache(asset);
  68. }
  69. }
  70. }
  71. else if (pInput.Type == OSDType.Map)
  72. {
  73. OSDMap item = (OSDMap) pInput;
  74. ret.Add(new WearableCacheItem(){
  75. TextureIndex = item["textureindex"].AsUInteger(),
  76. CacheId = item["cacheid"].AsUUID(),
  77. TextureID = item["textureid"].AsUUID()
  78. });
  79. if (dataCache != null && item.ContainsKey("assetdata"))
  80. {
  81. string assetCreator = item["assetcreator"].AsString();
  82. string assetName = item["assetname"].AsString();
  83. AssetBase asset = new AssetBase(item["textureid"].AsUUID(), assetName, (sbyte)AssetType.Texture, assetCreator);
  84. asset.Temporary = true;
  85. asset.Data = item["assetdata"].AsBinary();
  86. dataCache.Cache(asset);
  87. }
  88. }
  89. else
  90. {
  91. return new WearableCacheItem[0];
  92. }
  93. return ret.ToArray();
  94. }
  95. public static OSD ToOSD(WearableCacheItem[] pcacheItems, IAssetCache dataCache)
  96. {
  97. OSDArray arr = new OSDArray();
  98. foreach (WearableCacheItem item in pcacheItems)
  99. {
  100. OSDMap itemmap = new OSDMap();
  101. itemmap.Add("textureindex", OSD.FromUInteger(item.TextureIndex));
  102. itemmap.Add("cacheid", OSD.FromUUID(item.CacheId));
  103. itemmap.Add("textureid", OSD.FromUUID(item.TextureID));
  104. if (dataCache != null)
  105. {
  106. if (dataCache.Check(item.TextureID.ToString()))
  107. {
  108. AssetBase assetItem;
  109. dataCache.Get(item.TextureID.ToString(), out assetItem);
  110. if (assetItem != null)
  111. {
  112. itemmap.Add("assetdata", OSD.FromBinary(assetItem.Data));
  113. itemmap.Add("assetcreator", OSD.FromString(assetItem.CreatorID));
  114. itemmap.Add("assetname", OSD.FromString(assetItem.Name));
  115. }
  116. }
  117. }
  118. arr.Add(itemmap);
  119. }
  120. return arr;
  121. }
  122. public static OSDArray BakedToOSD(WearableCacheItem[] pcacheItems)
  123. {
  124. if (pcacheItems.Length < AvatarAppearance.BAKE_INDICES[AvatarAppearance.BAKE_INDICES.Length - 1])
  125. return null;
  126. OSDArray arr = new OSDArray();
  127. for (int i = 0; i < AvatarAppearance.BAKE_INDICES.Length; i++)
  128. {
  129. int idx = AvatarAppearance.BAKE_INDICES[i];
  130. WearableCacheItem item = pcacheItems[idx];
  131. OSDMap itemmap = new OSDMap();
  132. itemmap.Add("textureindex", OSD.FromUInteger(item.TextureIndex));
  133. itemmap.Add("cacheid", OSD.FromUUID(item.CacheId));
  134. itemmap.Add("textureid", OSD.FromUUID(item.TextureID));
  135. /*
  136. if (item.TextureAsset != null)
  137. {
  138. itemmap.Add("assetdata", OSD.FromBinary(item.TextureAsset.Data));
  139. itemmap.Add("assetcreator", OSD.FromString(item.TextureAsset.CreatorID));
  140. itemmap.Add("assetname", OSD.FromString(item.TextureAsset.Name));
  141. }
  142. */
  143. arr.Add(itemmap);
  144. }
  145. return arr;
  146. }
  147. public static WearableCacheItem[] BakedFromOSD(OSD pInput)
  148. {
  149. WearableCacheItem[] pcache = WearableCacheItem.GetDefaultCacheItem();
  150. if (pInput.Type == OSDType.Array)
  151. {
  152. OSDArray itemarray = (OSDArray)pInput;
  153. foreach (OSDMap item in itemarray)
  154. {
  155. int idx = (int)item["textureindex"].AsUInteger();
  156. if (idx < 0 || idx > pcache.Length)
  157. continue;
  158. pcache[idx].CacheId = item["cacheid"].AsUUID();
  159. pcache[idx].TextureID = item["textureid"].AsUUID();
  160. /*
  161. if (item.ContainsKey("assetdata"))
  162. {
  163. AssetBase asset = new AssetBase(item["textureid"].AsUUID(), "BakedTexture", (sbyte)AssetType.Texture, UUID.Zero.ToString());
  164. asset.Temporary = true;
  165. asset.Local = true;
  166. asset.Data = item["assetdata"].AsBinary();
  167. pcache[idx].TextureAsset = asset;
  168. }
  169. else
  170. */
  171. pcache[idx].TextureAsset = null;
  172. }
  173. }
  174. return pcache;
  175. }
  176. public static WearableCacheItem SearchTextureIndex(uint pTextureIndex,WearableCacheItem[] pcacheItems)
  177. {
  178. for (int i = 0; i < pcacheItems.Length; i++)
  179. {
  180. if (pcacheItems[i].TextureIndex == pTextureIndex)
  181. return pcacheItems[i];
  182. }
  183. return null;
  184. }
  185. public static WearableCacheItem SearchTextureCacheId(UUID pCacheId, WearableCacheItem[] pcacheItems)
  186. {
  187. for (int i = 0; i < pcacheItems.Length; i++)
  188. {
  189. if (pcacheItems[i].CacheId == pCacheId)
  190. return pcacheItems[i];
  191. }
  192. return null;
  193. }
  194. public static WearableCacheItem SearchTextureTextureId(UUID pTextureId, WearableCacheItem[] pcacheItems)
  195. {
  196. for (int i = 0; i < pcacheItems.Length; i++)
  197. {
  198. if (pcacheItems[i].TextureID == pTextureId)
  199. return pcacheItems[i];
  200. }
  201. return null;
  202. }
  203. }
  204. }