Annotation of /trunk/ModularRex/RexParts/RexPython/RexEventManager.cs
Parent Directory
|
Revision Log
Revision 39 - (view) (download)
| 1 : | mikkopa | 12 | using System; |
| 2 : | mikkopa | 18 | using System.Reflection; |
| 3 : | mikkopa | 12 | using System.Collections.Generic; |
| 4 : | using System.Text; | ||
| 5 : | using OpenMetaverse; | ||
| 6 : | mikkopa | 39 | using OpenSim; |
| 7 : | mikkopa | 12 | using OpenSim.Framework; |
| 8 : | using OpenSim.Region.Environment.Scenes; | ||
| 9 : | mikkopa | 18 | using log4net; |
| 10 : | mikkopa | 12 | |
| 11 : | namespace ModularRex.RexParts.RexPython | ||
| 12 : | { | ||
| 13 : | [Serializable] | ||
| 14 : | class RexEventManager | ||
| 15 : | { | ||
| 16 : | mikkopa | 19 | private RexScriptEngine m_scriptEngine; |
| 17 : | mikkopa | 18 | private static readonly ILog m_log |
| 18 : | = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
| 19 : | mikkopa | 12 | |
| 20 : | // tuco fixme, is there a better way to do this search??? | ||
| 21 : | private EntityBase GetEntityBase(uint vId) | ||
| 22 : | { | ||
| 23 : | mikkopa | 19 | SceneObjectPart part = m_scriptEngine.World.GetSceneObjectPart(vId); |
| 24 : | mikkopa | 12 | if (part != null && (EntityBase)(part.ParentGroup) != null) |
| 25 : | return (EntityBase)(part.ParentGroup); | ||
| 26 : | else | ||
| 27 : | return null; | ||
| 28 : | } | ||
| 29 : | |||
| 30 : | mikkopa | 19 | public RexEventManager(RexScriptEngine scriptEngine) |
| 31 : | mikkopa | 12 | { |
| 32 : | mikkopa | 19 | m_scriptEngine = scriptEngine; |
| 33 : | mikkopa | 18 | m_log.InfoFormat("[RexScriptEngine]: Hooking up to server events"); |
| 34 : | mikkopa | 19 | m_scriptEngine.World.EventManager.OnObjectGrab += touch_start; |
| 35 : | mikkopa | 12 | // myScriptEngine.World.EventManager.OnRezScript += OnRezScript; |
| 36 : | // myScriptEngine.World.EventManager.OnRemoveScript += OnRemoveScript; | ||
| 37 : | // myScriptEngine.World.EventManager.OnFrame += OnFrame; | ||
| 38 : | //myScriptEngine.World.EventManager.OnNewClient += OnNewClient; | ||
| 39 : | mikkopa | 19 | m_scriptEngine.World.EventManager.OnNewPresence += OnNewPresence; |
| 40 : | m_scriptEngine.World.EventManager.OnRemovePresence += OnRemovePresence; | ||
| 41 : | m_scriptEngine.World.EventManager.OnShutdown += OnShutDown; | ||
| 42 : | mikkopa | 12 | |
| 43 : | ///TODO: | ||
| 44 : | ///These events were added to forked version. Some of them can be handled | ||
| 45 : | ///other way, some need changes to core and some need changes to physics engine. | ||
| 46 : | mikkopa | 18 | //myScriptEngine.World.EventManager.OnAddEntity += OnAddEntity; //this was previously launched from Scene or InnerScene |
| 47 : | //myScriptEngine.World.EventManager.OnRemoveEntity += OnRemoveEntity; //this was previously launched from Scene | ||
| 48 : | //myScriptEngine.World.EventManager.OnPythonClassChange += OnPythonClassChange; //this was launched from SceneObjectPart | ||
| 49 : | //myScriptEngine.World.EventManager.OnPrimVolumeCollision += OnPrimVolumeCollision; //this was launched from PhysicActor | ||
| 50 : | mikkopa | 19 | m_scriptEngine.World.EventManager.OnChatFromWorld += OnRexScriptListen; |
| 51 : | m_scriptEngine.World.EventManager.OnChatBroadcast += OnRexScriptListen; | ||
| 52 : | m_scriptEngine.World.EventManager.OnChatFromClient += OnRexScriptListen; | ||
| 53 : | mikkopa | 20 | OpenSim.OpenSim.RegisterCmd("python", PythonScriptCommand, "Rex python commands. Type \"python help\" for more information."); |
| 54 : | mikkopa | 12 | } |
| 55 : | |||
| 56 : | mikkopa | 15 | private void PythonScriptCommand(string[] cmdparams) |
| 57 : | { | ||
| 58 : | try | ||
| 59 : | { | ||
| 60 : | if (cmdparams.Length >= 1) | ||
| 61 : | { | ||
| 62 : | string command = cmdparams[0].ToLower(); | ||
| 63 : | switch (command) | ||
| 64 : | { | ||
| 65 : | case "help": | ||
| 66 : | mikkopa | 18 | m_log.Info("[RexScriptEngine]: Python commands available:"); |
| 67 : | m_log.Info("[RexScriptEngine]: python restart - restarts the python engine"); | ||
| 68 : | mikkopa | 15 | break; |
| 69 : | case "restart": | ||
| 70 : | mikkopa | 19 | m_scriptEngine.RestartPythonEngine(); |
| 71 : | mikkopa | 15 | break; |
| 72 : | default: | ||
| 73 : | mikkopa | 18 | m_log.WarnFormat("[RexScriptEngine]: Unknown PythonScriptEngine command:" + cmdparams[0]); |
| 74 : | mikkopa | 15 | break; |
| 75 : | } | ||
| 76 : | } | ||
| 77 : | } | ||
| 78 : | catch (Exception e) | ||
| 79 : | { | ||
| 80 : | mikkopa | 18 | m_log.WarnFormat("[RexScriptEngine]: OnPythonScriptCommand: " + e.ToString()); |
| 81 : | mikkopa | 15 | } |
| 82 : | } | ||
| 83 : | mikkopa | 12 | |
| 84 : | mikkopa | 19 | public void touch_start(uint localID, uint originalID, Vector3 offsetPos, IClientAPI remoteClient, SurfaceTouchEventArgs surfaceArgs)//(uint localID, Vector3 offsetPos, IClientAPI remoteClient) |
| 85 : | mikkopa | 12 | { |
| 86 : | string EventParams = "\"touch_start\"," + localID.ToString() + "," + "\"" + remoteClient.AgentId.ToString() + "\""; | ||
| 87 : | mikkopa | 19 | m_scriptEngine.ExecutePythonCommand("CreateEventWithName(" + EventParams + ")"); |
| 88 : | mikkopa | 12 | } |
| 89 : | |||
| 90 : | /* | ||
| 91 : | public void OnRezScript(uint localID, UUID itemID, string script) | ||
| 92 : | { | ||
| 93 : | |||
| 94 : | } | ||
| 95 : | public void OnRemoveScript(uint localID, UUID itemID) | ||
| 96 : | { | ||
| 97 : | |||
| 98 : | } | ||
| 99 : | |||
| 100 : | public void OnFrame() | ||
| 101 : | { | ||
| 102 : | |||
| 103 : | } | ||
| 104 : | |||
| 105 : | public void OnNewClient(IClientAPI vClient) | ||
| 106 : | { | ||
| 107 : | string EventParams = "\"new_client\"," + "\"" + vClient.AgentId.ToString() + "\""; | ||
| 108 : | myScriptEngine.ExecutePythonCommand("CreateEventWithName(" + EventParams + ")"); | ||
| 109 : | } | ||
| 110 : | */ | ||
| 111 : | |||
| 112 : | public void OnNewPresence(ScenePresence vPresence) | ||
| 113 : | { | ||
| 114 : | try | ||
| 115 : | { | ||
| 116 : | mikkopa | 14 | //IRexBot related |
| 117 : | mikkopa | 12 | if (vPresence.ControllingClient is IRexBot) |
| 118 : | { | ||
| 119 : | string EventParams = "\"add_bot\"," + vPresence.LocalId.ToString() + "," + "\"" + vPresence.UUID.ToString() + "\""; | ||
| 120 : | mikkopa | 19 | m_scriptEngine.ExecutePythonCommand("CreateEventWithName(" + EventParams + ")"); |
| 121 : | mikkopa | 12 | } |
| 122 : | else | ||
| 123 : | { | ||
| 124 : | string EventParams = "\"add_presence\"," + vPresence.LocalId.ToString() + "," + "\"" + vPresence.UUID.ToString() + "\""; | ||
| 125 : | mikkopa | 19 | m_scriptEngine.ExecutePythonCommand("CreateEventWithName(" + EventParams + ")"); |
| 126 : | mikkopa | 12 | } |
| 127 : | mikkopa | 14 | |
| 128 : | //Tie up some RexClientView events | ||
| 129 : | RexNetwork.RexClientView rex; | ||
| 130 : | if (vPresence.ClientView.TryGet(out rex)) | ||
| 131 : | { | ||
| 132 : | rex.OnReceiveRexStartUp += OnRexClientStartUp; | ||
| 133 : | rex.OnReceiveRexClientScriptCmd += OnRexClientScriptCommand; | ||
| 134 : | } | ||
| 135 : | mikkopa | 12 | } |
| 136 : | catch (Exception e) | ||
| 137 : | { | ||
| 138 : | mikkopa | 18 | m_log.WarnFormat("[RexScriptEngine]: OnNewPresence: " + e.ToString()); |
| 139 : | mikkopa | 12 | } |
| 140 : | } | ||
| 141 : | |||
| 142 : | public void OnRemovePresence(UUID uuid) | ||
| 143 : | { | ||
| 144 : | try | ||
| 145 : | { | ||
| 146 : | // python handles if this presence was bot or human | ||
| 147 : | string EventParams = "\"remove_presence\"," + "\"" + uuid.ToString() + "\""; | ||
| 148 : | mikkopa | 19 | m_scriptEngine.ExecutePythonCommand("CreateEventWithName(" + EventParams + ")"); |
| 149 : | mikkopa | 12 | } |
| 150 : | catch (Exception e) | ||
| 151 : | { | ||
| 152 : | mikkopa | 18 | m_log.WarnFormat("[RexScriptEngine]: OnRemovePresence: " + e.ToString()); |
| 153 : | mikkopa | 12 | } |
| 154 : | } | ||
| 155 : | |||
| 156 : | public void OnShutDown() | ||
| 157 : | { | ||
| 158 : | Console.WriteLine("REX OnShutDown"); | ||
| 159 : | } | ||
| 160 : | |||
| 161 : | public void OnAddEntity(uint localID) | ||
| 162 : | { | ||
| 163 : | try | ||
| 164 : | { | ||
| 165 : | string PythonClassName = "rxactor.Actor"; | ||
| 166 : | string PythonTag = ""; | ||
| 167 : | |||
| 168 : | RexObjects.RexObjectGroup tempobj = (RexObjects.RexObjectGroup)GetEntityBase(localID); | ||
| 169 : | //SceneObjectGroup tempobj = (SceneObjectGroup)GetEntityBase(localID); | ||
| 170 : | if (tempobj != null && tempobj.RootPart != null && tempobj.RootPart.RexClassName.Length > 0) | ||
| 171 : | PythonClassName = tempobj.RootPart.RexClassName; | ||
| 172 : | |||
| 173 : | // Create the actor directly without using an event. | ||
| 174 : | mikkopa | 19 | m_scriptEngine.CreateActorToPython(localID.ToString(), PythonClassName, PythonTag); |
| 175 : | mikkopa | 12 | } |
| 176 : | catch (Exception e) | ||
| 177 : | { | ||
| 178 : | mikkopa | 18 | m_log.WarnFormat("[RexScriptEngine]: OnAddEntity: " + e.ToString()); |
| 179 : | mikkopa | 12 | } |
| 180 : | } | ||
| 181 : | |||
| 182 : | public void OnRemoveEntity(uint localID) | ||
| 183 : | { | ||
| 184 : | try | ||
| 185 : | { | ||
| 186 : | string EventParams = "\"remove_entity\"," + "\"" + localID.ToString() + "\""; | ||
| 187 : | mikkopa | 19 | m_scriptEngine.ExecutePythonCommand("CreateEventWithName(" + EventParams + ")"); |
| 188 : | mikkopa | 12 | } |
| 189 : | catch (Exception e) | ||
| 190 : | { | ||
| 191 : | mikkopa | 18 | m_log.WarnFormat("[RexScriptEngine]: OnRemoveEntity: " + e.ToString()); |
| 192 : | mikkopa | 12 | } |
| 193 : | } | ||
| 194 : | |||
| 195 : | public void OnPythonClassChange(uint localID) | ||
| 196 : | { | ||
| 197 : | try | ||
| 198 : | { | ||
| 199 : | string PythonClassName = "rxactor.Actor"; | ||
| 200 : | string PythonTag = ""; | ||
| 201 : | |||
| 202 : | RexObjects.RexObjectGroup tempobj = (RexObjects.RexObjectGroup)GetEntityBase(localID); | ||
| 203 : | //SceneObjectGroup tempobj = (SceneObjectGroup)GetEntityBase(localID); | ||
| 204 : | if (tempobj != null && tempobj.RootPart != null && tempobj.RootPart.RexClassName.Length > 0) | ||
| 205 : | { | ||
| 206 : | int tagindex = tempobj.RootPart.RexClassName.IndexOf("?", 0); | ||
| 207 : | if (tagindex > -1) | ||
| 208 : | { | ||
| 209 : | PythonClassName = tempobj.RootPart.RexClassName.Substring(0, tagindex); | ||
| 210 : | PythonTag = tempobj.RootPart.RexClassName.Substring(tagindex + 1); | ||
| 211 : | } | ||
| 212 : | else | ||
| 213 : | PythonClassName = tempobj.RootPart.RexClassName; | ||
| 214 : | } | ||
| 215 : | mikkopa | 19 | if (m_scriptEngine.IsEngineStarted) |
| 216 : | m_scriptEngine.CreateActorToPython(localID.ToString(), PythonClassName, PythonTag); | ||
| 217 : | mikkopa | 12 | } |
| 218 : | catch (Exception e) | ||
| 219 : | { | ||
| 220 : | mikkopa | 18 | m_log.WarnFormat("[RexScriptEngine]: OnPythonClassChange: " + e.ToString()); |
| 221 : | mikkopa | 12 | } |
| 222 : | } | ||
| 223 : | |||
| 224 : | mikkopa | 14 | public void OnRexClientScriptCommand(RexNetwork.RexClientView remoteClient, UUID agentID, List<string> commands) |
| 225 : | mikkopa | 12 | { |
| 226 : | try | ||
| 227 : | { | ||
| 228 : | string Paramlist = ""; | ||
| 229 : | mikkopa | 14 | foreach (string s in commands) |
| 230 : | mikkopa | 12 | Paramlist = Paramlist + "," + "\"" + s + "\""; |
| 231 : | mikkopa | 14 | |
| 232 : | string EventParams = "\"client_event\",\"" + agentID.ToString() + "\"" + Paramlist; | ||
| 233 : | mikkopa | 19 | m_scriptEngine.ExecutePythonCommand("CreateEventWithName(" + EventParams + ")"); |
| 234 : | mikkopa | 12 | } |
| 235 : | catch (Exception e) | ||
| 236 : | { | ||
| 237 : | mikkopa | 18 | m_log.WarnFormat("[RexScriptEngine]: OnRexClientScriptCommand: " + e.ToString()); |
| 238 : | mikkopa | 12 | } |
| 239 : | } | ||
| 240 : | |||
| 241 : | public void OnPrimVolumeCollision(uint ownID, uint colliderID) | ||
| 242 : | { | ||
| 243 : | try | ||
| 244 : | { | ||
| 245 : | string EventParams = "\"primvol_col\"," + ownID.ToString() + "," + "\"" + colliderID.ToString() + "\""; | ||
| 246 : | mikkopa | 19 | m_scriptEngine.ExecutePythonCommand("CreateEventWithName(" + EventParams + ")"); |
| 247 : | mikkopa | 12 | } |
| 248 : | catch (Exception e) | ||
| 249 : | { | ||
| 250 : | mikkopa | 18 | m_log.WarnFormat("[RexScriptEngine]: OnPrimVolumeCollision: " + e.ToString()); |
| 251 : | mikkopa | 12 | } |
| 252 : | } | ||
| 253 : | |||
| 254 : | mikkopa | 18 | /// <summary> |
| 255 : | /// Listens to all world scripts and clients. Sends event from that to python scripts | ||
| 256 : | /// </summary> | ||
| 257 : | public void OnRexScriptListen(object sender, OSChatMessage chat)//uint vPrimLocalId, int vChannel, string vName, UUID vId, string vMessage) | ||
| 258 : | mikkopa | 12 | { |
| 259 : | try | ||
| 260 : | { | ||
| 261 : | mikkopa | 18 | if (chat.Message != "") |
| 262 : | { | ||
| 263 : | uint localId = 0; | ||
| 264 : | string sid = "0"; | ||
| 265 : | string name = chat.From; | ||
| 266 : | mikkopa | 12 | |
| 267 : | mikkopa | 19 | SceneObjectPart sop = m_scriptEngine.World.GetSceneObjectPart(chat.SenderUUID); |
| 268 : | mikkopa | 18 | if (sop != null) |
| 269 : | { | ||
| 270 : | localId = sop.LocalId; | ||
| 271 : | sid = sop.ParentGroup.LocalId.ToString(); | ||
| 272 : | } | ||
| 273 : | |||
| 274 : | if (chat.Sender != null) | ||
| 275 : | { | ||
| 276 : | mikkopa | 19 | ScenePresence sp = m_scriptEngine.World.GetScenePresence(chat.Sender.AgentId); |
| 277 : | mikkopa | 18 | if (sp != null) |
| 278 : | { | ||
| 279 : | localId = sp.LocalId; | ||
| 280 : | sid = chat.Sender.AgentId.ToString(); | ||
| 281 : | if (name == "" || name == null) name = chat.Sender.Name; | ||
| 282 : | } | ||
| 283 : | } | ||
| 284 : | |||
| 285 : | //if (chat.SenderObject != null) | ||
| 286 : | //{ | ||
| 287 : | // m_log.Info("sender is an "+chat.SenderObject.GetType()); | ||
| 288 : | //} | ||
| 289 : | |||
| 290 : | string eventParams = "\"listen\"," + localId + "," + chat.Channel.ToString() + "," + | ||
| 291 : | "\"" + name + "\"" + "," + "\"" + sid + "\"" + "," + "\"" + chat.Message + "\""; | ||
| 292 : | mikkopa | 19 | m_scriptEngine.ExecutePythonCommand("CreateEventWithName(" + eventParams + ")"); |
| 293 : | mikkopa | 18 | //m_log.Info(eventParams); |
| 294 : | mikkopa | 12 | } |
| 295 : | } | ||
| 296 : | catch (Exception e) | ||
| 297 : | { | ||
| 298 : | mikkopa | 18 | m_log.WarnFormat("[RexScriptEngine]: OnRexScriptListen: " + e.ToString()); |
| 299 : | mikkopa | 12 | } |
| 300 : | } | ||
| 301 : | |||
| 302 : | mikkopa | 14 | public void OnRexClientStartUp(RexNetwork.RexClientView client, UUID agentID, string status) |
| 303 : | mikkopa | 12 | { |
| 304 : | try | ||
| 305 : | { | ||
| 306 : | mikkopa | 14 | string EventParams = "\"client_startup\",\"" + agentID.ToString() + "\",\"" + status + "\""; |
| 307 : | mikkopa | 19 | m_scriptEngine.ExecutePythonCommand("CreateEventWithName(" + EventParams + ")"); |
| 308 : | mikkopa | 12 | } |
| 309 : | catch (Exception e) | ||
| 310 : | { | ||
| 311 : | mikkopa | 18 | m_log.WarnFormat("[RexScriptEngine]: OnRexClientStartUp: " + e.ToString()); |
| 312 : | mikkopa | 12 | } |
| 313 : | } | ||
| 314 : | |||
| 315 : | |||
| 316 : | |||
| 317 : | |||
| 318 : | // TODO: Replace placeholders below | ||
| 319 : | // These needs to be hooked up to OpenSim during init of this class. | ||
| 320 : | // When queued in EventQueueManager they need to be LSL compatible (name and params) | ||
| 321 : | |||
| 322 : | //public void state_entry() { } // | ||
| 323 : | public void state_exit() { } | ||
| 324 : | //public void touch_start() { } | ||
| 325 : | public void touch() { } | ||
| 326 : | public void touch_end() { } | ||
| 327 : | public void collision_start() { } | ||
| 328 : | public void collision() { } | ||
| 329 : | public void collision_end() { } | ||
| 330 : | public void land_collision_start() { } | ||
| 331 : | public void land_collision() { } | ||
| 332 : | public void land_collision_end() { } | ||
| 333 : | public void timer() { } | ||
| 334 : | public void listen() { } | ||
| 335 : | public void on_rez() { } | ||
| 336 : | public void sensor() { } | ||
| 337 : | public void no_sensor() { } | ||
| 338 : | public void control() { } | ||
| 339 : | public void money() { } | ||
| 340 : | public void email() { } | ||
| 341 : | public void at_target() { } | ||
| 342 : | public void not_at_target() { } | ||
| 343 : | public void at_rot_target() { } | ||
| 344 : | public void not_at_rot_target() { } | ||
| 345 : | public void run_time_permissions() { } | ||
| 346 : | public void changed() { } | ||
| 347 : | public void attach() { } | ||
| 348 : | public void dataserver() { } | ||
| 349 : | public void link_message() { } | ||
| 350 : | public void moving_start() { } | ||
| 351 : | public void moving_end() { } | ||
| 352 : | public void object_rez() { } | ||
| 353 : | public void remote_data() { } | ||
| 354 : | public void http_response() { } | ||
| 355 : | |||
| 356 : | } | ||
| 357 : | |||
| 358 : | |||
| 359 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

