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

