WearableCacheItem.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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, IImprovedAssetCache 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, IImprovedAssetCache 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 = dataCache.Get(item.TextureID.ToString());
  109. if (assetItem != null)
  110. {
  111. itemmap.Add("assetdata", OSD.FromBinary(assetItem.Data));
  112. itemmap.Add("assetcreator", OSD.FromString(assetItem.CreatorID));
  113. itemmap.Add("assetname", OSD.FromString(assetItem.Name));
  114. }
  115. }
  116. }
  117. arr.Add(itemmap);
  118. }
  119. return arr;
  120. }
  121. public static OSDArray BakedToOSD(WearableCacheItem[] pcacheItems)
  122. {
  123. if (pcacheItems.Length < AvatarAppearance.BAKE_INDICES[AvatarAppearance.BAKE_INDICES.Length - 1])
  124. return null;
  125. OSDArray arr = new OSDArray();
  126. for (int i = 0; i < AvatarAppearance.BAKE_INDICES.Length; i++)
  127. {
  128. int idx = AvatarAppearance.BAKE_INDICES[i];
  129. WearableCacheItem item = pcacheItems[idx];
  130. OSDMap itemmap = new OSDMap();
  131. itemmap.Add("textureindex", OSD.FromUInteger(item.TextureIndex));
  132. itemmap.Add("cacheid", OSD.FromUUID(item.CacheId));
  133. itemmap.Add("textureid", OSD.FromUUID(item.TextureID));
  134. /*
  135. if (item.TextureAsset != null)
  136. {
  137. itemmap.Add("assetdata", OSD.FromBinary(item.TextureAsset.Data));
  138. itemmap.Add("assetcreator", OSD.FromString(item.TextureAsset.CreatorID));
  139. itemmap.Add("assetname", OSD.FromString(item.TextureAsset.Name));
  140. }
  141. */
  142. arr.Add(itemmap);
  143. }
  144. return arr;
  145. }
  146. public static WearableCacheItem[] BakedFromOSD(OSD pInput)
  147. {
  148. WearableCacheItem[] pcache = WearableCacheItem.GetDefaultCacheItem();
  149. if (pInput.Type == OSDType.Array)
  150. {
  151. OSDArray itemarray = (OSDArray)pInput;
  152. foreach (OSDMap item in itemarray)
  153. {
  154. int idx = (int)item["textureindex"].AsUInteger();
  155. if (idx < 0 || idx > pcache.Length)
  156. continue;
  157. pcache[idx].CacheId = item["cacheid"].AsUUID();
  158. pcache[idx].TextureID = item["textureid"].AsUUID();
  159. /*
  160. if (item.ContainsKey("assetdata"))
  161. {
  162. AssetBase asset = new AssetBase(item["textureid"].AsUUID(), "BakedTexture", (sbyte)AssetType.Texture, UUID.Zero.ToString());
  163. asset.Temporary = true;
  164. asset.Local = true;
  165. asset.Data = item["assetdata"].AsBinary();
  166. pcache[idx].TextureAsset = asset;
  167. }
  168. else
  169. */
  170. pcache[idx].TextureAsset = null;
  171. }
  172. }
  173. return pcache;
  174. }
  175. public static WearableCacheItem SearchTextureIndex(uint pTextureIndex,WearableCacheItem[] pcacheItems)
  176. {
  177. for (int i = 0; i < pcacheItems.Length; i++)
  178. {
  179. if (pcacheItems[i].TextureIndex == pTextureIndex)
  180. return pcacheItems[i];
  181. }
  182. return null;
  183. }
  184. public static WearableCacheItem SearchTextureCacheId(UUID pCacheId, WearableCacheItem[] pcacheItems)
  185. {
  186. for (int i = 0; i < pcacheItems.Length; i++)
  187. {
  188. if (pcacheItems[i].CacheId == pCacheId)
  189. return pcacheItems[i];
  190. }
  191. return null;
  192. }
  193. public static WearableCacheItem SearchTextureTextureId(UUID pTextureId, WearableCacheItem[] pcacheItems)
  194. {
  195. for (int i = 0; i < pcacheItems.Length; i++)
  196. {
  197. if (pcacheItems[i].TextureID == pTextureId)
  198. return pcacheItems[i];
  199. }
  200. return null;
  201. }
  202. }
  203. }