IAvatarService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. byte[] binary = appearance.VisualParams;
  148. string[] vps = new string[binary.Length];
  149. for (int i = 0; i < binary.Length; i++)
  150. {
  151. vps[i] = binary[i].ToString();
  152. }
  153. Data["VisualParams"] = String.Join(",", vps);
  154. // Attachments
  155. List<AvatarAttachment> attachments = appearance.GetAttachments();
  156. Dictionary<int, List<string>> atts = new Dictionary<int, List<string>>();
  157. foreach (AvatarAttachment attach in attachments)
  158. {
  159. if (attach.ItemID != UUID.Zero)
  160. {
  161. if (!atts.ContainsKey(attach.AttachPoint))
  162. atts[attach.AttachPoint] = new List<string>();
  163. atts[attach.AttachPoint].Add(attach.ItemID.ToString());
  164. }
  165. }
  166. foreach (KeyValuePair<int, List<string>> kvp in atts)
  167. Data["_ap_" + kvp.Key] = string.Join(",", kvp.Value.ToArray());
  168. }
  169. public AvatarAppearance ToAvatarAppearance()
  170. {
  171. AvatarAppearance appearance = new AvatarAppearance();
  172. if (Data.Count == 0)
  173. return appearance;
  174. appearance.ClearWearables();
  175. try
  176. {
  177. if (Data.ContainsKey("Serial"))
  178. appearance.Serial = Int32.Parse(Data["Serial"]);
  179. if (Data.ContainsKey("AvatarHeight"))
  180. {
  181. float h = float.Parse(Data["AvatarHeight"]);
  182. if( h == 0f)
  183. h = 1.9f;
  184. appearance.SetSize(new Vector3(0.45f, 0.6f, h ));
  185. // appearance.AvatarHeight = float.Parse(Data["AvatarHeight"]);
  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[AvatarAppearance.VISUALPARAM_COUNT];
  244. // for (int i = 0 ; i < vps.Length && i < binary.Length ; i++)
  245. byte[] binary = new byte[vps.Length];
  246. for (int i = 0; i < vps.Length; i++)
  247. binary[i] = (byte)Convert.ToInt32(vps[i]);
  248. appearance.VisualParams = binary;
  249. }
  250. // New style wearables
  251. foreach (KeyValuePair<string, string> _kvp in Data)
  252. {
  253. if (_kvp.Key.StartsWith("Wearable "))
  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. appearance.Wearables[index].Add(itemID, assetID);
  262. }
  263. }
  264. // Attachments
  265. Dictionary<string, string> attchs = new Dictionary<string, string>();
  266. foreach (KeyValuePair<string, string> _kvp in Data)
  267. if (_kvp.Key.StartsWith("_ap_"))
  268. attchs[_kvp.Key] = _kvp.Value;
  269. foreach (KeyValuePair<string, string> _kvp in attchs)
  270. {
  271. string pointStr = _kvp.Key.Substring(4);
  272. int point = 0;
  273. if (!Int32.TryParse(pointStr, out point))
  274. continue;
  275. List<string> idList = new List<string>(_kvp.Value.Split(new char[] {','}));
  276. appearance.SetAttachment(point, UUID.Zero, UUID.Zero);
  277. foreach (string id in idList)
  278. {
  279. UUID uuid = UUID.Zero;
  280. UUID.TryParse(id, out uuid);
  281. appearance.SetAttachment(point | 0x80, uuid, UUID.Zero);
  282. }
  283. }
  284. if (appearance.Wearables[AvatarWearable.BODY].Count == 0)
  285. appearance.Wearables[AvatarWearable.BODY].Wear(
  286. AvatarWearable.DefaultWearables[
  287. AvatarWearable.BODY][0]);
  288. if (appearance.Wearables[AvatarWearable.SKIN].Count == 0)
  289. appearance.Wearables[AvatarWearable.SKIN].Wear(
  290. AvatarWearable.DefaultWearables[
  291. AvatarWearable.SKIN][0]);
  292. if (appearance.Wearables[AvatarWearable.HAIR].Count == 0)
  293. appearance.Wearables[AvatarWearable.HAIR].Wear(
  294. AvatarWearable.DefaultWearables[
  295. AvatarWearable.HAIR][0]);
  296. if (appearance.Wearables[AvatarWearable.EYES].Count == 0)
  297. appearance.Wearables[AvatarWearable.EYES].Wear(
  298. AvatarWearable.DefaultWearables[
  299. AvatarWearable.EYES][0]);
  300. }
  301. catch
  302. {
  303. // We really should report something here, returning null
  304. // will at least break the wrapper
  305. return null;
  306. }
  307. return appearance;
  308. }
  309. }
  310. }