XBakesModule.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 OpenMetaverse;
  28. using Nini.Config;
  29. using System;
  30. using System.IO;
  31. using System.Text;
  32. using System.Xml;
  33. using System.Xml.Serialization;
  34. using System.Collections;
  35. using System.Collections.Generic;
  36. using System.Reflection;
  37. using System.Threading.Tasks;
  38. using log4net;
  39. using OpenSim.Framework;
  40. using OpenSim.Framework.ServiceAuth;
  41. using OpenSim.Region.Framework.Interfaces;
  42. using OpenSim.Region.Framework.Scenes;
  43. using OpenSim.Services.Interfaces;
  44. using Mono.Addins;
  45. namespace OpenSim.Region.CoreModules.Avatar.BakedTextures
  46. {
  47. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "XBakes.Module")]
  48. public class XBakesModule : INonSharedRegionModule, IBakedTextureModule
  49. {
  50. protected Scene m_Scene;
  51. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. private UTF8Encoding enc = new UTF8Encoding();
  53. private string m_URL = String.Empty;
  54. private static XmlSerializer m_serializer = new XmlSerializer(typeof(AssetBase));
  55. private static bool m_enabled = false;
  56. private static IServiceAuth m_Auth;
  57. public void Initialise(IConfigSource configSource)
  58. {
  59. IConfig config = configSource.Configs["XBakes"];
  60. if (config == null)
  61. return;
  62. m_URL = config.GetString("URL", String.Empty);
  63. if (m_URL == String.Empty)
  64. return;
  65. m_enabled = true;
  66. m_Auth = ServiceAuth.Create(configSource, "XBakes");
  67. }
  68. public void AddRegion(Scene scene)
  69. {
  70. if (!m_enabled)
  71. return;
  72. // m_log.InfoFormat("[XBakes]: Enabled for region {0}", scene.RegionInfo.RegionName);
  73. m_Scene = scene;
  74. scene.RegisterModuleInterface<IBakedTextureModule>(this);
  75. }
  76. public void RegionLoaded(Scene scene)
  77. {
  78. }
  79. public void RemoveRegion(Scene scene)
  80. {
  81. }
  82. public void Close()
  83. {
  84. }
  85. public string Name
  86. {
  87. get { return "XBakes.Module"; }
  88. }
  89. public Type ReplaceableInterface
  90. {
  91. get { return null; }
  92. }
  93. public WearableCacheItem[] Get(UUID id)
  94. {
  95. if (m_URL == String.Empty)
  96. return null;
  97. using (RestClient rc = new RestClient(m_URL))
  98. {
  99. List<WearableCacheItem> ret = new List<WearableCacheItem>();
  100. rc.AddResourcePath("bakes/" + id.ToString());
  101. rc.RequestMethod = "GET";
  102. try
  103. {
  104. using(MemoryStream s = rc.Request(m_Auth))
  105. {
  106. using(XmlTextReader sr = new XmlTextReader(s))
  107. {
  108. sr.ReadStartElement("BakedAppearance");
  109. while (sr.LocalName == "BakedTexture")
  110. {
  111. string sTextureIndex = sr.GetAttribute("TextureIndex");
  112. int lTextureIndex = Convert.ToInt32(sTextureIndex);
  113. string sCacheId = sr.GetAttribute("CacheId");
  114. UUID.TryParse(sCacheId, out UUID lCacheId);
  115. sr.ReadStartElement("BakedTexture");
  116. if (sr.Name == "AssetBase")
  117. {
  118. AssetBase a = (AssetBase)m_serializer.Deserialize(sr);
  119. ret.Add(new WearableCacheItem()
  120. {
  121. CacheId = lCacheId,
  122. TextureIndex = (uint)lTextureIndex,
  123. TextureAsset = a,
  124. TextureID = a.FullID
  125. });
  126. sr.ReadEndElement();
  127. }
  128. }
  129. while (sr.LocalName == "BESetA")
  130. {
  131. string sTextureIndex = sr.GetAttribute("TextureIndex");
  132. int lTextureIndex = Convert.ToInt32(sTextureIndex);
  133. string sCacheId = sr.GetAttribute("CacheId");
  134. UUID.TryParse(sCacheId, out UUID lCacheId);
  135. sr.ReadStartElement("BESetA");
  136. if (sr.Name == "AssetBase")
  137. {
  138. AssetBase a = (AssetBase)m_serializer.Deserialize(sr);
  139. ret.Add(new WearableCacheItem()
  140. {
  141. CacheId = lCacheId,
  142. TextureIndex = (uint)lTextureIndex,
  143. TextureAsset = a,
  144. TextureID = a.FullID
  145. });
  146. sr.ReadEndElement();
  147. }
  148. }
  149. m_log.DebugFormat("[XBakes]: read {0} textures for user {1}",ret.Count,id);
  150. }
  151. return ret.ToArray();
  152. }
  153. }
  154. catch (XmlException)
  155. {
  156. return null;
  157. }
  158. }
  159. }
  160. public void Store(UUID agentId)
  161. {
  162. }
  163. public void UpdateMeshAvatar(UUID agentId)
  164. {
  165. }
  166. public async Task Store(UUID agentId, WearableCacheItem[] data)
  167. {
  168. if (m_URL == String.Empty)
  169. return;
  170. int numberWears = 0;
  171. byte[] uploadData;
  172. using (MemoryStream bakeStream = new MemoryStream())
  173. using (XmlTextWriter bakeWriter = new XmlTextWriter(bakeStream, null))
  174. {
  175. bakeWriter.WriteStartElement(String.Empty, "BakedAppearance", String.Empty);
  176. List<int> extended = new List<int>();
  177. for (int i = 0; i < data.Length; i++)
  178. {
  179. if (data[i] != null && data[i].TextureAsset != null)
  180. {
  181. if(data[i].TextureIndex > 26)
  182. {
  183. extended.Add(i);
  184. continue;
  185. }
  186. bakeWriter.WriteStartElement(String.Empty, "BakedTexture", String.Empty);
  187. bakeWriter.WriteAttributeString(String.Empty, "TextureIndex", String.Empty, data[i].TextureIndex.ToString());
  188. bakeWriter.WriteAttributeString(String.Empty, "CacheId", String.Empty, data[i].CacheId.ToString());
  189. // if (data[i].TextureAsset != null)
  190. m_serializer.Serialize(bakeWriter, data[i].TextureAsset);
  191. bakeWriter.WriteEndElement();
  192. numberWears++;
  193. }
  194. }
  195. if(extended.Count > 0)
  196. {
  197. foreach(int i in extended)
  198. {
  199. bakeWriter.WriteStartElement(String.Empty, "BESetA", String.Empty);
  200. bakeWriter.WriteAttributeString(String.Empty, "TextureIndex", String.Empty, data[i].TextureIndex.ToString());
  201. bakeWriter.WriteAttributeString(String.Empty, "CacheId", String.Empty, data[i].CacheId.ToString());
  202. m_serializer.Serialize(bakeWriter, data[i].TextureAsset);
  203. bakeWriter.WriteEndElement();
  204. numberWears++;
  205. }
  206. }
  207. bakeWriter.WriteEndElement();
  208. bakeWriter.Flush();
  209. uploadData = bakeStream.ToArray();
  210. }
  211. //Util.FireAndForget(
  212. // delegate
  213. // {
  214. using(RestClient rc = new RestClient(m_URL))
  215. {
  216. rc.AddResourcePath("bakes/" + agentId.ToString());
  217. await rc.AsyncPOSTRequest(uploadData, m_Auth).ConfigureAwait(false);
  218. m_log.DebugFormat("[XBakes]: stored {0} textures for user {1}", numberWears, agentId);
  219. }
  220. uploadData = null;
  221. // }, null, "XBakesModule.Store"
  222. //);
  223. }
  224. }
  225. }