AvatarService.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.Generic;
  29. using System.Net;
  30. using System.Reflection;
  31. using Nini.Config;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Data;
  36. using OpenSim.Services.Interfaces;
  37. using OpenMetaverse;
  38. namespace OpenSim.Services.AvatarService
  39. {
  40. public class AvatarService : AvatarServiceBase, IAvatarService
  41. {
  42. private static readonly ILog m_log =
  43. LogManager.GetLogger(
  44. MethodBase.GetCurrentMethod().DeclaringType);
  45. public AvatarService(IConfigSource config)
  46. : base(config)
  47. {
  48. m_log.Debug("[AVATAR SERVICE]: Starting avatar service");
  49. }
  50. public AvatarAppearance GetAppearance(UUID principalID)
  51. {
  52. AvatarData avatar = GetAvatar(principalID);
  53. return avatar.ToAvatarAppearance();
  54. }
  55. public bool SetAppearance(UUID principalID, AvatarAppearance appearance)
  56. {
  57. AvatarData avatar = new AvatarData(appearance);
  58. return SetAvatar(principalID,avatar);
  59. }
  60. public AvatarData GetAvatar(UUID principalID)
  61. {
  62. AvatarBaseData[] av = m_Database.Get("PrincipalID", principalID.ToString());
  63. AvatarData ret = new AvatarData();
  64. ret.Data = new Dictionary<string,string>();
  65. if (av.Length == 0)
  66. {
  67. ret.AvatarType = 1; // SL avatar
  68. return ret;
  69. }
  70. foreach (AvatarBaseData b in av)
  71. {
  72. if (b.Data["Name"] == "AvatarType")
  73. ret.AvatarType = Convert.ToInt32(b.Data["Value"]);
  74. else
  75. ret.Data[b.Data["Name"]] = b.Data["Value"];
  76. }
  77. return ret;
  78. }
  79. public bool SetAvatar(UUID principalID, AvatarData avatar)
  80. {
  81. int count = 0;
  82. foreach (KeyValuePair<string, string> kvp in avatar.Data)
  83. if (kvp.Key.StartsWith("_"))
  84. count++;
  85. // m_log.DebugFormat("[AVATAR SERVICE]: SetAvatar for {0}, attachs={1}", principalID, count);
  86. m_Database.Delete("PrincipalID", principalID.ToString());
  87. AvatarBaseData av = new AvatarBaseData();
  88. av.Data = new Dictionary<string,string>();
  89. av.PrincipalID = principalID;
  90. av.Data["Name"] = "AvatarType";
  91. av.Data["Value"] = avatar.AvatarType.ToString();
  92. if (!m_Database.Store(av))
  93. return false;
  94. foreach (KeyValuePair<string,string> kvp in avatar.Data)
  95. {
  96. av.Data["Name"] = kvp.Key;
  97. // justincc 20110730. Yes, this is a hack to get around the fact that a bug in OpenSim is causing
  98. // various simulators on osgrid to inject bad values. Since these simulators might be around for a
  99. // long time, we are going to manually police the value.
  100. //
  101. // It should be possible to remove this in half a year if we don't want to police values server side.
  102. if (kvp.Key == "AvatarHeight")
  103. {
  104. float height;
  105. if (!float.TryParse(kvp.Value, out height) || height < 0 || height > 10)
  106. {
  107. string rawHeight = kvp.Value.Replace(",", ".");
  108. if (!float.TryParse(rawHeight, out height) || height < 0 || height > 10)
  109. height = 1.771488f;
  110. m_log.DebugFormat(
  111. "[AVATAR SERVICE]: Rectifying height of avatar {0} from {1} to {2}",
  112. principalID, kvp.Value, height);
  113. }
  114. av.Data["Value"] = height.ToString();
  115. }
  116. else
  117. {
  118. av.Data["Value"] = kvp.Value;
  119. }
  120. if (!m_Database.Store(av))
  121. {
  122. m_Database.Delete("PrincipalID", principalID.ToString());
  123. return false;
  124. }
  125. }
  126. return true;
  127. }
  128. public bool ResetAvatar(UUID principalID)
  129. {
  130. return m_Database.Delete("PrincipalID", principalID.ToString());
  131. }
  132. public bool SetItems(UUID principalID, string[] names, string[] values)
  133. {
  134. AvatarBaseData av = new AvatarBaseData();
  135. av.Data = new Dictionary<string,string>();
  136. av.PrincipalID = principalID;
  137. if (names.Length != values.Length)
  138. return false;
  139. for (int i = 0 ; i < names.Length ; i++)
  140. {
  141. av.Data["Name"] = names[i];
  142. av.Data["Value"] = values[i];
  143. if (!m_Database.Store(av))
  144. return false;
  145. }
  146. return true;
  147. }
  148. public bool RemoveItems(UUID principalID, string[] names)
  149. {
  150. foreach (string name in names)
  151. {
  152. m_Database.Delete(principalID, name);
  153. }
  154. return true;
  155. }
  156. }
  157. }