XBakesModule.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 log4net;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Communications;
  40. using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Region.Framework.Scenes;
  42. using OpenSim.Services.Interfaces;
  43. using Mono.Addins;
  44. namespace OpenSim.Region.CoreModules.Avatar.BakedTextures
  45. {
  46. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "XBakes.Module")]
  47. public class XBakesModule : INonSharedRegionModule, IBakedTextureModule
  48. {
  49. protected Scene m_Scene;
  50. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. private UTF8Encoding enc = new UTF8Encoding();
  52. private string m_URL = String.Empty;
  53. private static XmlSerializer m_serializer = new XmlSerializer(typeof(AssetBase));
  54. public void Initialise(IConfigSource configSource)
  55. {
  56. IConfig config = configSource.Configs["XBakes"];
  57. if (config == null)
  58. return;
  59. m_URL = config.GetString("URL", String.Empty);
  60. }
  61. public void AddRegion(Scene scene)
  62. {
  63. // m_log.InfoFormat("[XBakes]: Enabled for region {0}", scene.RegionInfo.RegionName);
  64. m_Scene = scene;
  65. scene.RegisterModuleInterface<IBakedTextureModule>(this);
  66. }
  67. public void RegionLoaded(Scene scene)
  68. {
  69. }
  70. public void RemoveRegion(Scene scene)
  71. {
  72. }
  73. public void Close()
  74. {
  75. }
  76. public string Name
  77. {
  78. get { return "XBakes.Module"; }
  79. }
  80. public Type ReplaceableInterface
  81. {
  82. get { return null; }
  83. }
  84. public WearableCacheItem[] Get(UUID id)
  85. {
  86. if (m_URL == String.Empty)
  87. return null;
  88. int size = 0;
  89. RestClient rc = new RestClient(m_URL);
  90. List<WearableCacheItem> ret = new List<WearableCacheItem>();
  91. rc.AddResourcePath("bakes");
  92. rc.AddResourcePath(id.ToString());
  93. rc.RequestMethod = "GET";
  94. try
  95. {
  96. Stream s = rc.Request();
  97. XmlTextReader sr = new XmlTextReader(s);
  98. sr.ReadStartElement("BakedAppearance");
  99. while (sr.LocalName == "BakedTexture")
  100. {
  101. string sTextureIndex = sr.GetAttribute("TextureIndex");
  102. int lTextureIndex = Convert.ToInt32(sTextureIndex);
  103. string sCacheId = sr.GetAttribute("CacheId");
  104. UUID lCacheId = UUID.Zero;
  105. if (!(UUID.TryParse(sCacheId, out lCacheId)))
  106. {
  107. // ?? Nothing here
  108. }
  109. ++size;
  110. sr.ReadStartElement("BakedTexture");
  111. AssetBase a = (AssetBase)m_serializer.Deserialize(sr);
  112. ret.Add(new WearableCacheItem() { CacheId = lCacheId, TextureIndex = (uint)lTextureIndex, TextureAsset = a, TextureID = a.FullID });
  113. sr.ReadEndElement();
  114. }
  115. m_log.DebugFormat("[XBakes]: Ended reading");
  116. sr.Close();
  117. s.Close();
  118. return ret.ToArray();
  119. }
  120. catch (XmlException e)
  121. {
  122. return null;
  123. }
  124. }
  125. public void Store(UUID agentId, WearableCacheItem[] data)
  126. {
  127. if (m_URL == String.Empty)
  128. return;
  129. MemoryStream bakeStream = new MemoryStream();
  130. XmlTextWriter bakeWriter = new XmlTextWriter(bakeStream, null);
  131. bakeWriter.WriteStartElement(String.Empty, "BakedAppearance", String.Empty);
  132. for (int i = 0; i < data.Length; i++)
  133. {
  134. if (data[i] != null)
  135. {
  136. bakeWriter.WriteStartElement(String.Empty, "BakedTexture", String.Empty);
  137. bakeWriter.WriteAttributeString(String.Empty, "TextureIndex", String.Empty, data[i].TextureIndex.ToString());
  138. bakeWriter.WriteAttributeString(String.Empty, "CacheId", String.Empty, data[i].CacheId.ToString());
  139. if (data[i].TextureAsset != null)
  140. m_serializer.Serialize(bakeWriter, data[i].TextureAsset);
  141. bakeWriter.WriteEndElement();
  142. }
  143. }
  144. bakeWriter.WriteEndElement();
  145. bakeWriter.Flush();
  146. RestClient rc = new RestClient(m_URL);
  147. rc.AddResourcePath("bakes");
  148. rc.AddResourcePath(agentId.ToString());
  149. rc.RequestMethod = "POST";
  150. MemoryStream reqStream = new MemoryStream(bakeStream.ToArray());
  151. Util.FireAndForget(
  152. delegate
  153. {
  154. rc.Request(reqStream);
  155. }
  156. );
  157. }
  158. }
  159. }