1
0

SimianAvatarServiceConnector.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. // DEBUG ON
  31. using System.Diagnostics;
  32. // DEBUG OFF
  33. using System.Reflection;
  34. using log4net;
  35. using Mono.Addins;
  36. using Nini.Config;
  37. using OpenSim.Framework;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  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", Id = "SimianAvatarServiceConnector")]
  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. private bool m_Enabled = false;
  57. #region ISharedRegionModule
  58. public Type ReplaceableInterface { get { return null; } }
  59. public void RegionLoaded(Scene scene) { }
  60. public void PostInitialise() { }
  61. public void Close() { }
  62. public SimianAvatarServiceConnector() { }
  63. public string Name { get { return "SimianAvatarServiceConnector"; } }
  64. public void AddRegion(Scene scene) { if (m_Enabled) { scene.RegisterModuleInterface<IAvatarService>(this); } }
  65. public void RemoveRegion(Scene scene) { if (m_Enabled) { scene.UnregisterModuleInterface<IAvatarService>(this); } }
  66. #endregion ISharedRegionModule
  67. public SimianAvatarServiceConnector(IConfigSource source)
  68. {
  69. CommonInit(source);
  70. }
  71. public void Initialise(IConfigSource source)
  72. {
  73. IConfig moduleConfig = source.Configs["Modules"];
  74. if (moduleConfig != null)
  75. {
  76. string name = moduleConfig.GetString("AvatarServices", "");
  77. if (name == Name)
  78. CommonInit(source);
  79. }
  80. }
  81. private void CommonInit(IConfigSource source)
  82. {
  83. IConfig gridConfig = source.Configs["AvatarService"];
  84. if (gridConfig != null)
  85. {
  86. string serviceUrl = gridConfig.GetString("AvatarServerURI");
  87. if (!String.IsNullOrEmpty(serviceUrl))
  88. {
  89. if (!serviceUrl.EndsWith("/") && !serviceUrl.EndsWith("="))
  90. serviceUrl = serviceUrl + '/';
  91. m_serverUrl = serviceUrl;
  92. m_Enabled = true;
  93. }
  94. }
  95. if (String.IsNullOrEmpty(m_serverUrl))
  96. m_log.Info("[SIMIAN AVATAR CONNECTOR]: No AvatarServerURI specified, disabling connector");
  97. }
  98. #region IAvatarService
  99. // <summary>
  100. // Retrieves the LLPackedAppearance field from user data and unpacks
  101. // it into an AvatarAppearance structure
  102. // </summary>
  103. // <param name="userID"></param>
  104. public AvatarAppearance GetAppearance(UUID userID)
  105. {
  106. NameValueCollection requestArgs = new NameValueCollection
  107. {
  108. { "RequestMethod", "GetUser" },
  109. { "UserID", userID.ToString() }
  110. };
  111. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  112. if (response["Success"].AsBoolean())
  113. {
  114. OSDMap map = null;
  115. try { map = OSDParser.DeserializeJson(response["LLPackedAppearance"].AsString()) as OSDMap; }
  116. catch { }
  117. if (map != null)
  118. {
  119. AvatarAppearance appearance = new AvatarAppearance(map);
  120. // DEBUG ON
  121. m_log.WarnFormat("[SIMIAN AVATAR CONNECTOR] retrieved appearance for {0}:\n{1}",userID,appearance.ToString());
  122. // DEBUG OFF
  123. return appearance;
  124. }
  125. m_log.WarnFormat("[SIMIAN AVATAR CONNECTOR]: Failed to decode appearance for {0}",userID);
  126. return null;
  127. }
  128. m_log.WarnFormat("[SIMIAN AVATAR CONNECTOR]: Failed to get appearance for {0}: {1}",
  129. userID,response["Message"].AsString());
  130. return null;
  131. }
  132. // <summary>
  133. // </summary>
  134. // <param name=""></param>
  135. public bool SetAppearance(UUID userID, AvatarAppearance appearance)
  136. {
  137. EntityTransferContext ctx = new EntityTransferContext();
  138. OSDMap map = appearance.Pack(ctx);
  139. if (map == null)
  140. {
  141. m_log.WarnFormat("[SIMIAN AVATAR CONNECTOR]: Failed to encode appearance for {0}",userID);
  142. return false;
  143. }
  144. // m_log.DebugFormat("[SIMIAN AVATAR CONNECTOR] save appearance for {0}",userID);
  145. NameValueCollection requestArgs = new NameValueCollection
  146. {
  147. { "RequestMethod", "AddUserData" },
  148. { "UserID", userID.ToString() },
  149. { "LLPackedAppearance", OSDParser.SerializeJsonString(map) }
  150. };
  151. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  152. bool success = response["Success"].AsBoolean();
  153. if (! success)
  154. m_log.WarnFormat("[SIMIAN AVATAR CONNECTOR]: Failed to save appearance for {0}: {1}",
  155. userID,response["Message"].AsString());
  156. return success;
  157. }
  158. // <summary>
  159. // </summary>
  160. // <param name=""></param>
  161. public AvatarData GetAvatar(UUID userID)
  162. {
  163. NameValueCollection requestArgs = new NameValueCollection
  164. {
  165. { "RequestMethod", "GetUser" },
  166. { "UserID", userID.ToString() }
  167. };
  168. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  169. if (response["Success"].AsBoolean())
  170. {
  171. OSDMap map = null;
  172. try { map = OSDParser.DeserializeJson(response["LLAppearance"].AsString()) as OSDMap; }
  173. catch { }
  174. if (map != null)
  175. {
  176. AvatarWearable[] wearables = new AvatarWearable[13];
  177. wearables[0] = new AvatarWearable(map["ShapeItem"].AsUUID(), map["ShapeAsset"].AsUUID());
  178. wearables[1] = new AvatarWearable(map["SkinItem"].AsUUID(), map["SkinAsset"].AsUUID());
  179. wearables[2] = new AvatarWearable(map["HairItem"].AsUUID(), map["HairAsset"].AsUUID());
  180. wearables[3] = new AvatarWearable(map["EyesItem"].AsUUID(), map["EyesAsset"].AsUUID());
  181. wearables[4] = new AvatarWearable(map["ShirtItem"].AsUUID(), map["ShirtAsset"].AsUUID());
  182. wearables[5] = new AvatarWearable(map["PantsItem"].AsUUID(), map["PantsAsset"].AsUUID());
  183. wearables[6] = new AvatarWearable(map["ShoesItem"].AsUUID(), map["ShoesAsset"].AsUUID());
  184. wearables[7] = new AvatarWearable(map["SocksItem"].AsUUID(), map["SocksAsset"].AsUUID());
  185. wearables[8] = new AvatarWearable(map["JacketItem"].AsUUID(), map["JacketAsset"].AsUUID());
  186. wearables[9] = new AvatarWearable(map["GlovesItem"].AsUUID(), map["GlovesAsset"].AsUUID());
  187. wearables[10] = new AvatarWearable(map["UndershirtItem"].AsUUID(), map["UndershirtAsset"].AsUUID());
  188. wearables[11] = new AvatarWearable(map["UnderpantsItem"].AsUUID(), map["UnderpantsAsset"].AsUUID());
  189. wearables[12] = new AvatarWearable(map["SkirtItem"].AsUUID(), map["SkirtAsset"].AsUUID());
  190. AvatarAppearance appearance = new AvatarAppearance();
  191. appearance.Wearables = wearables;
  192. appearance.AvatarHeight = (float)map["Height"].AsReal();
  193. AvatarData avatar = new AvatarData(appearance);
  194. // Get attachments
  195. map = null;
  196. try { map = OSDParser.DeserializeJson(response["LLAttachments"].AsString()) as OSDMap; }
  197. catch { }
  198. if (map != null)
  199. {
  200. foreach (KeyValuePair<string, OSD> kvp in map)
  201. avatar.Data[kvp.Key] = kvp.Value.AsString();
  202. }
  203. return avatar;
  204. }
  205. else
  206. {
  207. m_log.Warn("[SIMIAN AVATAR CONNECTOR]: Failed to get user appearance for " + userID +
  208. ", LLAppearance is missing or invalid");
  209. return null;
  210. }
  211. }
  212. else
  213. {
  214. m_log.Warn("[SIMIAN AVATAR CONNECTOR]: Failed to get user appearance for " + userID + ": " +
  215. response["Message"].AsString());
  216. }
  217. return null;
  218. }
  219. // <summary>
  220. // </summary>
  221. // <param name=""></param>
  222. public bool SetAvatar(UUID userID, AvatarData avatar)
  223. {
  224. m_log.Debug("[SIMIAN AVATAR CONNECTOR]: SetAvatar called for " + userID);
  225. if (avatar.AvatarType == 1) // LLAvatar
  226. {
  227. AvatarAppearance appearance = avatar.ToAvatarAppearance();
  228. OSDMap map = new OSDMap();
  229. map["Height"] = OSD.FromReal(appearance.AvatarHeight);
  230. map["BodyItem"] = appearance.Wearables[AvatarWearable.BODY][0].ItemID.ToString();
  231. map["EyesItem"] = appearance.Wearables[AvatarWearable.EYES][0].ItemID.ToString();
  232. map["GlovesItem"] = appearance.Wearables[AvatarWearable.GLOVES][0].ItemID.ToString();
  233. map["HairItem"] = appearance.Wearables[AvatarWearable.HAIR][0].ItemID.ToString();
  234. map["JacketItem"] = appearance.Wearables[AvatarWearable.JACKET][0].ItemID.ToString();
  235. map["PantsItem"] = appearance.Wearables[AvatarWearable.PANTS][0].ItemID.ToString();
  236. map["ShirtItem"] = appearance.Wearables[AvatarWearable.SHIRT][0].ItemID.ToString();
  237. map["ShoesItem"] = appearance.Wearables[AvatarWearable.SHOES][0].ItemID.ToString();
  238. map["SkinItem"] = appearance.Wearables[AvatarWearable.SKIN][0].ItemID.ToString();
  239. map["SkirtItem"] = appearance.Wearables[AvatarWearable.SKIRT][0].ItemID.ToString();
  240. map["SocksItem"] = appearance.Wearables[AvatarWearable.SOCKS][0].ItemID.ToString();
  241. map["UnderPantsItem"] = appearance.Wearables[AvatarWearable.UNDERPANTS][0].ItemID.ToString();
  242. map["UnderShirtItem"] = appearance.Wearables[AvatarWearable.UNDERSHIRT][0].ItemID.ToString();
  243. map["BodyAsset"] = appearance.Wearables[AvatarWearable.BODY][0].AssetID.ToString();
  244. map["EyesAsset"] = appearance.Wearables[AvatarWearable.EYES][0].AssetID.ToString();
  245. map["GlovesAsset"] = appearance.Wearables[AvatarWearable.GLOVES][0].AssetID.ToString();
  246. map["HairAsset"] = appearance.Wearables[AvatarWearable.HAIR][0].AssetID.ToString();
  247. map["JacketAsset"] = appearance.Wearables[AvatarWearable.JACKET][0].AssetID.ToString();
  248. map["PantsAsset"] = appearance.Wearables[AvatarWearable.PANTS][0].AssetID.ToString();
  249. map["ShirtAsset"] = appearance.Wearables[AvatarWearable.SHIRT][0].AssetID.ToString();
  250. map["ShoesAsset"] = appearance.Wearables[AvatarWearable.SHOES][0].AssetID.ToString();
  251. map["SkinAsset"] = appearance.Wearables[AvatarWearable.SKIN][0].AssetID.ToString();
  252. map["SkirtAsset"] = appearance.Wearables[AvatarWearable.SKIRT][0].AssetID.ToString();
  253. map["SocksAsset"] = appearance.Wearables[AvatarWearable.SOCKS][0].AssetID.ToString();
  254. map["UnderPantsAsset"] = appearance.Wearables[AvatarWearable.UNDERPANTS][0].AssetID.ToString();
  255. map["UnderShirtAsset"] = appearance.Wearables[AvatarWearable.UNDERSHIRT][0].AssetID.ToString();
  256. OSDMap items = new OSDMap();
  257. foreach (KeyValuePair<string, string> kvp in avatar.Data)
  258. {
  259. if (kvp.Key.StartsWith("_ap_"))
  260. items.Add(kvp.Key, OSD.FromString(kvp.Value));
  261. }
  262. NameValueCollection requestArgs = new NameValueCollection
  263. {
  264. { "RequestMethod", "AddUserData" },
  265. { "UserID", userID.ToString() },
  266. { "LLAppearance", OSDParser.SerializeJsonString(map) },
  267. { "LLAttachments", OSDParser.SerializeJsonString(items) }
  268. };
  269. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  270. bool success = response["Success"].AsBoolean();
  271. if (!success)
  272. m_log.Warn("[SIMIAN AVATAR CONNECTOR]: Failed saving appearance for " + userID + ": " + response["Message"].AsString());
  273. return success;
  274. }
  275. else
  276. {
  277. m_log.Error("[SIMIAN AVATAR CONNECTOR]: Can't save appearance for " + userID + ". Unhandled avatar type " + avatar.AvatarType);
  278. return false;
  279. }
  280. }
  281. public bool ResetAvatar(UUID userID)
  282. {
  283. m_log.Error("[SIMIAN AVATAR CONNECTOR]: ResetAvatar called for " + userID + ", implement this");
  284. return false;
  285. }
  286. public bool SetItems(UUID userID, string[] names, string[] values)
  287. {
  288. m_log.Error("[SIMIAN AVATAR CONNECTOR]: SetItems called for " + userID + " with " + names.Length + " names and " + values.Length + " values, implement this");
  289. return false;
  290. }
  291. public bool RemoveItems(UUID userID, string[] names)
  292. {
  293. m_log.Error("[SIMIAN AVATAR CONNECTOR]: RemoveItems called for " + userID + " with " + names.Length + " names, implement this");
  294. return false;
  295. }
  296. #endregion IAvatarService
  297. }
  298. }