IAvatarService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. foreach (AvatarAttachment attach in attachments)
  154. {
  155. Data["_ap_" + attach.AttachPoint] = attach.ItemID.ToString();
  156. }
  157. }
  158. public AvatarAppearance ToAvatarAppearance(UUID owner)
  159. {
  160. AvatarAppearance appearance = new AvatarAppearance(owner);
  161. if (Data.Count == 0)
  162. return appearance;
  163. appearance.ClearWearables();
  164. try
  165. {
  166. if (Data.ContainsKey("Serial"))
  167. appearance.Serial = Int32.Parse(Data["Serial"]);
  168. if (Data.ContainsKey("AvatarHeight"))
  169. appearance.AvatarHeight = float.Parse(Data["AvatarHeight"]);
  170. // Legacy Wearables
  171. if (Data.ContainsKey("BodyItem"))
  172. appearance.Wearables[AvatarWearable.BODY].Wear(
  173. UUID.Parse(Data["BodyItem"]),
  174. UUID.Parse(Data["BodyAsset"]));
  175. if (Data.ContainsKey("SkinItem"))
  176. appearance.Wearables[AvatarWearable.SKIN].Wear(
  177. UUID.Parse(Data["SkinItem"]),
  178. UUID.Parse(Data["SkinAsset"]));
  179. if (Data.ContainsKey("HairItem"))
  180. appearance.Wearables[AvatarWearable.HAIR].Wear(
  181. UUID.Parse(Data["HairItem"]),
  182. UUID.Parse(Data["HairAsset"]));
  183. if (Data.ContainsKey("EyesItem"))
  184. appearance.Wearables[AvatarWearable.EYES].Wear(
  185. UUID.Parse(Data["EyesItem"]),
  186. UUID.Parse(Data["EyesAsset"]));
  187. if (Data.ContainsKey("ShirtItem"))
  188. appearance.Wearables[AvatarWearable.SHIRT].Wear(
  189. UUID.Parse(Data["ShirtItem"]),
  190. UUID.Parse(Data["ShirtAsset"]));
  191. if (Data.ContainsKey("PantsItem"))
  192. appearance.Wearables[AvatarWearable.PANTS].Wear(
  193. UUID.Parse(Data["PantsItem"]),
  194. UUID.Parse(Data["PantsAsset"]));
  195. if (Data.ContainsKey("ShoesItem"))
  196. appearance.Wearables[AvatarWearable.SHOES].Wear(
  197. UUID.Parse(Data["ShoesItem"]),
  198. UUID.Parse(Data["ShoesAsset"]));
  199. if (Data.ContainsKey("SocksItem"))
  200. appearance.Wearables[AvatarWearable.SOCKS].Wear(
  201. UUID.Parse(Data["SocksItem"]),
  202. UUID.Parse(Data["SocksAsset"]));
  203. if (Data.ContainsKey("JacketItem"))
  204. appearance.Wearables[AvatarWearable.JACKET].Wear(
  205. UUID.Parse(Data["JacketItem"]),
  206. UUID.Parse(Data["JacketAsset"]));
  207. if (Data.ContainsKey("GlovesItem"))
  208. appearance.Wearables[AvatarWearable.GLOVES].Wear(
  209. UUID.Parse(Data["GlovesItem"]),
  210. UUID.Parse(Data["GlovesAsset"]));
  211. if (Data.ContainsKey("UnderShirtItem"))
  212. appearance.Wearables[AvatarWearable.UNDERSHIRT].Wear(
  213. UUID.Parse(Data["UnderShirtItem"]),
  214. UUID.Parse(Data["UnderShirtAsset"]));
  215. if (Data.ContainsKey("UnderPantsItem"))
  216. appearance.Wearables[AvatarWearable.UNDERPANTS].Wear(
  217. UUID.Parse(Data["UnderPantsItem"]),
  218. UUID.Parse(Data["UnderPantsAsset"]));
  219. if (Data.ContainsKey("SkirtItem"))
  220. appearance.Wearables[AvatarWearable.SKIRT].Wear(
  221. UUID.Parse(Data["SkirtItem"]),
  222. UUID.Parse(Data["SkirtAsset"]));
  223. if (Data.ContainsKey("VisualParams"))
  224. {
  225. string[] vps = Data["VisualParams"].Split(new char[] {','});
  226. byte[] binary = new byte[AvatarAppearance.VISUALPARAM_COUNT];
  227. for (int i = 0 ; i < vps.Length && i < binary.Length ; i++)
  228. binary[i] = (byte)Convert.ToInt32(vps[i]);
  229. appearance.VisualParams = binary;
  230. }
  231. // New style wearables
  232. foreach (KeyValuePair<string, string> _kvp in Data)
  233. {
  234. if (_kvp.Key.StartsWith("Wearable "))
  235. {
  236. string wearIndex = _kvp.Key.Substring(9);
  237. string[] wearIndices = wearIndex.Split(new char[] {':'});
  238. int index = Convert.ToInt32(wearIndices[0]);
  239. string[] ids = _kvp.Value.Split(new char[] {':'});
  240. UUID itemID = new UUID(ids[0]);
  241. UUID assetID = new UUID(ids[1]);
  242. appearance.Wearables[index].Add(itemID, assetID);
  243. }
  244. }
  245. // Attachments
  246. Dictionary<string, string> attchs = new Dictionary<string, string>();
  247. foreach (KeyValuePair<string, string> _kvp in Data)
  248. if (_kvp.Key.StartsWith("_ap_"))
  249. attchs[_kvp.Key] = _kvp.Value;
  250. foreach (KeyValuePair<string, string> _kvp in attchs)
  251. {
  252. string pointStr = _kvp.Key.Substring(4);
  253. int point = 0;
  254. if (!Int32.TryParse(pointStr, out point))
  255. continue;
  256. UUID uuid = UUID.Zero;
  257. UUID.TryParse(_kvp.Value, out uuid);
  258. appearance.SetAttachment(point,uuid,UUID.Zero);
  259. }
  260. if (appearance.Wearables[AvatarWearable.BODY].Count == 0)
  261. appearance.Wearables[AvatarWearable.BODY].Wear(
  262. AvatarWearable.DefaultWearables[
  263. AvatarWearable.BODY][0]);
  264. if (appearance.Wearables[AvatarWearable.SKIN].Count == 0)
  265. appearance.Wearables[AvatarWearable.SKIN].Wear(
  266. AvatarWearable.DefaultWearables[
  267. AvatarWearable.SKIN][0]);
  268. if (appearance.Wearables[AvatarWearable.HAIR].Count == 0)
  269. appearance.Wearables[AvatarWearable.HAIR].Wear(
  270. AvatarWearable.DefaultWearables[
  271. AvatarWearable.HAIR][0]);
  272. if (appearance.Wearables[AvatarWearable.EYES].Count == 0)
  273. appearance.Wearables[AvatarWearable.EYES].Wear(
  274. AvatarWearable.DefaultWearables[
  275. AvatarWearable.EYES][0]);
  276. }
  277. catch
  278. {
  279. // We really should report something here, returning null
  280. // will at least break the wrapper
  281. return null;
  282. }
  283. return appearance;
  284. }
  285. }
  286. }