WearableCacheItem.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 = 21;
  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 + 1};
  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 WearableCacheItem SearchTextureIndex(uint pTextureIndex,WearableCacheItem[] pcacheItems)
  122. {
  123. for (int i = 0; i < pcacheItems.Length; i++)
  124. {
  125. if (pcacheItems[i].TextureIndex == pTextureIndex)
  126. return pcacheItems[i];
  127. }
  128. return null;
  129. }
  130. public static WearableCacheItem SearchTextureCacheId(UUID pCacheId, WearableCacheItem[] pcacheItems)
  131. {
  132. for (int i = 0; i < pcacheItems.Length; i++)
  133. {
  134. if (pcacheItems[i].CacheId == pCacheId)
  135. return pcacheItems[i];
  136. }
  137. return null;
  138. }
  139. public static WearableCacheItem SearchTextureTextureId(UUID pTextureId, WearableCacheItem[] pcacheItems)
  140. {
  141. for (int i = 0; i < pcacheItems.Length; i++)
  142. {
  143. if (pcacheItems[i].TextureID == pTextureId)
  144. return pcacheItems[i];
  145. }
  146. return null;
  147. }
  148. }
  149. }