1
0

WearableCacheItem.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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;
  37. public UUID CacheId;
  38. public UUID TextureID;
  39. public AssetBase TextureAsset;
  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, int start, int end)
  123. {
  124. OSDArray arr = new OSDArray();
  125. if(start < 0)
  126. start = 0;
  127. if (end < 0 || end > AvatarAppearance.BAKE_INDICES.Length)
  128. end = AvatarAppearance.BAKE_INDICES.Length;
  129. if (start > end)
  130. return null;
  131. for (int i = start; i < end; i++)
  132. {
  133. int idx = AvatarAppearance.BAKE_INDICES[i];
  134. if(idx >= pcacheItems.Length)
  135. continue;
  136. WearableCacheItem item = pcacheItems[idx];
  137. OSDMap itemmap = new OSDMap();
  138. itemmap.Add("textureindex", OSD.FromUInteger(item.TextureIndex));
  139. itemmap.Add("cacheid", OSD.FromUUID(item.CacheId));
  140. itemmap.Add("textureid", OSD.FromUUID(item.TextureID));
  141. arr.Add(itemmap);
  142. }
  143. return arr;
  144. }
  145. public static WearableCacheItem[] BakedFromOSD(OSD pInput)
  146. {
  147. WearableCacheItem[] pcache = WearableCacheItem.GetDefaultCacheItem();
  148. if (pInput.Type == OSDType.Array)
  149. {
  150. OSDArray itemarray = (OSDArray)pInput;
  151. foreach (OSDMap item in itemarray)
  152. {
  153. int idx = item["textureindex"].AsInteger();
  154. if (idx < 0 || idx >= pcache.Length)
  155. continue;
  156. pcache[idx].CacheId = item["cacheid"].AsUUID();
  157. pcache[idx].TextureID = item["textureid"].AsUUID();
  158. pcache[idx].TextureAsset = null;
  159. }
  160. }
  161. return pcache;
  162. }
  163. public static WearableCacheItem SearchTextureIndex(uint pTextureIndex,WearableCacheItem[] pcacheItems)
  164. {
  165. for (int i = 0; i < pcacheItems.Length; i++)
  166. {
  167. if (pcacheItems[i].TextureIndex == pTextureIndex)
  168. return pcacheItems[i];
  169. }
  170. return null;
  171. }
  172. public static WearableCacheItem SearchTextureCacheId(UUID pCacheId, WearableCacheItem[] pcacheItems)
  173. {
  174. for (int i = 0; i < pcacheItems.Length; i++)
  175. {
  176. if (pcacheItems[i].CacheId.Equals(pCacheId))
  177. return pcacheItems[i];
  178. }
  179. return null;
  180. }
  181. public static WearableCacheItem SearchTextureTextureId(UUID pTextureId, WearableCacheItem[] pcacheItems)
  182. {
  183. for (int i = 0; i < pcacheItems.Length; i++)
  184. {
  185. if (pcacheItems[i].TextureID.Equals(pTextureId))
  186. return pcacheItems[i];
  187. }
  188. return null;
  189. }
  190. }
  191. }