IAvatarService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. // TODO: With COF, is this even needed?
  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. // Visual Params
  145. //string[] vps = new string[AvatarAppearance.VISUALPARAM_COUNT];
  146. //byte[] binary = appearance.VisualParams;
  147. // for (int i = 0 ; i < AvatarAppearance.VISUALPARAM_COUNT ; i++)
  148. byte[] binary = appearance.VisualParams;
  149. string[] vps = new string[binary.Length];
  150. for (int i = 0; i < binary.Length; i++)
  151. {
  152. vps[i] = binary[i].ToString();
  153. }
  154. Data["VisualParams"] = String.Join(",", vps);
  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. // appearance.AvatarHeight = float.Parse(Data["AvatarHeight"]);
  187. }
  188. // Legacy Wearables
  189. if (Data.ContainsKey("BodyItem"))
  190. appearance.Wearables[AvatarWearable.BODY].Wear(
  191. UUID.Parse(Data["BodyItem"]),
  192. UUID.Parse(Data["BodyAsset"]));
  193. if (Data.ContainsKey("SkinItem"))
  194. appearance.Wearables[AvatarWearable.SKIN].Wear(
  195. UUID.Parse(Data["SkinItem"]),
  196. UUID.Parse(Data["SkinAsset"]));
  197. if (Data.ContainsKey("HairItem"))
  198. appearance.Wearables[AvatarWearable.HAIR].Wear(
  199. UUID.Parse(Data["HairItem"]),
  200. UUID.Parse(Data["HairAsset"]));
  201. if (Data.ContainsKey("EyesItem"))
  202. appearance.Wearables[AvatarWearable.EYES].Wear(
  203. UUID.Parse(Data["EyesItem"]),
  204. UUID.Parse(Data["EyesAsset"]));
  205. if (Data.ContainsKey("ShirtItem"))
  206. appearance.Wearables[AvatarWearable.SHIRT].Wear(
  207. UUID.Parse(Data["ShirtItem"]),
  208. UUID.Parse(Data["ShirtAsset"]));
  209. if (Data.ContainsKey("PantsItem"))
  210. appearance.Wearables[AvatarWearable.PANTS].Wear(
  211. UUID.Parse(Data["PantsItem"]),
  212. UUID.Parse(Data["PantsAsset"]));
  213. if (Data.ContainsKey("ShoesItem"))
  214. appearance.Wearables[AvatarWearable.SHOES].Wear(
  215. UUID.Parse(Data["ShoesItem"]),
  216. UUID.Parse(Data["ShoesAsset"]));
  217. if (Data.ContainsKey("SocksItem"))
  218. appearance.Wearables[AvatarWearable.SOCKS].Wear(
  219. UUID.Parse(Data["SocksItem"]),
  220. UUID.Parse(Data["SocksAsset"]));
  221. if (Data.ContainsKey("JacketItem"))
  222. appearance.Wearables[AvatarWearable.JACKET].Wear(
  223. UUID.Parse(Data["JacketItem"]),
  224. UUID.Parse(Data["JacketAsset"]));
  225. if (Data.ContainsKey("GlovesItem"))
  226. appearance.Wearables[AvatarWearable.GLOVES].Wear(
  227. UUID.Parse(Data["GlovesItem"]),
  228. UUID.Parse(Data["GlovesAsset"]));
  229. if (Data.ContainsKey("UnderShirtItem"))
  230. appearance.Wearables[AvatarWearable.UNDERSHIRT].Wear(
  231. UUID.Parse(Data["UnderShirtItem"]),
  232. UUID.Parse(Data["UnderShirtAsset"]));
  233. if (Data.ContainsKey("UnderPantsItem"))
  234. appearance.Wearables[AvatarWearable.UNDERPANTS].Wear(
  235. UUID.Parse(Data["UnderPantsItem"]),
  236. UUID.Parse(Data["UnderPantsAsset"]));
  237. if (Data.ContainsKey("SkirtItem"))
  238. appearance.Wearables[AvatarWearable.SKIRT].Wear(
  239. UUID.Parse(Data["SkirtItem"]),
  240. UUID.Parse(Data["SkirtAsset"]));
  241. if (Data.ContainsKey("VisualParams"))
  242. {
  243. string[] vps = Data["VisualParams"].Split(new char[] {','});
  244. //byte[] binary = new byte[AvatarAppearance.VISUALPARAM_COUNT];
  245. //for (int i = 0 ; i < vps.Length && i < binary.Length ; i++)
  246. byte[] binary = new byte[vps.Length];
  247. for (int i = 0; i < vps.Length; i++)
  248. binary[i] = (byte)Convert.ToInt32(vps[i]);
  249. appearance.VisualParams = binary;
  250. }
  251. // New style wearables
  252. foreach (KeyValuePair<string, string> _kvp in Data)
  253. {
  254. if (_kvp.Key.StartsWith("Wearable "))
  255. {
  256. string wearIndex = _kvp.Key.Substring(9);
  257. string[] wearIndices = wearIndex.Split(new char[] {':'});
  258. int index = Convert.ToInt32(wearIndices[0]);
  259. string[] ids = _kvp.Value.Split(new char[] {':'});
  260. UUID itemID = new UUID(ids[0]);
  261. UUID assetID = new UUID(ids[1]);
  262. appearance.Wearables[index].Add(itemID, assetID);
  263. }
  264. }
  265. // Attachments
  266. Dictionary<string, string> attchs = new Dictionary<string, string>();
  267. foreach (KeyValuePair<string, string> _kvp in Data)
  268. if (_kvp.Key.StartsWith("_ap_"))
  269. attchs[_kvp.Key] = _kvp.Value;
  270. foreach (KeyValuePair<string, string> _kvp in attchs)
  271. {
  272. string pointStr = _kvp.Key.Substring(4);
  273. int point = 0;
  274. if (!Int32.TryParse(pointStr, out point))
  275. continue;
  276. List<string> idList = new List<string>(_kvp.Value.Split(new char[] {','}));
  277. appearance.SetAttachment(point, UUID.Zero, UUID.Zero);
  278. foreach (string id in idList)
  279. {
  280. UUID uuid = UUID.Zero;
  281. UUID.TryParse(id, out uuid);
  282. appearance.SetAttachment(point | 0x80, uuid, UUID.Zero);
  283. }
  284. }
  285. if (appearance.Wearables[AvatarWearable.BODY].Count == 0)
  286. appearance.Wearables[AvatarWearable.BODY].Wear(
  287. AvatarWearable.DefaultWearables[
  288. AvatarWearable.BODY][0]);
  289. if (appearance.Wearables[AvatarWearable.SKIN].Count == 0)
  290. appearance.Wearables[AvatarWearable.SKIN].Wear(
  291. AvatarWearable.DefaultWearables[
  292. AvatarWearable.SKIN][0]);
  293. if (appearance.Wearables[AvatarWearable.HAIR].Count == 0)
  294. appearance.Wearables[AvatarWearable.HAIR].Wear(
  295. AvatarWearable.DefaultWearables[
  296. AvatarWearable.HAIR][0]);
  297. if (appearance.Wearables[AvatarWearable.EYES].Count == 0)
  298. appearance.Wearables[AvatarWearable.EYES].Wear(
  299. AvatarWearable.DefaultWearables[
  300. AvatarWearable.EYES][0]);
  301. }
  302. catch
  303. {
  304. // We really should report something here, returning null
  305. // will at least break the wrapper
  306. return null;
  307. }
  308. return appearance;
  309. }
  310. }
  311. }