diff options
Diffstat (limited to 'OpenSim/Services/Connectors/SimianGrid/SimianAvatarServiceConnector.cs')
-rw-r--r-- | OpenSim/Services/Connectors/SimianGrid/SimianAvatarServiceConnector.cs | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/SimianGrid/SimianAvatarServiceConnector.cs b/OpenSim/Services/Connectors/SimianGrid/SimianAvatarServiceConnector.cs new file mode 100644 index 0000000..a18cb22 --- /dev/null +++ b/OpenSim/Services/Connectors/SimianGrid/SimianAvatarServiceConnector.cs | |||
@@ -0,0 +1,262 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Collections.Specialized; | ||
31 | using System.IO; | ||
32 | using System.Net; | ||
33 | using System.Reflection; | ||
34 | using log4net; | ||
35 | using Mono.Addins; | ||
36 | using Nini.Config; | ||
37 | using OpenSim.Framework; | ||
38 | using OpenSim.Region.Framework.Interfaces; | ||
39 | using OpenSim.Region.Framework.Scenes; | ||
40 | using OpenSim.Server.Base; | ||
41 | using OpenSim.Services.Interfaces; | ||
42 | using OpenMetaverse; | ||
43 | using OpenMetaverse.StructuredData; | ||
44 | |||
45 | namespace OpenSim.Services.Connectors.SimianGrid | ||
46 | { | ||
47 | /// <summary> | ||
48 | /// Connects avatar appearance data to the SimianGrid backend | ||
49 | /// </summary> | ||
50 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")] | ||
51 | public class SimianAvatarServiceConnector : IAvatarService, ISharedRegionModule | ||
52 | { | ||
53 | private static readonly ILog m_log = | ||
54 | LogManager.GetLogger( | ||
55 | MethodBase.GetCurrentMethod().DeclaringType); | ||
56 | private static string ZeroID = UUID.Zero.ToString(); | ||
57 | |||
58 | private string m_serverUrl = String.Empty; | ||
59 | |||
60 | #region ISharedRegionModule | ||
61 | |||
62 | public Type ReplaceableInterface { get { return null; } } | ||
63 | public void RegionLoaded(Scene scene) { } | ||
64 | public void PostInitialise() { } | ||
65 | public void Close() { } | ||
66 | |||
67 | public SimianAvatarServiceConnector() { } | ||
68 | public string Name { get { return "SimianAvatarServiceConnector"; } } | ||
69 | public void AddRegion(Scene scene) { if (!String.IsNullOrEmpty(m_serverUrl)) { scene.RegisterModuleInterface<IAvatarService>(this); } } | ||
70 | public void RemoveRegion(Scene scene) { if (!String.IsNullOrEmpty(m_serverUrl)) { scene.UnregisterModuleInterface<IAvatarService>(this); } } | ||
71 | |||
72 | #endregion ISharedRegionModule | ||
73 | |||
74 | public SimianAvatarServiceConnector(IConfigSource source) | ||
75 | { | ||
76 | Initialise(source); | ||
77 | } | ||
78 | |||
79 | public void Initialise(IConfigSource source) | ||
80 | { | ||
81 | if (Simian.IsSimianEnabled(source, "AvatarServices")) | ||
82 | { | ||
83 | IConfig gridConfig = source.Configs["AvatarService"]; | ||
84 | if (gridConfig == null) | ||
85 | { | ||
86 | m_log.Error("[AVATAR CONNECTOR]: AvatarService missing from OpenSim.ini"); | ||
87 | throw new Exception("Avatar connector init error"); | ||
88 | } | ||
89 | |||
90 | string serviceUrl = gridConfig.GetString("AvatarServerURI"); | ||
91 | if (String.IsNullOrEmpty(serviceUrl)) | ||
92 | { | ||
93 | m_log.Error("[AVATAR CONNECTOR]: No AvatarServerURI in section AvatarService"); | ||
94 | throw new Exception("Avatar connector init error"); | ||
95 | } | ||
96 | |||
97 | if (!serviceUrl.EndsWith("/")) | ||
98 | serviceUrl = serviceUrl + '/'; | ||
99 | |||
100 | m_serverUrl = serviceUrl; | ||
101 | } | ||
102 | } | ||
103 | |||
104 | #region IAvatarService | ||
105 | |||
106 | public AvatarData GetAvatar(UUID userID) | ||
107 | { | ||
108 | NameValueCollection requestArgs = new NameValueCollection | ||
109 | { | ||
110 | { "RequestMethod", "GetUser" }, | ||
111 | { "UserID", userID.ToString() } | ||
112 | }; | ||
113 | |||
114 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
115 | if (response["Success"].AsBoolean()) | ||
116 | { | ||
117 | OSDMap map = null; | ||
118 | try { map = OSDParser.DeserializeJson(response["LLAppearance"].AsString()) as OSDMap; } | ||
119 | catch { } | ||
120 | |||
121 | if (map != null) | ||
122 | { | ||
123 | AvatarWearable[] wearables = new AvatarWearable[13]; | ||
124 | wearables[0] = new AvatarWearable(map["ShapeItem"].AsUUID(), map["ShapeAsset"].AsUUID()); | ||
125 | wearables[1] = new AvatarWearable(map["SkinItem"].AsUUID(), map["SkinAsset"].AsUUID()); | ||
126 | wearables[2] = new AvatarWearable(map["HairItem"].AsUUID(), map["HairAsset"].AsUUID()); | ||
127 | wearables[3] = new AvatarWearable(map["EyesItem"].AsUUID(), map["EyesAsset"].AsUUID()); | ||
128 | wearables[4] = new AvatarWearable(map["ShirtItem"].AsUUID(), map["ShirtAsset"].AsUUID()); | ||
129 | wearables[5] = new AvatarWearable(map["PantsItem"].AsUUID(), map["PantsAsset"].AsUUID()); | ||
130 | wearables[6] = new AvatarWearable(map["ShoesItem"].AsUUID(), map["ShoesAsset"].AsUUID()); | ||
131 | wearables[7] = new AvatarWearable(map["SocksItem"].AsUUID(), map["SocksAsset"].AsUUID()); | ||
132 | wearables[8] = new AvatarWearable(map["JacketItem"].AsUUID(), map["JacketAsset"].AsUUID()); | ||
133 | wearables[9] = new AvatarWearable(map["GlovesItem"].AsUUID(), map["GlovesAsset"].AsUUID()); | ||
134 | wearables[10] = new AvatarWearable(map["UndershirtItem"].AsUUID(), map["UndershirtAsset"].AsUUID()); | ||
135 | wearables[11] = new AvatarWearable(map["UnderpantsItem"].AsUUID(), map["UnderpantsAsset"].AsUUID()); | ||
136 | wearables[12] = new AvatarWearable(map["SkirtItem"].AsUUID(), map["SkirtAsset"].AsUUID()); | ||
137 | |||
138 | AvatarAppearance appearance = new AvatarAppearance(userID); | ||
139 | appearance.Wearables = wearables; | ||
140 | appearance.AvatarHeight = (float)map["Height"].AsReal(); | ||
141 | |||
142 | AvatarData avatar = new AvatarData(appearance); | ||
143 | |||
144 | // Get attachments | ||
145 | map = null; | ||
146 | try { map = OSDParser.DeserializeJson(response["LLAttachments"].AsString()) as OSDMap; } | ||
147 | catch { } | ||
148 | |||
149 | if (map != null) | ||
150 | { | ||
151 | foreach (KeyValuePair<string, OSD> kvp in map) | ||
152 | avatar.Data[kvp.Key] = kvp.Value.AsString(); | ||
153 | } | ||
154 | |||
155 | return avatar; | ||
156 | } | ||
157 | else | ||
158 | { | ||
159 | m_log.Warn("[AVATAR CONNECTOR]: Failed to get user appearance for " + userID + | ||
160 | ", LLAppearance is missing or invalid"); | ||
161 | return null; | ||
162 | } | ||
163 | } | ||
164 | else | ||
165 | { | ||
166 | m_log.Warn("[AVATAR CONNECTOR]: Failed to get user appearance for " + userID + ": " + | ||
167 | response["Message"].AsString()); | ||
168 | } | ||
169 | |||
170 | return null; | ||
171 | } | ||
172 | |||
173 | public bool SetAvatar(UUID userID, AvatarData avatar) | ||
174 | { | ||
175 | m_log.Debug("[AVATAR CONNECTOR]: SetAvatar called for " + userID); | ||
176 | |||
177 | if (avatar.AvatarType == 1) // LLAvatar | ||
178 | { | ||
179 | AvatarAppearance appearance = avatar.ToAvatarAppearance(userID); | ||
180 | |||
181 | OSDMap map = new OSDMap(); | ||
182 | |||
183 | map["Height"] = OSD.FromReal(appearance.AvatarHeight); | ||
184 | |||
185 | map["ShapeItem"] = OSD.FromUUID(appearance.BodyItem); | ||
186 | map["ShapeAsset"] = OSD.FromUUID(appearance.BodyAsset); | ||
187 | map["SkinItem"] = OSD.FromUUID(appearance.SkinItem); | ||
188 | map["SkinAsset"] = OSD.FromUUID(appearance.SkinAsset); | ||
189 | map["HairItem"] = OSD.FromUUID(appearance.HairItem); | ||
190 | map["HairAsset"] = OSD.FromUUID(appearance.HairAsset); | ||
191 | map["EyesItem"] = OSD.FromUUID(appearance.EyesItem); | ||
192 | map["EyesAsset"] = OSD.FromUUID(appearance.EyesAsset); | ||
193 | map["ShirtItem"] = OSD.FromUUID(appearance.ShirtItem); | ||
194 | map["ShirtAsset"] = OSD.FromUUID(appearance.ShirtAsset); | ||
195 | map["PantsItem"] = OSD.FromUUID(appearance.PantsItem); | ||
196 | map["PantsAsset"] = OSD.FromUUID(appearance.PantsAsset); | ||
197 | map["ShoesItem"] = OSD.FromUUID(appearance.ShoesItem); | ||
198 | map["ShoesAsset"] = OSD.FromUUID(appearance.ShoesAsset); | ||
199 | map["SocksItem"] = OSD.FromUUID(appearance.SocksItem); | ||
200 | map["SocksAsset"] = OSD.FromUUID(appearance.SocksAsset); | ||
201 | map["JacketItem"] = OSD.FromUUID(appearance.JacketItem); | ||
202 | map["JacketAsset"] = OSD.FromUUID(appearance.JacketAsset); | ||
203 | map["GlovesItem"] = OSD.FromUUID(appearance.GlovesItem); | ||
204 | map["GlovesAsset"] = OSD.FromUUID(appearance.GlovesAsset); | ||
205 | map["UndershirtItem"] = OSD.FromUUID(appearance.UnderShirtItem); | ||
206 | map["UndershirtAsset"] = OSD.FromUUID(appearance.UnderShirtAsset); | ||
207 | map["UnderpantsItem"] = OSD.FromUUID(appearance.UnderPantsItem); | ||
208 | map["UnderpantsAsset"] = OSD.FromUUID(appearance.UnderPantsAsset); | ||
209 | map["SkirtItem"] = OSD.FromUUID(appearance.SkirtItem); | ||
210 | map["SkirtAsset"] = OSD.FromUUID(appearance.SkirtAsset); | ||
211 | |||
212 | OSDMap items = new OSDMap(); | ||
213 | foreach (KeyValuePair<string, string> kvp in avatar.Data) | ||
214 | { | ||
215 | if (kvp.Key.StartsWith("_ap_")) | ||
216 | items.Add(kvp.Key, OSD.FromString(kvp.Value)); | ||
217 | } | ||
218 | |||
219 | NameValueCollection requestArgs = new NameValueCollection | ||
220 | { | ||
221 | { "RequestMethod", "AddUserData" }, | ||
222 | { "UserID", userID.ToString() }, | ||
223 | { "LLAppearance", OSDParser.SerializeJsonString(map) }, | ||
224 | { "LLAttachments", OSDParser.SerializeJsonString(items) } | ||
225 | }; | ||
226 | |||
227 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
228 | bool success = response["Success"].AsBoolean(); | ||
229 | |||
230 | if (!success) | ||
231 | m_log.Warn("[AVATAR CONNECTOR]: Failed saving appearance for " + userID + ": " + response["Message"].AsString()); | ||
232 | |||
233 | return success; | ||
234 | } | ||
235 | else | ||
236 | { | ||
237 | m_log.Error("[AVATAR CONNECTOR]: Can't save appearance for " + userID + ". Unhandled avatar type " + avatar.AvatarType); | ||
238 | return false; | ||
239 | } | ||
240 | } | ||
241 | |||
242 | public bool ResetAvatar(UUID userID) | ||
243 | { | ||
244 | m_log.Error("[AVATAR CONNECTOR]: ResetAvatar called for " + userID + ", implement this"); | ||
245 | return false; | ||
246 | } | ||
247 | |||
248 | public bool SetItems(UUID userID, string[] names, string[] values) | ||
249 | { | ||
250 | m_log.Error("[AVATAR CONNECTOR]: SetItems called for " + userID + " with " + names.Length + " names and " + values.Length + " values, implement this"); | ||
251 | return false; | ||
252 | } | ||
253 | |||
254 | public bool RemoveItems(UUID userID, string[] names) | ||
255 | { | ||
256 | m_log.Error("[AVATAR CONNECTOR]: RemoveItems called for " + userID + " with " + names.Length + " names, implement this"); | ||
257 | return false; | ||
258 | } | ||
259 | |||
260 | #endregion IAvatarService | ||
261 | } | ||
262 | } | ||