SimianAvatarServiceConnector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 System.Collections.Specialized;
  30. using System.IO;
  31. using System.Net;
  32. using System.Reflection;
  33. using log4net;
  34. using Mono.Addins;
  35. using Nini.Config;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Server.Base;
  40. using OpenSim.Services.Interfaces;
  41. using OpenMetaverse;
  42. using OpenMetaverse.StructuredData;
  43. namespace OpenSim.Services.Connectors.SimianGrid
  44. {
  45. /// <summary>
  46. /// Connects avatar appearance data to the SimianGrid backend
  47. /// </summary>
  48. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
  49. public class SimianAvatarServiceConnector : IAvatarService, ISharedRegionModule
  50. {
  51. private static readonly ILog m_log =
  52. LogManager.GetLogger(
  53. MethodBase.GetCurrentMethod().DeclaringType);
  54. private static string ZeroID = UUID.Zero.ToString();
  55. private string m_serverUrl = String.Empty;
  56. #region ISharedRegionModule
  57. public Type ReplaceableInterface { get { return null; } }
  58. public void RegionLoaded(Scene scene) { }
  59. public void PostInitialise() { }
  60. public void Close() { }
  61. public SimianAvatarServiceConnector() { }
  62. public string Name { get { return "SimianAvatarServiceConnector"; } }
  63. public void AddRegion(Scene scene) { if (!String.IsNullOrEmpty(m_serverUrl)) { scene.RegisterModuleInterface<IAvatarService>(this); } }
  64. public void RemoveRegion(Scene scene) { if (!String.IsNullOrEmpty(m_serverUrl)) { scene.UnregisterModuleInterface<IAvatarService>(this); } }
  65. #endregion ISharedRegionModule
  66. public SimianAvatarServiceConnector(IConfigSource source)
  67. {
  68. Initialise(source);
  69. }
  70. public void Initialise(IConfigSource source)
  71. {
  72. if (Simian.IsSimianEnabled(source, "AvatarServices", this.Name))
  73. {
  74. IConfig gridConfig = source.Configs["AvatarService"];
  75. if (gridConfig == null)
  76. {
  77. m_log.Error("[SIMIAN AVATAR CONNECTOR]: AvatarService missing from OpenSim.ini");
  78. throw new Exception("Avatar connector init error");
  79. }
  80. string serviceUrl = gridConfig.GetString("AvatarServerURI");
  81. if (String.IsNullOrEmpty(serviceUrl))
  82. {
  83. m_log.Error("[SIMIAN AVATAR CONNECTOR]: No AvatarServerURI in section AvatarService");
  84. throw new Exception("Avatar connector init error");
  85. }
  86. if (!serviceUrl.EndsWith("/"))
  87. serviceUrl = serviceUrl + '/';
  88. m_serverUrl = serviceUrl;
  89. }
  90. }
  91. #region IAvatarService
  92. public AvatarData GetAvatar(UUID userID)
  93. {
  94. NameValueCollection requestArgs = new NameValueCollection
  95. {
  96. { "RequestMethod", "GetUser" },
  97. { "UserID", userID.ToString() }
  98. };
  99. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  100. if (response["Success"].AsBoolean())
  101. {
  102. OSDMap map = null;
  103. try { map = OSDParser.DeserializeJson(response["LLAppearance"].AsString()) as OSDMap; }
  104. catch { }
  105. if (map != null)
  106. {
  107. AvatarWearable[] wearables = new AvatarWearable[13];
  108. wearables[0] = new AvatarWearable(map["ShapeItem"].AsUUID(), map["ShapeAsset"].AsUUID());
  109. wearables[1] = new AvatarWearable(map["SkinItem"].AsUUID(), map["SkinAsset"].AsUUID());
  110. wearables[2] = new AvatarWearable(map["HairItem"].AsUUID(), map["HairAsset"].AsUUID());
  111. wearables[3] = new AvatarWearable(map["EyesItem"].AsUUID(), map["EyesAsset"].AsUUID());
  112. wearables[4] = new AvatarWearable(map["ShirtItem"].AsUUID(), map["ShirtAsset"].AsUUID());
  113. wearables[5] = new AvatarWearable(map["PantsItem"].AsUUID(), map["PantsAsset"].AsUUID());
  114. wearables[6] = new AvatarWearable(map["ShoesItem"].AsUUID(), map["ShoesAsset"].AsUUID());
  115. wearables[7] = new AvatarWearable(map["SocksItem"].AsUUID(), map["SocksAsset"].AsUUID());
  116. wearables[8] = new AvatarWearable(map["JacketItem"].AsUUID(), map["JacketAsset"].AsUUID());
  117. wearables[9] = new AvatarWearable(map["GlovesItem"].AsUUID(), map["GlovesAsset"].AsUUID());
  118. wearables[10] = new AvatarWearable(map["UndershirtItem"].AsUUID(), map["UndershirtAsset"].AsUUID());
  119. wearables[11] = new AvatarWearable(map["UnderpantsItem"].AsUUID(), map["UnderpantsAsset"].AsUUID());
  120. wearables[12] = new AvatarWearable(map["SkirtItem"].AsUUID(), map["SkirtAsset"].AsUUID());
  121. AvatarAppearance appearance = new AvatarAppearance(userID);
  122. appearance.Wearables = wearables;
  123. appearance.AvatarHeight = (float)map["Height"].AsReal();
  124. AvatarData avatar = new AvatarData(appearance);
  125. // Get attachments
  126. map = null;
  127. try { map = OSDParser.DeserializeJson(response["LLAttachments"].AsString()) as OSDMap; }
  128. catch { }
  129. if (map != null)
  130. {
  131. foreach (KeyValuePair<string, OSD> kvp in map)
  132. avatar.Data[kvp.Key] = kvp.Value.AsString();
  133. }
  134. return avatar;
  135. }
  136. else
  137. {
  138. m_log.Warn("[SIMIAN AVATAR CONNECTOR]: Failed to get user appearance for " + userID +
  139. ", LLAppearance is missing or invalid");
  140. return null;
  141. }
  142. }
  143. else
  144. {
  145. m_log.Warn("[SIMIAN AVATAR CONNECTOR]: Failed to get user appearance for " + userID + ": " +
  146. response["Message"].AsString());
  147. }
  148. return null;
  149. }
  150. public bool SetAvatar(UUID userID, AvatarData avatar)
  151. {
  152. m_log.Debug("[SIMIAN AVATAR CONNECTOR]: SetAvatar called for " + userID);
  153. if (avatar.AvatarType == 1) // LLAvatar
  154. {
  155. AvatarAppearance appearance = avatar.ToAvatarAppearance(userID);
  156. OSDMap map = new OSDMap();
  157. map["Height"] = OSD.FromReal(appearance.AvatarHeight);
  158. map["ShapeItem"] = OSD.FromUUID(appearance.BodyItem);
  159. map["ShapeAsset"] = OSD.FromUUID(appearance.BodyAsset);
  160. map["SkinItem"] = OSD.FromUUID(appearance.SkinItem);
  161. map["SkinAsset"] = OSD.FromUUID(appearance.SkinAsset);
  162. map["HairItem"] = OSD.FromUUID(appearance.HairItem);
  163. map["HairAsset"] = OSD.FromUUID(appearance.HairAsset);
  164. map["EyesItem"] = OSD.FromUUID(appearance.EyesItem);
  165. map["EyesAsset"] = OSD.FromUUID(appearance.EyesAsset);
  166. map["ShirtItem"] = OSD.FromUUID(appearance.ShirtItem);
  167. map["ShirtAsset"] = OSD.FromUUID(appearance.ShirtAsset);
  168. map["PantsItem"] = OSD.FromUUID(appearance.PantsItem);
  169. map["PantsAsset"] = OSD.FromUUID(appearance.PantsAsset);
  170. map["ShoesItem"] = OSD.FromUUID(appearance.ShoesItem);
  171. map["ShoesAsset"] = OSD.FromUUID(appearance.ShoesAsset);
  172. map["SocksItem"] = OSD.FromUUID(appearance.SocksItem);
  173. map["SocksAsset"] = OSD.FromUUID(appearance.SocksAsset);
  174. map["JacketItem"] = OSD.FromUUID(appearance.JacketItem);
  175. map["JacketAsset"] = OSD.FromUUID(appearance.JacketAsset);
  176. map["GlovesItem"] = OSD.FromUUID(appearance.GlovesItem);
  177. map["GlovesAsset"] = OSD.FromUUID(appearance.GlovesAsset);
  178. map["UndershirtItem"] = OSD.FromUUID(appearance.UnderShirtItem);
  179. map["UndershirtAsset"] = OSD.FromUUID(appearance.UnderShirtAsset);
  180. map["UnderpantsItem"] = OSD.FromUUID(appearance.UnderPantsItem);
  181. map["UnderpantsAsset"] = OSD.FromUUID(appearance.UnderPantsAsset);
  182. map["SkirtItem"] = OSD.FromUUID(appearance.SkirtItem);
  183. map["SkirtAsset"] = OSD.FromUUID(appearance.SkirtAsset);
  184. OSDMap items = new OSDMap();
  185. foreach (KeyValuePair<string, string> kvp in avatar.Data)
  186. {
  187. if (kvp.Key.StartsWith("_ap_"))
  188. items.Add(kvp.Key, OSD.FromString(kvp.Value));
  189. }
  190. NameValueCollection requestArgs = new NameValueCollection
  191. {
  192. { "RequestMethod", "AddUserData" },
  193. { "UserID", userID.ToString() },
  194. { "LLAppearance", OSDParser.SerializeJsonString(map) },
  195. { "LLAttachments", OSDParser.SerializeJsonString(items) }
  196. };
  197. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  198. bool success = response["Success"].AsBoolean();
  199. if (!success)
  200. m_log.Warn("[SIMIAN AVATAR CONNECTOR]: Failed saving appearance for " + userID + ": " + response["Message"].AsString());
  201. return success;
  202. }
  203. else
  204. {
  205. m_log.Error("[SIMIAN AVATAR CONNECTOR]: Can't save appearance for " + userID + ". Unhandled avatar type " + avatar.AvatarType);
  206. return false;
  207. }
  208. }
  209. public bool ResetAvatar(UUID userID)
  210. {
  211. m_log.Error("[SIMIAN AVATAR CONNECTOR]: ResetAvatar called for " + userID + ", implement this");
  212. return false;
  213. }
  214. public bool SetItems(UUID userID, string[] names, string[] values)
  215. {
  216. m_log.Error("[SIMIAN AVATAR CONNECTOR]: SetItems called for " + userID + " with " + names.Length + " names and " + values.Length + " values, implement this");
  217. return false;
  218. }
  219. public bool RemoveItems(UUID userID, string[] names)
  220. {
  221. m_log.Error("[SIMIAN AVATAR CONNECTOR]: RemoveItems called for " + userID + " with " + names.Length + " names, implement this");
  222. return false;
  223. }
  224. #endregion IAvatarService
  225. }
  226. }