IAvatarService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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;
  29. using System.Collections.Generic;
  30. using OpenSim.Framework;
  31. using OpenMetaverse;
  32. namespace OpenSim.Services.Interfaces
  33. {
  34. public interface IAvatarService
  35. {
  36. /// <summary>
  37. /// Called by the login service
  38. /// </summary>
  39. /// <param name="userID"></param>
  40. /// <returns></returns>
  41. AvatarAppearance GetAppearance(UUID userID);
  42. /// <summary>
  43. /// Called by everyone who can change the avatar data (so, regions)
  44. /// </summary>
  45. /// <param name="userID"></param>
  46. /// <param name="appearance"></param>
  47. /// <returns></returns>
  48. bool SetAppearance(UUID userID, AvatarAppearance appearance);
  49. /// <summary>
  50. /// Called by the login service
  51. /// </summary>
  52. /// <param name="userID"></param>
  53. /// <returns></returns>
  54. AvatarData GetAvatar(UUID userID);
  55. /// <summary>
  56. /// Called by everyone who can change the avatar data (so, regions)
  57. /// </summary>
  58. /// <param name="userID"></param>
  59. /// <param name="avatar"></param>
  60. /// <returns></returns>
  61. bool SetAvatar(UUID userID, AvatarData avatar);
  62. /// <summary>
  63. /// Not sure if it's needed
  64. /// </summary>
  65. /// <param name="userID"></param>
  66. /// <returns></returns>
  67. bool ResetAvatar(UUID userID);
  68. /// <summary>
  69. /// These methods raison d'etre:
  70. /// No need to send the entire avatar data (SetAvatar) for changing attachments
  71. /// </summary>
  72. /// <param name="userID"></param>
  73. /// <param name="attach"></param>
  74. /// <returns></returns>
  75. bool SetItems(UUID userID, string[] names, string[] values);
  76. bool RemoveItems(UUID userID, string[] names);
  77. }
  78. /// <summary>
  79. /// Each region/client that uses avatars will have a data structure
  80. /// of this type representing the avatars.
  81. /// </summary>
  82. public class AvatarData
  83. {
  84. // This pretty much determines which name/value pairs will be
  85. // present below. The name/value pair describe a part of
  86. // the avatar. For SL avatars, these would be "shape", "texture1",
  87. // etc. For other avatars, they might be "mesh", "skin", etc.
  88. // The value portion is a URL that is expected to resolve to an
  89. // asset of the type required by the handler for that field.
  90. // It is required that regions can access these URLs. Allowing
  91. // direct access by a viewer is not required, and, if provided,
  92. // may be read-only. A "naked" UUID can be used to refer to an
  93. // asset int he current region's asset service, which is not
  94. // portable, but allows legacy appearance to continue to
  95. // function. Closed, LL-based grids will never need URLs here.
  96. public int AvatarType;
  97. public Dictionary<string,string> Data;
  98. public AvatarData()
  99. {
  100. }
  101. public AvatarData(Dictionary<string, object> kvp)
  102. {
  103. Data = new Dictionary<string, string>();
  104. if (kvp.ContainsKey("AvatarType"))
  105. Int32.TryParse(kvp["AvatarType"].ToString(), out AvatarType);
  106. foreach (KeyValuePair<string, object> _kvp in kvp)
  107. {
  108. if (_kvp.Value != null)
  109. Data[_kvp.Key] = _kvp.Value.ToString();
  110. }
  111. }
  112. /// <summary>
  113. /// </summary>
  114. /// <returns></returns>
  115. public Dictionary<string, object> ToKeyValuePairs()
  116. {
  117. Dictionary<string, object> result = new Dictionary<string, object>();
  118. result["AvatarType"] = AvatarType.ToString();
  119. foreach (KeyValuePair<string, string> _kvp in Data)
  120. {
  121. if (_kvp.Value != null)
  122. result[_kvp.Key] = _kvp.Value;
  123. }
  124. return result;
  125. }
  126. public AvatarData(AvatarAppearance appearance)
  127. {
  128. AvatarType = 1; // SL avatars
  129. Data = new Dictionary<string, string>();
  130. Data["Serial"] = appearance.Serial.ToString();
  131. // Wearables
  132. Data["AvatarHeight"] = appearance.AvatarHeight.ToString();
  133. for (int i = 0 ; i < AvatarWearable.MAX_WEARABLES ; i++)
  134. {
  135. for (int j = 0 ; j < appearance.Wearables[i].Count ; j++)
  136. {
  137. string fieldName = String.Format("Wearable {0}:{1}", i, j);
  138. Data[fieldName] = String.Format("{0}:{1}",
  139. appearance.Wearables[i][j].ItemID.ToString(),
  140. appearance.Wearables[i][j].AssetID.ToString());
  141. }
  142. }
  143. // Visual Params
  144. string[] vps = new string[AvatarAppearance.VISUALPARAM_COUNT];
  145. byte[] binary = appearance.VisualParams;
  146. for (int i = 0 ; i < AvatarAppearance.VISUALPARAM_COUNT ; i++)
  147. {
  148. vps[i] = binary[i].ToString();
  149. }
  150. Data["VisualParams"] = String.Join(",", vps);
  151. // Attachments
  152. List<AvatarAttachment> attachments = appearance.GetAttachments();
  153. Dictionary<int, List<string>> atts = new Dictionary<int, List<string>>();
  154. foreach (AvatarAttachment attach in attachments)
  155. {
  156. if (attach.ItemID != UUID.Zero)
  157. {
  158. if (!atts.ContainsKey(attach.AttachPoint))
  159. atts[attach.AttachPoint] = new List<string>();
  160. atts[attach.AttachPoint].Add(attach.ItemID.ToString());
  161. }
  162. }
  163. foreach (KeyValuePair<int, List<string>> kvp in atts)
  164. Data["_ap_" + kvp.Key] = string.Join(",", kvp.Value.ToArray());
  165. }
  166. public AvatarAppearance ToAvatarAppearance()
  167. {
  168. AvatarAppearance appearance = new AvatarAppearance();
  169. if (Data.Count == 0)
  170. return appearance;
  171. appearance.ClearWearables();
  172. try
  173. {
  174. if (Data.ContainsKey("Serial"))
  175. appearance.Serial = Int32.Parse(Data["Serial"]);
  176. if (Data.ContainsKey("AvatarHeight"))
  177. appearance.AvatarHeight = float.Parse(Data["AvatarHeight"]);
  178. // Legacy Wearables
  179. if (Data.ContainsKey("BodyItem"))
  180. appearance.Wearables[AvatarWearable.BODY].Wear(
  181. UUID.Parse(Data["BodyItem"]),
  182. UUID.Parse(Data["BodyAsset"]));
  183. if (Data.ContainsKey("SkinItem"))
  184. appearance.Wearables[AvatarWearable.SKIN].Wear(
  185. UUID.Parse(Data["SkinItem"]),
  186. UUID.Parse(Data["SkinAsset"]));
  187. if (Data.ContainsKey("HairItem"))
  188. appearance.Wearables[AvatarWearable.HAIR].Wear(
  189. UUID.Parse(Data["HairItem"]),
  190. UUID.Parse(Data["HairAsset"]));
  191. if (Data.ContainsKey("EyesItem"))
  192. appearance.Wearables[AvatarWearable.EYES].Wear(
  193. UUID.Parse(Data["EyesItem"]),
  194. UUID.Parse(Data["EyesAsset"]));
  195. if (Data.ContainsKey("ShirtItem"))
  196. appearance.Wearables[AvatarWearable.SHIRT].Wear(
  197. UUID.Parse(Data["ShirtItem"]),
  198. UUID.Parse(Data["ShirtAsset"]));
  199. if (Data.ContainsKey("PantsItem"))
  200. appearance.Wearables[AvatarWearable.PANTS].Wear(
  201. UUID.Parse(Data["PantsItem"]),
  202. UUID.Parse(Data["PantsAsset"]));
  203. if (Data.ContainsKey("ShoesItem"))
  204. appearance.Wearables[AvatarWearable.SHOES].Wear(
  205. UUID.Parse(Data["ShoesItem"]),
  206. UUID.Parse(Data["ShoesAsset"]));
  207. if (Data.ContainsKey("SocksItem"))
  208. appearance.Wearables[AvatarWearable.SOCKS].Wear(
  209. UUID.Parse(Data["SocksItem"]),
  210. UUID.Parse(Data["SocksAsset"]));
  211. if (Data.ContainsKey("JacketItem"))
  212. appearance.Wearables[AvatarWearable.JACKET].Wear(
  213. UUID.Parse(Data["JacketItem"]),
  214. UUID.Parse(Data["JacketAsset"]));
  215. if (Data.ContainsKey("GlovesItem"))
  216. appearance.Wearables[AvatarWearable.GLOVES].Wear(
  217. UUID.Parse(Data["GlovesItem"]),
  218. UUID.Parse(Data["GlovesAsset"]));
  219. if (Data.ContainsKey("UnderShirtItem"))
  220. appearance.Wearables[AvatarWearable.UNDERSHIRT].Wear(
  221. UUID.Parse(Data["UnderShirtItem"]),
  222. UUID.Parse(Data["UnderShirtAsset"]));
  223. if (Data.ContainsKey("UnderPantsItem"))
  224. appearance.Wearables[AvatarWearable.UNDERPANTS].Wear(
  225. UUID.Parse(Data["UnderPantsItem"]),
  226. UUID.Parse(Data["UnderPantsAsset"]));
  227. if (Data.ContainsKey("SkirtItem"))
  228. appearance.Wearables[AvatarWearable.SKIRT].Wear(
  229. UUID.Parse(Data["SkirtItem"]),
  230. UUID.Parse(Data["SkirtAsset"]));
  231. if (Data.ContainsKey("VisualParams"))
  232. {
  233. string[] vps = Data["VisualParams"].Split(new char[] {','});
  234. byte[] binary = new byte[AvatarAppearance.VISUALPARAM_COUNT];
  235. for (int i = 0 ; i < vps.Length && i < binary.Length ; i++)
  236. binary[i] = (byte)Convert.ToInt32(vps[i]);
  237. appearance.VisualParams = binary;
  238. }
  239. // New style wearables
  240. foreach (KeyValuePair<string, string> _kvp in Data)
  241. {
  242. if (_kvp.Key.StartsWith("Wearable "))
  243. {
  244. string wearIndex = _kvp.Key.Substring(9);
  245. string[] wearIndices = wearIndex.Split(new char[] {':'});
  246. int index = Convert.ToInt32(wearIndices[0]);
  247. string[] ids = _kvp.Value.Split(new char[] {':'});
  248. UUID itemID = new UUID(ids[0]);
  249. UUID assetID = new UUID(ids[1]);
  250. appearance.Wearables[index].Add(itemID, assetID);
  251. }
  252. }
  253. // Attachments
  254. Dictionary<string, string> attchs = new Dictionary<string, string>();
  255. foreach (KeyValuePair<string, string> _kvp in Data)
  256. if (_kvp.Key.StartsWith("_ap_"))
  257. attchs[_kvp.Key] = _kvp.Value;
  258. foreach (KeyValuePair<string, string> _kvp in attchs)
  259. {
  260. string pointStr = _kvp.Key.Substring(4);
  261. int point = 0;
  262. if (!Int32.TryParse(pointStr, out point))
  263. continue;
  264. List<string> idList = new List<string>(_kvp.Value.Split(new char[] {','}));
  265. appearance.SetAttachment(point, UUID.Zero, UUID.Zero);
  266. foreach (string id in idList)
  267. {
  268. UUID uuid = UUID.Zero;
  269. UUID.TryParse(id, out uuid);
  270. appearance.SetAttachment(point | 0x80, uuid, UUID.Zero);
  271. }
  272. }
  273. if (appearance.Wearables[AvatarWearable.BODY].Count == 0)
  274. appearance.Wearables[AvatarWearable.BODY].Wear(
  275. AvatarWearable.DefaultWearables[
  276. AvatarWearable.BODY][0]);
  277. if (appearance.Wearables[AvatarWearable.SKIN].Count == 0)
  278. appearance.Wearables[AvatarWearable.SKIN].Wear(
  279. AvatarWearable.DefaultWearables[
  280. AvatarWearable.SKIN][0]);
  281. if (appearance.Wearables[AvatarWearable.HAIR].Count == 0)
  282. appearance.Wearables[AvatarWearable.HAIR].Wear(
  283. AvatarWearable.DefaultWearables[
  284. AvatarWearable.HAIR][0]);
  285. if (appearance.Wearables[AvatarWearable.EYES].Count == 0)
  286. appearance.Wearables[AvatarWearable.EYES].Wear(
  287. AvatarWearable.DefaultWearables[
  288. AvatarWearable.EYES][0]);
  289. }
  290. catch
  291. {
  292. // We really should report something here, returning null
  293. // will at least break the wrapper
  294. return null;
  295. }
  296. return appearance;
  297. }
  298. }
  299. }