aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules/Avatar/AvatarFactory/AvatarFactoryModule.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/CoreModules/Avatar/AvatarFactory/AvatarFactoryModule.cs')
-rw-r--r--OpenSim/Region/CoreModules/Avatar/AvatarFactory/AvatarFactoryModule.cs232
1 files changed, 232 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/Avatar/AvatarFactory/AvatarFactoryModule.cs b/OpenSim/Region/CoreModules/Avatar/AvatarFactory/AvatarFactoryModule.cs
new file mode 100644
index 0000000..d084dbd
--- /dev/null
+++ b/OpenSim/Region/CoreModules/Avatar/AvatarFactory/AvatarFactoryModule.cs
@@ -0,0 +1,232 @@
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 OpenSim 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
28using System;
29using System.Collections.Generic;
30using System.Reflection;
31
32using System.Threading;
33using OpenMetaverse;
34using log4net;
35using Nini.Config;
36using OpenSim.Framework;
37using OpenSim.Framework.Communications.Cache;
38using OpenSim.Region.Framework.Interfaces;
39using OpenSim.Region.Framework.Scenes;
40
41namespace OpenSim.Region.CoreModules.Avatar.AvatarFactory
42{
43 public class AvatarFactoryModule : IAvatarFactory, IRegionModule
44 {
45 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
46 private Scene m_scene = null;
47 private static readonly AvatarAppearance def = new AvatarAppearance();
48
49 public bool TryGetAvatarAppearance(UUID avatarId, out AvatarAppearance appearance)
50 {
51 CachedUserInfo profile = m_scene.CommsManager.UserProfileCacheService.GetUserDetails(avatarId);
52 //if ((profile != null) && (profile.RootFolder != null))
53 if (profile != null)
54 {
55 appearance = m_scene.CommsManager.AvatarService.GetUserAppearance(avatarId);
56 if (appearance != null)
57 {
58 //SetAppearanceAssets(profile, ref appearance);
59 //m_log.DebugFormat("[APPEARANCE]: Found : {0}", appearance.ToString());
60 return true;
61 }
62 }
63
64 appearance = CreateDefault(avatarId);
65 m_log.ErrorFormat("[APPEARANCE]: Appearance not found for {0}, creating default", avatarId);
66 return false;
67 }
68
69 private AvatarAppearance CreateDefault(UUID avatarId)
70 {
71 AvatarAppearance appearance = null;
72 AvatarWearable[] wearables;
73 byte[] visualParams;
74 GetDefaultAvatarAppearance(out wearables, out visualParams);
75 appearance = new AvatarAppearance(avatarId, wearables, visualParams);
76
77 return appearance;
78 }
79
80 public void Initialise(Scene scene, IConfigSource source)
81 {
82 scene.RegisterModuleInterface<IAvatarFactory>(this);
83 scene.EventManager.OnNewClient += NewClient;
84
85 if (m_scene == null)
86 {
87 m_scene = scene;
88 }
89
90 }
91
92 public void PostInitialise()
93 {
94 }
95
96 public void Close()
97 {
98 }
99
100 public string Name
101 {
102 get { return "Default Avatar Factory"; }
103 }
104
105 public bool IsSharedModule
106 {
107 get { return false; }
108 }
109
110 public void NewClient(IClientAPI client)
111 {
112 client.OnAvatarNowWearing += AvatarIsWearing;
113 }
114
115 public void RemoveClient(IClientAPI client)
116 {
117 // client.OnAvatarNowWearing -= AvatarIsWearing;
118 }
119
120
121 public void SetAppearanceAssets(CachedUserInfo profile, ref AvatarAppearance appearance)
122 {
123 if (profile.RootFolder != null)
124 {
125 for (int i = 0; i < 13; i++)
126 {
127 if (appearance.Wearables[i].ItemID == UUID.Zero)
128 {
129 appearance.Wearables[i].AssetID = UUID.Zero;
130 }
131 else
132 {
133 InventoryItemBase baseItem = profile.RootFolder.FindItem(appearance.Wearables[i].ItemID);
134
135 if (baseItem != null)
136 {
137 appearance.Wearables[i].AssetID = baseItem.AssetID;
138 }
139 else
140 {
141 m_log.ErrorFormat("[APPEARANCE]: Can't find inventory item {0}, setting to default", appearance.Wearables[i].ItemID);
142 appearance.Wearables[i].AssetID = def.Wearables[i].AssetID;
143 }
144 }
145 }
146 }
147 else
148 {
149 m_log.Error("[APPEARANCE]: you have no inventory, appearance stuff isn't going to work");
150 }
151 }
152
153 /// <summary>
154 /// Update what the avatar is wearing using an item from their inventory.
155 /// </summary>
156 /// <param name="sender"></param>
157 /// <param name="e"></param>
158 public void AvatarIsWearing(Object sender, AvatarWearingArgs e)
159 {
160 IClientAPI clientView = (IClientAPI)sender;
161 ScenePresence avatar = m_scene.GetScenePresence(clientView.AgentId);
162
163 if (avatar == null)
164 {
165 m_log.Error("[APPEARANCE]: Avatar is child agent, ignoring AvatarIsWearing event");
166 return;
167 }
168
169 CachedUserInfo profile = m_scene.CommsManager.UserProfileCacheService.GetUserDetails(clientView.AgentId);
170
171 AvatarAppearance avatAppearance = null;
172 if (!TryGetAvatarAppearance(clientView.AgentId, out avatAppearance))
173 {
174 m_log.Warn("[APPEARANCE]: We didn't seem to find the appearance, falling back to ScenePresence");
175 avatAppearance = avatar.Appearance;
176 }
177
178 //m_log.DebugFormat("[APPEARANCE]: Received wearables for {0}", clientView.Name);
179
180 if (profile != null)
181 {
182 if (profile.RootFolder != null)
183 {
184 foreach (AvatarWearingArgs.Wearable wear in e.NowWearing)
185 {
186 if (wear.Type < 13)
187 {
188 avatAppearance.Wearables[wear.Type].ItemID = wear.ItemID;
189 }
190 }
191
192 SetAppearanceAssets(profile, ref avatAppearance);
193
194 m_scene.CommsManager.AvatarService.UpdateUserAppearance(clientView.AgentId, avatAppearance);
195 avatar.Appearance = avatAppearance;
196 }
197 else
198 {
199 m_log.WarnFormat(
200 "[APPEARANCE]: Inventory has not yet been received for {0}, cannot set wearables",
201 clientView.Name);
202 }
203 }
204 else
205 {
206 m_log.WarnFormat("[APPEARANCE]: Cannot set wearables for {0}, no user profile found", clientView.Name);
207 }
208 }
209
210 public static void GetDefaultAvatarAppearance(out AvatarWearable[] wearables, out byte[] visualParams)
211 {
212 visualParams = GetDefaultVisualParams();
213 wearables = AvatarWearable.DefaultWearables;
214 }
215
216 public void UpdateDatabase(UUID user, AvatarAppearance appearance)
217 {
218 m_scene.CommsManager.AvatarService.UpdateUserAppearance(user, appearance);
219 }
220
221 private static byte[] GetDefaultVisualParams()
222 {
223 byte[] visualParams;
224 visualParams = new byte[218];
225 for (int i = 0; i < 218; i++)
226 {
227 visualParams[i] = 100;
228 }
229 return visualParams;
230 }
231 }
232}