IAvatarService.cs 15 KB

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