diff options
Diffstat (limited to 'OpenSim/Region/OptionalModules/ViewerSupport/DynamicMenuModule.cs')
-rw-r--r-- | OpenSim/Region/OptionalModules/ViewerSupport/DynamicMenuModule.cs | 285 |
1 files changed, 285 insertions, 0 deletions
diff --git a/OpenSim/Region/OptionalModules/ViewerSupport/DynamicMenuModule.cs b/OpenSim/Region/OptionalModules/ViewerSupport/DynamicMenuModule.cs new file mode 100644 index 0000000..c68fe99 --- /dev/null +++ b/OpenSim/Region/OptionalModules/ViewerSupport/DynamicMenuModule.cs | |||
@@ -0,0 +1,285 @@ | |||
1 | // ****************************************************************** | ||
2 | // Copyright (c) 2008, 2009 Melanie Thielker | ||
3 | // | ||
4 | // All rights reserved | ||
5 | // | ||
6 | |||
7 | using System; | ||
8 | using System.IO; | ||
9 | using System.Reflection; | ||
10 | using System.Text; | ||
11 | using System.Collections.Generic; | ||
12 | using OpenMetaverse; | ||
13 | using OpenMetaverse.StructuredData; | ||
14 | using OpenSim; | ||
15 | using OpenSim.Region; | ||
16 | using OpenSim.Region.Framework; | ||
17 | using OpenSim.Region.Framework.Scenes; | ||
18 | using OpenSim.Region.Framework.Interfaces; | ||
19 | using OpenSim.Framework; | ||
20 | //using OpenSim.Framework.Capabilities; | ||
21 | using OpenSim.Framework.Servers; | ||
22 | using OpenSim.Framework.Servers.HttpServer; | ||
23 | using Nini.Config; | ||
24 | using log4net; | ||
25 | using Mono.Addins; | ||
26 | using Caps = OpenSim.Framework.Capabilities.Caps; | ||
27 | using OSDMap = OpenMetaverse.StructuredData.OSDMap; | ||
28 | |||
29 | namespace OpenSim.Region.OptionalModules.ViewerSupport | ||
30 | { | ||
31 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "DynamicMenu")] | ||
32 | public class DynamicMenuModule : INonSharedRegionModule, IDynamicMenuModule | ||
33 | { | ||
34 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
35 | |||
36 | private class MenuItemData | ||
37 | { | ||
38 | public string Title; | ||
39 | public UUID AgentID; | ||
40 | public InsertLocation Location; | ||
41 | public UserMode Mode; | ||
42 | public CustomMenuHandler Handler; | ||
43 | } | ||
44 | |||
45 | private Dictionary<UUID, List<MenuItemData>> m_menuItems = | ||
46 | new Dictionary<UUID, List<MenuItemData>>(); | ||
47 | |||
48 | private Scene m_scene; | ||
49 | |||
50 | public string Name | ||
51 | { | ||
52 | get { return "DynamicMenuModule"; } | ||
53 | } | ||
54 | |||
55 | public Type ReplaceableInterface | ||
56 | { | ||
57 | get { return null; } | ||
58 | } | ||
59 | |||
60 | public void Initialise(IConfigSource config) | ||
61 | { | ||
62 | } | ||
63 | |||
64 | public void Close() | ||
65 | { | ||
66 | } | ||
67 | |||
68 | public void AddRegion(Scene scene) | ||
69 | { | ||
70 | m_scene = scene; | ||
71 | scene.EventManager.OnRegisterCaps += OnRegisterCaps; | ||
72 | m_scene.RegisterModuleInterface<IDynamicMenuModule>(this); | ||
73 | } | ||
74 | |||
75 | public void RegionLoaded(Scene scene) | ||
76 | { | ||
77 | ISimulatorFeaturesModule featuresModule = m_scene.RequestModuleInterface<ISimulatorFeaturesModule>(); | ||
78 | |||
79 | if (featuresModule != null) | ||
80 | featuresModule.OnSimulatorFeaturesRequest += OnSimulatorFeaturesRequest; | ||
81 | } | ||
82 | |||
83 | public void RemoveRegion(Scene scene) | ||
84 | { | ||
85 | } | ||
86 | |||
87 | private void OnSimulatorFeaturesRequest(UUID agentID, ref OSDMap features) | ||
88 | { | ||
89 | OSD menus = new OSDMap(); | ||
90 | if (features.ContainsKey("menus")) | ||
91 | menus = features["menus"]; | ||
92 | |||
93 | OSDMap agent = new OSDMap(); | ||
94 | OSDMap world = new OSDMap(); | ||
95 | OSDMap tools = new OSDMap(); | ||
96 | OSDMap advanced = new OSDMap(); | ||
97 | OSDMap admin = new OSDMap(); | ||
98 | if (((OSDMap)menus).ContainsKey("agent")) | ||
99 | agent = (OSDMap)((OSDMap)menus)["agent"]; | ||
100 | if (((OSDMap)menus).ContainsKey("world")) | ||
101 | world = (OSDMap)((OSDMap)menus)["world"]; | ||
102 | if (((OSDMap)menus).ContainsKey("tools")) | ||
103 | tools = (OSDMap)((OSDMap)menus)["tools"]; | ||
104 | if (((OSDMap)menus).ContainsKey("advanced")) | ||
105 | advanced = (OSDMap)((OSDMap)menus)["advanced"]; | ||
106 | if (((OSDMap)menus).ContainsKey("admin")) | ||
107 | admin = (OSDMap)((OSDMap)menus)["admin"]; | ||
108 | |||
109 | if (m_menuItems.ContainsKey(UUID.Zero)) | ||
110 | { | ||
111 | foreach (MenuItemData d in m_menuItems[UUID.Zero]) | ||
112 | { | ||
113 | if (!m_scene.Permissions.IsGod(agentID)) | ||
114 | { | ||
115 | if (d.Mode == UserMode.RegionManager && (!m_scene.Permissions.IsAdministrator(agentID))) | ||
116 | continue; | ||
117 | } | ||
118 | |||
119 | OSDMap loc = null; | ||
120 | switch (d.Location) | ||
121 | { | ||
122 | case InsertLocation.Agent: | ||
123 | loc = agent; | ||
124 | break; | ||
125 | case InsertLocation.World: | ||
126 | loc = world; | ||
127 | break; | ||
128 | case InsertLocation.Tools: | ||
129 | loc = tools; | ||
130 | break; | ||
131 | case InsertLocation.Advanced: | ||
132 | loc = advanced; | ||
133 | break; | ||
134 | case InsertLocation.Admin: | ||
135 | loc = admin; | ||
136 | break; | ||
137 | } | ||
138 | |||
139 | if (loc == null) | ||
140 | continue; | ||
141 | |||
142 | loc[d.Title] = OSD.FromString(d.Title); | ||
143 | } | ||
144 | } | ||
145 | |||
146 | if (m_menuItems.ContainsKey(agentID)) | ||
147 | { | ||
148 | foreach (MenuItemData d in m_menuItems[agentID]) | ||
149 | { | ||
150 | if (d.Mode == UserMode.God && (!m_scene.Permissions.IsGod(agentID))) | ||
151 | continue; | ||
152 | |||
153 | OSDMap loc = null; | ||
154 | switch (d.Location) | ||
155 | { | ||
156 | case InsertLocation.Agent: | ||
157 | loc = agent; | ||
158 | break; | ||
159 | case InsertLocation.World: | ||
160 | loc = world; | ||
161 | break; | ||
162 | case InsertLocation.Tools: | ||
163 | loc = tools; | ||
164 | break; | ||
165 | case InsertLocation.Advanced: | ||
166 | loc = advanced; | ||
167 | break; | ||
168 | case InsertLocation.Admin: | ||
169 | loc = admin; | ||
170 | break; | ||
171 | } | ||
172 | |||
173 | if (loc == null) | ||
174 | continue; | ||
175 | |||
176 | loc[d.Title] = OSD.FromString(d.Title); | ||
177 | } | ||
178 | } | ||
179 | |||
180 | |||
181 | ((OSDMap)menus)["agent"] = agent; | ||
182 | ((OSDMap)menus)["world"] = world; | ||
183 | ((OSDMap)menus)["tools"] = tools; | ||
184 | ((OSDMap)menus)["advanced"] = advanced; | ||
185 | ((OSDMap)menus)["admin"] = admin; | ||
186 | |||
187 | features["menus"] = menus; | ||
188 | } | ||
189 | |||
190 | private void OnRegisterCaps(UUID agentID, Caps caps) | ||
191 | { | ||
192 | string capUrl = "/CAPS/" + UUID.Random() + "/"; | ||
193 | |||
194 | capUrl = "/CAPS/" + UUID.Random() + "/"; | ||
195 | caps.RegisterHandler("CustomMenuAction", new MenuActionHandler(capUrl, "CustomMenuAction", agentID, this, m_scene)); | ||
196 | } | ||
197 | |||
198 | internal void HandleMenuSelection(string action, UUID agentID, List<uint> selection) | ||
199 | { | ||
200 | if (m_menuItems.ContainsKey(agentID)) | ||
201 | { | ||
202 | foreach (MenuItemData d in m_menuItems[agentID]) | ||
203 | { | ||
204 | if (d.Title == action) | ||
205 | d.Handler(action, agentID, selection); | ||
206 | } | ||
207 | } | ||
208 | |||
209 | if (m_menuItems.ContainsKey(UUID.Zero)) | ||
210 | { | ||
211 | foreach (MenuItemData d in m_menuItems[UUID.Zero]) | ||
212 | { | ||
213 | if (d.Title == action) | ||
214 | d.Handler(action, agentID, selection); | ||
215 | } | ||
216 | } | ||
217 | } | ||
218 | |||
219 | public void AddMenuItem(string title, InsertLocation location, UserMode mode, CustomMenuHandler handler) | ||
220 | { | ||
221 | AddMenuItem(UUID.Zero, title, location, mode, handler); | ||
222 | } | ||
223 | |||
224 | public void AddMenuItem(UUID agentID, string title, InsertLocation location, UserMode mode, CustomMenuHandler handler) | ||
225 | { | ||
226 | if (!m_menuItems.ContainsKey(agentID)) | ||
227 | m_menuItems[agentID] = new List<MenuItemData>(); | ||
228 | |||
229 | m_menuItems[agentID].Add(new MenuItemData() { Title = title, AgentID = agentID, Location = location, Mode = mode, Handler = handler }); | ||
230 | } | ||
231 | |||
232 | public void RemoveMenuItem(string action) | ||
233 | { | ||
234 | foreach (KeyValuePair<UUID,List< MenuItemData>> kvp in m_menuItems) | ||
235 | { | ||
236 | List<MenuItemData> pendingDeletes = new List<MenuItemData>(); | ||
237 | foreach (MenuItemData d in kvp.Value) | ||
238 | { | ||
239 | if (d.Title == action) | ||
240 | pendingDeletes.Add(d); | ||
241 | } | ||
242 | |||
243 | foreach (MenuItemData d in pendingDeletes) | ||
244 | kvp.Value.Remove(d); | ||
245 | } | ||
246 | } | ||
247 | } | ||
248 | |||
249 | public class MenuActionHandler : BaseStreamHandler | ||
250 | { | ||
251 | private static readonly ILog m_log = | ||
252 | LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
253 | |||
254 | private UUID m_agentID; | ||
255 | private Scene m_scene; | ||
256 | private DynamicMenuModule m_module; | ||
257 | |||
258 | public MenuActionHandler(string path, string name, UUID agentID, DynamicMenuModule module, Scene scene) | ||
259 | :base("POST", path, name, agentID.ToString()) | ||
260 | { | ||
261 | m_agentID = agentID; | ||
262 | m_scene = scene; | ||
263 | m_module = module; | ||
264 | } | ||
265 | |||
266 | public override byte[] Handle(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse) | ||
267 | { | ||
268 | StreamReader reader = new StreamReader(request); | ||
269 | string requestBody = reader.ReadToEnd(); | ||
270 | |||
271 | OSD osd = OSDParser.DeserializeLLSDXml(requestBody); | ||
272 | |||
273 | string action = ((OSDMap)osd)["action"].AsString(); | ||
274 | OSDArray selection = (OSDArray)((OSDMap)osd)["selection"]; | ||
275 | List<uint> sel = new List<uint>(); | ||
276 | for (int i = 0 ; i < selection.Count ; i++) | ||
277 | sel.Add(selection[i].AsUInteger()); | ||
278 | |||
279 | Util.FireAndForget(x => { m_module.HandleMenuSelection(action, m_agentID, sel); }); | ||
280 | |||
281 | Encoding encoding = Encoding.UTF8; | ||
282 | return encoding.GetBytes(OSDParser.SerializeLLSDXmlString(new OSD())); | ||
283 | } | ||
284 | } | ||
285 | } | ||