AvatarService.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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(principalID);
  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. av.Data["Value"] = kvp.Value;
  98. if (!m_Database.Store(av))
  99. {
  100. m_Database.Delete("PrincipalID", principalID.ToString());
  101. return false;
  102. }
  103. }
  104. return true;
  105. }
  106. public bool ResetAvatar(UUID principalID)
  107. {
  108. return m_Database.Delete("PrincipalID", principalID.ToString());
  109. }
  110. public bool SetItems(UUID principalID, string[] names, string[] values)
  111. {
  112. AvatarBaseData av = new AvatarBaseData();
  113. av.Data = new Dictionary<string,string>();
  114. av.PrincipalID = principalID;
  115. if (names.Length != values.Length)
  116. return false;
  117. for (int i = 0 ; i < names.Length ; i++)
  118. {
  119. av.Data["Name"] = names[i];
  120. av.Data["Value"] = values[i];
  121. if (!m_Database.Store(av))
  122. return false;
  123. }
  124. return true;
  125. }
  126. public bool RemoveItems(UUID principalID, string[] names)
  127. {
  128. foreach (string name in names)
  129. {
  130. m_Database.Delete(principalID, name);
  131. }
  132. return true;
  133. }
  134. }
  135. }