AvatarService.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 AvatarData GetAvatar(UUID principalID)
  51. {
  52. AvatarBaseData[] av = m_Database.Get("PrincipalID", principalID.ToString());
  53. if (av.Length == 0)
  54. return null;
  55. AvatarData ret = new AvatarData();
  56. ret.Data = new Dictionary<string,string>();
  57. foreach (AvatarBaseData b in av)
  58. {
  59. if (b.Data["Name"] == "AvatarType")
  60. ret.AvatarType = Convert.ToInt32(b.Data["Value"]);
  61. else
  62. ret.Data[b.Data["Name"]] = b.Data["Value"];
  63. }
  64. return ret;
  65. }
  66. public bool SetAvatar(UUID principalID, AvatarData avatar)
  67. {
  68. int count = 0;
  69. foreach (KeyValuePair<string, string> kvp in avatar.Data)
  70. if (kvp.Key.StartsWith("_"))
  71. count++;
  72. m_log.DebugFormat("[AVATAR SERVICE]: SetAvatar for {0}, attachs={1}", principalID, count);
  73. m_Database.Delete("PrincipalID", principalID.ToString());
  74. AvatarBaseData av = new AvatarBaseData();
  75. av.Data = new Dictionary<string,string>();
  76. av.PrincipalID = principalID;
  77. av.Data["Name"] = "AvatarType";
  78. av.Data["Value"] = avatar.AvatarType.ToString();
  79. if (!m_Database.Store(av))
  80. return false;
  81. foreach (KeyValuePair<string,string> kvp in avatar.Data)
  82. {
  83. av.Data["Name"] = kvp.Key;
  84. av.Data["Value"] = kvp.Value;
  85. if (!m_Database.Store(av))
  86. {
  87. m_Database.Delete("PrincipalID", principalID.ToString());
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. public bool ResetAvatar(UUID principalID)
  94. {
  95. return m_Database.Delete("PrincipalID", principalID.ToString());
  96. }
  97. public bool SetItems(UUID principalID, string[] names, string[] values)
  98. {
  99. AvatarBaseData av = new AvatarBaseData();
  100. av.Data = new Dictionary<string,string>();
  101. av.PrincipalID = principalID;
  102. if (names.Length != values.Length)
  103. return false;
  104. for (int i = 0 ; i < names.Length ; i++)
  105. {
  106. av.Data["Name"] = names[i];
  107. av.Data["Value"] = values[i];
  108. if (!m_Database.Store(av))
  109. return false;
  110. }
  111. return true;
  112. }
  113. public bool RemoveItems(UUID principalID, string[] names)
  114. {
  115. foreach (string name in names)
  116. {
  117. m_Database.Delete(principalID, name);
  118. }
  119. return true;
  120. }
  121. }
  122. }